Paginas

14 August 2019

Format, Extend/Reduce and Create Volume Groups / Logical Volumes in Linux


Recently I had to set up some linux volumes and partitions in a production environment. I have compiled some points that I find relevant.
In this post I will show you how to do the following configurations in Linux Operating Systems:
  • Add a new Physical Volume and Create Partition;
  • Create new Volume Group and Logical Volume with one or more disks;
  • Extend Volume Group with new Logical Volume;
  • Extend Volume Group and the Logical Volume;
  • Reduce a Logical Volume;
  • Mount/Umount NFS Volumes

Some useful commands to work with linux disks and LVM:

Physical Disks:
  • pvscan - Scans all supported LVM block devices in the system for physical volumes
  • pvcreate - Initializes physical volumes for later use by the LVM
  • pvs - Produces formatted output about physical volumes
  • lsblk - Lists information about all available or the specified block devices
  • fdisk - Disk partitioning functions 
  • resize2fs - Resize ext2, ext3, or ext4 file systems. It can be used to enlarge or shrink a file system. It can be done online 
  • df - Get full summary of available and used disk space usage of the file system

Volume Groups:
  • vgdisplay - Display information about the volume groups
  • vgcreate - Creates a new volume group
  • vgextend - Adds one or more PVs to a VG. This increases the space available for LVs in the VG
  • vgs - Report information about volume groups

Logical Volumes:
  • lvdisplay - Display information about the logical volumes
  • lvcreate - Create a logical volume
  • lvextend - Extend the size of a logical volume
  • lvs - Report information about logical volumes


Add a new Physical Volume and Create Partition

For adding a new PV we have to use fdisk to create the LVM partition. First find out the name of the device (in my case it was xvda) and then create the partition.
[root@linux ~]# fdisk -l
[root@linux ~]# fdisk -cu /dev/xvda
Create new partition: Press n
Choose primary partition: Press p
Choose number of partition to create the primary partition: Press 1
Change the type: Press t 
Change the partition type to Linux LVM (Press L to view all): Type 8e
View the partition: Press p
Write the changes: Press w
It may be required to restart the system once completed.

List and check the partition we have created using fdisk
[root@linux ~]# fdisk -l /dev/xvda


Create New Volume Group and Logical Volume

I will create a Volume Group named VG1 with six new disks (already presented by iSCSI, FC or local storage) and create a Logical Volume named LV1 with all the free space. It will be mounted in /data whenever the system boots up. Create six new disks in the Storage and present it to the linux machine (physical or virtual). Type fdisk -l to find the name of the volumes presented.
1. Initialize the Physical Volumes
[root@linux ~]# pvcreate /dev/xvdc
[root@linux ~]# pvcreate /dev/xvdd
[root@linux ~]# pvcreate /dev/xvde
[root@linux ~]# pvcreate /dev/xvdf
[root@linux ~]# pvcreate /dev/xvdg
[root@linux ~]# pvcreate /dev/xvdh
2. Create a new Volume Group named VG1
[root@linux ~]# vgcreate vg1 /dev/xvdc
3. Add the remaining disks to the VG. This increases the space available for LVs in the VG
[root@linux ~]# vgextend vg1 /dev/xvdd
[root@linux ~]# vgextend vg1 /dev/xvde
[root@linux ~]# vgextend vg1 /dev/xvdf
[root@linux ~]# vgextend vg1 /dev/xvdg
[root@linux ~]# vgextend vg1 /dev/xvdh

4. Create a new Logical Volume named LV1 with all the free space
[root@linux ~]# lvcreate -l 100%FREE -n lv1 vg1

5. Format the block storage device with EXT4 file system
[root@linux ~]# mkfs -t ext4 /dev/mapper/vg1-lv1

6. Mount the Logical Volume (this case in the /data directory) and add entry to FSTAB for automate the process of mounting partitions when the system boots up
[root@linux ~]# mkdir /data
[root@linux ~]# mount -t ext4 /dev/mapper/vg1-lv1 /data -o noatime,nodiratime,rw
[root@linux ~]# vi /etc/fstab and add the following entry:
/dev/mapper/vg1-lv1    /data                    ext4    defaults,noatime,nodiratime        1 2

7. Optional: Change the user and group ownership of the mount directory
[root@linux ~]# chown USER:GROUP /data


Extend Volume Group with new Logical Volume

I presented a new disk with 400GB to create two new Logical Volumes in the Volume Group. First I need to extend the Volume Group and then create and mount the the LVs. Create a new disk in the Storage and present it to the linux machine (physical or virtual). Type fdisk -l to find the name of the volume presented.

1. Initialize the Physical Volume
[root@linux ~]# pvcreate /dev/xvdc

2. Extend the existing VG named VG1. This increases the space available for LVs in the VG
[root@linux ~]# vgextend vg1 /dev/xvdc

3. Create two new Logical Volumes DATA01 and DATA02 with 200GB each
[root@linux ~]# lvcreate -L 200G -n data01 vg1
[root@linux ~]# lvcreate -L 200G -n data02 vg1

4. Format the block storage devices with EXT4 file system
[root@linux ~]# mkfs -t ext4 /dev/mapper/vg1-data01
[root@linux ~]# mkfs -t ext4 /dev/mapper/vg1-data02

5. Mount the Logical Volumes (this case in the /data01 and /data02 directories) and add entries to FSTAB for automate the process of mounting partitions when the system boots up
[root@linux ~]# mkdir /data01
[root@linux ~]# mkdir /data02
[root@linux ~]# mount -t ext4 /dev/mapper/vg1-data01 /data01 -o noatime,nodiratime,remount,rw
[root@linux ~]# mount -t ext4 /dev/mapper/vg1-data02 /data02 -o noatime,nodiratime,remount,rw
[root@linux ~]# vi /etc/fstab and add the following entry:
/dev/mapper/vg1-data01    /data01                    ext4    defaults,noatime,nodiratime        1 2
/dev/mapper/vg1-data02    /data02                    ext4    defaults,noatime,nodiratime        1 2

