Mount a partition located inside a file or logical volume
Content |
Tested on |
Ubuntu (Xenial) |
Objective
To mount a partition that is located inside a disc image file or an LVM logical volume.
Background
For physical storage devices, GNU/Linux-based operating systems will usually detect whether there is a partition table, and present any data partitions as separate block devices. For example, partition 1 of the block device /dev/sda
would be presented as /dev/sda1
.
This does not happen when the partition table is located inside:
- a regular file, or
- an LVM logical volume.
This issue most commonly arises when the partitions have been created by a virtual machine which is using the file or logical volume as backing for a virtual hard drive. The VM will detect the partition table automatically, but should you wish to access the partition from outside the VM for any reason (for example, to copy data or reset passwords) then some means is needed to gain access to the partition inside the disc image.
Scenario
Suppose you wish to reset the root password of a virtual machine running GNU/Linux. It has one virtual disc, which is presented as /dev/vda
inside the VM, and mapped to the logical volume /dev/vg0/foo
outside the VM.
The virtual disc has a partition table containing three partitions. These are automatically detected by the operating system within the VM, and presented as /dev/vda1
(root), /dev/vda2
(home) and /dev/vda3
(swap), however the partitions are not visible from outside the VM.
Method
Overview
The method described here as three steps:
- Install the
kpartx
command (if not already present). - Use
kpartx
to present the partitions as separate block devices. - Mount the filesystem in the normal way.
Install the kpartx command
This command is provided by the kpartx
package on both Debian-based systems:
apt-get install kpartx
and also on Red Hat-based systems, where it is usually installed by default.
Use kpartx to present the partitions as separate block devices
Use the -a
(add) option to request that a new mapping be created for each partition found, and the -v
(verbose) option to request a list of those mappings:
kpartx -av /dev/vg0/foo
If successful the output should be similar to the following:
add map vg0-foo1 (252:3): 0 1048576 linear 252:2 2048 add map vg0-foo2 (252:4): 0 524288 linear 252:2 1050624 add map vg0-foo3 (252:5): 0 522240 linear 252:2 1574912
The mappings are created in /dev/mapper/
, so the first partition listed would be presented as /dev/mapper/vg0-foo1
.
Mount the filesystem in the normal way
In this scenario it is the first partition to which access is wanted. Now that it has been presented as a separate block device, it should be possible to mount it in the normal way:
mkdir /mnt/vmroot mount /dev/mapper/vg0-foo1 /mnt/vmroot
It is similarly possible to use commands such as mkfs
, fsck
, resize2fs
, or anything else which operates on a partition.
Partitions presented using kpartx
should not normally be listed in /etc/fstab
, because the effect of kpartx
does not persist across a reboot. If long-term access is required then the kpartx
and mount
commands would be need to be scripted to run at boot time.
Notes
There are two similarly named commands for mapping partitions: partx
, which is part of the util-linux
package, and a derivative named kpartx
, which is part of multipath-tools
. The main differences are that:
-
kpartx
can create mappings for block devices, whereaspartx
is limited to regular files. -
partx
is more likely to have been installed by default.
They accept similar arguments, and both have the ability to handle multiple types of partition table (including MBR and GPT). Note that whereas kpartx
creates mappings in /dev/mapper/
, partx
places them in /dev/
.
See also
Tags: lvm