virtual-dsm/run.sh

120 lines
4.3 KiB
Bash
Raw Normal View History

2019-03-11 00:35:19 -07:00
#!/usr/bin/env bash
2023-03-27 19:23:50 +02:00
set -eu
2023-03-28 18:42:02 +02:00
/run/server.sh 5000 > /dev/null &
2023-03-27 19:23:50 +02:00
2023-03-28 18:42:02 +02:00
if /run/install.sh; then
echo "Starting DSM for Docker..."
else
2023-03-28 22:00:23 +02:00
echo "Installation failed (code $?)" && exit 80
2023-03-26 23:22:02 +02:00
fi
2023-03-28 22:00:23 +02:00
IMG="/storage"
2023-04-02 08:58:12 +02:00
BASE=$(basename "$URL" .pat)
2023-04-02 08:56:20 +02:00
FILE="$IMG/$BASE.boot.img"
2023-04-02 17:55:25 +02:00
[ ! -f "$FILE" ] && echo "ERROR: Virtual DSM boot-image does not exist ($FILE)" && exit 81
2023-03-28 22:00:23 +02:00
2023-04-02 08:56:20 +02:00
FILE="$IMG/$BASE.system.img"
2023-04-02 17:55:25 +02:00
[ ! -f "$FILE" ] && echo "ERROR: Virtual DSM system-image does not exist ($FILE)" && exit 82
2023-03-28 22:00:23 +02:00
2023-04-02 08:28:31 +02:00
FILE="$IMG/data$DISK_SIZE.img"
2023-03-28 22:00:23 +02:00
if [ ! -f "$FILE" ]; then
2023-03-30 20:21:33 +02:00
truncate -s "$DISK_SIZE" "$FILE"
2023-04-02 03:57:41 +02:00
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"
2023-03-28 22:00:23 +02:00
fi
2023-04-02 17:55:25 +02:00
[ ! -f "$FILE" ] && echo "ERROR: Virtual DSM data-image does not exist ($FILE)" && exit 83
2023-03-28 22:00:23 +02:00
2023-04-02 17:55:25 +02:00
if ! /run/network.sh; then
echo "Network setup failed (code $?)" && exit 84
fi
2019-03-11 00:35:19 -07:00
2023-03-28 06:48:35 +02:00
# Start the Serial Emulator
HOST_SERIAL=$(/run/serial.sh)
GUEST_SERIAL=$(/run/serial.sh)
./run/serial.bin -cpu=1 \
-buildnumber=42962 \
-vmmts="1679863686" \
-hostsn="$HOST_SERIAL" \
2023-04-01 18:43:53 +02:00
-guestsn="$GUEST_SERIAL" \
-vmmversion="2.6.1-12139" \
-cpu_arch="QEMU, Virtual CPU, X86_64" \
2023-03-28 06:48:35 +02:00
-guestuuid="ba13a19a-c0c1-4fef-9346-915ed3b98341" > /dev/null 2>&1 &
2023-03-27 19:23:50 +02:00
# Stop the webserver
pkill -f server.sh
2023-03-27 01:20:30 +02:00
2023-03-28 18:42:02 +02:00
if [ -e /dev/kvm ] && sh -c 'echo -n > /dev/kvm' &> /dev/null; then
echo "Booting DSM image..."
else
2023-03-28 22:00:23 +02:00
echo "Error: KVM not available..." && exit 86
2023-03-28 18:42:02 +02:00
fi
2023-03-28 06:48:35 +02:00
# Configure QEMU for graceful shutdown
QEMU_MONPORT=7100
QEMU_POWERDOWN_TIMEOUT=30
2023-04-02 17:55:25 +02:00
QEMU_IFUP='/run/qemu-ifup'
QEMU_IFDOWN='/run/qemu-ifdown'
2023-03-28 06:48:35 +02:00
_graceful_shutdown() {
local COUNT=0
local QEMU_MONPORT="${QEMU_MONPORT:-7100}"
local QEMU_POWERDOWN_TIMEOUT="${QEMU_POWERDOWN_TIMEOUT:-120}"
set +e
2023-04-01 18:43:53 +02:00
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
2023-04-02 17:55:25 +02:00
echo 'system_powerdown' | nc -q 1 localhost "${QEMU_MONPORT}">/dev/null 2>&1
2023-03-28 06:48:35 +02:00
echo ""
2023-04-01 18:43:53 +02:00
2023-03-30 20:21:33 +02:00
while echo 'info version'|nc -q 1 localhost "${QEMU_MONPORT:-7100}">/dev/null 2>&1 && [ "${COUNT}" -lt "${QEMU_POWERDOWN_TIMEOUT}" ]; do
2023-03-30 19:53:48 +02:00
(( COUNT++ )) || true
2023-04-01 18:43:53 +02:00
echo "Shutting down, waiting... (${COUNT}/${QEMU_POWERDOWN_TIMEOUT})"
2023-03-28 06:48:35 +02:00
sleep 1
done
2023-03-30 20:21:33 +02:00
if echo 'info version'|nc -q 1 localhost "${QEMU_MONPORT:-7100}">/dev/null 2>&1; then
2023-04-01 18:43:53 +02:00
echo "Killing the VM.."
2023-03-30 20:21:33 +02:00
echo 'quit' | nc -q 1 localhost "${QEMU_MONPORT}">/dev/null 2>&1 || true
2023-03-28 06:48:35 +02:00
fi
2023-04-01 18:43:53 +02:00
2023-03-28 06:48:35 +02:00
echo "Exiting..."
}
trap _graceful_shutdown SIGINT SIGTERM SIGHUP
2019-03-11 00:35:19 -07:00
# And run the VM! A brief explaination of the options here:
2023-03-28 07:04:22 +02:00
# -accel=kvm: use KVM for this VM (much faster for our case).
2019-03-11 00:35:19 -07:00
# -nographic: disable SDL graphics.
# -serial mon:stdio: use "monitored stdio" as our serial output.
2023-04-01 18:43:53 +02:00
2023-04-02 17:55:25 +02:00
exec qemu-system-x86_64 -name Synology -m "$RAM_SIZE" -enable-kvm -machine accel=kvm,usb=off -cpu host -nographic \
2023-04-01 18:43:53 +02:00
-serial mon:stdio \
2023-03-30 20:21:33 +02:00
-monitor telnet:localhost:"${QEMU_MONPORT:-7100}",server,nowait,nodelay \
2023-04-02 17:55:25 +02:00
-device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x4 \
2023-04-01 18:43:53 +02:00
-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 \
2023-03-27 01:20:30 +02:00
-device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=vchannel \
2023-03-30 20:21:33 +02:00
-device virtio-net,netdev=tap0 -netdev tap,id=tap0,ifname=Tap,script="$QEMU_IFUP",downscript="$QEMU_IFDOWN" \
2023-04-01 18:43:53 +02:00
-device virtio-scsi-pci,id=hw-synoboot,bus=pci.0,addr=0xa \
2023-04-02 08:56:20 +02:00
-drive file="$IMG"/"$BASE".boot.img,if=none,id=drive-synoboot,format=raw,cache=none,aio=native,detect-zeroes=on \
2023-03-26 20:25:00 +02:00
-device scsi-hd,bus=hw-synoboot.0,channel=0,scsi-id=0,lun=0,drive=drive-synoboot,id=synoboot0,bootindex=1 \
2023-04-01 18:43:53 +02:00
-device virtio-scsi-pci,id=hw-synosys,bus=pci.0,addr=0xb \
2023-04-02 08:56:20 +02:00
-drive file="$IMG"/"$BASE".system.img,if=none,id=drive-synosys,format=raw,cache=none,aio=native,detect-zeroes=on \
2023-03-26 20:25:00 +02:00
-device scsi-hd,bus=hw-synosys.0,channel=0,scsi-id=0,lun=0,drive=drive-synosys,id=synosys0,bootindex=2 \
2023-04-01 18:43:53 +02:00
-device virtio-scsi-pci,id=hw-userdata,bus=pci.0,addr=0xc \
2023-04-02 08:28:31 +02:00
-drive file="$IMG"/data"$DISK_SIZE".img,if=none,id=drive-userdata,format=raw,cache=none,aio=native,detect-zeroes=on \
2023-04-01 18:43:53 +02:00
-device scsi-hd,bus=hw-userdata.0,channel=0,scsi-id=0,lun=0,drive=drive-userdata,id=userdata0,bootindex=3 &
2023-03-27 22:44:39 +02:00
2023-03-28 06:48:35 +02:00
wait $!