Skip to content

Instantly share code, notes, and snippets.

@skt041959
Created December 1, 2015 16:02
Show Gist options
  • Save skt041959/fb976bf05e9590dca562 to your computer and use it in GitHub Desktop.
Save skt041959/fb976bf05e9590dca562 to your computer and use it in GitHub Desktop.
make usb disk
#!/bin/bash
# Erase old VMDK entries
rm ~/.VirtualBox/*.vmdk
# Clean up VBox-Registry
sed -i '/sd/d' ~/.VirtualBox/VirtualBox.xml
# Remove old harddisks from existing machines
find ~/.VirtualBox/Machines -name \*.xml | while read file; do
line=`grep -e "type\=\"HardDisk\"" -n $file | cut -d ':' -f 1`
if [ -n "$line" ]; then
sed -i ${line}d $file
sed -i ${line}d $file
sed -i ${line}d $file
fi
sed -i "/rg/d" $file
done
# Delete prev-files created by VirtualBox
find ~/.VirtualBox/Machines -name \*-prev -exec rm '{}' \;
# Recreate VMDKs
ls -l /dev/disk/by-uuid | cut -d ' ' -f 9,11 | while read ln; do
if [ -n "$ln" ]; then
uuid=`echo "$ln" | cut -d ' ' -f 1`
device=`echo "$ln" | cut -d ' ' -f 2 | cut -d '/' -f 3 | cut -b 1-3`
# determine whether drive is mounted already
checkstr1=`mount | grep $uuid`
checkstr2=`mount | grep $device`
checkstr3=`ls ~/.VirtualBox/*.vmdk | grep $device`
if [[ -z "$checkstr1" && -z "$checkstr2" && -z "$checkstr3" ]]; then
VBoxManage internalcommands createrawvmdk -filename ~/.VirtualBox/$device.vmdk -rawdisk /dev/$device -register
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment