Skip to content

Instantly share code, notes, and snippets.

@tgunr
Created November 15, 2019 18:49
Show Gist options
  • Save tgunr/cd20ff97178f1f5babf3b45ef3202439 to your computer and use it in GitHub Desktop.
Save tgunr/cd20ff97178f1f5babf3b45ef3202439 to your computer and use it in GitHub Desktop.
#How to mount a qcow2 disk image
#This is a quick guide to mounting a qcow2 disk images on your host server.
#This is useful to reset passwords, edit
#files, or recover something without the virtual machine running.
function mount-qcow2 () {
modprobe nbd max_part=8
qemu-nbd --connect=/dev/nbd0 "$1"
fdisk /dev/nbd0 -l
if [[ ! -d "$2"/nbd0p1 ]]; then
mkdir "$2"/nbd0p1
fi
if [[ ! -d "$2"/nbd0p2 ]]; then
mkdir "$2"/nbd0p2
fi
mount /dev/nbd0p1 "$2"/nbd0p1
mount /dev/nbd0p2 "$2"/nbd0p2
}
function unmount-qcow2 () {
umount "$1"/nbd0p1
umount "$1"/nbd0p2
qemu-nbd --disconnect /dev/nbd0
rmmod nbd
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment