Skip to content

Instantly share code, notes, and snippets.

@ljjjustin
Created December 8, 2016 08:55
Show Gist options
  • Save ljjjustin/39cd68fc53a15bcf0a898a2d85e60506 to your computer and use it in GitHub Desktop.
Save ljjjustin/39cd68fc53a15bcf0a898a2d85e60506 to your computer and use it in GitHub Desktop.
modify qcow2 image using qemu-nbd
#!/bin/bash
if [ $# -ne 1 ]; then
echo "usage: $0 <qcow2 image>"
exit
fi
image=$1
target=$(mktemp -d)
cleanup_on_exit() {
exit_code=$?
umount $target && rm -fr $target
qemu-nbd --disconnect /dev/nbd0
exit $exit_code
}
trap "cleanup_on_exit" EXIT
setup() {
qemu-nbd --connect=/dev/nbd0 $image
mkdir -p $target
mount /dev/nbd0p1 $target
chroot $target
}
setup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment