diff --git a/src/check.sh b/src/check.sh index a12aac3..e45ea71 100644 --- a/src/check.sh +++ b/src/check.sh @@ -19,28 +19,28 @@ if [ ! -f "$file" ]; then if [[ "$result" != "success" ]] ; then { msg=$(echo "$json" | jq -r '.message'); rc=$?; } || : - echo "Guest replied ${result}: $msg" && exit 1 + echo "Guest replied $result: $msg" && exit 1 fi { port=$(echo "$json" | jq -r '.data.data.dsm_setting.data.http_port'); rc=$?; } || : (( rc != 0 )) && echo "Failed to parse response from guest: jq error $rc ( $json )" && exit 1 [[ "$port" == "null" ]] && echo "Guest has not set a portnumber yet.." && exit 1 - [ -z "${port}" ] && echo "Guest has not set a portnumber yet.." && exit 1 + [ -z "$port" ] && echo "Guest has not set a portnumber yet.." && exit 1 { ip=$(echo "$json" | jq -r '.data.data.ip.data[] | select((.name=="eth0") and has("ip")).ip'); rc=$?; } || : (( rc != 0 )) && echo "Failed to parse response from guest: jq error $rc ( $json )" && exit 1 [[ "$ip" == "null" ]] && echo "Guest returned invalid response: $json" && exit 1 - [ -z "${ip}" ] && echo "Guest has not received an IP yet.." && exit 1 + [ -z "$ip" ] && echo "Guest has not received an IP yet.." && exit 1 - echo "${ip}:${port}" > $file + echo "$ip:$port" > $file fi location=$(cat "$file") -if ! curl -m 20 -ILfSs "http://${location}/" > /dev/null; then +if ! curl -m 20 -ILfSs "http://$location/" > /dev/null; then rm -f $file - echo "Failed to reach http://${location}" + echo "Failed to reach http://$location" exit 1 fi diff --git a/src/config.sh b/src/config.sh index 1805072..60e6c05 100644 --- a/src/config.sh +++ b/src/config.sh @@ -12,29 +12,29 @@ else [ -e /dev/kvm ] && KVM_ERR="(no write access)" || KVM_ERR="(device file missing)" fi -if [ -n "${KVM_ERR}" ]; then +if [ -n "$KVM_ERR" ]; then if [ "$ARCH" == "amd64" ]; then - error "KVM acceleration not detected ${KVM_ERR}, see the FAQ about this." - [[ "${DEBUG}" != [Yy1]* ]] && exit 88 + error "KVM acceleration not detected $KVM_ERR, see the FAQ about this." + [[ "$DEBUG" != [Yy1]* ]] && exit 88 fi else KVM_OPTS=",accel=kvm -enable-kvm -cpu host" fi DEF_OPTS="-nographic -nodefaults -boot strict=on -display none" -RAM_OPTS=$(echo "-m ${RAM_SIZE}" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g') -CPU_OPTS="-smp ${CPU_CORES},sockets=1,dies=1,cores=${CPU_CORES},threads=1" +RAM_OPTS=$(echo "-m $RAM_SIZE" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g') +CPU_OPTS="-smp $CPU_CORES,sockets=1,dies=1,cores=$CPU_CORES,threads=1" MAC_OPTS="-machine type=q35,usb=off,dump-guest-core=off,hpet=off${KVM_OPTS}" EXTRA_OPTS="-device virtio-balloon-pci,id=balloon0,bus=pcie.0,addr=0x4" EXTRA_OPTS="$EXTRA_OPTS -object rng-random,id=objrng0,filename=/dev/urandom" EXTRA_OPTS="$EXTRA_OPTS -device virtio-rng-pci,rng=objrng0,id=rng0,bus=pcie.0,addr=0x1c" -if [[ "${GPU}" == [Yy1]* ]] && [[ "$ARCH" == "amd64" ]]; then +if [[ "$GPU" == [Yy1]* ]] && [[ "$ARCH" == "amd64" ]]; 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" + DEF_OPTS="$DEF_OPTS -device virtio-vga,id=video0,max_outputs=1,bus=pcie.0,addr=0x1" fi -ARGS="${DEF_OPTS} ${CPU_OPTS} ${RAM_OPTS} ${MAC_OPTS} ${MON_OPTS} ${SERIAL_OPTS} ${NET_OPTS} ${DISK_OPTS} ${EXTRA_OPTS} ${ARGUMENTS}" +ARGS="$DEF_OPTS $CPU_OPTS $RAM_OPTS $MAC_OPTS $MON_OPTS $SERIAL_OPTS $NET_OPTS $DISK_OPTS $EXTRA_OPTS $ARGUMENTS" ARGS=$(echo "$ARGS" | sed 's/\t/ /g' | tr -s ' ') return 0 diff --git a/src/disk.sh b/src/disk.sh index 1dd4b0a..ba250b0 100644 --- a/src/disk.sh +++ b/src/disk.sh @@ -17,11 +17,11 @@ SYSTEM="$STORAGE/$BASE.system.img" DISK_OPTS="\ -device virtio-scsi-pci,id=hw-synoboot,bus=pcie.0,addr=0xa \ - -drive file=${BOOT},if=none,id=drive-synoboot,format=raw,cache=${DISK_CACHE},aio=${DISK_IO},discard=${DISK_DISCARD},detect-zeroes=on \ - -device scsi-hd,bus=hw-synoboot.0,channel=0,scsi-id=0,lun=0,drive=drive-synoboot,id=synoboot0,rotation_rate=${DISK_ROTATION},bootindex=1 \ + -drive file=$BOOT,if=none,id=drive-synoboot,format=raw,cache=$DISK_CACHE,aio=$DISK_IO,discard=$DISK_DISCARD,detect-zeroes=on \ + -device scsi-hd,bus=hw-synoboot.0,channel=0,scsi-id=0,lun=0,drive=drive-synoboot,id=synoboot0,rotation_rate=$DISK_ROTATION,bootindex=1 \ -device virtio-scsi-pci,id=hw-synosys,bus=pcie.0,addr=0xb \ - -drive file=${SYSTEM},if=none,id=drive-synosys,format=raw,cache=${DISK_CACHE},aio=${DISK_IO},discard=${DISK_DISCARD},detect-zeroes=on \ - -device scsi-hd,bus=hw-synosys.0,channel=0,scsi-id=0,lun=0,drive=drive-synosys,id=synosys0,rotation_rate=${DISK_ROTATION},bootindex=2" + -drive file=$SYSTEM,if=none,id=drive-synosys,format=raw,cache=$DISK_CACHE,aio=$DISK_IO,discard=$DISK_DISCARD,detect-zeroes=on \ + -device scsi-hd,bus=hw-synosys.0,channel=0,scsi-id=0,lun=0,drive=drive-synosys,id=synosys0,rotation_rate=$DISK_ROTATION,bootindex=2" fmt2ext() { local DISK_FMT=$1 @@ -50,7 +50,7 @@ ext2fmt() { echo "raw" ;; *) - error "Unrecognized file extension: .${DISK_EXT}" && exit 88 + error "Unrecognized file extension: .$DISK_EXT" && exit 88 ;; esac } @@ -92,7 +92,7 @@ resizeDisk() { GB=$(( (CUR_SIZE + 1073741823)/1073741824 )) info "Resizing $DISK_DESC from ${GB}G to $DISK_SPACE .." - FAIL="Could not resize $DISK_FMT file of $DISK_DESC (${DISK_FILE}) from ${GB}G to $DISK_SPACE .." + FAIL="Could not resize $DISK_FMT file of $DISK_DESC ($DISK_FILE) from ${GB}G to $DISK_SPACE .." REQ=$((DATA_SIZE-CUR_SIZE)) (( REQ < 1 )) && error "Shrinking disks is not supported!" && exit 84 @@ -163,11 +163,11 @@ createDisk() { local DISK_DESC=$3 local DISK_FMT=$4 - FAIL="Could not create a $DISK_SPACE $DISK_FMT file for $DISK_DESC (${DISK_FILE})" + FAIL="Could not create a $DISK_SPACE $DISK_FMT file for $DISK_DESC ($DISK_FILE)" case "${DISK_FMT,,}" in raw) - if [[ "${ALLOCATE}" == [Nn]* ]]; then + if [[ "$ALLOCATE" == [Nn]* ]]; then # Create an empty file if ! truncate -s "$DISK_SPACE" "$DISK_FILE"; then @@ -221,7 +221,7 @@ addDisk () { local DISK_ADDRESS=$7 local DISK_FMT=$8 - DISK_FILE="${DISK_BASE}.${DISK_EXT}" + DISK_FILE="$DISK_BASE.$DISK_EXT" DIR=$(dirname "$DISK_FILE") [ ! -d "$DIR" ] && return 0 @@ -249,15 +249,15 @@ addDisk () { PREV_FMT="qcow2" fi PREV_EXT="$(fmt2ext "$PREV_FMT")" - PREV_FILE="${DISK_BASE}.${PREV_EXT}" + PREV_FILE="$DISK_BASE.$PREV_EXT" if [ -f "$PREV_FILE" ] ; then - info "Detected that ${DISK_DESC^^}_FMT changed from \"${PREV_FMT}\" to \"${DISK_FMT}\"." + info "Detected that ${DISK_DESC^^}_FMT changed from \"$PREV_FMT\" to \"$DISK_FMT\"." info "Starting conversion of $DISK_DESC to this new format, please wait until completed..." local TMP_FILE - TMP_FILE="${DISK_BASE}.tmp" + TMP_FILE="$DISK_BASE.tmp" rm -f "$TMP_FILE" if ! convertDisk "$PREV_FILE" "$PREV_FMT" "$TMP_FILE" "$DISK_FMT" ; then @@ -285,44 +285,44 @@ addDisk () { fi - DISK_OPTS="${DISK_OPTS} \ - -device virtio-scsi-pci,id=hw-${DISK_ID},bus=pcie.0,addr=${DISK_ADDRESS} \ - -drive file=${DISK_FILE},if=none,id=drive-${DISK_ID},format=${DISK_FMT},cache=${DISK_CACHE},aio=${DISK_IO},discard=${DISK_DISCARD},detect-zeroes=on \ - -device scsi-hd,bus=hw-${DISK_ID}.0,channel=0,scsi-id=0,lun=0,drive=drive-${DISK_ID},id=${DISK_ID},rotation_rate=${DISK_ROTATION},bootindex=${DISK_INDEX}" + DISK_OPTS="$DISK_OPTS \ + -device virtio-scsi-pci,id=hw-$DISK_ID,bus=pcie.0,addr=$DISK_ADDRESS \ + -drive file=$DISK_FILE,if=none,id=drive-$DISK_ID,format=$DISK_FMT,cache=$DISK_CACHE,aio=$DISK_IO,discard=$DISK_DISCARD,detect-zeroes=on \ + -device scsi-hd,bus=hw-$DISK_ID.0,channel=0,scsi-id=0,lun=0,drive=drive-$DISK_ID,id=$DISK_ID,rotation_rate=$DISK_ROTATION,bootindex=$DISK_INDEX" return 0 } DISK_EXT="$(fmt2ext "$DISK_FMT")" || exit $? -DISK1_FILE="${STORAGE}/data" -if [[ ! -f "${DISK1_FILE}.img" ]] && [[ -f "${STORAGE}/data${DISK_SIZE}.img" ]]; then +DISK1_FILE="$STORAGE/data" +if [[ ! -f "$DISK1_FILE.img" ]] && [[ -f "$STORAGE/data${DISK_SIZE}.img" ]]; then # Fallback for legacy installs - mv "${STORAGE}/data${DISK_SIZE}.img" "${DISK1_FILE}.img" + mv "$STORAGE/data${DISK_SIZE}.img" "$DISK1_FILE.img" fi DISK2_FILE="/storage2/data2" -if [ ! -f "${DISK2_FILE}.img" ]; then +if [ ! -f "$DISK2_FILE.img" ]; then # Fallback for legacy installs FALLBACK="/storage2/data.img" - if [[ -f "${DISK1_FILE}.img" ]] && [[ -f "$FALLBACK" ]]; then + if [[ -f "$DISK1_FILE.img" ]] && [[ -f "$FALLBACK" ]]; then SIZE1=$(stat -c%s "$FALLBACK") - SIZE2=$(stat -c%s "${DISK1_FILE}.img") + SIZE2=$(stat -c%s "$DISK1_FILE.img") if [[ SIZE1 -ne SIZE2 ]]; then - mv "$FALLBACK" "${DISK2_FILE}.img" + mv "$FALLBACK" "$DISK2_FILE.img" fi fi fi DISK3_FILE="/storage3/data3" -if [ ! -f "${DISK3_FILE}.img" ]; then +if [ ! -f "$DISK3_FILE.img" ]; then # Fallback for legacy installs FALLBACK="/storage3/data.img" - if [[ -f "${DISK1_FILE}.img" ]] && [[ -f "$FALLBACK" ]]; then + if [[ -f "$DISK1_FILE.img" ]] && [[ -f "$FALLBACK" ]]; then SIZE1=$(stat -c%s "$FALLBACK") - SIZE2=$(stat -c%s "${DISK1_FILE}.img") + SIZE2=$(stat -c%s "$DISK1_FILE.img") if [[ SIZE1 -ne SIZE2 ]]; then - mv "$FALLBACK" "${DISK3_FILE}.img" + mv "$FALLBACK" "$DISK3_FILE.img" fi fi fi @@ -354,10 +354,10 @@ addDevice () { [ -z "$DISK_DEV" ] && return 0 [ ! -b "$DISK_DEV" ] && error "Device $DISK_DEV cannot be found! Please add it to the 'devices' section of your compose file." && exit 55 - DISK_OPTS="${DISK_OPTS} \ - -device virtio-scsi-pci,id=hw-${DISK_ID},bus=pcie.0,addr=${DISK_ADDRESS} \ - -drive file=${DISK_DEV},if=none,id=drive-${DISK_ID},format=raw,cache=${DISK_CACHE},aio=${DISK_IO},discard=${DISK_DISCARD},detect-zeroes=on \ - -device scsi-hd,bus=hw-${DISK_ID}.0,channel=0,scsi-id=0,lun=0,drive=drive-${DISK_ID},id=${DISK_ID},rotation_rate=${DISK_ROTATION},bootindex=${DISK_INDEX}" + DISK_OPTS="$DISK_OPTS \ + -device virtio-scsi-pci,id=hw-$DISK_ID,bus=pcie.0,addr=$DISK_ADDRESS \ + -drive file=$DISK_DEV,if=none,id=drive-$DISK_ID,format=raw,cache=$DISK_CACHE,aio=$DISK_IO,discard=$DISK_DISCARD,detect-zeroes=on \ + -device scsi-hd,bus=hw-$DISK_ID.0,channel=0,scsi-id=0,lun=0,drive=drive-$DISK_ID,id=$DISK_ID,rotation_rate=$DISK_ROTATION,bootindex=$DISK_INDEX" return 0 } diff --git a/src/entry.sh b/src/entry.sh index 5738399..c936130 100755 --- a/src/entry.sh +++ b/src/entry.sh @@ -17,17 +17,17 @@ cd /run trap - ERR -if [[ "${CONSOLE}" == [Yy]* ]]; then - exec qemu-system-x86_64 -pidfile "${QEMU_PID}" ${ARGS:+ $ARGS} +if [[ "$CONSOLE" == [Yy]* ]]; then + exec qemu-system-x86_64 -pidfile "$QEMU_PID" ${ARGS:+ $ARGS} exit $? fi set -m ( - [[ "${DEBUG}" == [Yy1]* ]] && info "$VERS" && set -x - qemu-system-x86_64 ${ARGS:+ $ARGS} & echo $! > "${QEMU_PID}" + [[ "$DEBUG" == [Yy1]* ]] && info "$VERS" && set -x + qemu-system-x86_64 ${ARGS:+ $ARGS} & echo $! > "$QEMU_PID" { set +x; } 2>/dev/null ) set +m -tail --pid "$(cat "${QEMU_PID}")" --follow /dev/null & wait $! +tail --pid "$(cat "$QEMU_PID")" --follow /dev/null & wait $! diff --git a/src/gpu.sh b/src/gpu.sh index 733d02f..76dac9a 100644 --- a/src/gpu.sh +++ b/src/gpu.sh @@ -1,7 +1,7 @@ #!/bin/bash set -Eeuo pipefail -if [[ "${GPU}" != [Yy1]* ]] || [[ "$ARCH" != "amd64" ]]; then +if [[ "$GPU" != [Yy1]* ]] || [[ "$ARCH" != "amd64" ]]; then return 0 fi diff --git a/src/install.sh b/src/install.sh index 38e4bf1..04cc46d 100644 --- a/src/install.sh +++ b/src/install.sh @@ -5,7 +5,7 @@ set -Eeuo pipefail : ${DEV:='Y'} # Controls whether device nodes are created. if [ -f "$STORAGE"/dsm.ver ]; then - BASE=$(cat "${STORAGE}/dsm.ver") + BASE=$(cat "$STORAGE/dsm.ver") else # Fallback for old installs BASE="DSM_VirtualDSM_42962" @@ -42,7 +42,7 @@ fi BASE=$(basename "$URL" .pat) -if [[ "$URL" != "file://${STORAGE}/${BASE}.pat" ]]; then +if [[ "$URL" != "file://$STORAGE/$BASE.pat" ]]; then rm -f "$STORAGE"/"$BASE".pat fi @@ -50,7 +50,7 @@ rm -f "$STORAGE"/"$BASE".agent rm -f "$STORAGE"/"$BASE".boot.img rm -f "$STORAGE"/"$BASE".system.img -[[ "${DEBUG}" == [Yy1]* ]] && set -x +[[ "$DEBUG" == [Yy1]* ]] && set -x # Check filesystem MIN_ROOT=471859200 @@ -58,7 +58,7 @@ MIN_SPACE=6442450944 FS=$(stat -f -c %T "$STORAGE") if [[ "$FS" == "overlay"* ]]; then - info "Warning: the filesystem of ${STORAGE} is OverlayFS, this usually means it was binded to an invalid path!" + info "Warning: the filesystem of $STORAGE is OverlayFS, this usually means it was binded to an invalid path!" fi if [[ "$FS" != "fat"* && "$FS" != "vfat"* && "$FS" != "exfat"* && \ @@ -69,7 +69,7 @@ else SPACE=$(df --output=avail -B 1 /tmp | tail -n 1) if (( MIN_SPACE > SPACE )); then TMP="$STORAGE/tmp" - info "Warning: the ${FS} filesystem of ${STORAGE} does not support UNIX permissions.." + info "Warning: the $FS filesystem of $STORAGE does not support UNIX permissions.." fi fi @@ -81,12 +81,12 @@ SPACE=$(df --output=avail -B 1 / | tail -n 1) SPACE=$(df --output=avail -B 1 "$TMP" | tail -n 1) SPACE_GB=$(( (SPACE + 1073741823)/1073741824 )) -(( MIN_SPACE > SPACE )) && error "Not enough free space for installation in ${STORAGE}, have ${SPACE_GB} GB available but need at least 6 GB." && exit 95 +(( MIN_SPACE > SPACE )) && error "Not enough free space for installation in $STORAGE, have $SPACE_GB GB available but need at least 6 GB." && exit 95 if [[ "$TMP" != "$STORAGE/tmp" ]]; then SPACE=$(df --output=avail -B 1 "$STORAGE" | tail -n 1) SPACE_GB=$(( (SPACE + 1073741823)/1073741824 )) - (( MIN_SPACE > SPACE )) && error "Not enough free space for installation in ${STORAGE}, have ${SPACE_GB} GB available but need at least 6 GB." && exit 94 + (( MIN_SPACE > SPACE )) && error "Not enough free space for installation in $STORAGE, have $SPACE_GB GB available but need at least 6 GB." && exit 94 fi # Check if output is to interactive TTY @@ -100,7 +100,7 @@ fi RDC="$STORAGE/dsm.rd" -if [ ! -f "${RDC}" ]; then +if [ ! -f "$RDC" ]; then info "Install: Downloading installer..." @@ -123,7 +123,7 @@ if [ ! -f "${RDC}" ]; then { wget "$LOC" -O "$PAT" -q --no-check-certificate --show-progress "$PROGRESS"; rc=$?; } || : (( rc != 0 )) && error "Failed to download $LOC, reason: $rc" && exit 60 - tar --extract --file="$PAT" --directory="$(dirname "${RD}")"/. "$(basename "${RD}")" + tar --extract --file="$PAT" --directory="$(dirname "$RD")"/. "$(basename "$RD")" rm "$PAT" fi @@ -132,12 +132,12 @@ if [ ! -f "${RDC}" ]; then fi -if [ -f "${RDC}" ]; then +if [ -f "$RDC" ]; then { xz -dc <"$RDC" >"$TMP/rd" 2>/dev/null; rc=$?; } || : (( rc != 1 )) && error "Failed to unxz $RDC, reason $rc" && exit 91 - if [[ "${DEV}" == [Nn]* ]]; then + if [[ "$DEV" == [Nn]* ]]; then # Exclude dev/ from cpio extract { (cd "$TMP" && cpio -it < "$TMP/rd" | grep -Ev 'dev/' | while read -r entry; do cpio -idm "$entry" < "$TMP/rd" 2>/dev/null; done); rc=$?; } || : else @@ -240,26 +240,26 @@ SYSTEM_SIZE=4954537983 # Check free diskspace SPACE=$(df --output=avail -B 1 "$TMP" | tail -n 1) SPACE_GB=$(( (SPACE + 1073741823)/1073741824 )) -(( SYSTEM_SIZE > SPACE )) && error "Not enough free space to create a 4 GB system disk, have only ${SPACE_GB} GB available." && exit 87 +(( SYSTEM_SIZE > SPACE )) && error "Not enough free space to create a 4 GB system disk, have only $SPACE_GB GB available." && exit 87 -if ! fallocate -l "${SYSTEM_SIZE}" "${SYSTEM}"; then - if ! truncate -s "${SYSTEM_SIZE}" "${SYSTEM}"; then - rm -f "${SYSTEM}" && error "Could not allocate a file for the system disk." && exit 88 +if ! fallocate -l "$SYSTEM_SIZE" "$SYSTEM"; then + if ! truncate -s "$SYSTEM_SIZE" "$SYSTEM"; then + rm -f "$SYSTEM" && error "Could not allocate a file for the system disk." && exit 88 fi fi # Check if file exists -[ ! -f "${SYSTEM}" ] && error "System disk does not exist ($SYSTEM)" && exit 89 +[ ! -f "$SYSTEM" ] && error "System disk does not exist ($SYSTEM)" && exit 89 # Check the filesize -SIZE=$(stat -c%s "${SYSTEM}") -[[ SIZE -ne SYSTEM_SIZE ]] && rm -f "${SYSTEM}" && error "System disk has the wrong size: ${SIZE}" && exit 90 +SIZE=$(stat -c%s "$SYSTEM") +[[ SIZE -ne SYSTEM_SIZE ]] && rm -f "$SYSTEM" && error "System disk has the wrong size: $SIZE" && exit 90 PART="$TMP/partition.fdisk" { echo "label: dos" echo "label-id: 0x6f9ee2e9" - echo "device: ${SYSTEM}" + echo "device: $SYSTEM" echo "unit: sectors" echo "sector-size: 512" echo "" @@ -276,7 +276,7 @@ rm -rf "$MOUNT" && mkdir -p "$MOUNT" mv "$HDA.tgz" "$HDA.txz" -if [[ "${DEV}" == [Nn]* ]]; then +if [[ "$DEV" == [Nn]* ]]; then # Exclude dev/ from tar extract tar xpfJ "$HDA.txz" --absolute-names --exclude="dev" -C "$MOUNT/" else @@ -301,7 +301,7 @@ rm -rf "$MOUNT" echo "$BASE" > "$STORAGE"/dsm.ver -if [[ "$URL" == "file://${STORAGE}/${BASE}.pat" ]]; then +if [[ "$URL" == "file://$STORAGE/$BASE.pat" ]]; then rm -f "$PAT" else mv -f "$PAT" "$STORAGE"/"$BASE".pat @@ -313,6 +313,6 @@ mv -f "$SYSTEM" "$STORAGE"/"$BASE".system.img rm -rf "$TMP" { set +x; } 2>/dev/null -[[ "${DEBUG}" == [Yy1]* ]] && echo +[[ "$DEBUG" == [Yy1]* ]] && echo return 0 diff --git a/src/network.sh b/src/network.sh index c48ad9c..00d9046 100644 --- a/src/network.sh +++ b/src/network.sh @@ -23,37 +23,37 @@ configureDHCP() { # Create a macvtap network for the VM guest - { ip link add link "${VM_NET_DEV}" name "${VM_NET_TAP}" address "${VM_NET_MAC}" type macvtap mode bridge ; rc=$?; } || : + { ip link add link "$VM_NET_DEV" name "$VM_NET_TAP" address "$VM_NET_MAC" type macvtap mode bridge ; rc=$?; } || : if (( rc != 0 )); then error "Cannot create macvtap interface. Please make sure the network type is 'macvlan' and not 'ipvlan'," error "and that the NET_ADMIN capability has been added to the container config: --cap-add NET_ADMIN" && exit 16 fi - while ! ip link set "${VM_NET_TAP}" up; do + while ! ip link set "$VM_NET_TAP" up; do info "Waiting for address to become available..." sleep 2 done - TAP_NR=$(>"$TAP_PATH"; rc=$?; } 2>/dev/null || : if (( rc != 0 )); then error "Cannot create TAP interface ($rc). Please add the following docker settings to your " - error "container: --device-cgroup-rule='c ${MAJOR}:* rwm' --device=/dev/vhost-net" && exit 21 + error "container: --device-cgroup-rule='c $MAJOR:* rwm' --device=/dev/vhost-net" && exit 21 fi { exec 40>>/dev/vhost-net; rc=$?; } 2>/dev/null || : @@ -74,17 +74,17 @@ configureDNS () { DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-range=$VM_NET_IP,$VM_NET_IP --dhcp-host=$VM_NET_MAC,,$VM_NET_IP,$VM_NET_HOST,infinite --dhcp-option=option:netmask,255.255.255.0" # Create lease file for faster resolve - echo "0 $VM_NET_MAC $VM_NET_IP $VM_NET_HOST 01:${VM_NET_MAC}" > /var/lib/misc/dnsmasq.leases + echo "0 $VM_NET_MAC $VM_NET_IP $VM_NET_HOST 01:$VM_NET_MAC" > /var/lib/misc/dnsmasq.leases chmod 644 /var/lib/misc/dnsmasq.leases # Set DNS server and gateway DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-option=option:dns-server,${VM_NET_IP%.*}.1 --dhcp-option=option:router,${VM_NET_IP%.*}.1" DNSMASQ_OPTS=$(echo "$DNSMASQ_OPTS" | sed 's/\t/ /g' | tr -s ' ' | sed 's/^ *//') - [[ "${DEBUG}" == [Yy1]* ]] && set -x + [[ "$DEBUG" == [Yy1]* ]] && set -x $DNSMASQ ${DNSMASQ_OPTS:+ $DNSMASQ_OPTS} { set +x; } 2>/dev/null - [[ "${DEBUG}" == [Yy1]* ]] && echo + [[ "$DEBUG" == [Yy1]* ]] && echo return 0 } @@ -94,7 +94,7 @@ configureNAT () { # Create a bridge with a static IP for the VM guest VM_NET_IP='20.20.20.21' - [[ "${DEBUG}" == [Yy1]* ]] && set -x + [[ "$DEBUG" == [Yy1]* ]] && set -x { ip link add dev dockerbridge type bridge ; rc=$?; } || : @@ -111,19 +111,19 @@ configureNAT () { done # QEMU Works with taps, set tap to the bridge created - ip tuntap add dev "${VM_NET_TAP}" mode tap + ip tuntap add dev "$VM_NET_TAP" mode tap - while ! ip link set "${VM_NET_TAP}" up promisc on; do + while ! ip link set "$VM_NET_TAP" up promisc on; do info "Waiting for tap to become available..." sleep 2 done - ip link set dev "${VM_NET_TAP}" master dockerbridge + ip link set dev "$VM_NET_TAP" master dockerbridge # Add internet connection to the VM - iptables -t nat -A POSTROUTING -o "${VM_NET_DEV}" -j MASQUERADE - iptables -t nat -A PREROUTING -i "${VM_NET_DEV}" -d "${IP}" -p tcp -j DNAT --to $VM_NET_IP - iptables -t nat -A PREROUTING -i "${VM_NET_DEV}" -d "${IP}" -p udp -j DNAT --to $VM_NET_IP + iptables -t nat -A POSTROUTING -o "$VM_NET_DEV" -j MASQUERADE + iptables -t nat -A PREROUTING -i "$VM_NET_DEV" -d "$IP" -p tcp -j DNAT --to "$VM_NET_IP" + iptables -t nat -A PREROUTING -i "$VM_NET_DEV" -d "$IP" -p udp -j DNAT --to "$VM_NET_IP" if (( KERNEL > 4 )); then # Hack for guest VMs complaining about "bad udp checksums in 5 packets" @@ -131,7 +131,7 @@ configureNAT () { fi { set +x; } 2>/dev/null - [[ "${DEBUG}" == [Yy1]* ]] && echo + [[ "$DEBUG" == [Yy1]* ]] && echo # Check port forwarding flag if [[ $(< /proc/sys/net/ipv4/ip_forward) -eq 0 ]]; then @@ -141,7 +141,7 @@ configureNAT () { fi fi - NET_OPTS="-netdev tap,ifname=${VM_NET_TAP},script=no,downscript=no,id=hostnet0" + NET_OPTS="-netdev tap,ifname=$VM_NET_TAP,script=no,downscript=no,id=hostnet0" { exec 40>>/dev/vhost-net; rc=$?; } 2>/dev/null || : (( rc == 0 )) && NET_OPTS="$NET_OPTS,vhost=on,vhostfd=40" @@ -153,15 +153,15 @@ configureNAT () { closeNetwork () { - if [[ "${DHCP}" == [Yy1]* ]]; then + if [[ "$DHCP" == [Yy1]* ]]; then - ip link set "${VM_NET_TAP}" down || true - ip link delete "${VM_NET_TAP}" || true + ip link set "$VM_NET_TAP" down || true + ip link delete "$VM_NET_TAP" || true else - ip link set "${VM_NET_TAP}" down promisc off || true - ip link delete "${VM_NET_TAP}" || true + ip link set "$VM_NET_TAP" down promisc off || true + ip link delete "$VM_NET_TAP" || true ip link set dockerbridge down || true ip link delete dockerbridge || true @@ -195,16 +195,16 @@ update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy > /dev/null VM_NET_MAC="${VM_NET_MAC//-/:}" GATEWAY=$(ip r | grep default | awk '{print $3}') -IP=$(ip address show dev "${VM_NET_DEV}" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/) +IP=$(ip address show dev "$VM_NET_DEV" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/) -if [[ "${DEBUG}" == [Yy1]* ]]; then - info "Container IP is ${IP} with gateway ${GATEWAY}" && echo +if [[ "$DEBUG" == [Yy1]* ]]; then + info "Container IP is $IP with gateway $GATEWAY" && echo fi -if [[ "${DHCP}" == [Yy1]* ]]; then +if [[ "$DHCP" == [Yy1]* ]]; then if [[ "$GATEWAY" == "172."* ]]; then - if [[ "${DEBUG}" == [Yy1]* ]]; then + if [[ "$DEBUG" == [Yy1]* ]]; then info "Warning: Are you sure the container is on a macvlan network?" else error "You can only enable DHCP while the container is on a macvlan network!" && exit 86 @@ -224,6 +224,6 @@ else fi -NET_OPTS="${NET_OPTS} -device virtio-net-pci,romfile=,netdev=hostnet0,mac=${VM_NET_MAC},id=net0" +NET_OPTS="$NET_OPTS -device virtio-net-pci,romfile=,netdev=hostnet0,mac=$VM_NET_MAC,id=net0" return 0 diff --git a/src/power.sh b/src/power.sh index 201359a..22e0fa9 100644 --- a/src/power.sh +++ b/src/power.sh @@ -8,8 +8,8 @@ QEMU_TIMEOUT=50 QEMU_PID=/run/qemu.pid QEMU_COUNT=/run/qemu.count -rm -f "${QEMU_PID}" -rm -f "${QEMU_COUNT}" +rm -f "$QEMU_PID" +rm -f "$QEMU_COUNT" _trap(){ func="$1" ; shift @@ -22,10 +22,10 @@ _graceful_shutdown() { set +e - [ ! -f "${QEMU_PID}" ] && exit 130 - [ -f "${QEMU_COUNT}" ] && return + [ ! -f "$QEMU_PID" ] && exit 130 + [ -f "$QEMU_COUNT" ] && return - echo 0 > "${QEMU_COUNT}" + echo 0 > "$QEMU_COUNT" echo && info "Received $1 signal, shutting down..." # Don't send the powerdown signal because vDSM ignores ACPI signals @@ -34,34 +34,34 @@ _graceful_shutdown() { # Send shutdown command to guest agent via serial port RESPONSE=$(curl -sk -m 30 -S http://127.0.0.1:2210/read?command=6 2>&1) - if [[ ! "${RESPONSE}" =~ "\"success\"" ]]; then + if [[ ! "$RESPONSE" =~ "\"success\"" ]]; then echo && error "Failed to send shutdown command ( ${RESPONSE} )." - kill -15 "$(cat "${QEMU_PID}")" + kill -15 "$(cat "$QEMU_PID")" pkill -f qemu-system-x86_64 || true fi - while [ "$(cat ${QEMU_COUNT})" -lt "${QEMU_TIMEOUT}" ]; do + while [ "$(cat $QEMU_COUNT)" -lt "$QEMU_TIMEOUT" ]; do # Increase the counter - echo $(($(cat ${QEMU_COUNT})+1)) > ${QEMU_COUNT} + echo $(($(cat $QEMU_COUNT)+1)) > $QEMU_COUNT # Try to connect to qemu - if echo 'info version'| nc -q 1 -w 1 localhost "${QEMU_PORT}" >/dev/null 2>&1 ; then + if echo 'info version'| nc -q 1 -w 1 localhost "$QEMU_PORT" >/dev/null 2>&1 ; then sleep 1 - CNT="$(cat ${QEMU_COUNT})/${QEMU_TIMEOUT}" - [[ "${DEBUG}" == [Yy1]* ]] && info "Shutting down, waiting... (${CNT})" + CNT="$(cat $QEMU_COUNT)/$QEMU_TIMEOUT" + [[ "$DEBUG" == [Yy1]* ]] && info "Shutting down, waiting... ($CNT)" fi done echo && echo "❯ Quitting..." - echo 'quit' | nc -q 1 -w 1 localhost "${QEMU_PORT}" >/dev/null 2>&1 || true + echo 'quit' | nc -q 1 -w 1 localhost "$QEMU_PORT" >/dev/null 2>&1 || true closeNetwork @@ -70,4 +70,4 @@ _graceful_shutdown() { _trap _graceful_shutdown SIGTERM SIGHUP SIGINT SIGABRT SIGQUIT -MON_OPTS="-monitor telnet:localhost:${QEMU_PORT},server,nowait,nodelay" +MON_OPTS="-monitor telnet:localhost:$QEMU_PORT,server,nowait,nodelay" diff --git a/src/print.sh b/src/print.sh index 1a6f998..8b31f0b 100644 --- a/src/print.sh +++ b/src/print.sh @@ -23,20 +23,20 @@ do if [[ "$result" != "success" ]] ; then { msg=$(echo "$json" | jq -r '.message'); rc=$?; } || : - error "Guest replied ${result}: $msg" && continue + error "Guest replied $result: $msg" && continue fi { port=$(echo "$json" | jq -r '.data.data.dsm_setting.data.http_port'); rc=$?; } || : (( rc != 0 )) && error "Failed to parse response from guest: jq error $rc ( $json )" && continue [[ "$port" == "null" ]] && error "Guest returned invalid response: $json" && continue - [ -z "${port}" ] && continue + [ -z "$port" ] && continue { ip=$(echo "$json" | jq -r '.data.data.ip.data[] | select((.name=="eth0") and has("ip")).ip'); rc=$?; } || : (( rc != 0 )) && error "Failed to parse response from guest: jq error $rc ( $json )" && continue [[ "$ip" == "null" ]] && error "Guest returned invalid response: $json" && continue - [ -z "${ip}" ] && continue + [ -z "$ip" ] && continue - echo "${ip}:${port}" > $file + echo "$ip:$port" > $file done @@ -44,7 +44,7 @@ location=$(cat "$file") if [[ "$location" != "20.20"* ]]; then - msg="http://${location}" + msg="http://$location" else @@ -52,15 +52,15 @@ else port="${location##*:}" if [[ "$ip" == "172."* ]]; then - msg="port ${port}" + msg="port $port" else - msg="http://${ip}:${port}" + msg="http://$ip:$port" fi fi echo "" >&2 -info "--------------------------------------------------------" -info " You can now login to DSM at ${msg}" -info "--------------------------------------------------------" +info "-----------------------------------------------------------" +info " You can now login to DSM at $msg" +info "-----------------------------------------------------------" echo "" >&2 diff --git a/src/reset.sh b/src/reset.sh index b5e7035..23d7fb3 100644 --- a/src/reset.sh +++ b/src/reset.sh @@ -4,7 +4,7 @@ set -Eeuo pipefail info () { printf "%b%s%b" "\E[1;34m❯ \E[1;36m" "$1" "\E[0m\n"; } error () { printf "%b%s%b" "\E[1;31m❯ " "ERROR: $1" "\E[0m\n" >&2; } -trap 'error "Status $? while: ${BASH_COMMAND} (line $LINENO/$BASH_LINENO)"' ERR +trap 'error "Status $? while: $BASH_COMMAND (line $LINENO/$BASH_LINENO)"' ERR [ ! -f "/run/entry.sh" ] && error "Script must run inside Docker container!" && exit 11 [ "$(id -u)" -ne "0" ] && error "Script must be executed with root privileges." && exit 12 @@ -31,7 +31,7 @@ VERS=$(qemu-system-x86_64 --version | head -n 1 | cut -d '(' -f 1) # Check folder STORAGE="/storage" -[ ! -d "$STORAGE" ] && error "Storage folder (${STORAGE}) not found!" && exit 13 +[ ! -d "$STORAGE" ] && error "Storage folder ($STORAGE) not found!" && exit 13 # Cleanup files diff --git a/src/serial.sh b/src/serial.sh index 39a793d..924497c 100644 --- a/src/serial.sh +++ b/src/serial.sh @@ -25,15 +25,15 @@ else fi HOST_ARGS=() -HOST_ARGS+=("-cpu=${CPU_CORES}") -HOST_ARGS+=("-cpu_arch=${HOST_CPU}") +HOST_ARGS+=("-cpu=$CPU_CORES") +HOST_ARGS+=("-cpu_arch=$HOST_CPU") -[ -n "$HOST_MAC" ] && HOST_ARGS+=("-mac=${HOST_MAC}") -[ -n "$HOST_MODEL" ] && HOST_ARGS+=("-model=${HOST_MODEL}") -[ -n "$HOST_SERIAL" ] && HOST_ARGS+=("-hostsn=${HOST_SERIAL}") -[ -n "$GUEST_SERIAL" ] && HOST_ARGS+=("-guestsn=${GUEST_SERIAL}") +[ -n "$HOST_MAC" ] && HOST_ARGS+=("-mac=$HOST_MAC") +[ -n "$HOST_MODEL" ] && HOST_ARGS+=("-model=$HOST_MODEL") +[ -n "$HOST_SERIAL" ] && HOST_ARGS+=("-hostsn=$HOST_SERIAL") +[ -n "$GUEST_SERIAL" ] && HOST_ARGS+=("-guestsn=$GUEST_SERIAL") -if [[ "${HOST_DEBUG}" == [Yy1]* ]]; then +if [[ "$HOST_DEBUG" == [Yy1]* ]]; then set -x ./host.bin "${HOST_ARGS[@]}" & { set +x; } 2>/dev/null diff --git a/src/server.sh b/src/server.sh index 4f5f91a..267c8f2 100644 --- a/src/server.sh +++ b/src/server.sh @@ -14,9 +14,9 @@ trap 'stop' EXIT SIGINT SIGTERM SIGHUP html() { local h="VirtualDSM" - h="${h} " - h="${h}

$1

" + h="$h" + h="$h

$1

" echo "$h" } @@ -33,8 +33,8 @@ if [[ "$2" != "/"* ]]; then HTML=$(html "$BODY") printf '%b' "HTTP/1.1 200 OK\nContent-Length: ${#HTML}\nConnection: close\n\n$HTML" > "$TMP_FILE" - socat TCP4-LISTEN:80,reuseaddr,fork,crlf SYSTEM:"cat ${TMP_FILE}" 2> /dev/null & - socat TCP4-LISTEN:"${1:-5000}",reuseaddr,fork,crlf SYSTEM:"cat ${TMP_FILE}" 2> /dev/null & wait $! + socat TCP4-LISTEN:80,reuseaddr,fork,crlf SYSTEM:"cat $TMP_FILE" 2> /dev/null & + socat TCP4-LISTEN:"${1:-5000}",reuseaddr,fork,crlf SYSTEM:"cat $TMP_FILE" 2> /dev/null & wait $! exit @@ -46,15 +46,15 @@ if [[ "$2" != "/run/ip.sh" ]]; then else - BODY="The location of DSM is http://\${LOCATION}" + BODY="The location of DSM is http://\$LOCATION" WAIT="Please wait while discovering IP..." HTML=$(html "xxx") { echo "#!/bin/bash" echo "[ -f \"/run/dsm.url\" ] && LOCATION=\$(cat \"/run/dsm.url\")" - echo "HTML=\"$HTML\"; [ -z \"\${LOCATION}\" ] && BODY=\"$WAIT\" || BODY=\"$BODY\"; HTML=\${HTML/xxx/\$BODY}" + echo "HTML=\"$HTML\"; [ -z \"\$LOCATION\" ] && BODY=\"$WAIT\" || BODY=\"$BODY\"; HTML=\${HTML/xxx/\$BODY}" echo "printf '%b' \"HTTP/1.1 200 OK\\nContent-Length: \${#HTML}\\nConnection: close\\n\\n\$HTML\"" } > "$TMP_FILE"