Mounting partitions contained in an LVM

Introduction

When creating a virtual machine with an LVM partition, it will create partitions within that LVM partition. Mounting those ‘sub’ partitions requires an extra step to access them. I used this when I was cloning virtual machines so that I could change the hostname of the copied host before booting it up.

A tool called kpartx is your friend here. It allows the system to see partitions within a LVM partition, so they can be mounted. In this guide we will be accessing partitions created as part of my mogilefs playground virtual machines. Below are the partitions that we are trying to access:

$ sudo fdisk -l /dev/vhosts/mogile2

Disk /dev/vhosts/mogile2: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders, total 10485760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000b30be

Device Boot Start End Blocks Id System
/dev/vhosts/mogile2p1 * 2048 9437183 4717568 83 Linux
/dev/vhosts/mogile2p2 9439230 10483711 522241 5 Extended
/dev/vhosts/mogile2p5 9439232 10483711 522240 82 Linux swap / Solaris

NOTE: Make sure that the virtual machine is NOT running before mounting these partitions

So let’s try and mount one of these partitions (/dev/vhosts/mogile2p1):


$ sudo mount /dev/vhosts/mogile2p1 /mnt/temp
mount: special device /dev/vhosts/mogile2p1 does not exist

So the following will help map these partitions to the host OS so they can be mounted. If you haven’t got kpartx installed, then on debian/ubuntu systems install with sudo apt-get install kpartx

kpartx guide

To allow the host system to see the partitions for mounting, we run the following:

$ sudo kpartx -a /dev/vhosts/mogile2

We can then mount with

$ sudo mount /dev/mapper/vhosts-mogile2p1 /mnt/temp

And then use it as we normally would. In this case, I used it to edit the /mnt/temp/etc/hostname and /mnt/temp/etc/hosts file

Finally un mount it:
$ sudo umount /mnt/temp

And if you are done with it, and cleaned up, then remove it from the partition tables:

$ sudo kpartx -d /dev/vhosts/mogile2

Reference(s):

https://www.normation.com/en/blog/2011/04/07/mounting-partitions-stored-in-a-logical-volume-or-a-disk-image/

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *