virtual-dsm/run/run.sh

108 lines
3.3 KiB
Bash
Raw Normal View History

2019-03-11 00:35:19 -07:00
#!/usr/bin/env bash
2023-05-03 19:52:06 +02:00
set -Eeuo pipefail
2023-03-27 19:23:50 +02:00
2023-04-23 01:46:54 +02:00
# Docker environment variables
2023-04-18 17:42:59 +02:00
2023-04-26 21:54:43 +02:00
: ${URL:=''} # URL of the PAT file
2023-10-09 01:54:25 +02:00
: ${GPU:='N'} # Enable GPU passthrough
2023-04-26 21:54:43 +02:00
: ${DEBUG:='N'} # Enable debug mode
: ${ALLOCATE:='Y'} # Preallocate diskspace
2023-07-18 19:44:09 +02:00
: ${ARGUMENTS:=''} # Extra QEMU parameters
2023-04-26 21:54:43 +02:00
: ${CPU_CORES:='1'} # Amount of CPU cores
: ${DISK_SIZE:='16G'} # Initial data disk size
: ${RAM_SIZE:='512M'} # Maximum RAM amount
2023-04-18 17:42:59 +02:00
2023-05-11 17:23:40 +02:00
echo " Starting Virtual DSM for Docker v${VERSION}..."
2023-05-11 04:10:19 +02:00
info () { echo -e "\E[1;34m \E[1;36m$1\E[0m" ; }
2023-05-11 03:45:10 +02:00
error () { echo -e >&2 "\E[1;31m ERROR: $1\E[0m" ; }
2023-05-11 17:23:40 +02:00
trap 'error "Status $? while: ${BASH_COMMAND} (line $LINENO/$BASH_LINENO)"' ERR
2023-05-11 03:41:54 +02:00
[ ! -f "/run/run.sh" ] && error "Script must run inside Docker container!" && exit 11
[ "$(id -u)" -ne "0" ] && error "Script must be executed with root privileges." && exit 12
2023-03-26 23:22:02 +02:00
2023-04-16 16:22:37 +02:00
STORAGE="/storage"
2023-04-22 01:38:55 +02:00
KERNEL=$(uname -r | cut -b 1)
2023-07-03 11:11:47 +02:00
MINOR=$(uname -r | cut -d '.' -f2)
2023-05-19 16:54:14 +02:00
ARCH=$(dpkg --print-architecture)
2023-05-25 20:41:30 +02:00
VERS=$(qemu-system-x86_64 --version | head -n 1 | cut -d '(' -f 1)
2023-04-22 01:38:55 +02:00
2023-05-11 03:41:54 +02:00
[ ! -d "$STORAGE" ] && error "Storage folder (${STORAGE}) not found!" && exit 13
2023-04-14 16:58:45 +02:00
2023-04-16 16:22:37 +02:00
if [ -f "$STORAGE"/dsm.ver ]; then
BASE=$(cat "${STORAGE}/dsm.ver")
2023-04-14 16:58:45 +02:00
else
2023-04-16 16:06:21 +02:00
# Fallback for old installs
BASE="DSM_VirtualDSM_42962"
2023-04-14 16:58:45 +02:00
fi
2023-04-16 16:16:33 +02:00
[ -n "$URL" ] && BASE=$(basename "$URL" .pat)
2023-04-16 16:06:21 +02:00
2023-04-16 16:22:37 +02:00
if [[ ! -f "$STORAGE/$BASE.boot.img" ]] || [[ ! -f "$STORAGE/$BASE.system.img" ]]; then
2023-04-14 16:58:45 +02:00
. /run/install.sh
fi
2023-04-14 18:14:54 +02:00
# Initialize disks
2023-04-14 01:20:24 +02:00
. /run/disk.sh
2023-04-14 18:14:54 +02:00
# Initialize network
2023-04-14 01:20:24 +02:00
. /run/network.sh
2023-04-14 18:14:54 +02:00
2023-04-26 23:00:31 +02:00
# Initialize serialport
2023-04-14 01:20:24 +02:00
. /run/serial.sh
2023-04-14 18:14:54 +02:00
# Configure shutdown
2023-04-14 01:20:24 +02:00
. /run/power.sh
2023-03-27 01:20:30 +02:00
2023-04-27 11:40:44 +02:00
KVM_ERR=""
2023-04-27 20:06:09 +02:00
KVM_OPTS=""
2023-04-04 21:25:04 +02:00
2023-03-28 18:42:02 +02:00
if [ -e /dev/kvm ] && sh -c 'echo -n > /dev/kvm' &> /dev/null; then
2023-04-27 11:40:44 +02:00
if ! grep -q -e vmx -e svm /proc/cpuinfo; then
2023-05-19 16:29:24 +02:00
KVM_ERR="(vmx/svm disabled)"
2023-04-02 21:38:34 +02:00
fi
2023-04-27 11:10:41 +02:00
else
[ -e /dev/kvm ] && KVM_ERR="(no write access)" || KVM_ERR="(device file missing)"
2023-03-28 18:42:02 +02:00
fi
2023-04-27 11:51:09 +02:00
if [ -n "${KVM_ERR}" ]; then
2023-05-24 23:24:57 +02:00
if [ "$ARCH" == "amd64" ]; then
error "KVM acceleration not detected ${KVM_ERR}, see the FAQ about this."
[[ "${DEBUG}" != [Yy1]* ]] && exit 88
fi
2023-04-27 20:06:09 +02:00
else
KVM_OPTS=",accel=kvm -enable-kvm -cpu host"
fi
2023-03-28 06:48:35 +02:00
2023-04-22 20:22:38 +02:00
DEF_OPTS="-nographic -nodefaults -boot strict=on -display none"
2023-04-19 16:11:49 +02:00
RAM_OPTS=$(echo "-m ${RAM_SIZE}" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g')
2023-04-22 20:17:05 +02:00
CPU_OPTS="-smp ${CPU_CORES},sockets=1,dies=1,cores=${CPU_CORES},threads=1"
2023-04-27 11:40:44 +02:00
MAC_OPTS="-machine type=q35,usb=off,dump-guest-core=off,hpet=off${KVM_OPTS}"
2023-04-22 20:22:38 +02:00
EXTRA_OPTS="-device virtio-balloon-pci,id=balloon0,bus=pcie.0,addr=0x4"
2023-04-22 20:17:05 +02:00
EXTRA_OPTS="$EXTRA_OPTS -object rng-random,id=objrng0,filename=/dev/urandom"
2023-04-22 20:19:00 +02:00
EXTRA_OPTS="$EXTRA_OPTS -device virtio-rng-pci,rng=objrng0,id=rng0,bus=pcie.0,addr=0x1c"
2023-04-22 16:57:30 +02:00
2023-10-09 01:54:25 +02:00
if [[ "${GPU}" == [Yy1]* ]]; then
DEF_OPTS="-nodefaults -boot strict=on -display egl-headless,rendernode=/dev/dri/renderD128"
DEF_OPTS="${DEF_OPTS} -device virtio-vga,id=video0,max_outputs=1,bus=pcie.0,addr=0x1"
fi
2023-07-18 19:44:09 +02:00
ARGS="${DEF_OPTS} ${CPU_OPTS} ${RAM_OPTS} ${MAC_OPTS} ${MON_OPTS} ${SERIAL_OPTS} ${NET_OPTS} ${DISK_OPTS} ${EXTRA_OPTS} ${ARGUMENTS}"
2023-04-22 17:45:48 +02:00
ARGS=$(echo "$ARGS" | sed 's/\t/ /g' | tr -s ' ')
2023-04-21 10:38:06 +02:00
2023-05-03 18:12:11 +02:00
trap - ERR
set -m
(
2023-05-25 20:41:30 +02:00
[[ "${DEBUG}" == [Yy1]* ]] && info "$VERS" && set -x
2023-04-21 11:14:07 +02:00
qemu-system-x86_64 ${ARGS:+ $ARGS} & echo $! > "${_QEMU_PID}"
2023-05-02 01:24:03 +02:00
{ set +x; } 2>/dev/null
)
set +m
2023-07-18 20:16:47 +02:00
#if (( KERNEL > 5 )) || ( (( KERNEL == 5 )) && (( MINOR > 2 )) ); then
# pidwait -F "${_QEMU_PID}" & wait $!
#else
tail --pid "$(cat "${_QEMU_PID}")" --follow /dev/null & wait $!