Linux LVM (Logical Volume Management)
In this article we will cover some of the basics of LVM (Logical Volume Management). We will look at configuring LVM from the command line and we will also look at resizing volumes.
In this article we have five gigabytes of free space, we will need to create two partitions of type LVM with the size of two gigabytes each. The first partition that we will create is for the initial LVM setup and the second partition will be used to show how to extend a logical volume.
The partitioning utility which we will use in this article is the “fdisk” utility along with the “partprobe” utility to avoid rebooting the machine after partitioning the disk. The current partition table can be viewed by issues the “fdisk -l” command as shown in Figure 1.
linux-6j7i:~ # fdisk -l Disk /dev/sda: 21.4 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sda2 654 784 1052257+ 82 Linux swap / Solaris /dev/sda3 * 785 2610 14667345 83 Linux linux-6j7i:~ #
Figure 1: Current partition table.
Once you know the layout of your partition table you can start the “fdisk” utility by issuing the “fdisk /dev/sda” command. Once the “fdisk” command has been executed you will be able to create a new partition as shown in Figure 1.2.
linux-6j7i:~ # fdisk /dev/sda The number of cylinders for this disk is set to 2610. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with: 1) software that runs at boot time (e.g., old versions of LILO) 2) booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK) Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-2610, default 1): Using default value 1 Last cylinder or +size or +sizeM or +sizeK (1-653, default 653): +2048M Command (m for help):
Figure 1.2: Creating a new 2GB partition.
Once the partition has been created you may notice that the “partition system id” field for that partition is set to Linux, this needs to be change to LVM which has the hex value of “8e”. The “partition system id” field can be change simply by issuing the “t” option in the “fdisk” utility as shown in Figure 1.3.
Command (m for help): p Disk /dev/sda: 21.4 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sda1 1 250 2008093+ 83 Linux /dev/sda2 654 784 1052257+ 82 Linux swap / Solaris /dev/sda3 * 785 2610 14667345 83 Linux Command (m for help): t Partition number (1-4): 1 Hex code (type L to list codes): 8e Changed system type of partition 1 to 8e (Linux LVM) Command (m for help):
Figure 1.3: Changing the “partition system id”.
Once you have successfully created the LVM you can repeat the steps again to create the second LVM which we will use later in this article. Once you are happy with the partitioning scheme, you can write the new partition table by issuing the “w” command within the “fdisk” utility as shown in Figure 1.4.
Command (m for help): p Disk /dev/sda: 21.4 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sda1 1 250 2008093+ 8e Linux LVM /dev/sda2 654 784 1052257+ 82 Linux swap / Solaris /dev/sda3 * 785 2610 14667345 83 Linux /dev/sda4 251 500 2008125 8e Linux LVM Partition table entries are not in disk order Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot. Syncing disks.
Figure 1.4: Writing the new partition table.
Once the partition table has been written you can issue the “partprobe” command to have the kernel re-read the partition table and have the newly created partitions available without the need of rebooting the machine, as shown in Figure 1.5.
linux-6j7i:~ # partprobe /dev/sda linux-6j7i:~ # echo $? 0
Figure 1.5: Have the partition table re-read.
In Figure 1.5 you may be wondering what the second command does “echo $?“, this checks the exit status of the previous enter command, if it returns a zero then the command was successfully executed. Once the partition table has been re-read you are ready to configure LVM.
Creating an LVM
In this section of the article we are going to look at configuring LVM from the command line. We will not use the second partition until the “Extending LVM” section. The partition that we will be working with is the “/dev/sda1” partition which is the first partition we created.
Creating a Physical volume
The first step that we need to do is create a physical volume. A physical volume is usually a hard disk or a device that is similar to a hard disk. The command that we will use to create a physical volume is the “pvcreate” command, Figure 2 shows the command used to create a physical volume using the “/dev/sda1” partition.
linux-6j7i:~ # pvcreate /dev/sda1 Physical volume "/dev/sda1" successfully created
Figure 2: Creating a physical volume.
Once you had created the physical volume you can use the “pvdisplay” command to display the current physical volumes on the system as shown in Figure 2.1.
linux-6j7i:~ # pvdisplay --- NEW Physical volume --- PV Name /dev/sda1 VG Name PV Size 1.92 GB Allocatable NO PE Size (KByte) 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID xm3zis-2G4N-Fbs9-xBIF-uN03-5TEF-rb6K1H
Figure 2.1: Displaying what physical volumes are available.
Creating a Volume group
Once the physical volume has been created you can create a volume group which groups all the physical volumes and logical volumes together. The command used to create a volume group is “vgcreate” as shown in Figure 2.2.
linux-6j7i:~ # vgcreate VolumeGroup00 /dev/sda1 Volume group "VolumeGroup00" successfully created
Figure 2.2: Creating a volume group.
The command in Figure 2.2 creates a volume group with the name of “VolumeGroup00”. Once the volume group has been created you can use the “vgdisplay” command to view the volume group as, shown in Figure 2.3.
linux-6j7i:~ # vgdisplay --- Volume group --- VG Name VolumeGroup00 System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 1 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 1 Act PV 1 VG Size 1.91 GB PE Size 4.00 MB Total PE 490 Alloc PE / Size 0 / 0 Free PE / Size 490 / 1.91 GB VG UUID bDqBGo-y82K-09Zw-kxz4-Lb3s-G61E-A2hCdJ
Figure 2.3: Displaying all volumes on the current system.
Creating a Logical volume
Once the volume group has been successfully created you will need to create a logical volume. The logical volume is the equivalent to a partition in a non-LVM environment. The command we will use to create the logical volume is “lvcreate” as shown in Figure 2.4, Table 1 explains what each qualifier does.
linux-6j7i:~ # lvcreate -L+1.91GB -n "Private" VolumeGroup00 Rounding up size to full physical extent 1.91 GB Logical volume "Private" created
Figure 2.4: Creating a logical volume.
|-L+1.91GB||This qualifier specified how big the logical volume will be extended.|
|-n “Private”||This qualifier specifies the logical volume’s name.|
Table 1: Figure 2.4 explained.
In Figure 2.4 you maybe wondering where I got the available disk size for the logical volume. The available space for the logical volume can be obtained using the “vgdisplay” command followed by the “-C” qualifier as shown in Figure 2.5.
linux-6j7i:~ # vgdisplay -C VolumeGroup00 VG #PV #LV #SN Attr VSize VFree VolumeGroup00 1 1 0 wz--n- 1.91G 4.00M
Figure 2.5: Checking the available space on the “VolumeGroup00” volume.
Creating a file system
Once you have created the logical volume you will need to format the logical volume with a filesystem. In this article we will use the format the logical volume with the ReiserFS filesystem. The command we will use to do this is “mkfs.reiserfs” as shown in Figure 2.6.
linux-6j7i:~ # mkfs.reiserfs /dev/VolumeGroup00/Private mkfs.reiserfs 3.6.19 (2003 www.namesys.com) A pair of credits: The Defense Advanced Research Projects Agency (DARPA, www.darpa.mil) is the primary sponsor of Reiser4. DARPA does not endorse this project; it merely sponsors it. Lycos Europe (www.lycos-europe.com) had a support contract with us that consistently came in just when we would otherwise have missed payroll, and that they kept doubling every year. Much thanks to them. Guessing about desired format.. Kernel 220.127.116.11-0.12-default is running. Format 3.6 with standard journal Count of blocks on the device: 500736 Number of blocks consumed by mkreiserfs formatting process: 8227 Blocksize: 4096 Hash function used to sort names: "r5" Journal Size 8193 blocks (first block 18) Journal Max transaction length 1024 inode generation number: 0 UUID: c3c7502b-6054-4300-b997-0912af1eda6e ATTENTION: YOU SHOULD REBOOT AFTER FDISK! ALL DATA WILL BE LOST ON '/dev/VolumeGroup00/Private'! Continue (y/n):y Initializing journal - 0%....20%....40%....60%....80%....100% Syncing..ok ReiserFS is successfully created on /dev/VolumeGroup00/Private.
Figure 2.6: Formatting the “Private” logical volume.
Mounting the Logical volume
Once the logical volume has been formatted with a filesystem of your choice you will need to create a mount point on the filesystem for the logical volume to be usable, as shown in Figure 2.7.
linux-6j7i:~ # mkdir /media/private linux-6j7i:~ # mount /dev/VolumeGroup00/Private /media/private
Figure 2.7: Mounting the “Private” logical volume.
Once the logical volume has been mounted you can issue the “df -h” command to see if your logical volume has been mounted successfully, as shown in Figure 2.8.
linux-6j7i:~ # df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 14G 2.4G 12G 17% / udev 126M 132K 126M 1% /dev /dev/mapper/VolumeGroup00-Private 2.0G 33M 1.9G 2% /media/private
Figure 2.8: Checking the currently mounted partitions.
In this section of the article we are going to resize the “Private” volume without rebooting the machine, however, we will need to unmount the “Private” logical volume first as we need to resize the filesystem. The first step is to partition the hard disk drive again to create another LVM partition, you can repeat the sames steps shown in the “Partitioning” section.
Once the partition has been created you will need to add the second partition to the LVM, as shown in Figure 2 using the “pvcreate” command. Once the physical volume has been created you will need to extend the “VolumeGroup00” volume group, this can be done with the “vgextend” command as shown in Figure 2.9.
linux-6j7i:~ # vgextend VolumeGroup00 /dev/sda4 Volume group "VolumeGroup00" successfully extended
Figure 2.9: Extending the “VolumeGroup00” volume group.
Once you have successfully extended the volume group you can use the “vgdisplay” command with the “-C” qualifier to see how much space you have free, as shown in Figure 3.
linux-6j7i:~ # vgdisplay -C VG #PV #LV #SN Attr VSize VFree VolumeGroup00 2 1 0 wz--n- 3.83G 1.92G
Figure 3: Display all volume groups.
In Figure 3 you can see that there is 1.92GB free, we can now extent the “Private” logical volume by 1.92GB. This can be done using the “lvextend” command as shown in Figure 3.1. Table 2 explains what each qualifier does.
linux-6j7i:~ # lvextend -L+1.91GB /dev/VolumeGroup00/Private Rounding up size to full physical extent 1.91 GB Extending logical volume Private to 3.82 GB Logical volume Private successfully resized
Figure 3.1: Resizing the “Private” logical volume.
|-L+1.91GB||This qualifier specifies how much you want to extent the logical volume by. Note: if you don’t specify the plus (+) at the beginning of the size it will not be extended the logical volume will be set to the size you specified.|
|/dev/VolumeGroup00/Private||This is the path in which the “Private” logical volume lives. All logical volumes are mounted under the volume group name under the “/dev” directory.|
Table 2: Figure 2.4 explained.
Once you have extended the logical volume you can use the “lvdisplay” command with the “-C” qualifier to see the new size, as shown in Figure 3.2.
linux-6j7i:~ # lvdisplay -C LV VG Attr LSize Origin Snap% Move Log Copy% Private VolumeGroup00 -wi-ao 3.82G
Figure 3.2: Displaying all logical volume and there sizes.
If you issue the “df -h” command you will notice that the “Private” logical volume is still the same size. The reason for this is because the filesystem has not been resized to occupy the new space, the command we can use to resize the ReiserFS filesystem is “resize_reiserfs“. Before resizing the filesystem you should first “unmout” the logical volume as shown in Figure 3.3.
linux-6j7i:~ # umount /media/private/
Figure 3.3: unmounting the “Private” volume.
Once you have unmounted the “Private” logical volume you can resize the ReiserFS filesystem as shown in Figure 3.4.
linux-6j7i:~ # resize_reiserfs /dev/VolumeGroup00/Private resize_reiserfs 3.6.19 (2003 www.namesys.com) ReiserFS report: blocksize 4096 block count 1001472 (500736) free blocks 993230 (492509) bitmap block count 31 (16) Syncing..done resize_reiserfs: Resizing finished successfully.
Figure 3.4: Resizing the “Private” logical volume.
Once you have resized the ReiserFS filesystem you can mount the “Private” logical volume again and you will notice the volume is now larger as shown in Figure 3.5.
linux-6j7i:~ # mount /dev/VolumeGroup00/Private /media/private linux-6j7i:~ # df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 14G 2.4G 12G 17% / udev 126M 132K 126M 1% /dev /dev/mapper/VolumeGroup00-Private 3.9G 33M 3.8G 1% /media/private
Figure 3.5: Remounting the “Private” volume.
The LVM that Linux provides is an excellent feature for managing storage, I would recommend visiting the LVM documentation website to find out more about LVM . The LVM website explains more about LVM’s advanced features that were not explained in this article such as; creating snapshots, mirroring (similar to RAID1), striping(similar to RAID0) and may others.