mirror of
				https://github.com/oSoWoSo/DistroHopper.git
				synced 2024-08-14 22:46:53 +00:00 
			
		
		
		
	Quote all file/directory variables. Closes #12
File handling is more robust and will support filename and directories with spaces in them.
This commit is contained in:
		
							parent
							
								
									142eac3001
								
							
						
					
					
						commit
						735e3a4ee8
					
				
					 1 changed files with 66 additions and 29 deletions
				
			
		
							
								
								
									
										95
									
								
								quickemu
									
										
									
									
									
								
							
							
						
						
									
										95
									
								
								quickemu
									
										
									
									
									
								
							|  | @ -87,8 +87,8 @@ function get_port() { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function vm_boot() { | function vm_boot() { | ||||||
|   local VMNAME=$(basename ${VM} .conf) |   local VMNAME=$(basename "${VM}" .conf) | ||||||
|   local VMDIR=$(dirname ${disk_img}) |   local VMDIR=$(dirname "${disk_img}") | ||||||
|   local BIOS="" |   local BIOS="" | ||||||
|   local CPU="-cpu host,kvm=on" |   local CPU="-cpu host,kvm=on" | ||||||
|   local GUEST_TWEAKS="" |   local GUEST_TWEAKS="" | ||||||
|  | @ -106,10 +106,11 @@ function vm_boot() { | ||||||
|   boot=$(echo ${boot,,}) |   boot=$(echo ${boot,,}) | ||||||
|   if [ "${boot}" == "efi" ] || [ "${boot}" == "uefi" ]; then |   if [ "${boot}" == "efi" ] || [ "${boot}" == "uefi" ]; then | ||||||
|     if [ -e "${VIRGIL_PATH}/usr/share/qemu/edk2-x86_64-code.fd" ] ; then |     if [ -e "${VIRGIL_PATH}/usr/share/qemu/edk2-x86_64-code.fd" ] ; then | ||||||
|       if [ ! -e "${VMDIR}/${VMNAME}-vars.fd" ]; then |       local EFI_CODE="${VIRGIL_PATH}/usr/share/qemu/edk2-x86_64-code.fd" | ||||||
|         cp "${VIRGIL_PATH}/usr/share/qemu/edk2-i386-vars.fd" "${VMDIR}/${VMNAME}-vars.fd" |       local EFI_VARS="${VMDIR}/${VMNAME}-vars.fd" | ||||||
|  |       if [ ! -e "${EFI_VARS}" ]; then | ||||||
|  |         cp "${VIRGIL_PATH}/usr/share/qemu/edk2-i386-vars.fd" "${EFI_VARS}" | ||||||
|       fi |       fi | ||||||
|       BIOS="-drive if=pflash,format=raw,readonly,file=${VIRGIL_PATH}/usr/share/qemu/edk2-x86_64-code.fd -drive if=pflash,format=raw,file=${VMDIR}/${VMNAME}-vars.fd" |  | ||||||
|       echo " - BOOT:     EFI" |       echo " - BOOT:     EFI" | ||||||
|     else |     else | ||||||
|       echo " - BOOT:     Legacy BIOS" |       echo " - BOOT:     Legacy BIOS" | ||||||
|  | @ -141,7 +142,12 @@ function vm_boot() { | ||||||
|   echo " - Disk:     ${disk_img} (${disk})" |   echo " - Disk:     ${disk_img} (${disk})" | ||||||
|   if [ ! -f "${disk_img}" ]; then |   if [ ! -f "${disk_img}" ]; then | ||||||
|       # If there is no disk image, create a new image. |       # If there is no disk image, create a new image. | ||||||
|  |       mkdir -p "${VMDIR}" 2>/dev/null | ||||||
|       ${QEMU_IMG} create -q -f qcow2 "${disk_img}" "${disk}" |       ${QEMU_IMG} create -q -f qcow2 "${disk_img}" "${disk}" | ||||||
|  |       if [ $? -ne 0 ]; then | ||||||
|  |         echo "ERROR! Failed to create ${disk_img}" | ||||||
|  |         exit 1 | ||||||
|  |       fi | ||||||
|       if [ -z "${iso}" ]; then |       if [ -z "${iso}" ]; then | ||||||
|         echo "ERROR! You haven't specified a .iso image to boot from." |         echo "ERROR! You haven't specified a .iso image to boot from." | ||||||
|         exit 1 |         exit 1 | ||||||
|  | @ -151,9 +157,9 @@ function vm_boot() { | ||||||
|       echo "ERROR! Failed to create ${disk_img} of ${disk}. Stopping here." |       echo "ERROR! Failed to create ${disk_img} of ${disk}. Stopping here." | ||||||
|       exit 1 |       exit 1 | ||||||
|     fi |     fi | ||||||
|   elif [ -e ${disk_img} ]; then |   elif [ -e "${disk_img}" ]; then | ||||||
|     # Check there isn't already a process attached to the disk image. |     # Check there isn't already a process attached to the disk image. | ||||||
|     QEMU_LOCK_TEST=$(${QEMU_IMG} info ${disk_img} 2>/dev/null) |     QEMU_LOCK_TEST=$(${QEMU_IMG} info "${disk_img}" 2>/dev/null) | ||||||
|     if [ $? -ne 0 ]; then |     if [ $? -ne 0 ]; then | ||||||
|       echo "             Failed to get "write" lock. Is another process using the disk?" |       echo "             Failed to get "write" lock. Is another process using the disk?" | ||||||
|       exit 1 |       exit 1 | ||||||
|  | @ -179,12 +185,10 @@ function vm_boot() { | ||||||
|   fi |   fi | ||||||
| 
 | 
 | ||||||
|   if [ -n "${iso}" ] && [ -e "${iso}" ]; then |   if [ -n "${iso}" ] && [ -e "${iso}" ]; then | ||||||
|     local ISO_BOOT="-drive media=cdrom,file=${iso},index=0" |  | ||||||
|     echo " - Boot:     ${iso}" |     echo " - Boot:     ${iso}" | ||||||
|   fi |   fi | ||||||
| 
 | 
 | ||||||
|   if [ -n "${driver_iso}" ] && [ -e "${driver_iso}" ]; then |   if [ -n "${driver_iso}" ] && [ -e "${driver_iso}" ]; then | ||||||
|     local ISO_DRIVER="-drive media=cdrom,file=${driver_iso},index=1" |  | ||||||
|     echo " - Drivers:  ${driver_iso}" |     echo " - Drivers:  ${driver_iso}" | ||||||
|   fi |   fi | ||||||
| 
 | 
 | ||||||
|  | @ -272,24 +276,57 @@ function vm_boot() { | ||||||
|   fi |   fi | ||||||
| 
 | 
 | ||||||
|   # Boot the iso image |   # Boot the iso image | ||||||
|   ${QEMU} \ |   if [ "${boot}" == "efi" ] || [ "${boot}" == "uefi" ]; then | ||||||
|     -name ${VMNAME},process=${VMNAME} \ |     ${QEMU} \ | ||||||
|     -enable-kvm -machine q35 ${BIOS} ${GUEST_TWEAKS} \ |       -name ${VMNAME},process=${VMNAME} \ | ||||||
|     ${CPU} -smp ${CORES_VM} \ |       -enable-kvm -machine q35 ${GUEST_TWEAKS} \ | ||||||
|     -m ${RAM_VM} -device virtio-balloon \ |       ${CPU} -smp ${CORES_VM} \ | ||||||
|     -drive if=none,id=drive0,cache=directsync,aio=native,format=qcow2,file=${disk_img} \ |       -m ${RAM_VM} -device virtio-balloon \ | ||||||
|     -device virtio-blk-pci,drive=drive0,scsi=off ${STATUS_QUO} \ |       -drive if=pflash,format=raw,readonly,file="${EFI_CODE}" \ | ||||||
|     ${ISO_BOOT} ${ISO_DRIVER} \ |       -drive if=pflash,format=raw,file="${EFI_VARS}" \ | ||||||
|     ${VIDEO} -display ${OUTPUT},gl=${GL}${OUTPUT_EXTRA} \ |       -drive media=cdrom,index=0,file="${iso}" \ | ||||||
|     -device qemu-xhci,id=xhci,p2=8,p3=8 -device usb-kbd,bus=xhci.0 -device usb-tablet,bus=xhci.0 \ |       -drive media=cdrom,index=1,file="${driver_iso}" \ | ||||||
|     -device virtio-net,netdev=nic -netdev ${NET},id=nic \ |       -drive if=none,id=drive0,cache=directsync,aio=native,format=qcow2,file="${disk_img}" \ | ||||||
|     -audiodev pa,id=pa,server=unix:${XDG_RUNTIME_DIR}/pulse/native,out.stream-name=${LAUNCHER}-${VMNAME},in.stream-name=${LAUNCHER}-${VMNAME} \ |       -device virtio-blk-pci,drive=drive0,scsi=off ${STATUS_QUO} \ | ||||||
|     -device intel-hda -device hda-duplex,audiodev=pa,mixer=off \ |       ${VIDEO} -display ${OUTPUT},gl=${GL}${OUTPUT_EXTRA} \ | ||||||
|     -rtc base=localtime,clock=host \ |       -device qemu-xhci,id=xhci,p2=8,p3=8 -device usb-kbd,bus=xhci.0 -device usb-tablet,bus=xhci.0 ${USB_PASSTHROUGH} \ | ||||||
|     -object rng-random,id=rng0,filename=/dev/urandom \ |       -device virtio-net,netdev=nic -netdev ${NET},id=nic \ | ||||||
|     -device virtio-rng-pci,rng=rng0 \ |       -audiodev pa,id=pa,server=unix:${XDG_RUNTIME_DIR}/pulse/native,out.stream-name=${LAUNCHER}-${VMNAME},in.stream-name=${LAUNCHER}-${VMNAME} \ | ||||||
|     -serial mon:stdio \ |       -device intel-hda -device hda-duplex,audiodev=pa,mixer=off \ | ||||||
|     "$@" |       -rtc base=localtime,clock=host \ | ||||||
|  |       -object rng-random,id=rng0,filename=/dev/urandom \ | ||||||
|  |       -device virtio-rng-pci,rng=rng0 \ | ||||||
|  |       -spice port=5930,disable-ticketing \ | ||||||
|  |       -device virtio-serial-pci \ | ||||||
|  |       -device virtserialport,chardev=spicechannel0,name=com.redhat.spice.0 \ | ||||||
|  |       -chardev spicevmc,id=spicechannel0,name=vdagent \ | ||||||
|  |       -serial mon:stdio \ | ||||||
|  |       "${@}" | ||||||
|  |   else | ||||||
|  |     ${QEMU} \ | ||||||
|  |       -name ${VMNAME},process=${VMNAME} \ | ||||||
|  |       -enable-kvm -machine q35 ${GUEST_TWEAKS} \ | ||||||
|  |       ${CPU} -smp ${CORES_VM} \ | ||||||
|  |       -m ${RAM_VM} -device virtio-balloon \ | ||||||
|  |       -drive media=cdrom,index=0,file="${iso}" \ | ||||||
|  |       -drive media=cdrom,index=1,file="${driver_iso}" \ | ||||||
|  |       -drive if=none,id=drive0,cache=directsync,aio=native,format=qcow2,file="${disk_img}" \ | ||||||
|  |       -device virtio-blk-pci,drive=drive0,scsi=off ${STATUS_QUO} \ | ||||||
|  |       ${VIDEO} -display ${OUTPUT},gl=${GL}${OUTPUT_EXTRA} \ | ||||||
|  |       -device qemu-xhci,id=xhci,p2=8,p3=8 -device usb-kbd,bus=xhci.0 -device usb-tablet,bus=xhci.0 ${USB_PASSTHROUGH} \ | ||||||
|  |       -device virtio-net,netdev=nic -netdev ${NET},id=nic \ | ||||||
|  |       -audiodev pa,id=pa,server=unix:${XDG_RUNTIME_DIR}/pulse/native,out.stream-name=${LAUNCHER}-${VMNAME},in.stream-name=${LAUNCHER}-${VMNAME} \ | ||||||
|  |       -device intel-hda -device hda-duplex,audiodev=pa,mixer=off \ | ||||||
|  |       -rtc base=localtime,clock=host \ | ||||||
|  |       -object rng-random,id=rng0,filename=/dev/urandom \ | ||||||
|  |       -device virtio-rng-pci,rng=rng0 \ | ||||||
|  |       -spice port=5930,disable-ticketing \ | ||||||
|  |       -device virtio-serial-pci \ | ||||||
|  |       -device virtserialport,chardev=spicechannel0,name=com.redhat.spice.0 \ | ||||||
|  |       -chardev spicevmc,id=spicechannel0,name=vdagent \ | ||||||
|  |       -serial mon:stdio \ | ||||||
|  |       "${@}" | ||||||
|  |    fi | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function usage() { | function usage() { | ||||||
|  | @ -322,7 +359,7 @@ SNAPSHOT_TAG="" | ||||||
| STATUS_QUO="" | STATUS_QUO="" | ||||||
| VM="" | VM="" | ||||||
| 
 | 
 | ||||||
| readonly LAUNCHER=$(basename $0) | readonly LAUNCHER=$(basename "${0}") | ||||||
| readonly DISK_MIN_SIZE=$((197632 * 8)) | readonly DISK_MIN_SIZE=$((197632 * 8)) | ||||||
| readonly QEMU="/snap/bin/qemu-virgil" | readonly QEMU="/snap/bin/qemu-virgil" | ||||||
| readonly QEMU_IMG="/snap/bin/qemu-virgil.qemu-img" | readonly QEMU_IMG="/snap/bin/qemu-virgil.qemu-img" | ||||||
|  | @ -351,7 +388,7 @@ while [ $# -gt 0 ]; do | ||||||
|       STATUS_QUO="-snapshot" |       STATUS_QUO="-snapshot" | ||||||
|       shift;; |       shift;; | ||||||
|     -vm|--vm) |     -vm|--vm) | ||||||
|       VM="$2" |       VM="${2}" | ||||||
|       shift |       shift | ||||||
|       shift;; |       shift;; | ||||||
|     -h|--h|-help|--help) |     -h|--h|-help|--help) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue