Skip to content

Instantly share code, notes, and snippets.

@MircoT
Created April 4, 2018 11:07
Show Gist options
  • Select an option

  • Save MircoT/1668164668e6156fbc698b370a32a667 to your computer and use it in GitHub Desktop.

Select an option

Save MircoT/1668164668e6156fbc698b370a32a667 to your computer and use it in GitHub Desktop.
Script to connect and mount a qcow2 image in a temporary directory. Plus, you can close the connection and check the status with fdisk or install the dependencies.
#!/bin/bash
# $2: have to be a qcow2 image
if [ $EUID != 0 ]; then
echo "You need sudo to exec commands..." ;
echo "- Remember also to install dependencies with install-deps command !"
else
case "$1" in
"connect-status" )
fdisk /dev/nbd0 -l ;;
"install-deps" )
apt install -y qemu-kvm ; modprobe nbd max_part=8 ;;
"open" )
mkdir -p /mnt/tmp ; qemu-nbd --connect=/dev/nbd0 "$2" ; mount /dev/nbd0p1 /mnt/tmp ;;
"close" )
umount /mnt/tmp ; qemu-nbd --disconnect /dev/nbd0 ;;
esac
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment