如何掛載建立在LVM Logical Volumn中Partition

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)

how to find out lvm Invalid snapshot ?

If we use crontab to make snapshot for lvm, we maybe check snapshot ofen. Does snapshot had out of size ? And how to know which snapshot had out of size ?

The next sample code can do it.

# work fine on centos
lvs -o lv_path,snap_percent|awk '{if($2==100.00){print $1;}}'

# work fine on ubuntu
lvs -o vg_name,lv_name,snap_percent|awk '{if($3==100.00){print $1/$2;}}'

use -o option to show column only lv_path and snap_percent then use awk to find snapshot path that had 100.00 percent.  But I can find option lv_path on ubuntu,so I use vg_name,lv_name columns to show path.

We can also autoremove the Invalid snapshots.

# work on centos
lvs -o lv_path,snap_percent|awk '{if($2==100.00){print $1;}}'|xargs -l lvremove -f

#work on ubutnu
lvs -o vg_name,lv_name,snap_percent|awk '{if($3==100.00){print $1/$2;}}'|xargs -l lvremove -f

使用 virsh >> pool-refresh 重新整理 VG

怕忘記所以寫下來方便查詢,指令很簡單只要一個動作。不然以前都先關掉虛擬主機,再將本機重新開機,新增加的 LV 才會出現在 圖形操作介面上。

root@jerry-P5Q:/home/jerry# virsh
歡迎使用 virsh - 虛擬化的互動模式終端機。

類型:  「help」以取得指令的求助畫面
        「quit」離開

virsh # pool-refresh vg01
Pool vg01 refreshed

virsh # quit

繼續閱讀