Skip to content

Instantly share code, notes, and snippets.

@starise
Last active July 20, 2022 11:29
Show Gist options
  • Save starise/eafaaed690227ffd0f0ff7d1068ed619 to your computer and use it in GitHub Desktop.
Save starise/eafaaed690227ffd0f0ff7d1068ed619 to your computer and use it in GitHub Desktop.
Create and mount a qcow2 disk image

How to create a qcow2 disk image

$ qemu-img create -f qcow2 ./disk.img 2G

Connect the image to a virtual device

# Enable QEMU Disk Network Block Device Server
$ sudo modprobe nbd max_part=8

# Connect /dev/nbd0 to the image
$ sudo qemu-nbd -c /dev/nbd0 ./disk.img

/dev/nbd0 is now available for manipulation.

Partition and mount

Create new partition and filesystem, then mount it.

# Create a new single partition
$ sudo sgdisk -n 1:0:0 -c 1:"Linux filesystem" -t 1:8300 /dev/nbd0

# Create a btrfs filesystem on the partition
$ sudo mkfs.btrfs /dev/nbd0p1 -L "Virtual Disk"

# Mount the partition on a mountpoint
$ sudo mkdir -p /mnt/vdisk
$ sudo mount -t btrfs /dev/nbd0p1 /mnt/vdisk

Unmount and disconnect the device

$ sudo umount /mnt/vdisk
$ sudo qemu-nbd -d /dev/nbd0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment