Partitions on loop and network block devices

Some time ago I wanted to access a disk image for a virtual machine using a loop device, but I couldn’t access the partitions – /dev/loop0 pointed to the “disk”, no partitions available. Well, I learned that it is possible, and here’s how.

Let’s take a closer look at the nbd module:

# modinfo nbd
filename: /lib/modules/2.6.34/kernel/drivers/block/nbd.ko
license: GPL
description: Network Block Device
vermagic: 2.6.34 mod_unload
parm: nbds_max:number of network block devices to initialize (default: 16) (int)
parm: max_part:number of partitions per device (default: 0) (int)
parm: debugflags:flags for controlling debug output (int)

max_part really says it all. The loop module has a parameter of the same name. Just do:

# modprobe nbd max_part=8

Connect to the target device as usual, use fdisk (or another partitioning tool) on the NBD, and you’ll get a lot of devices named something like /dev/nbd0p1, /dev/nbd0p2 and so on. I tested a layout with two primary, one extended and two logical partitions. Excerpt from mount output after formatting and mounting three of them:

/dev/nbd0p1 on /mnt/p1 type ext2 (rw)
/dev/nbd0p5 on /mnt/p5 type ext4 (rw)
/dev/nbd0p6 on /mnt/p6 type btrfs (rw)

As you probably guessed, the only difference between partitions on an NBD and a loop device is how to set up the “disk”. Just load the right module with an appropriate value for max_part and have fun!

(I read about the parameter here and in the comments to this post.)

Leave a Comment

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: