LVM logical volumes are used from the top down, but they are created from the bottom up. First you create one or more physical volumes (pv), use the physical volumes to create volume groups (vg), and then create logical volumes from thevolume groups (lv).
Commands for working with each LVM component begin with the letters pv, vg, and lv.
For example, pvdisplay shows physical volumes, vgdisplay shows volume groups, and lvdisplay shows logical volumes. The following procedure takes you through the steps of creating LVM volumes from scratch. To do this procedure, you could use the USB flash drive and partitions. Use these steps:
- Obtain a disk with some spare space on it and create a disk partition on it of the LVM type (8e). Then use the pvcreate command to identify this partition as anLVM physical volume. The process of doing this is described in the section “Creating a multiple-partition disk” using the /dev/sdc6 device in that example.
- To add that physical volume to a new volume group, use the vgcreate command. The following command shows you how to create a volume group called myvg0 using the /dev/sdc6 device:
# vgcreate myvg0 /dev/sdc6
Volume group “myvg0” successfully created
- To see the new volume group, type the following:
# vgdisplay myvg0
— Volume group —
VG Name myvg0
VG Size 396.00 MiB
PE Size 4.00 MiB
Total PE 99
Alloc PE / Size 0 / 0
Free PE / Size 99 / 396.00 MiB
- Of the 400MiB partition, 396MiB of space can be used in blocks of 4MiB. Here’s how to create a logical volume from some of the space in that volume group and then check that the device for that logical volume exists:
# lvcreate -n music -L 100M myvg0
Logical volume “music” created
# ls /dev/mapper/myvg0*
- The procedure created a device named /dev/mapper/myvg0-music. That device can now be used to put a filesystem on and mount, just as you did with regular partitions in the fi rst part of this chapter. For example:
# mkfs -t ext4 /dev/mapper/myvg0-music
# mkdir /mnt/mymusic
# mount /dev/mapper/myvg0-music /mnt/mymusic
# df -h /mnt/mymusic
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/myvg0-music 97M 5.6M 87M 7% /mnt/mymusic
- As with regular partitions, logical volumes can be mounted permanently by adding an entry to the /etc/fstab file, such as:
/dev/mapper/myvg0-music /mnt/mymusic ext4 defaults 1 2
The next time you reboot, the logical volume is automatically mounted on /mnt/mymusic.
(Be sure to unmount the logical volume and remove this line if you want to remove the USB
flash drive from your computer.)