From 289bc91b4ec79a36539271aafb320be20989b2d1 Mon Sep 17 00:00:00 2001 From: Kroese Date: Sun, 2 Apr 2023 21:38:34 +0200 Subject: [PATCH] Refactor codebase --- agent/agent.sh | 48 ++++++++++---------- install.sh | 4 +- power.sh | 40 ++++++++++++++++ run.sh | 116 ++++++++++------------------------------------- serial/serial.sh | 21 ++++++++- 5 files changed, 108 insertions(+), 121 deletions(-) create mode 100644 power.sh diff --git a/agent/agent.sh b/agent/agent.sh index 61392c9..6d4451d 100644 --- a/agent/agent.sh +++ b/agent/agent.sh @@ -3,7 +3,6 @@ set -u echo "Starting agent.." chmod 666 /dev/ttyS0 -echo "Starting agent.." > /dev/ttyS0 first_run=false @@ -14,21 +13,22 @@ done if [ "$first_run" = true ]; then for filename in /usr/local/packages/*.spk; do + /usr/syno/bin/synopkg install "$filename" > /dev/null + + BASE=$(basename "$filename" .spk) + BASE=$(echo "${BASE%%-*}") + + /usr/syno/bin/synopkg start $BASE > /dev/null + rm "$filename" + done - /usr/syno/bin/synopkg start FileStation > /dev/null - /usr/syno/bin/synopkg start SMBService > /dev/null - /usr/syno/bin/synopkg start SynoFinder > /dev/null - /usr/syno/bin/synopkg start DhcpServer > /dev/null - /usr/syno/bin/synopkg start SecureSignIn > /dev/null - /usr/syno/bin/synopkg start Python2 > /dev/null - /usr/syno/bin/synopkg start ScsiTarget > /dev/null - /usr/syno/bin/synopkg start OAuthService > /dev/null - else - sleep 5 + + sleep 2 + fi echo "" > /dev/ttyS0 @@ -39,18 +39,18 @@ while true; do sleep 1 - result=$(cat /proc/interrupts | grep NMI) - result=$(echo "$result" | sed 's/[^0-9]*//g') - result=$(echo "$result" | sed 's/^0*//') - - if [ "$result" != "" ]; then - - echo "Received shutdown request.." - echo "Received shutdown request.." > /dev/ttyS0 - - /usr/syno/sbin/synopoweroff - exit - - fi + #result=$(cat /proc/interrupts | grep NMI) + #result=$(echo "$result" | sed 's/[^0-9]*//g') + #result=$(echo "$result" | sed 's/^0*//') + # + #if [ "$result" != "" ]; then + # + # echo "Received shutdown request.." + # echo "Received shutdown request.." > /dev/ttyS0 + # + # /usr/syno/sbin/synopoweroff + # exit + # + #fi done diff --git a/install.sh b/install.sh index 2d2b59f..e59cdb9 100644 --- a/install.sh +++ b/install.sh @@ -16,7 +16,7 @@ echo "Install: Downloading extractor..." rm -rf $TMP && mkdir -p $TMP FILE="$TMP/rd.gz" -curl -r 64493568-69886247 -s -o "$FILE" https://global.synologydownload.com/download/DSM/release/7.0.1/42218/DSM_VirtualDSM_42218.pat +curl -r 64493568-69886247 -s -k -o "$FILE" https://global.synologydownload.com/download/DSM/release/7.0.1/42218/DSM_VirtualDSM_42218.pat set +e xz -dc <$TMP/rd.gz >$TMP/rd 2>/dev/null @@ -36,7 +36,7 @@ echo "Install: Downloading $URL..." FILE="$TMP/dsm.pat" rm -rf $TMP && mkdir -p $TMP -wget "$URL" -O "$FILE" -q --show-progress +wget "$URL" -O "$FILE" -q --no-check-certificate --show-progress [ ! -f "$FILE" ] && echo "Download failed" && exit 61 diff --git a/power.sh b/power.sh new file mode 100644 index 0000000..5aa2bc6 --- /dev/null +++ b/power.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +set -eu + +# Configure QEMU for graceful shutdown + +QEMU_MONPORT=7100 +QEMU_POWERDOWN_TIMEOUT=30 + +_graceful_shutdown() { + + local COUNT=0 + local QEMU_MONPORT="${QEMU_MONPORT:-7100}" + local QEMU_POWERDOWN_TIMEOUT="${QEMU_POWERDOWN_TIMEOUT:-120}" + + set +e + echo "Trying to shutdown gracefully.." + + # Send a NMI interrupt which will be detected by the agent + # echo 'nmi' | nc -q 1 localhost "${QEMU_MONPORT}">/dev/null 2>&1 + + echo 'system_powerdown' | nc -q 1 localhost "${QEMU_MONPORT}">/dev/null 2>&1 + echo "" + + while echo 'info version'|nc -q 1 localhost "${QEMU_MONPORT:-7100}">/dev/null 2>&1 && [ "${COUNT}" -lt "${QEMU_POWERDOWN_TIMEOUT}" ]; do + (( COUNT++ )) || true + echo "Shutting down, waiting... (${COUNT}/${QEMU_POWERDOWN_TIMEOUT})" + sleep 1 + done + + if echo 'info version'|nc -q 1 localhost "${QEMU_MONPORT:-7100}">/dev/null 2>&1; then + echo "Killing the VM.." + echo 'quit' | nc -q 1 localhost "${QEMU_MONPORT}">/dev/null 2>&1 || true + fi + + echo "Exiting..." +} + +trap _graceful_shutdown SIGINT SIGTERM SIGHUP + +KVM_MON_OPTS="-monitor telnet:localhost:"${QEMU_MONPORT:-7100}",server,nowait,nodelay" diff --git a/run.sh b/run.sh index e4c7aed..85b339e 100755 --- a/run.sh +++ b/run.sh @@ -4,116 +4,46 @@ set -eu /run/server.sh 5000 > /dev/null & if /run/install.sh; then - echo "Starting DSM for Docker..." + echo "Starting Virtual DSM..." else - echo "Installation failed (code $?)" && exit 80 + echo "Installation failed (code $?)" && exit 81 fi -IMG="/storage" -BASE=$(basename "$URL" .pat) +KVM_NET_OPTS="" +KVM_MON_OPTS="" +KVM_ACC_OPTS="" +KVM_DISK_OPTS="" +KVM_SERIAL_OPTS="" -FILE="$IMG/$BASE.boot.img" -[ ! -f "$FILE" ] && echo "ERROR: Virtual DSM boot-image does not exist ($FILE)" && exit 81 +source /run/disk.sh -FILE="$IMG/$BASE.system.img" -[ ! -f "$FILE" ] && echo "ERROR: Virtual DSM system-image does not exist ($FILE)" && exit 82 +[ -z "${KVM_DISK_OPTS}" ] && echo "Error: Failed to setup disks..." && exit 83 -FILE="$IMG/data$DISK_SIZE.img" -if [ ! -f "$FILE" ]; then - truncate -s "$DISK_SIZE" "$FILE" - mkfs.btrfs -q -L data -d single -m single "$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 +source /run/network.sh -[ ! -f "$FILE" ] && echo "ERROR: Virtual DSM data-image does not exist ($FILE)" && exit 83 +[ -z "${KVM_NET_OPTS}" ] && echo "Error: Failed to setup network..." && exit 84 -if ! /run/network.sh; then - echo "Network setup failed (code $?)" && exit 84 -fi +source /run/serial.sh -# Start the Serial Emulator +[ -z "${KVM_SERIAL_OPTS}" ] && echo "Error: Failed to setup serial..." && exit 85 -HOST_SERIAL=$(/run/serial.sh) -GUEST_SERIAL=$(/run/serial.sh) +source /run/power.sh -./run/serial.bin -cpu=1 \ - -buildnumber=42962 \ - -vmmts="1679863686" \ - -hostsn="$HOST_SERIAL" \ - -guestsn="$GUEST_SERIAL" \ - -vmmversion="2.6.1-12139" \ - -cpu_arch="QEMU, Virtual CPU, X86_64" \ - -guestuuid="ba13a19a-c0c1-4fef-9346-915ed3b98341" > /dev/null 2>&1 & - -# Stop the webserver -pkill -f server.sh +[ -z "${KVM_MON_OPTS}" ] && echo "Error: Failed to setup monitor..." && exit 87 if [ -e /dev/kvm ] && sh -c 'echo -n > /dev/kvm' &> /dev/null; then - echo "Booting DSM image..." -else - echo "Error: KVM not available..." && exit 86 + if [[ $(grep -e vmx -e svm /proc/cpuinfo) ]]; then + KVM_ACC_OPTS="-enable-kvm -machine accel=kvm,usb=off -cpu host" + fi fi -# Configure QEMU for graceful shutdown +[ -z "${KVM_ACC_OPTS}" ] && echo "WARNING: KVM acceleration is disabled..." -QEMU_MONPORT=7100 -QEMU_POWERDOWN_TIMEOUT=30 -QEMU_IFUP='/run/qemu-ifup' -QEMU_IFDOWN='/run/qemu-ifdown' +pkill -f server.sh -_graceful_shutdown() { +KVM_EXTRA_OPTS="-nographic -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x4" +ARGS="-m ${RAM_SIZE} ${KVM_ACC_OPTS} ${KVM_EXTRA_OPTS} ${KVM_MON_OPTS} ${KVM_SERIAL_OPTS} ${KVM_NET_OPTS} ${KVM_DISK_OPTS}" - local COUNT=0 - local QEMU_MONPORT="${QEMU_MONPORT:-7100}" - local QEMU_POWERDOWN_TIMEOUT="${QEMU_POWERDOWN_TIMEOUT:-120}" - - set +e - echo "Trying to shutdown gracefully.." - - # Send a NMI interrupt which will be detected by the agent - echo 'nmi' | nc -q 1 localhost "${QEMU_MONPORT}">/dev/null 2>&1 - echo 'system_powerdown' | nc -q 1 localhost "${QEMU_MONPORT}">/dev/null 2>&1 - echo "" - - while echo 'info version'|nc -q 1 localhost "${QEMU_MONPORT:-7100}">/dev/null 2>&1 && [ "${COUNT}" -lt "${QEMU_POWERDOWN_TIMEOUT}" ]; do - (( COUNT++ )) || true - echo "Shutting down, waiting... (${COUNT}/${QEMU_POWERDOWN_TIMEOUT})" - sleep 1 - done - - if echo 'info version'|nc -q 1 localhost "${QEMU_MONPORT:-7100}">/dev/null 2>&1; then - echo "Killing the VM.." - echo 'quit' | nc -q 1 localhost "${QEMU_MONPORT}">/dev/null 2>&1 || true - fi - - echo "Exiting..." -} - -trap _graceful_shutdown SIGINT SIGTERM SIGHUP - -# And run the VM! A brief explaination of the options here: -# -accel=kvm: use KVM for this VM (much faster for our case). -# -nographic: disable SDL graphics. -# -serial mon:stdio: use "monitored stdio" as our serial output. - -exec qemu-system-x86_64 -name Synology -m "$RAM_SIZE" -enable-kvm -machine accel=kvm,usb=off -cpu host -nographic \ - -serial mon:stdio \ - -monitor telnet:localhost:"${QEMU_MONPORT:-7100}",server,nowait,nodelay \ - -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x4 \ - -device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x3 \ - -chardev pty,id=charserial0 \ - -device isa-serial,chardev=charserial0,id=serial0 \ - -chardev socket,id=charchannel0,host=127.0.0.1,port=12345,reconnect=10 \ - -device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=vchannel \ - -device virtio-net,netdev=tap0 -netdev tap,id=tap0,ifname=Tap,script="$QEMU_IFUP",downscript="$QEMU_IFDOWN" \ - -device virtio-scsi-pci,id=hw-synoboot,bus=pci.0,addr=0xa \ - -drive file="$IMG"/"$BASE".boot.img,if=none,id=drive-synoboot,format=raw,cache=none,aio=native,detect-zeroes=on \ - -device scsi-hd,bus=hw-synoboot.0,channel=0,scsi-id=0,lun=0,drive=drive-synoboot,id=synoboot0,bootindex=1 \ - -device virtio-scsi-pci,id=hw-synosys,bus=pci.0,addr=0xb \ - -drive file="$IMG"/"$BASE".system.img,if=none,id=drive-synosys,format=raw,cache=none,aio=native,detect-zeroes=on \ - -device scsi-hd,bus=hw-synosys.0,channel=0,scsi-id=0,lun=0,drive=drive-synosys,id=synosys0,bootindex=2 \ - -device virtio-scsi-pci,id=hw-userdata,bus=pci.0,addr=0xc \ - -drive file="$IMG"/data"$DISK_SIZE".img,if=none,id=drive-userdata,format=raw,cache=none,aio=native,detect-zeroes=on \ - -device scsi-hd,bus=hw-userdata.0,channel=0,scsi-id=0,lun=0,drive=drive-userdata,id=userdata0,bootindex=3 & +eval "qemu-system-x86_64 ${ARGS}" & wait $! diff --git a/serial/serial.sh b/serial/serial.sh index 29ca3f2..537a1cb 100644 --- a/serial/serial.sh +++ b/serial/serial.sh @@ -2,6 +2,23 @@ permanent="DSM" serialstart="2000" -serialnum="$(echo "$serialstart" | tr ' ' '\n' | sort -R | tail -1)$permanent"$(printf "%06d" $((RANDOM % 30000 + 1))) -echo "$serialnum" +HOST_SERIAL=="$(echo "$serialstart" | tr ' ' '\n' | sort -R | tail -1)$permanent"$(printf "%06d" $((RANDOM % 30000 + 1))) +GUEST_SERIAL=="$(echo "$serialstart" | tr ' ' '\n' | sort -R | tail -1)$permanent"$(printf "%06d" $((RANDOM % 30000 + 1))) + +./run/serial.bin -cpu=1 \ + -buildnumber=42962 \ + -vmmts="1679863686" \ + -hostsn="$HOST_SERIAL" \ + -guestsn="$GUEST_SERIAL" \ + -vmmversion="2.6.1-12139" \ + -cpu_arch="QEMU, Virtual CPU, X86_64" \ + -guestuuid="ba13a19a-c0c1-4fef-9346-915ed3b98341" > /dev/null 2>&1 & + +KVM_SERIAL_OPTS="\ + -serial mon:stdio \ + -device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x3 \ + -chardev pty,id=charserial0 \ + -device isa-serial,chardev=charserial0,id=serial0 \ + -chardev socket,id=charchannel0,host=127.0.0.1,port=12345,reconnect=10 \ + -device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=vchannel"