Skip to content

Instantly share code, notes, and snippets.

@noahbliss
Created July 30, 2019 16:50
Show Gist options
  • Save noahbliss/65d4e61aa9ac1b280e05881927fdc708 to your computer and use it in GitHub Desktop.
Save noahbliss/65d4e61aa9ac1b280e05881927fdc708 to your computer and use it in GitHub Desktop.
Manages devices to be passed to a VM using directed IO.
#!/usr/bin/env sh
#Wooo only running ash... adventurous. ;P
#Noah Bliss -- 2017.10.22
thisdevice=$(cat /sys/class/dmi/id/product_uuid)
configdir=$HOME/system_configs
devinfofile=device_info.conf
devtmpfile=/tmp/alldevs.lst
devtmphostfile=/tmp/hostdevs.lst
alldevs=$(lspci -nn > $devtmpfile)
trap "{ rm -f $devtmpfile; rm -f $devtmphostfile;}" EXIT SIGINT SIGTERM
#Functionize me Cap'n!
list() {
echo "-- Client Devices --"
echo
if ! [ -d "$configdir"/"$thisdevice" ]; then echo "Current system is not yet configured."; fi
devcount=$(find "$configdir"/"$thisdevice"/* -maxdepth 1 -type d 2>/dev/null | wc -l)
totaldevcount=$(wc -l < $devtmpfile)
if [ "$devcount" -eq "0" ]; then echo "No devices are passed to the client."; else
while read line
do
for d in "$configdir"/"$thisdevice"/*
do
d=${d##*/}
echo $line | grep $d
done
done < $devtmpfile
fi
echo
echo "-- Host Devices --"
echo
if [ "$devcount" -eq "0" ]; then cat $devtmpfile; elif [ "$devcount" -eq "$totaldevcount" ]; then echo "No devices remaining for the host."; else
for d in "$configdir"/"$thisdevice"/*
do
d=${d##*/}
grep -v "$d" $devtmpfile > $devtmphostfile
done
cat $devtmphostfile
fi
echo
}
add () {
tdevice="$1"
if [ -d "$configdir"/"$thisdevice"/"$tdevice" ]; then echo "Device already added."; return; fi
device_info=$(lspci -nn | grep $tdevice)
pci_short=$tdevice
pci_id='0000:'"$pci_short"
device_id=$(lspci -n | grep "$tdevice" | cut -d" " -f3 | sed 's/:/ /')
mkdir -p "$configdir"/"$thisdevice"/"$tdevice"
echo '#'"$device_info" > "$configdir"/"$thisdevice"/"$tdevice"/"$devinfofile"
echo 'pci_short='"\"$pci_short\"" >> "$configdir"/"$thisdevice"/"$tdevice"/"$devinfofile"
echo 'pci_id='"\"$pci_id\"" >> "$configdir"/"$thisdevice"/"$tdevice"/"$devinfofile"
echo 'device_id='"\"$device_id\"" >> "$configdir"/"$thisdevice"/"$tdevice"/"$devinfofile"
echo "Added device \"$pci_id\" with ID \"$device_id\" to the passthrough device list."
}
remove() {
tdevice="$1"
if ! [ -d "$configdir"/"$thisdevice"/"$tdevice" ]; then "Device not found."; return; fi
rm -rf "$configdir"/"$thisdevice"/"$tdevice"
echo "Device removed."
}
if [ "$1" == "list" ]; then list; exit; fi
if ! [ -z $2 ]; then $1 $2; else
echo "Usage is ${0##*/} list|add|remove PCINUMBERID"
exit
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment