Minor fixes

Network/serial reworked
This commit is contained in:
Kroese 2023-04-14 01:36:47 +02:00 committed by GitHub
commit 9adbaf1981
8 changed files with 85 additions and 101 deletions

View File

@ -22,5 +22,5 @@ jobs:
- name: Run ShellCheck
uses: ludeeus/action-shellcheck@master
env:
SHELLCHECK_OPTS: -e SC2001 -e SC2002 -e SC2116 -e SC2034 -e SC1091 -e SC2143 -e SC2223 -e SC2086 -e SC2145 -e SC2015 -e SC2268 -e SC2207 -e SC2064
SHELLCHECK_OPTS: -e SC2001 -e SC2002 -e SC2116 -e SC2034 -e SC1091 -e SC2143 -e SC2223 -e SC2086 -e SC2145 -e SC2015 -e SC2268 -e SC2207 -e SC2064 -e SC2166

View File

@ -57,12 +57,9 @@ EXPOSE 445
EXPOSE 5000
EXPOSE 5001
ENV URL ""
ENV CPU_CORES 1
ENV DISK_SIZE 16G
ENV RAM_SIZE 512M
#ENV URL https://global.synologydownload.com/download/DSM/beta/7.2/64216/DSM_VirtualDSM_64216.pat
#ENV URL https://global.synologydownload.com/download/DSM/release/7.0.1/42218/DSM_VirtualDSM_42218.pat
ENV URL https://global.synologydownload.com/download/DSM/release/7.1.1/42962-1/DSM_VirtualDSM_42962.pat
ENTRYPOINT ["/run/run.sh"]

12
disk.sh
View File

@ -16,16 +16,20 @@ NEW_SIZE=$(numfmt --from=iec "${DISK_SIZE}")
FILE="$IMG/data$DISK_SIZE.img"
if [ ! -f "$FILE" ]; then
# Create an empty file
truncate -s "${NEW_SIZE}" "${FILE}"
# Format as BTRFS filesystem
mkfs.btrfs -q -L data -d single -m dup "${FILE}" > /dev/null
#qemu-img convert -f raw -O qcow2 -o extended_l2=on,cluster_size=128k,compression_type=zstd,preallocation=metadata "$TMP" "$FILE"
fi
[ ! -f "$FILE" ] && echo "ERROR: Virtual DSM data-image does not exist ($FILE)" && exit 83
#OLD_SIZE=$(stat -c%s "${FILE}")
# Resizing requires mounting a loop device which in turn requires
# the container to be privileged, so we must disable it for now.
#
#if [ "$NEW_SIZE" -ne "$OLD_SIZE" ]; then
# OLD_SIZE=$(stat -c%s "${FILE}")
#
# if [ "$NEW_SIZE" -ne "$OLD_SIZE" ]; then
# echo "Resizing data disk from $OLD_SIZE to $NEW_SIZE bytes"
#
# if [ "$NEW_SIZE" -gt "$OLD_SIZE" ]; then
@ -37,7 +41,7 @@ fi
# btrfs filesystem resize "${NEW_SIZE}" "${FILE}"
# truncate -s "${NEW_SIZE}" "${FILE}"
# fi
#fi
# fi
KVM_DISK_OPTS="\
-device virtio-scsi-pci,id=hw-synoboot,bus=pcie.0,addr=0xa \

View File

@ -1,6 +1,12 @@
#!/usr/bin/env bash
set -eu
if [ -z $URL ]; then
#URL="https://global.synologydownload.com/download/DSM/beta/7.2/64216/DSM_VirtualDSM_64216.pat"
#URL="https://global.synologydownload.com/download/DSM/release/7.0.1/42218/DSM_VirtualDSM_42218.pat"
URL="https://global.synologydownload.com/download/DSM/release/7.1.1/42962-1/DSM_VirtualDSM_42962.pat"
fi
IMG="/storage"
BASE=$(basename "$URL" .pat)
@ -8,7 +14,7 @@ BASE=$(basename "$URL" .pat)
[ ! -f "/run/server.sh" ] && echo "Script must run inside Docker container!" && exit 60
[ ! -f "$IMG/$BASE.boot.img" ] && rm -f "$IMG"/"$BASE".system.img
[ -f "$IMG/$BASE.system.img" ] && exit 0
[ -f "$IMG/$BASE.system.img" ] && return
# Display wait message on port 5000
/run/server.sh 5000 > /dev/null &
@ -150,5 +156,3 @@ mv -f "$BOOT" "$IMG"/"$BASE".boot.img
mv -f "$SYSTEM" "$IMG"/"$BASE".system.img
rm -rf $TMP
exit 0

View File

