Difference between revisions of "LVM"

From Leaky
Jump to: navigation, search
m (Extending a filesystem to include a new partition)
(Extending a filesystem to include a new partition: added info about creating new LV)
Line 1: Line 1:
 +
 +
== Creating new volumes ==
 +
 +
Assuming that <code>/dev/sdb</code> is a new disk with a single full size partition <code>/dev/sdb1</code>, the following will create a new volume group called <code>data</code> and a couple of logical volumes inside called <code>projects</code> and <code>backups</code>
 +
 +
# pvcreate /dev/sdb1
 +
 +
# vgcreate data /dev/sdb1
 +
 +
# lvcreate -n projects -L 20G data
 +
# lvcreate -n backups -l 100%FREE data
  
 
== Extending a filesystem to include a new partition ==
 
== Extending a filesystem to include a new partition ==

Revision as of 08:18, 27 September 2019

Creating new volumes

Assuming that /dev/sdb is a new disk with a single full size partition /dev/sdb1, the following will create a new volume group called data and a couple of logical volumes inside called projects and backups

# pvcreate /dev/sdb1

# vgcreate data /dev/sdb1

# lvcreate -n projects -L 20G data
# lvcreate -n backups -l 100%FREE data

Extending a filesystem to include a new partition

IMPORTANT: Check the partitions, do not just copy/paste below!

Initialise the LVM metadata on the partition to add into the filesystem

# pvcreate /dev/sdb1

Add the partition to the volume group

# vgextend sys /dev/sdb1
  Volume group "sys" successfully extended

Extend the relevant logical volume to include all of the newly added space

# lvextend -l +100%FREE /dev/sys/root
 Size of logical volume sys/root changed from 27.88 GiB (892 extents) to 47.84 GiB (1531 extents).
 Logical volume root successfully resized

Resize the underlying filesystem to use the whole LVM device

# resize2fs /dev/sys/root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/sys/root is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 3
Performing an on-line resize of /dev/sys/root to 12541952 (4k) blocks.
The filesystem on /dev/sys/root is now 12541952 blocks long.

Mounting a filesystem from a Xen disk file that has LVM on it

# losetup /dev/loop7 xenguest1.xvda.img
# kpartx -v -a xenguest1.xvda.img

If the disk partitions were just normal ones, you would be able to mount them now as /dev/loop7p1 /dev/loop7p2 etc. If the filesystems are contained within an LVM volume group there are some extra steps.

Scan physical volumes for LVM partitions

# pvscan
PV /dev/mapper/loop7p2   VG vg_xenguest1  lvm2 [29.51 GiB / 0    free]

Scan the partitions for volume groups

# vgscan
Reading all physical volumes.  This may take a while...
Found volume group "vg_xenguest1" using metadata type lvm2

Scan for logical volumes. Any that are currently in use will show ACTIVE in the first column.

# lvscan
 inactive   '/dev/vg_xenguest1/lv_root' [28.54 GiB] inherit
 inactive   '/dev/vg_xenguest1/lv_swap' [992.00 MiB] inherit

Since these are inactive, they can't be used as devices (yet).

# lvdisplay /dev/vg_xenguest1/lv_root
...
 LV Status              NOT available
 LV Size                28.54 GiB
...

First we need to set the volume group active, and then the logical volumes should work fine.

# vgchange -ay vg_xenguest1
2 logical volume(s) in volume group "vg_xenguest1" now active

And now we can mount the root filesystem.

# mkdir /mnt/xenguest1
# mount -t ext3 /dev/mapper/vg_xenguest1/lv_root /mnt/xenguest1

When you're finished with it, use the following commands to make the volumes inactive, remove the partitions from the kernel partition map and then remove the loopback device.

# vgchange -an vg_xenguest1 
# kpartx -v -d /dev/loop7
# losetup -d /dev/loop7