Posts Tagged LVM
Sometimes it is necessary to extend a logical volume, expanding the physical volume where it is hosted. If this is the case, you can follow this example extending a volume of 450 GB to 500 GB: First, Let’s check the disk geometry:
:~# fdisk -l /dev/sdb
Disk /dev/sdb: 483.2 GB, 483183820800 bytes 255 heads, 63 sectors/track, 58743 cylinders, total 943718400 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x0001178e
After expanding the volume you must make the system to rescan the volume:
:~# echo 1 > /sys/block/sdb/device/rescan
Then check the disk geometry one more time:
:~# fdisk -l /dev/sdb
Disk /dev/sdb: 536.9 GB, 536870912000 bytes 255 heads, 63 sectors/track, 65270 cylinders, total 1048576000 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x0001178e
As you can see, the disk size grew up but this is only known by the operating system, because the LVM still has the same information since the physical volume hasn’t been expanded yet:
:~# pvs PV VG Fmt Attr PSize PFree /dev/sda5 debian lvm2 a-- 3.76g 0 /dev/sda6 debian lvm2 a-- 13.00g 0 /dev/sdb svn_vg lvm2 a-- 450.00g 0
If you extend the physical volume with the following command:
:~# pvresize /dev/sdb Physical volume "/dev/sdb" changed 1 physical volume(s) resized / 0 physical volume(s) not resized
And check the physical volume sizes you will see the added space:
:~# pvs PV VG Fmt Attr PSize PFree /dev/sda5 debian lvm2 a-- 3.76g 0 /dev/sda6 debian lvm2 a-- 13.00g 0 /dev/sdb svn_vg lvm2 a-- 500.00g 50.00g
Now you have to extend the logical volume and the system file size on the fly. You can do both actions using just one command:
:~# lvresize -r -L +50G /dev/mapper/svn_vg-svn
Extending logical volume svn to 500.00 GiB Logical volume svn successfully resized resize2fs 1.42.5 (29-Jul-2012) Filesystem at /dev/mapper/svn_vg-svn is mounted on /var/data/svn; on-line resizing required old_desc_blocks = 29, new_desc_blocks = 32 Performing an on-line resize of /dev/mapper/svn_vg-svn to 131070976 (4k) blocks. The filesystem on /dev/mapper/svn_vg-svn is now 131070976 blocks long.
Finally, check the free space available to see the added 50 GB in the system file:
:~# df -h Filesystem Size Used Avail Use% Mounted on rootfs 993M 287M 655M 31% / udev 10M 0 10M 0% /dev tmpfs 397M 228K 397M 1% /run /dev/mapper/debian-root 993M 287M 655M 31% / tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 794M 0 794M 0% /run/shm /dev/sda1 228M 34M 183M 16% /boot /dev/mapper/debian-home 2.0G 39M 1.9G 3% /home /dev/mapper/debian-tmp 3.9G 7.1M 3.7G 1% /tmp /dev/mapper/debian-usr 2.0G 869M 1.1G 46% /usr /dev/mapper/debian-var 6.7G 2.1G 4.3G 33% /var /dev/mapper/svn_vg-svn 493G 432G 62G 88% /var/data/svn
If you need to move data from one disk to another one you could think in copying the data with cp, rsync, or any other tool, and then umount the partition, edit the /etc/fstab file to point to the new path, and all that involve to remove a disk or partition for another one. But if you forcasted and partitioned using LVM, there is a cleaner way of changing partitions on the fly, I mean, without umounting partitions and even leaving the same references in the /etc/file, because the same LVM partition will be used. Let’s see an example: if you have a 50 GB disk as a physical volume in /dev/sdb, which is set in a volume grup called data, and you need to move all data to another disk /dev/sdc, just do the following. First the initial distribution si shown:
You can check the current state of the physical volumes using the pvs command:
# pvs PV VG Fmt Attr PSize PFree /dev/sda2 system lvm2 a- 19.77g 0 /dev/sdb data lvm2 a- 50.00g 0
Then create the physical volume where the data will be moved:
# pvcreate /dev/sdc Physical volume "/dev/sdc" successfully created
Check the physical volumes again:
# pvs PV VG Fmt Attr PSize PFree /dev/sda2 system lvm2 a- 19.77g 0 /dev/sdb data lvm2 a- 50.00g 0 /dev/sdc lvm2 a- 50.00g 50.00g
Now check the volume groups:
# vgs VG #PV #LV #SN Attr VSize VFree data 1 1 0 wz--n- 50.00g 0 system 1 6 0 wz--n- 19.77g 0
As you can see the volume group called data sizes 50 GB. Because this is the volume group which have the physical volume you want to move (/dev/sdb), you must add the physical volume to this group:
# vgextend data /dev/sdc Volume group "data" successfully extended
If you check it one more time you will see that the volume group has 100 GB which correspond to /dev/sdb (0 GB free) y /dev/sdc ( 50 GB free):
# pvs PV VG Fmt Attr PSize PFree /dev/sda2 system lvm2 a- 19.77g 0 /dev/sdb data lvm2 a- 50.00g 0 /dev/sdc data lvm2 a- 50.00g 50.00g
# vgs VG #PV #LV #SN Attr VSize VFree data 2 1 0 wz--n- 99.99g 50.00g system 1 6 0 wz--n- 19.77g 0
Now let’s proceed to move the data from the physical volume /dev/sdb to /dev/sdc:
# pvmove /dev/sdb /dev/sdb: Moved: 0.0% /dev/sdb: Moved: 11.8% /dev/sdb: Moved: 30.5% /dev/sdb: Moved: 75.7% /dev/sdb: Moved: 95.0% /dev/sdb: Moved: 99.3% /dev/sdb: Moved: 100.0%
There is not need to specify the other volume because the data will be distributed among the remaining physical volumes of the volume group, and in this case it is just /dev/sdc. Now, let’s check the distribution of the physical volumes:
# pvs PV VG Fmt Attr PSize PFree /dev/sda2 system lvm2 a- 19.77g 0 /dev/sdb data lvm2 a- 50.00g 50.00g /dev/sdc data lvm2 a- 50.00g 0
As you can see the /dev/sdb has no data (50 GB free) while /dev/sdc has the data (0 GB free). Just
Como puede verse el disco /dev/sdb no tiene datos (50 GB libres) mientras que /dev/sdc tiene los datos (0 GB libres). Now remove the physical volume to disincorporate from the volume group:
# vgreduce data /dev/sdb Removed "/dev/sdb" from volume group "data"
Now check the volume groups and the physical volumes:
# pvs PV VG Fmt Attr PSize PFree /dev/sda2 system lvm2 a- 19.77g 0 /dev/sdb lvm2 a- 50.00g 50.00g /dev/sdc data lvm2 a- 50.00g 0
# vgs VG #PV #LV #SN Attr VSize VFree data 1 1 0 wz--n- 50.00g 0 system 1 6 0 wz--n- 19.77g 0
Finally proceed to wipe the physical volume::
# pvremove /dev/sdb Labels on physical volume "/dev/sdb" successfully wiped
# pvs PV VG Fmt Attr PSize PFree /dev/sda2 system lvm2 a- 19.77g 0 /dev/sdc data lvm2 a- 50.00g 0
This allows data to be moved from one disk to another without copying the data on operating system level, without interrupting services or restarting the computer. At the end we will have the disk /dev/sdc as the physical volume of the volume data as shown below:
Link to the video http://blip.tv/lgallardo/lvm-on-fedora-red-hat-5908557
On this video you can see the installing process using a LVM on Fedora (it’s also valid on Red Hat). As a side note, the directory that must be outside the LVM is /boot, altoogh I said it was the file system’s root directory /, which also includes /boot.
If you don’t understand some of the LVM terms, you can check out the previous article about basic LVM concepts and some commands to manage a LVM by hand.
In the traditional disk partitioning scheme, administrators plan ahead the use of each partition. For instance, a laptop with 120 GB hard drive, 1 GB RAM can have the following partition scheme:
- 1 GB for swap (/dev/sda1),
- 12 GB for root (/dev/sda2),
- 107 GB for /home (/dev/sda3), meaning, the remaining space.
So far so go, but what if the RAM is increased to 2 GB? In order to hibernate the system you must increase the swap, and because the whole hard drive was partitioned leaving no space there isn’t other way than repartitioning the hard drive using a utility, if the file system allows it (for instance, ext[2|3|4] file systems let you resize them).
There isn’t anything more flexible? Yes, it’s called LVM (Logical Volume Manager), and basically it lets you hot resizing of partitions. For example, you can release 1GB from /home (/dev/sda3) and give it to the swap partition (/dev/sda1).
LVM (Logical Volume Manager)
In order to use LVM, first you must assign physical volumes, then volume groups and finally logical groups. All this might be confusing at first, but let’s try to explain each of these terms and how they are related:
- Physical volumes: This is the support media where the data will be stored, and they can be made of several hard drives or partitions of a drive. They don’t have a direct relation with the mount points. Physical Volume examples: /dev/sda , /dev/sdb1, etc.
- Volume groups: It’s the grouping of logical volumes in a volume to see them as one device. Physical volume example: /dev/vg00
- Logical volumes: Here is were the file systems (for example, ext2, ext3,xfs,vfat) will reside and so the mount points (/, /home, swap, /usr, /var, etc). At this level the files systems are formated, and it’s where the data will be distributed logically, as you are costumed to with the traditional partitions. Logical volumes examples: /dev/vg00/lv_rootfs, /dev/vg00/lv_swap, /dev/vg00/lv_home.
On Debian you can install LVM with this package:
aptitude install lvm2
On other distributions like Fedora, Red Hat or Ubuntu you must search the name of the package for LVM.
In order to create a LVM using physical, groups and logical volumes let’s see some commands available:
|pvcreate||Initialize physical volume(s) for use by LVM|
|pvs||Display information about physical volumes|
|pvdisplay||Display various attributes of physical volume(s)|
|pvresize||Resize physical volume(s)|
|pvmove||Move extents from one physical volume to another|
|vgcreate||Create a volume group|
|vgs||Display information about volume groups|
|vgdisplay||Display volume group information such as attributes|
|vgextend||Add physical volumes to a volume group|
|vgreduce||Remove physical volume(s) from a volume group|
|vgremove||Remove volume group(s)|
|lvcreate||Create a logical volume|
|lvs||Display information about logical volumes|
|lvdisplay||Display information about a logical volume|
|lvresize | lvreduce | lvextend||Resize a logical volume|
|lvmove||Remove logical volume(s) from the system|
Example of how to create a LVM
Let’ s suppose you have free 8 GB hard drive in /dev/sdb , with two partitions of 4 GB each (/dev/sdb1 and /dev/sdb2), and you want to create a LVM on those two partitions to use these mount points: 1 GB for the system’s root (/), 1 GB for swap (swap) and the remaining 6 GB for users directory (/home). To create the LVM you can type the following commands:
root@buckbeak:~# pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created root@buckbeak:~#pvcreate /dev/sdb2 Physical volume "/dev/sdb2" successfully created root@buckbeak:~# pvs PV VG Fmt Attr PSize PFree /dev/sdb1 lvm2 a- 4.00g 4.00g /dev/sdb2 lvm2 a- 3.99g 3.99g root@buckbeak:~# vgcreate vg_group01 /dev/sdb1 /dev/sdb2 Volume group "vg_group01" successfully created root@buckbeak:~# vgs VG #PV #LV #SN Attr VSize VFree vg_group01 2 0 0 wz--n- 7.99g 7.99g root@buckbeak:~# lvcreate -n lv_rootfs vg_group01 -L 1G Logical volume "lv_rootfs" created root@buckbeak:~# lvcreate -n lv_swap vg_group01 -L 1G Logical volume "lv_swap" created root@buckbeak:~# lvcreate -n lv_home vg_group01 -L 6G Volume group "vg_group01" has insufficient free space (1534 extents): 1536 required root@buckbeak:~# lvcreate -n lv_home vg_group01 -l 1534 Logical volume "lv_home" created root@buckbeak:~# lvs LV VG Attr LSize Origin Snap% Move Log Copy% Convert lv_home vg_group01 -wi-a- 5.99g lv_rootfs vg_group01 -wi-a- 1.00g lv_swap vg_group01 -wi-a- 1.00g
Now you can make the file system for each logical volume:
root@buckbeak:~# mkfs.ext4 /dev/vg_group01/lv_rootfs mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 65536 inodes, 262144 blocks 13107 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=268435456 8 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376 Writing inode tables: done Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 34 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. root@buckbeak:~# mkfs.ext4 /dev/vg_group01/lv_home mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 393216 inodes, 1570816 blocks 78540 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1610612736 48 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 20 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. root@buckbeak:~# mkswap /dev/vg_group01/lv_swap mkswap: /dev/vg_group01/lv_swap: warning: don't erase bootbits sectors on whole disk. Use -f to force. Setting up swapspace version 1, size = 1048572 KiB no label, UUID=e743b4f3-1c80-4503-be88-6934d575cd55 root@buckbeak:~# swapon /dev/vg_group01/lv_swap root@buckbeak:~# swapon -s Filename Type Size Used Priority /dev/dm-1 partition 2064376 8 -1 /dev/dm-5 partition 1048568 0 -2
If you want to use these logical volumes, you can mount them using the mount command or add them to the /etc/fstab file to be mount when the system boots.