From d1044e1b404731d1df02c451310458704fa95207 Mon Sep 17 00:00:00 2001 From: Phil Date: Fri, 4 Mar 2022 00:05:28 +0000 Subject: [PATCH 1/4] Annotate generated lines The lines above some are credited elsewhere (sensors-detect) so I feel correct attribution might be helpful in the future --- lib/set_MODULES.sh | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/set_MODULES.sh b/lib/set_MODULES.sh index aad04cd..e3dbacc 100755 --- a/lib/set_MODULES.sh +++ b/lib/set_MODULES.sh @@ -5,14 +5,18 @@ function set_MODULES () { source "$SCRIPTDIR/lib/paths.sh" # Write "/etc/modules" - printf "vfio + printf " +# Generated by vfio-setup +vfio vfio_iommu_type1 vfio_pci ids=$1 vfio_virqfd " >> "$SCRIPTDIR/$MODULES" # Write "/etc/initramfs-tools/modules" - printf "vfio + printf " +# Generated by vfio-setup +vfio vfio_iommu_type1 vfio_pci vfio_virqfd @@ -38,7 +42,7 @@ vfio_virqfd GPU_DEVID="$GPU_DEVID disable_vga=1" ;; esac - + # Write the vfio modprobe config printf "# This is an autogenerated file that stubs your graphic card for use with vfio # This file should be placed inside /etc/modprobe.d/ @@ -57,7 +61,7 @@ softdep radeon pre: vfio vfio_pci function main () { SCRIPTDIR=$(dirname `which $0` | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//") - + set_MODULES $1 } From 7fe88321e6456ad989354453b492f515e77fbfed Mon Sep 17 00:00:00 2001 From: Phil Date: Fri, 4 Mar 2022 00:37:46 +0000 Subject: [PATCH 2/4] Start some pacification of shellcheck not overly obsessive but focussing on safety --- lib/set_CMDLINE.sh | 10 +++++----- lib/set_MODULES.sh | 20 ++++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/set_CMDLINE.sh b/lib/set_CMDLINE.sh index 54268ed..da2277b 100755 --- a/lib/set_CMDLINE.sh +++ b/lib/set_CMDLINE.sh @@ -3,9 +3,9 @@ function set_CMDLINE () { clear # Get the config paths - source "$SCRIPTDIR/lib/paths.sh" + source "${SCRIPTDIR}/lib/paths.sh" - local CMDLINE=$(cat "$SCRIPTDIR/config/kernel_args") + local CMDLINE=$(cat "${SCRIPTDIR}/config/kernel_args") printf "Configuration is now complete, however no changes have been done to your system. The files needed have just been written to $SCRIPTDIR/config/etc @@ -33,8 +33,8 @@ the required information that the QuickEMU project can hook into and use to add ###################################################################### #### In the future, when I have enough confirmation that this script works for other people. -#### This page will get replaced with a prompt asking if you want to apply the changes and make backups -#### of your current system config. +#### This page will get replaced with a prompt asking if you want to apply the changes and make backups +#### of your current system config. ###################################################################### " @@ -42,7 +42,7 @@ the required information that the QuickEMU project can hook into and use to add function main () { - SCRIPTDIR=$(dirname `which $0` | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//") + SCRIPTDIR=$(dirname "$(which $0)" | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//") set_CMDLINE } diff --git a/lib/set_MODULES.sh b/lib/set_MODULES.sh index e3dbacc..dda523f 100755 --- a/lib/set_MODULES.sh +++ b/lib/set_MODULES.sh @@ -9,9 +9,9 @@ function set_MODULES () { # Generated by vfio-setup vfio vfio_iommu_type1 -vfio_pci ids=$1 +vfio_pci ids=%s vfio_virqfd -" >> "$SCRIPTDIR/$MODULES" +" "$1" >> "$SCRIPTDIR/$MODULES" # Write "/etc/initramfs-tools/modules" printf " @@ -27,42 +27,42 @@ vfio_virqfd # Update the kernel_args file CMDLINE=$(cat "$SCRIPTDIR/config/kernel_args") - echo "$CMDLINE vfio_pci.ids=$GPU_DEVID" > "$SCRIPTDIR/config/kernel_args" + echo "${CMDLINE} vfio_pci.ids=$GPU_DEVID" > "$SCRIPTDIR/config/kernel_args" # Ask if we shall disable video output on this card read -p "Do you want to force disable video output in linux on this card? [Y/n]: " DISABLE_VGA case "${DISABLE_VGA}" in [Yy]*) - GPU_DEVID="$GPU_DEVID disable_vga=1" + GPU_DEVID="${GPU_DEVID} disable_vga=1" ;; [Nn]*) echo "" ;; *) - GPU_DEVID="$GPU_DEVID disable_vga=1" + GPU_DEVID="${GPU_DEVID} disable_vga=1" ;; esac # Write the vfio modprobe config printf "# This is an autogenerated file that stubs your graphic card for use with vfio # This file should be placed inside /etc/modprobe.d/ -options vfio_pci ids=$GPU_DEVID +options vfio_pci ids=%s # Make sure vfio_pci is loaded before these modules: nvidia, nouveau, amdgpu and radeon softdep nvidia pre: vfio vfio_pci softdep nouveau pre: vfio vfio_pci softdep amdgpu pre: vfio vfio_pci softdep radeon pre: vfio vfio_pci -" > "$SCRIPTDIR/$MODPROBE/vfio.conf" +" "${GPU_DEVID}" > "$SCRIPTDIR/$MODPROBE/vfio.conf" exec "$SCRIPTDIR/lib/get_USB_CTL.sh" } function main () { - SCRIPTDIR=$(dirname `which $0` | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//") + SCRIPTDIR=$(dirname "$(which $0)" | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//") - set_MODULES $1 + set_MODULES "$1" } -main $1 +main "$1" From 08e4da4575c6b89a0a07732ebfcb3bb1f6f57ee0 Mon Sep 17 00:00:00 2001 From: Phil Date: Fri, 4 Mar 2022 02:08:10 +0000 Subject: [PATCH 3/4] more linting and whitespace --- lib/get_GPU.sh | 4 ++-- lib/get_GPU_GROUP.sh | 6 +++--- lib/get_GPU_ROM.sh | 18 +++++++++--------- lib/get_USB_CTL.sh | 4 ++-- lib/get_USB_CTL_GROUP.sh | 8 ++++---- vfio-setup | 10 +++++----- 6 files changed, 25 insertions(+), 25 deletions(-) diff --git a/lib/get_GPU.sh b/lib/get_GPU.sh index 0660ee7..cf8772c 100755 --- a/lib/get_GPU.sh +++ b/lib/get_GPU.sh @@ -33,8 +33,8 @@ DO NOT use any of the files from $SCRIPTDIR/config ! } function main () { - SCRIPTDIR=$(dirname `which $0` | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//") - + SCRIPTDIR=$(dirname "$(which $0)" | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//") + get_GPU } diff --git a/lib/get_GPU_GROUP.sh b/lib/get_GPU_GROUP.sh index 2899ed2..c20b4c5 100755 --- a/lib/get_GPU_GROUP.sh +++ b/lib/get_GPU_GROUP.sh @@ -45,7 +45,7 @@ USB_CTL_ID=() # Get the GPU ROM "$SCRIPTDIR/lib/get_GPU_ROM.sh" "$ROM_PCI_ID" - + # Start setting up modules exec "$SCRIPTDIR/lib/set_MODULES.sh" $GPU_DEVID ;; @@ -56,8 +56,8 @@ USB_CTL_ID=() } function main () { - SCRIPTDIR=$(dirname `which $0` | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//") - + SCRIPTDIR=$(dirname "$(which $0)" | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//") + get_GROUP $1 } diff --git a/lib/get_GPU_ROM.sh b/lib/get_GPU_ROM.sh index 62695bc..f4e6672 100755 --- a/lib/get_GPU_ROM.sh +++ b/lib/get_GPU_ROM.sh @@ -5,7 +5,7 @@ function get_GPU_ROM () { # Get the config paths source "$SCRIPTDIR/lib/paths.sh" - VBIOS_PATH=$(find /sys/devices -name rom | grep $1) + VBIOS_PATH=$(find /sys/devices -name rom | grep "$1") printf "We will now attempt to dump the vbios of your selected GPU. Passing a VBIOS rom to the card used for passthrough is required for some cards, but not all. Some cards also requires you to patch your VBIOS romfile, check online if this is neccessary for your card. @@ -20,14 +20,14 @@ echo 0 | sudo tee $VBIOS_PATH read -p "Do you want to dump the VBIOS, choosing N will skip this step [y/N]: " YESNO case "${YESNO}" in [Yy]*) - echo 1 | sudo tee $VBIOS_PATH - sudo cat $VBIOS_PATH > "$SCRIPTDIR/$QUICKEMU/vfio_card.rom" - sudo md5sum $VBIOS_PATH | cut -d " " -f 1 > "$SCRIPTDIR/$QUICKEMU/vfio_card.rom.md5" - local ROM_MD5=$(sudo md5sum $VBIOS_PATH | cut -d " " -f 1) - echo 0 | sudo tee $VBIOS_PATH + echo 1 | sudo tee "$VBIOS_PATH" + sudo cat "$VBIOS_PATH" > "$SCRIPTDIR/$QUICKEMU/vfio_card.rom" + sudo md5sum "$VBIOS_PATH" | cut -d " " -f 1 > "$SCRIPTDIR/$QUICKEMU/vfio_card.rom.md5" + local ROM_MD5=$(sudo md5sum "$VBIOS_PATH" | cut -d " " -f 1) + echo 0 | sudo tee "$VBIOS_PATH" local ROMFILE_MD5=$(md5sum "$SCRIPTDIR/$QUICKEMU/vfio_card.rom" | cut -d " " -f 1) - if [ -f $SCRIPTDIR/$QUICKEMU/vfio_card.rom ]; + if [ -f "$SCRIPTDIR"/$QUICKEMU/vfio_card.rom ]; then if [ "$ROM_MD5" == "$ROMFILE_MD5" ]; then @@ -59,8 +59,8 @@ echo 0 | sudo tee $VBIOS_PATH function main () { - SCRIPTDIR=$(dirname `which $0` | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//") - + SCRIPTDIR=$(dirname "$(which $0)" | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//") + get_GPU_ROM $1 } diff --git a/lib/get_USB_CTL.sh b/lib/get_USB_CTL.sh index a0f3517..9fdce52 100755 --- a/lib/get_USB_CTL.sh +++ b/lib/get_USB_CTL.sh @@ -10,7 +10,7 @@ is only needed if you intend to use other devices than just mouse and keyboard w " echo "#------------------------------------------#" exec "$SCRIPTDIR/utils/ls-iommu" | grep -i "usb controller" | cut -d " " -f 1-4,8- - echo "#------------------------------------------#" + echo "#------------------------------------------#" printf " Press q to quit " @@ -31,7 +31,7 @@ Press q to quit } function main () { - SCRIPTDIR=$(dirname `which $0` | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//") + SCRIPTDIR=$(dirname "$(which $0)" | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//") get_USB_CTL } diff --git a/lib/get_USB_CTL_GROUP.sh b/lib/get_USB_CTL_GROUP.sh index ba3aece..4ab9c87 100755 --- a/lib/get_USB_CTL_GROUP.sh +++ b/lib/get_USB_CTL_GROUP.sh @@ -14,7 +14,7 @@ if you do not know what you are doing. echo "#------------------------------------------#" exec "$SCRIPTDIR/utils/ls-iommu" | grep -i "group $1" | cut -d " " -f 1-4,8- | perl -pe "s/\[[0-9a-f]{4}\]: //" echo "#------------------------------------------#" - + printf " To use any of the devices shown for passthrough, all of them have to be passed through @@ -26,7 +26,7 @@ To return to the previous page just press ENTER. [Yy]*) # Get the PCI ids local PCI_ID=$($SCRIPTDIR/utils/ls-iommu | grep -i "group $1" | cut -d " " -f 4 | perl -pe "s/\n/ /" | perl -pe "s/\s$//") - + # Replace the blank USB_CTL_ID with the PCI_ID for the usb controller the user wants to pass through perl -pi -e "s/USB_CTL_ID=\(\)/USB_CTL_ID=\($PCI_ID\)/" "$SCRIPTDIR/$QUICKEMU/qemu-vfio_vars.conf" exec "$SCRIPTDIR/lib/set_CMDLINE.sh" @@ -38,8 +38,8 @@ To return to the previous page just press ENTER. } function main () { - SCRIPTDIR=$(dirname `which $0` | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//") - + SCRIPTDIR=$(dirname "$(which $0)" | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//") + get_USB_CTL_GROUP $1 } diff --git a/vfio-setup b/vfio-setup index c5a2a8a..11fa7fd 100755 --- a/vfio-setup +++ b/vfio-setup @@ -1,8 +1,8 @@ #!/bin/bash # Get the scripts directory -SCRIPTDIR=$(dirname `which $0`) -cd $SCRIPTDIR +SCRIPTDIR=$(dirname "$(which $0)") +cd "$SCRIPTDIR" # Get the config paths source "$SCRIPTDIR/lib/paths.sh" @@ -41,11 +41,11 @@ A full documentation for debian/ubuntu systems can be found here: https://github Press ENTER to continue once you have made a backup of your system. " read ENTER -clear +clear printf "This script assumes a few things: * You have already enabled IOMMU, VT-d and/or AMD-v inside your UEFI/BIOS advanced settings. -* You have already added \"$CMDLINE\" to your +* You have already added \"$CMDLINE\" to your kernel boot arguments and booted your system with these kernel arguments active. * You are comfortable with navigating and changing settings in your UEFI/BIOS. * You know how edit your bootloader configuration and kernel arguments. @@ -113,7 +113,7 @@ then else touch "$SCRIPTDIR/$INITRAMFS" fi - + # Run ls-iommu so we can verify that IOMMU properly working LS_IOMMU=$($SCRIPTDIR/utils/ls-iommu) From 7d92058efc00d67bff46a22c39add202c7dcde2c Mon Sep 17 00:00:00 2001 From: Phil Date: Fri, 4 Mar 2022 02:18:44 +0000 Subject: [PATCH 4/4] more shellcheck --- vfio-verify | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vfio-verify b/vfio-verify index d9cb3d4..ef119d2 100755 --- a/vfio-verify +++ b/vfio-verify @@ -1,7 +1,7 @@ #!/bin/bash # Get the scripts directory -SCRIPTDIR=$(dirname `which $0`) +SCRIPTDIR=$(dirname "$(which $0)") cd $SCRIPTDIR # If there is a config generated, then $SCRIPTDIR/config/kernel_args @@ -25,4 +25,4 @@ else # Tell user to run the setup first if the kernel_args file is not found echo "Please run \"$SCRIPTDIR/vfio-setup\" first!" -fi \ No newline at end of file +fi \ No newline at end of file