Mounting a File System on a Partition Inside of an LVM Volume
Tags: fdisk, filesystem, kpartx, kvm, linux, lvm, partition, storage, sysadmin, unix, Virtualization, Xen
In my linux virtual environment I am using LVM volumes as the backing devices for virtual machines. Each of these LVM volumes contains a partition table splitting the LVM volume into at least one linux partition and one swap partition. In order to access these partitions from the dom0 host itself we can use the kpartx command to create device mapper entries which correspond to each of the partitions.
In this example we want to access the ext3 filesystem contained on the first partition of the “vm_example” logical volume.
[root@vm ~]# lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
vm_example vg0 -wi-ao 6.00G
[root@vm ~]# fdisk -l /dev/vg0/vm_example
Disk /dev/vg0/vm_example: 6442 MB, 6442450944 bytes
255 heads, 63 sectors/track, 783 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/vg0/vm_example1 1 653 5245191 83 Linux
/dev/vg0/vm_example2 654 783 1044225 82 Linux swap / Solaris
As you can see, partition 1 is type linux and partition 2 is type linux swap.
Now we use kpartx with the -a flag to create the device mapper entries for the partitions displayed above.
[root@vm ~]# kpartx -a /dev/vg0/vm_example
And now we can interact with the /dev/mapper devices as you normally would to mount, fsck, etc.
[root@vm ~]# file -s /dev/mapper/vm_example1
/dev/mapper/vm_example1: Linux rev 1.0 ext3 filesystem data (large files)
[root@vm ~]# file -s /dev/mapper/vm_example2
/dev/mapper/vm_example2: Linux/i386 swap file (new style) 1 (4K pages) size 261055 pages
Then, when you’re finished, clean up with the kpartx -d command. The logical volume will remain in-use until this is done.
[root@vm ~]# kpartx -d /dev/vg0/vm_example
kpart 主要功能建立裝置映射依硬碟分割表
KPARTX(8) Linux Administrator's Manual KPARTX(8) NAME kpartx - Create device maps from partition tables SYNOPSIS kpartx [-a | -d | -l] [-v] wholedisk DESCRIPTION This tool, derived from util-linux' partx, reads partition tables on specified device and create device maps over partitions segments detected. It is called from hotplug upon device maps creation and dele‐ tion. OPTIONS -a Add partition mappings -r Read-only partition mappings -d Delete partition mappings -l List partition mappings that would be added -a -p set device name-partition number delimiter -g force GUID partition table (GPT) -v Operate verbosely EXAMPLE To mount all the partitions in a raw disk image: kpartx -av disk.img This will output lines such as: loop3p1 : 0 20964762 /dev/loop3 63 The loop3p1 is the name of a device file under /dev/mapper which you can use to access the partition, for example to fsck it: fsck /dev/mapper/loop3p1 When you're done, you need to remove the devices: kpartx -d disk.img SEE ALSO multipath(8) multipathd(8) hotplug(8) AUTHORS This man page was assembled By Patrick Caulfield for the Debian project. From documentation provided by the multipath author Christophe Varoqui, <christophe.varoqui@opensvc.com> and others. July 2006 KPARTX(8)