6. Optional: Change the user and group ownership of the mount directory
[root@linux ~]# chown USER:GROUP /data01
[root@linux ~]# chown USER:GROUP /data02


Extend Volume Group and the Logical Volume with New Disk

In this case, I presented a new disk to extend the Volume Group and then extend the Logical Volume with all the free space. Since we are only extending the disk, it was already formatted and mounted. Create a new disk in the Storage and present it to the linux machine (physical or virtual). Type fdisk -l to find the name of the volume presented.
1. Initialize the Physical Volume
[root@linux ~]# pvcreate /dev/xvdc

2. Extend the existing VG named VG1. This increases the space available for LVs in the VG
[root@linux ~]# vgextend vg1 /dev/xvdc

3. Extend the existing Logical Volume LV1 with all the free space
[root@linux ~]# lvextend -l +100%FREE  /dev/vg1/lv1

3. Resize the file system to the maximum space available. It was done online without disruption
[root@linux ~]# resize2fs /dev/mapper/vg1-lv1


Extend Logical Volume with Same Disk - Updated

In this case, I extended the Physical disk (in VMware or Hyper-V) and then extend the Logical Volume with all the free space. This is one of the most used scenarios and if you are extending the root partition you may have to reboot the machine because you can't umount.
1. Extend the disk in your virtual environment
   
2. Scan disk so you can see the new size
[root@linux ~]# fdisk -l /dev/sda
[root@linux ~]# echo 1>/sys/class/block/sda/device/rescan
[root@linux ~]# fdisk -l /dev/sda

   
3. Change partition table
[root@linux ~]# fdisk /dev/sda
Delete a partition: Press d
Choose partition number: Default
Add a new partition: Press n
Select primary: Press p
Partition number: Default
First sector: Default (it must be the first sector after the previous partition)
Last Sector: Default (the last sector of the expanded disk)
Print the configuration: Press p
Review if all setting are correct
Write the changes: Press w


4.Inform the operating system kernel of partition table changes
[root@linux ~]# partprobe (you might need a restart if the opertaing system is using this partition)
[root@linux ~]# reboot
[root@linux ~]# partprobe (if you did a restart on the previous step)


5.Resize the Physical disk
[root@linux ~]# pvresize /dev/sda2

6.Extend the existing Logical Volume with all the free space
[root@linux ~]# lvextend -l +100%FREE /dev/centos_centos7/root

7.Resize the file system to the maximum space available. It was done online without disruption
[root@linux ~]# resize2fs /dev/sda2 or xfs_growfs /dev/centos_centos7/root


Reduce a Logical Volume

Normally this type of operations are not usually performed and are high risk. If you have to do it, you should have updated backups if anything goes wrong. While extending a volume we can do it online, to reduce we have to unmount the file system.
For this demonstration, I have a 100GB volume and I want to reduce it to 60GB without data loss (I have 20GB of data on the volume. Of course that you must not have more than 60GB of data to reduce it).
1. Check the file system information
[root@linux ~]# df -h
2. Umount the file system for reducing
[root@linux ~]# umount -v /mnt/volume1

3. Check for the file system errors (if any). Must pass in every 5 steps of file-system check. If not, there might be some issue with your file-system.
[root@linux ~]# e2fsck -ff /dev/vg1/lv1

4. Reduce the file system
[root@linux ~]# resize2fs /dev/vg1/lv1 60GB

5. Reduce the Logical Volume
[root@linux ~]# lvreduce -L -40G /dev/vg1/lv1

6. Resize the file-system. In this step, if there is any error that means we have messed-up our file-system.
[root@linux ~]# resize2fs /dev/vg1/lv1

7. Mount the file system
[root@linux ~]# mount /dev/vg1/lv1 /mnt/volume1

8. Check the size
[root@linux ~]# lvdisplay vg1


Mount NFS Volumes

It's very common to mount a NFS share in linux, so you can do your backups for example.
1. Create a new directory for our mount point
[root@linux ~]# mkdir /teste

2. Mount the NFS Share to this directory
[root@linux ~]# mount -t nfs IPADDR:/nas/share /teste/
[root@linux ~]# mount | grep nfs

3. Add entry to FSTAB for automate the process of mounting partitions when the system boots up
[root@linux ~]# vi /etc/fstab and add the following entry:
IPADDR:/nas/share /teste/ nfs defaults 0 0

4. To dismount the volume just type the following
[root@linux ~]# umount /teste/


Some Errors

I found two errors on the procedures above. If you encounter the same, you already know how to solve them.
  • When you try to extend the volume group you may find this error:
[root@linux ~]# vgextend vg1 /dev/sda1
Couldn't create temporary archive name
Most likely because your root partition is full.
Solution
Try clearing out some files in the root (/) partition before running vgextend.

  • Error when you try to resize with resize2fs command:
[root@linux ~]# resize2fs /dev/mapper/vg1-lv1
resize2fs 1.42.9 (28-Dec-2013) 
resize2fs: Bad magic number in super-block while trying to open /dev/mapper/vg1-lv1
Solution
Install xfsprogs and then use it to resize
  • yum install xfsprogs
  • xfs_growfs /dev/mapper/vg1-lv1

I hope this article helps you when you have to configure linux disks and/or partitions.
See you next time!

No comments:

Post a Comment