This page contains some of the tricks and experiences that I have came into with dealing with disks in Linux.
Expand Disks in Linux
Create a New LVM Volume
The example below will create a new logical volume based on an existing volume group with free space available. Use vgdisplay -v to display the available volume groups. If you need to expand the volume group refer to the Expand Volume Group section. In the example below, datavg is returned. Use lvcreate -n lvname -L size [MB|GB] datavg. If you want to use all the free space in the volume group use -l +100%FREE instead of the -L size parameter. After you create the logical volume you’ll need to create the file system using mkfs.ext3, mkfs.ext4, mkfs.xfs or whatever you want.
1. vgdisplay -v 2a. lvcreate -n lvname -L 10GB datavg OR 2b. lvcreate -n lvname -l +100%FREE datavg 3. mkfs.ext4 /dev/datavg/lvname Add the new lvname to the fstab if desired 4. mount -a 5. mount # verify new mount is there
Create a New LVM Swap Volume
The example below will create a new swap volume based on an existing volume group with free space available. Use vgdisplay -v to display the available volume groups. If you need to expand the volume group refer to the Expand Volume Group section. In the example below, lvswap is returned. Use lvcreate -n lvswap -L size [MB|GB] osvg. If you want to use all the free space in the volume group use -l +100%FREE instead of the -L size parameter. After you create the logical volume you’ll need to create the swap file system using mkswap and enable it.
1. vgdisplay -v 2a. lvcreate -n lvswap -L 2GB osvg OR 2b. lvcreate -n lvswap -l +100%FREE osvg 3. mkswap /dev/osvg/lvswap 4. Add the following line to /etc/fstab /dev/osvg/lvswap swap swap defaults 0 0 4. swapon -a 5. swapon -s
Expand LVM Volume
The example below will expand a logical volume based on an existing volume group with free space available. Use vgdisplay -v to display the available volume groups. If you need to expand the volume group refer to the Expand Volume Group section. In the example below, datavg is returned. Use lvextend -r -L size [MB|GB]
lvname. If you want to use all the free space in the volume group use -l +100%FREE instead of the -L size parameter. The -r command does online resizing. If this option fails you may have to unmount the file system and run the resize2fs on the unmounted device to expand it. Then remount when the resize2fs completes.
1. vgdisplay -v 2. Find the VG that LV is part of (If you need to expand the VG, refer to the Expand Volume Group Section 3. lvextend -r -L +10G /dev/datavg/lvname 4. df -h # to verify the new size
Add a New Drive to a Existing LVM Volume Group
The example below will add a new drive to a LVM Volume Group. The first thing you need to do is scan for the new drive(s), if it’s a new VMWare, Hyper-V or SAN volume. The results of the scan can be found at the tail of dmesg output. The output should look something like /dev/sd? where ? is a new drive character. Using this drive character you can create the physical volume and then add it to the volume group. You can specify the letter that ? will be directly or you can use the ? wildcard which will add all new /dev/sd? found.
1. cd /sys/class/scsi_host 2. for i in *; do echo '- - -' > $i/scan; done 3. dmesg | tail # to find the new drive letter 4. pvcreate /dev/sd? 5. vgextend /dev/datavg /dev/sd?
Add a New Drive to a New LVM Volume Group
The example below will add a newly added disk to a Linux server. This disk could be added in a Hypervisor or Physical system. The process is the same. This process is similar to the process of Add a New Drive to an Existing LVM Volume Group except instead of extending an existing Volume Group you are creating one.
The first thing you need to do is scan for the new drive(s), if it’s a new VMWare, Hyper-V or SAN volume. The results of the scan can be found at the tail of dmesg output. The output should look something like /dev/sd? where ? is a new drive character. Using this drive character you can create the physical volume and then use it to create the volume group. You can specify the letter that ? will be directly or you can use the ? wildcard which will add all new /dev/sd? found.
1. cd /sys/class/scsi_host 2. for i in *; do echo '- - -' > $i/scan; done 3. dmesg | tail # to find the new drive letter 4. pvcreate /dev/sd? 5. vgcreate datavg /dev/sd?
Expand an Already Attached Disk in a Volume Group
In the example below we already have a disk attached to a volume group called datavg
. Here we will expand it in Hyper-V or VMware by 50G.
1. display existing physical volume size: [root@server1~]$ pvs PV VG Fmt Attr PSize PFree /dev/sda3 Volume00 lvm2 a-- 35.30g <11.04g /dev/sdb Volume01 lvm2 a-- <50.00g 0 2. You'll need to run pvresize twice on the disk [root@server1~]$ pvresize /dev/sdb Physical volume "/dev/sdb" changed 1 physical volume(s) resized or updated / 0 physical volume(s) not resized [root@server1~]$ pvresize /dev/sdb Physical volume "/dev/sdb" changed 1 physical volume(s) resized or updated / 0 physical volume(s) not resized 3. Verify the resize happened with pvs again. [root@server1~]$ pvs PV VG Fmt Attr PSize PFree /dev/sda3 Volume00 lvm2 a-- 35.30g <11.04g /dev/sdb Volume01 lvm2 a-- <100.00g 50.00g
Now you can proceed with expanding the logical volume lvextend
as listed above.