From 89e46404f5c45611a5afdea0f5b00f25b4928e4d Mon Sep 17 00:00:00 2001 From: Kroese Date: Wed, 5 Jun 2024 17:53:12 +0200 Subject: [PATCH] feat: Use virtioblk for CD-ROM devices (#102) --- src/boot.sh | 6 ++--- src/config.sh | 4 +-- src/disk.sh | 68 ++++++++++++++++++++++++++++++-------------------- src/install.sh | 14 ++++++----- src/network.sh | 14 +++++------ src/proc.sh | 2 +- src/reset.sh | 4 +-- 7 files changed, 64 insertions(+), 48 deletions(-) diff --git a/src/boot.sh b/src/boot.sh index 1967eaf..0a6265e 100644 --- a/src/boot.sh +++ b/src/boot.sh @@ -40,7 +40,7 @@ esac if [ -n "$BIOS" ]; then - BOOT_OPTS="$BOOT_OPTS -bios $DIR/$BIOS" + BOOT_OPTS+=" -bios $DIR/$BIOS" return 0 fi @@ -62,7 +62,7 @@ if [ ! -s "$DEST.vars" ] || [ ! -f "$DEST.vars" ]; then dd "if=$AAVMF/$VARS" "of=$DEST.vars" conv=notrunc status=none fi -BOOT_OPTS="$BOOT_OPTS -drive file=$DEST.rom,if=pflash,unit=0,format=raw,readonly=on" -BOOT_OPTS="$BOOT_OPTS -drive file=$DEST.vars,if=pflash,unit=1,format=raw" +BOOT_OPTS+=" -drive file=$DEST.rom,if=pflash,unit=0,format=raw,readonly=on" +BOOT_OPTS+=" -drive file=$DEST.vars,if=pflash,unit=1,format=raw" return 0 diff --git a/src/config.sh b/src/config.sh index c79c8c3..83898bc 100644 --- a/src/config.sh +++ b/src/config.sh @@ -13,8 +13,8 @@ CPU_OPTS="-cpu $CPU_FLAGS -smp $CPU_CORES,sockets=1,dies=1,cores=$CPU_CORES,thre MON_OPTS="-monitor $MONITOR -name $PROCESS,process=$PROCESS,debug-threads=on" MAC_OPTS="-machine type=${MACHINE},secure=${SECURE},dump-guest-core=off${KVM_OPTS}" DEV_OPTS="-object rng-random,id=objrng0,filename=/dev/urandom" -DEV_OPTS="$DEV_OPTS -device virtio-rng-pci,rng=objrng0,id=rng0,bus=pcie.0,addr=0x1c" -[[ "${BOOT_MODE,,}" != "windows"* ]] && DEV_OPTS="$DEV_OPTS -device virtio-balloon-pci,id=balloon0,bus=pcie.0,addr=0x4" +DEV_OPTS+=" -device virtio-rng-pci,rng=objrng0,id=rng0,bus=pcie.0,addr=0x1c" +[[ "${BOOT_MODE,,}" != "windows"* ]] && DEV_OPTS+=" -device virtio-balloon-pci,id=balloon0,bus=pcie.0,addr=0x4" ARGS="$DEF_OPTS $CPU_OPTS $RAM_OPTS $MAC_OPTS $DISPLAY_OPTS $MON_OPTS $SERIAL_OPTS $USB_OPTS $NET_OPTS $DISK_OPTS $BOOT_OPTS $DEV_OPTS $ARGUMENTS" ARGS=$(echo "$ARGS" | sed 's/\t/ /g' | tr -s ' ') diff --git a/src/disk.sh b/src/disk.sh index a4b6948..7fdf1c1 100644 --- a/src/disk.sh +++ b/src/disk.sh @@ -84,6 +84,7 @@ supportsDirect() { } createDisk() { + local DISK_FILE=$1 local DISK_SPACE=$2 local DISK_DESC=$3 @@ -146,8 +147,8 @@ createDisk() { qcow2) local DISK_PARAM="$DISK_ALLOC" - isCow "$FS" && DISK_PARAM="$DISK_PARAM,nocow=on" - [ -n "$DISK_FLAGS" ] && DISK_PARAM="$DISK_PARAM,$DISK_FLAGS" + isCow "$FS" && DISK_PARAM+=",nocow=on" + [ -n "$DISK_FLAGS" ] && DISK_PARAM+=",$DISK_FLAGS" if ! qemu-img create -f "$DISK_FMT" -o "$DISK_PARAM" -- "$DISK_FILE" "$DATA_SIZE" ; then rm -f "$DISK_FILE" @@ -167,6 +168,7 @@ createDisk() { } resizeDisk() { + local DISK_FILE=$1 local DISK_SPACE=$2 local DISK_DESC=$3 @@ -232,6 +234,7 @@ resizeDisk() { } convertDisk() { + local SOURCE_FILE=$1 local SOURCE_FMT=$2 local DST_FILE=$3 @@ -262,18 +265,19 @@ convertDisk() { fi fi - html "Converting $DISK_DESC to $DST_FMT..." - info "Converting $DISK_DESC to $DST_FMT, please wait until completed..." + local msg="Converting $DISK_DESC to $DST_FMT" + html "$msg..." + info "$msg, please wait until completed..." local CONV_FLAGS="-p" local DISK_PARAM="$DISK_ALLOC" - isCow "$FS" && DISK_PARAM="$DISK_PARAM,nocow=on" + isCow "$FS" && DISK_PARAM+=",nocow=on" if [[ "$DST_FMT" != "raw" ]]; then if [[ "$ALLOCATE" == [Nn]* ]]; then - CONV_FLAGS="$CONV_FLAGS -c" + CONV_FLAGS+=" -c" fi - [ -n "$DISK_FLAGS" ] && DISK_PARAM="$DISK_PARAM,$DISK_FLAGS" + [ -n "$DISK_FLAGS" ] && DISK_PARAM+=",$DISK_FLAGS" fi # shellcheck disable=SC2086 @@ -302,13 +306,15 @@ convertDisk() { fi fi - html "Conversion of $DISK_DESC completed..." - info "Conversion of $DISK_DESC to $DST_FMT completed succesfully!" + msg="Conversion of $DISK_DESC" + html "$msg completed..." + info "$msg to $DST_FMT completed succesfully!" return 0 } checkFS () { + local FS=$1 local DISK_FILE=$2 local DISK_DESC=$3 @@ -342,6 +348,7 @@ checkFS () { } createDevice () { + local DISK_FILE=$1 local DISK_TYPE=$2 local DISK_INDEX=$3 @@ -360,22 +367,22 @@ createDevice () { echo "$result" ;; "usb" ) - result="$result,if=none \ + result+=",if=none \ -device usb-storage,drive=${DISK_ID}${index}" echo "$result" ;; "ide" ) - result="$result,if=none \ + result+=",if=none \ -device ide-hd,drive=${DISK_ID},bus=ide.$DISK_INDEX,rotation_rate=$DISK_ROTATION${index}" echo "$result" ;; "blk" | "virtio-blk" ) - result="$result,if=none \ + result+=",if=none \ -device virtio-blk-pci,drive=${DISK_ID},scsi=off,bus=pcie.0,addr=$DISK_ADDRESS,iothread=io2${index}" echo "$result" ;; "scsi" | "virtio-scsi" ) - result="$result,if=none \ + result+=",if=none \ -device virtio-scsi-pci,id=${DISK_ID}b,bus=pcie.0,addr=$DISK_ADDRESS,iothread=io2 \ -device scsi-hd,drive=${DISK_ID},bus=${DISK_ID}b.0,channel=0,scsi-id=0,lun=0,rotation_rate=$DISK_ROTATION${index}" echo "$result" @@ -386,6 +393,7 @@ createDevice () { } addMedia () { + local DISK_FILE=$1 local DISK_TYPE=$2 local DISK_BUS=$3 @@ -402,22 +410,22 @@ addMedia () { echo "$result" ;; "usb" ) - result="$result,if=none \ + result+=",if=none \ -device usb-storage,drive=${DISK_ID}${index},removable=on" echo "$result" ;; "ide" ) - result="$result,if=none \ + result+=",if=none \ -device ide-cd,drive=${DISK_ID},bus=ide.${DISK_BUS}${index}" echo "$result" ;; "blk" | "virtio-blk" ) - result="$result,if=none \ + result+=",if=none \ -device virtio-blk-pci,drive=${DISK_ID},scsi=off,bus=pcie.0,addr=$DISK_ADDRESS,iothread=io2${index}" echo "$result" ;; "scsi" | "virtio-scsi" ) - result="$result,if=none \ + result+=",if=none \ -device virtio-scsi-pci,id=${DISK_ID}b,bus=pcie.0,addr=$DISK_ADDRESS,iothread=io2 \ -device scsi-cd,drive=${DISK_ID},bus=${DISK_ID}b.0${index}" echo "$result" @@ -428,6 +436,7 @@ addMedia () { } addDisk () { + local DISK_BASE=$1 local DISK_TYPE=$2 local DISK_DESC=$3 @@ -468,6 +477,7 @@ addDisk () { else PREV_FMT="qcow2" fi + PREV_EXT=$(fmt2ext "$PREV_FMT") if [ -s "$DISK_BASE.$PREV_EXT" ] ; then @@ -490,12 +500,13 @@ addDisk () { fi OPTS=$(createDevice "$DISK_FILE" "$DISK_TYPE" "$DISK_INDEX" "$DISK_ADDRESS" "$DISK_FMT" "$DISK_IO" "$DISK_CACHE") - DISK_OPTS="$DISK_OPTS $OPTS" + DISK_OPTS+=" $OPTS" return 0 } addDevice () { + local DISK_DEV=$1 local DISK_TYPE=$2 local DISK_INDEX=$3 @@ -506,31 +517,34 @@ addDevice () { local OPTS OPTS=$(createDevice "$DISK_DEV" "$DISK_TYPE" "$DISK_INDEX" "$DISK_ADDRESS" "raw" "$DISK_IO" "$DISK_CACHE") - DISK_OPTS="$DISK_OPTS $OPTS" + DISK_OPTS+=" $OPTS" return 0 } -DISK_OPTS="" html "Initializing disks..." +[ -z "${DISK_OPTS:-}" ] && DISK_OPTS="" +[ -z "${DISK_TYPE:-}" ] && DISK_TYPE="scsi" + case "${DISK_TYPE,,}" in - "blk" ) MEDIA_TYPE="auto" ;; - "" ) DISK_TYPE="scsi" && MEDIA_TYPE="$DISK_TYPE" ;; - "auto" | "ide" | "usb" | "scsi" ) MEDIA_TYPE="$DISK_TYPE" ;; + "ide" | "usb" | "scsi" | "blk" | "auto" ) ;; * ) error "Invalid DISK_TYPE, value \"$DISK_TYPE\" is unrecognized!" && exit 80 ;; esac +[[ "${MACHINE,,}" != "virt" ]] && MEDIA_TYPE="ide" || MEDIA_TYPE="auto" + if [ -f "$BOOT" ] && [ -s "$BOOT" ]; then - DISK_OPTS=$(addMedia "$BOOT" "$MEDIA_TYPE" "0" "$BOOT_INDEX" "0x5") + ADD_OPTS=$(addMedia "$BOOT" "$MEDIA_TYPE" "0" "$BOOT_INDEX" "0x5") + DISK_OPTS+=" $ADD_OPTS" fi DRIVERS="/drivers.iso" [ ! -f "$DRIVERS" ] || [ ! -s "$DRIVERS" ] && DRIVERS="$STORAGE/drivers.iso" if [ -f "$DRIVERS" ] && [ -s "$DRIVERS" ]; then - DRIVER_OPTS=$(addMedia "$DRIVERS" "auto" "1" "" "0x6") - DISK_OPTS="$DISK_OPTS $DRIVER_OPTS" + ADD_OPTS=$(addMedia "$DRIVERS" "$MEDIA_TYPE" "1" "" "0x6") + DISK_OPTS+=" $ADD_OPTS" fi DISK1_FILE="$STORAGE/data" @@ -602,7 +616,7 @@ else addDisk "$DISK4_FILE" "$DISK_TYPE" "disk4" "$DISK4_SIZE" "6" "0xd" "$DISK_FMT" "$DISK_IO" "$DISK_CACHE" || exit $? fi -DISK_OPTS="$DISK_OPTS -object iothread,id=io2" +DISK_OPTS+=" -object iothread,id=io2" html "Initialized disks successfully..." diff --git a/src/install.sh b/src/install.sh index c3739b5..b520257 100644 --- a/src/install.sh +++ b/src/install.sh @@ -37,17 +37,19 @@ else progress="--progress=dot:giga" fi -msg="Downloading $base..." -info "$msg" && html "$msg" +msg="Downloading $base" +info "$msg..." && html "$msg..." -/run/progress.sh "$TMP" "" "Downloading $base ([P])..." & +/run/progress.sh "$TMP" "" "$msg ([P])..." & { wget "$BOOT" -O "$TMP" -q --timeout=30 --show-progress "$progress"; rc=$?; } || : fKill "progress.sh" -(( rc == 4 )) && error "Failed to download $BOOT , network failure!" && exit 60 -(( rc != 0 )) && error "Failed to download $BOOT , reason: $rc" && exit 60 -[ ! -s "$TMP" ] && error "Failed to download $BOOT" && exit 61 +msg="Failed to download $BOOT" +(( rc == 4 )) && error "$msg , network failure!" && exit 60 +(( rc == 8 )) && error "$msg , server issued an error response!" && exit 60 +(( rc != 0 )) && error "$msg , reason: $rc" && exit 60 +[ ! -s "$TMP" ] && error "$msg" && exit 61 html "Download finished successfully..." diff --git a/src/network.sh b/src/network.sh index 170ee13..c90c04b 100644 --- a/src/network.sh +++ b/src/network.sh @@ -81,17 +81,17 @@ configureDHCP() { configureDNS() { # dnsmasq configuration: - 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" + 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 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+=" --dhcp-option=option:dns-server,${VM_NET_IP%.*}.1 --dhcp-option=option:router,${VM_NET_IP%.*}.1" # Add DNS entry for container - DNSMASQ_OPTS="$DNSMASQ_OPTS --address=/host.lan/${VM_NET_IP%.*}.1" + DNSMASQ_OPTS+=" --address=/host.lan/${VM_NET_IP%.*}.1" DNSMASQ_OPTS=$(echo "$DNSMASQ_OPTS" | sed 's/\t/ /g' | tr -s ' ' | sed 's/^ *//') [[ "$DEBUG" == [Yy1]* ]] && set -x @@ -111,10 +111,10 @@ getPorts() { local vnc="5900" local web="8006" - [ -z "$list" ] && list="$web" || list="$list,$web" + [ -z "$list" ] && list="$web" || list+=",$web" if [[ "${DISPLAY,,}" == "vnc" ]] || [[ "${DISPLAY,,}" == "web" ]]; then - [ -z "$list" ] && list="$vnc" || list="$list,$vnc" + [ -z "$list" ] && list="$vnc" || list+=",$vnc" fi [ -z "$list" ] && echo "" && return 0 @@ -203,7 +203,7 @@ configureNAT() { if [ -c /dev/vhost-net ]; then { exec 40>>/dev/vhost-net; rc=$?; } 2>/dev/null || : - (( rc == 0 )) && NET_OPTS="$NET_OPTS,vhost=on,vhostfd=40" + (( rc == 0 )) && NET_OPTS+=",vhost=on,vhostfd=40" fi configureDNS @@ -346,7 +346,7 @@ else fi -NET_OPTS="$NET_OPTS -device virtio-net-pci,romfile=,netdev=hostnet0,mac=$VM_NET_MAC,id=net0" +NET_OPTS+=" -device virtio-net-pci,romfile=,netdev=hostnet0,mac=$VM_NET_MAC,id=net0" html "Initialized network successfully..." return 0 diff --git a/src/proc.sh b/src/proc.sh index 6a865ea..a8c9cfd 100644 --- a/src/proc.sh +++ b/src/proc.sh @@ -76,7 +76,7 @@ else fi if [[ "${BOOT_MODE,,}" == "windows" ]]; then - MACHINE="$MACHINE,virtualization=on" + MACHINE+=",virtualization=on" fi fi diff --git a/src/reset.sh b/src/reset.sh index b58ddbf..6551308 100644 --- a/src/reset.sh +++ b/src/reset.sh @@ -17,7 +17,7 @@ echo "❯ For support visit $SUPPORT" : "${BOOT:=""}" # URL of the ISO file : "${DEBUG:="N"}" # Disable debugging -: "${MACHINE:="virt"}" # Machine selection +: "${MACHINE:="virt"}" # Machine selection : "${ALLOCATE:=""}" # Preallocate diskspace : "${ARGUMENTS:=""}" # Extra QEMU parameters : "${CPU_CORES:="1"}" # Amount of CPU cores @@ -25,7 +25,7 @@ echo "❯ For support visit $SUPPORT" : "${RAM_CHECK:="Y"}" # Check available RAM : "${DISK_SIZE:="16G"}" # Initial data disk size : "${BOOT_INDEX:="10"}" # Boot index of CD drive -: "${BOOT_MODE:="uefi"}" # Boot in UEFI mode +: "${BOOT_MODE:="uefi"}" # Boot in UEFI mode # Helper variables