@ -2,8 +2,8 @@
set -eu
: ${VM_NET_TAP:=''}
: ${VM_NET_MAC:=''}
: ${VM_NET_IP:='20.20.20.21'}
: ${VM_NET_MAC:='02:11:32:AA:BB:CC'}
: ${DNS_SERVERS:=''}
: ${DNSMASQ:='/usr/sbin/dnsmasq'}
@ -14,18 +14,6 @@ set -eu
# Functions
# ######################################
setupLocalDhcp () {
IP="$2"
MAC="$1"
CIDR="24"
HOSTNAME="VirtualDSM"
# dnsmasq configuration:
DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-range=$IP,$IP --dhcp-host=$MAC,,$IP,$HOSTNAME,infinite --dhcp-option=option:netmask,255.255.255.0"
# Create lease file for faster resolve
echo "0 $MAC $IP $HOSTNAME 01:${MAC}" > /var/lib/misc/dnsmasq.leases
chmod 644 /var/lib/misc/dnsmasq.leases
}
# Setup macvtap device to connect later the VM and setup a new macvlan device to connect the host machine to the network
configureNatNetworks () {
@ -46,7 +34,15 @@ configureNatNetworks () {
#Enable port forwarding flag
[[ $(< /proc/sys/net/ipv4/ip_forward) -eq 0 ]] && sysctl -w net.ipv4.ip_forward=1
setupLocalDhcp $VM_NET_MAC $VM_NET_IP
CIDR="24"
HOSTNAME="VirtualDSM"
# dnsmasq configuration:
DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-range=$VM_NET_IP,$VM_NET_IP --dhcp-host=$VM_NET_MAC,,$VM_NET_IP,$HOSTNAME,infinite --dhcp-option=option:netmask,255.255.255.0"
# Create lease file for faster resolve
echo "0 $VM_NET_MAC $VM_NET_IP $HOSTNAME 01:${VM_NET_MAC}" > /var/lib/misc/dnsmasq.leases
chmod 644 /var/lib/misc/dnsmasq.leases
}
# ######################################
@ -61,14 +57,6 @@ if [ ! -c /dev/net/tun ]; then
fi
[ ! -c /dev/net/tun ] && echo "Error: TUN network interface not available..." && exit 85
[ ! -d "$IMG" ] && echo "Storage folder (${IMG}) not found!" && exit 86
#If environment variabele not set fall back to file
if [ -z "$VM_NET_MAC" ]; then
FILE="${IMG}/guest.mac"
[ ! -f "$FILE" ] && echo "00:11:32:2C:A7:85" > "$FILE"
VM_NET_MAC=$(cat "${FILE}")
fi
update-alternatives --set iptables /usr/sbin/iptables-legacy > /dev/null
update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy > /dev/null
@ -88,17 +76,28 @@ for nameserver in "${nameservers[@]}"; do
fi
done
if [ -z $DNS_SERVERS ]; then
DNS_SERVERS="1.1.1.1"
else
COMMAS=${DNS_SERVERS//[^,]/}
COMMAS=${#COMMAS}
((COMMAS < 1)) && DNS_SERVERS="$DNS_SERVERS,1.1.1.1"
fi
DNSMASQ_OPTS="$DNSMASQ_OPTS \
--dhcp-option=option:dns-server,$DNS_SERVERS \
--dhcp-option=option:router,${VM_NET_IP%.*}.1 \
--dhcp-option=option:domain-search,$searchdomains \
--dhcp-option=option:domain-name,$domainname"
--dhcp-option=option:router,${VM_NET_IP%.*}.1"
[[ -z $(hostname -d) ]] || DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-option=option:domain-name,$(hostname -d)"
if [ -n "$searchdomains" -a "$searchdomains" != "." ]; then
DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-option=option:domain-search,$searchdomains"
DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-option=option:domain-name,$domainname"
else
[[ -z $(hostname -d) ]] || DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-option=option:domain-name,$(hostname -d)"
fi
$DNSMASQ $DNSMASQ_OPTS
KVM_NET_OPTS="${KVM_NET_OPTS} -device virtio-net-pci,netdev=hostnet0,mac=${VM_NET_MAC},id=net0"
KVM_NET_OPTS="${KVM_NET_OPTS} -device virtio-net-pci,romfile=,netdev=hostnet0,mac=${VM_NET_MAC},id=net0"
# Hack for guest VMs complaining about "bad udp checksums in 5 packets"
iptables -A POSTROUTING -t mangle -p udp --dport bootpc -j CHECKSUM --checksum-fill

19
run.sh
View File

@ -1,25 +1,22 @@
#!/usr/bin/env bash
set -eu
if /run/install.sh; then
echo "Starting Virtual DSM..."
else
echo "Installation failed (code $?)" && exit 81
fi
echo "Starting Virtual DSM..."
source /run/disk.sh
. /run/install.sh
. /run/disk.sh
[ -z "${KVM_DISK_OPTS}" ] && echo "Error: Failed to setup disks..." && exit 83
source /run/network.sh
. /run/network.sh
[ -z "${KVM_NET_OPTS}" ] && echo "Error: Failed to setup network..." && exit 84
source /run/serial.sh
. /run/serial.sh
[ -z "${KVM_SERIAL_OPTS}" ] && echo "Error: Failed to setup serial..." && exit 85
source /run/power.sh
. /run/power.sh
[ -z "${KVM_MON_OPTS}" ] && echo "Error: Failed to setup monitor..." && exit 87
@ -39,7 +36,9 @@ ARGS="-m ${RAM_SIZE} -smp ${CPU_CORES} ${KVM_ACC_OPTS} ${EXTRA_OPTS} ${KVM_MON_O
set -m
(
for _SIGNAL in {1..64}; do trap "echo Caught trap ${_SIGNAL} for the QEMU process" "${_SIGNAL}"; done
for _SIGNAL in {1..64}; do
trap "echo Caught trap ${_SIGNAL} for the QEMU process" "${_SIGNAL}";
done
qemu-system-x86_64 ${ARGS} & echo $! > ${_QEMU_PID}
)
set +m

View File

@ -1,33 +1,14 @@
#!/bin/bash
set -eu
# Docker environment variabeles
: ${HOST_SERIAL:=''}
: ${GUEST_SERIAL:=''}
permanent="DSM"
serialstart="2000"
[ ! -d "$IMG" ] && echo "Storage folder (${IMG}) not found!" && exit 69
#If environment variabele not set fall back to file
if [ -z "$HOST_SERIAL" ]; then
FILE="${IMG}/host.serial"
if [ ! -f "$FILE" ]; then
SERIAL="$(echo "$serialstart" | tr ' ' '\n' | sort -R | tail -1)$permanent"$(printf "%06d" $((RANDOM % 30000 + 1)))
echo $SERIAL > "$FILE"
fi
HOST_SERIAL=$(cat "${FILE}")
fi
#If environment variabele not set fall back to file
if [ -z "$GUEST_SERIAL" ]; then
FILE="${IMG}/guest.serial"
if [ ! -f "$FILE" ]; then
SERIAL="$(echo "$serialstart" | tr ' ' '\n' | sort -R | tail -1)$permanent"$(printf "%06d" $((RANDOM % 30000 + 1)))
echo $SERIAL > "$FILE"
fi
GUEST_SERIAL=$(cat "${FILE}")
fi
: ${HOST_BUILD:='42962'}
: ${HOST_VERSION:='2.6.1-12139'}
: ${HOST_TIMESTAMP:='1679863686'}
: ${HOST_SERIAL:='0000000000000'}
: ${GUEST_SERIAL:='0000000000000'}
: ${GUEST_UUID:='ba13a19a-c0c1-4fef-9346-915ed3b98341'}
CPU=$(lscpu | sed -nr '/Model name/ s/.*:\s*(.*) @ .*/\1/p' | sed ':a;s/ / /;ta' | sed s/"(R)"//g | sed s/"-"//g | sed 's/[^[:alnum:] ]\+//g')
@ -39,12 +20,12 @@ fi
./run/serial.bin -cpu="${CPU_CORES}" \
-cpu_arch="${CPU}" \
-buildnumber=42962 \
-vmmts=1679863686 \
-hostsn="${HOST_SERIAL}" \
-guestsn="${GUEST_SERIAL}" \
-vmmversion="2.6.1-12139" \
-guestuuid="ba13a19a-c0c1-4fef-9346-915ed3b98341" > /dev/null 2>&1 &
-vmmts="${HOST_TIMESTAMP}" \
-vmmversion="${HOST_VERSION}" \
-buildnumber="${HOST_BUILD}" \
-guestuuid="${GUEST_UUID}" > /dev/null 2>&1 &
KVM_SERIAL_OPTS="\
-serial mon:stdio \

View File

@ -55,7 +55,7 @@ func main() {
listener, err := net.Listen("tcp", *ListenAddr)
if err != nil {
log.Println("Error listening", err.Error())
log.Fatalln("Error listening", err.Error())
return
}
@ -64,7 +64,7 @@ func main() {
for {
conn, err := listener.Accept()
if err != nil {
log.Println("Error on accept", err.Error())
log.Fatalln("Error on accept", err.Error())
return
}
log.Printf("New connection from %s\n", conn.RemoteAddr().String())