From 295ce19442e51dedc134e14795d6bd186b863cea Mon Sep 17 00:00:00 2001 From: Kroese Date: Sat, 1 Apr 2023 18:43:53 +0200 Subject: [PATCH] Guest agent --- Dockerfile | 2 ++ agent/agent.sh | 27 +++++++++++++++++++++++++++ install.sh | 13 ++++++++++++- run.sh | 43 +++++++++++++++++++++++++++---------------- 4 files changed, 68 insertions(+), 17 deletions(-) create mode 100644 agent/agent.sh diff --git a/Dockerfile b/Dockerfile index 7af2f03..d649e3e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,6 +23,8 @@ RUN apt-get update && apt-get -y upgrade && \ && apt-get clean COPY run.sh /run/ +COPY agent/agent.sh /agent/ +COPY agent/service.sh /agent/ COPY server.sh /run/ COPY install.sh /run/ COPY serial/serial.sh /run/ diff --git a/agent/agent.sh b/agent/agent.sh new file mode 100644 index 0000000..38be2ed --- /dev/null +++ b/agent/agent.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +set -u + +echo "Starting agent.." + +chmod 666 /dev/ttyS0 +echo "Starting agent.." > /dev/ttyS0 + +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 + +done diff --git a/install.sh b/install.sh index 8f30d20..e1d1705 100644 --- a/install.sh +++ b/install.sh @@ -75,16 +75,27 @@ mount -t ext4 -o loop,offset=$OFFSET $SYSTEM $MOUNT 2>/dev/null && PRIVILEGED=tr rm -rf ${MOUNT:?}/{,.[!.],..?}* mv -f $HDA.tgz $HDA.txz + tar xpfJ $HDP.txz --absolute-names -C $MOUNT/ tar xpfJ $HDA.txz --absolute-names -C $MOUNT/ tar xpfJ $IDB.txz --absolute-names -C $MOUNT/usr/syno/synoman/indexdb/ +LOC="$MOUNT/usr/local/bin" +mkdir -p $LOC +mv /agent/agent.sh $LOC/agent.sh +chmod +x $LOC/agent.sh + +LOC="$MOUNT/usr/local/etc/rc.d" +mkdir -p $LOC +mv /agent/service.sh $LOC/agent.sh +chmod +x $LOC/agent.sh + if [ "$PRIVILEGED" = false ]; then echo "Install: Installing system partition..." # Workaround for containers that are not privileged to mount loop devices - mke2fs -q -t ext4 -b 4096 -d $MOUNT/ -L $LABEL -E offset=$OFFSET $SYSTEM $NUMBLOCKS + mke2fs -q -t ext4 -b 4096 -d $MOUNT/ -L $LABEL -F -E offset=$OFFSET $SYSTEM $NUMBLOCKS else diff --git a/run.sh b/run.sh index 67d42ee..a17fddf 100755 --- a/run.sh +++ b/run.sh @@ -77,18 +77,17 @@ HOST_SERIAL=$(/run/serial.sh) GUEST_SERIAL=$(/run/serial.sh) ./run/serial.bin -cpu=1 \ - -vmmversion="2.6.1-12139" \ -buildnumber=42962 \ -vmmts="1679863686" \ - -cpu_arch string="VirtualDSM" \ - -guestsn="$GUEST_SERIAL" \ -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 -#[ ! -e /dev/fuse ] && echo "Error: FUSE interface not available..." && exit 84 [ ! -e /dev/net/tun ] && echo "Error: TUN interface not available..." && exit 85 if [ -e /dev/kvm ] && sh -c 'echo -n > /dev/kvm' &> /dev/null; then @@ -109,19 +108,25 @@ _graceful_shutdown() { local QEMU_POWERDOWN_TIMEOUT="${QEMU_POWERDOWN_TIMEOUT:-120}" set +e - echo "Trying to shut down the VM gracefully" - echo 'system_powerdown' | nc -q 1 localhost "${QEMU_MONPORT}">/dev/null 2>&1 + 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 "QEMU still running. Retrying... (${COUNT}/${QEMU_POWERDOWN_TIMEOUT})" + 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 "Killing the VM.." echo 'quit' | nc -q 1 localhost "${QEMU_MONPORT}">/dev/null 2>&1 || true fi + echo "Exiting..." } @@ -131,18 +136,24 @@ trap _graceful_shutdown SIGINT SIGTERM SIGHUP # -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" -machine accel=kvm -cpu host -nographic -serial mon:stdio \ + +exec qemu-system-x86_64 -name Synology -m "$RAM_SIZE" -enable-kvm -cpu host -nographic \ + -serial mon:stdio \ -monitor telnet:localhost:"${QEMU_MONPORT:-7100}",server,nowait,nodelay \ - -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 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"/boot.img,if=none,id=drive-synoboot,format=raw,cache=none,aio=native,detect-zeroes=on \ + -device virtio-scsi-pci,id=hw-synoboot,bus=pci.0,addr=0xa \ + -drive file="$IMG"/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"/system.img,if=none,id=drive-synosys,format=raw,cache=none,aio=native,detect-zeroes=on \ + -device virtio-scsi-pci,id=hw-synosys,bus=pci.0,addr=0xb \ + -drive file="$IMG"/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.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 \ - -device piix3-usb-uhci,id=usb,bus=pci.0,addr=0x1.0x2 & + -device virtio-scsi-pci,id=hw-userdata,bus=pci.0,addr=0xc \ + -drive file="$IMG"/data.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 & wait $!