This is the original disk layout before the disk extension
# pvcreate /dev/sda3
# vgs
# vgextend centos /dev/sda3
# lvdisplay | grep Path
# lvextend -l +100%FREE /dev/centos/root
# lvs
# df -Th /
# xfs_growfs /
# df -Th /
Linux is for everybody. Lets enjoy it.
This is the original disk layout before the disk extension
# pvcreate /dev/sda3
# vgs
# vgextend centos /dev/sda3
# lvdisplay | grep Path
# lvextend -l +100%FREE /dev/centos/root
# lvs
# df -Th /
# xfs_growfs /
# df -Th /
In previous post, we have covered the way to make LVM and filesystem aware of the disk size increase that happen in the virtual machine layer.
$ sudo lvmdiskscan
$ sudo pvcreate <path to the new disk>
$ sudo pvs
$ sudo vgextend <VG name> <PV name>
$ sudo vgs
$ sudo lvextend -l +100%FREE <LV PATH>
$ sudo lvdisplay | grep Path
$ sudo xfs_growfs <mountpoint>
$ sudo resize2fs <mountpoint>
$ df -Th
To increase a disk size in a virtual machine with Linux operating system configured with LVM, below are the steps (these steps were tested using virtualbox):
$ sudo pvresize <pv name>
$ sudo pvs
$ sudo vgs
$ sudo lvextend -l +100%FREE <LV path>
$ sudo lvdisplay | grep Path
$ df -Th
$ sudo xfs_growfs <mountpoint>
$ sudo resize2fs <mountpoint>
$ df -Th
The default partitioning scheme for CentOS is not ideal, because the installer only give 50Gb to /. and the rest to /home. Luckily, the partitions are being setup in LVM, thus enable us to change it without reinstalling the operating system.
In this example, I will show how to remove logical volume used by /home, and assign it to / instead.
First of all, you need to be root in order to do this.
This is the original partition layout:
# df -Th
Filesystem Type Size Used Avail Use% Mounted on
...
/dev/mapper/cl-root xfs 50G 4.1G 46G 9% /
/dev/mapper/cl-home xfs 1.9T 14G 1.9T 1% /home
/dev/vda1 ext4 976M 194M 716M 22% /boot
...
A usual scenario where rename and remount of LVM logical volume are needed, is when you install a box with a CentOS, and use the default LVM based partitioning scheme. This scheme will take 50G for your / partition, and the rest will be allocated to your /home, which is not practical. In this example, we will be remounting logical volume originally mounted to /home, to /var/lib/elasticsearch, renaming it along the way.
This is usually done, when the hardisk space for the CentOS VM is running low.
To extend the lv (logical volume), the command is lvextend. Please check beforehand that you have free disk space to be extended to.
Check which volume group the partition belongs to:
# df -lh
Filesystem Size Used Avail Use% Mounted on
...
/dev/mapper/vg01-db 6.0T 6.0T 20K 100% /var/lib/hwbackup/db
...
# lvdisplay vg01
--- Logical volume ---
LV Name /dev/vg01/db
VG Name vg01
LV UUID 2LvWW6-5TIG-ovrl-GUJt-1y3N-M6ME-3LgtUY
LV Write Access read/write
LV Status available
# open 1
LV Size 5.99 TB
Current LE 1569792
Segments 5
Allocation inherit
Read ahead sectors auto
- currently set to 4096
Block device 253:2
# vgdisplay vg01
...
Alloc PE / Size 4059648 / 15.49 TB
Free PE / Size 232788 / 909.33 GB
VG UUID iaFnIb-hKjs-q3fQ-yuTy-tKKl-uqSp-EXGyHF
...
# lvextend -t -v -L +300G /dev/mapper/vg01-dbwhere -t is for test mode, -v for verbose and -L is for the size that you want to extend
Test mode: Metadata will NOT be updated.
Finding volume group vg01
Using stripesize of last segment 512.00 KB
Test mode: Skipping archiving of volume group.
Extending logical volume db to 6.28 TB
Found volume group "vg01"
Found volume group "vg01"
Found volume group "vg01"
Test mode: Skipping volume group backup.
Logical volume db successfully resized
Test mode: Wiping internal cache
Wiping internal VG cache
[root@hitw-gc-backup-1 ~]# lvextend -v -L +300G /dev/mapper/vg01-db
Finding volume group vg01
Using stripesize of last segment 512.00 KB
Archiving volume group "vg01" metadata (seqno 19).
Extending logical volume db to 6.28 TB
Found volume group "vg01"
Found volume group "vg01"
Loading vg01-db table (253:2)
Suspending vg01-db (253:2) with device flush
Found volume group "vg01"
Resuming vg01-db (253:2)
Creating volume group backup "/etc/lvm/backup/vg01" (seqno 20).
Logical volume db successfully resized
# lvextend -v -L +300G /dev/mapper/vg01-db
Finding volume group vg01
Using stripesize of last segment 512.00 KB
Archiving volume group "vg01" metadata (seqno 19).
Extending logical volume db to 6.28 TB
Found volume group "vg01"
Found volume group "vg01"
Loading vg01-db table (253:2)
Suspending vg01-db (253:2) with device flush
Found volume group "vg01"
Resuming vg01-db (253:2)
Creating volume group backup "/etc/lvm/backup/vg01" (seqno 20).
Logical volume db successfully resized
# lvdisplay /dev/mapper/vg01-db
...
LV Size 6.28 TB
...
# umount /dev/mapper/vg01-db
# resize2fs /dev/mapper/vg01-dbFor xfs, you can resize it on the fly without unmounting it:
# mount /dev/mapper/vg01-db /var/lib/hwbackup/db
# xfs_growfs /var/lib/hwbackup/dbNow you can enjoy your newly extended lv:
# df -lh
Filesystem Size Used Avail Use% Mounted on
...
/dev/mapper/vg01-db 6.3T 5.7T 608G 91% /var/lib/hwbackup/db
...
Accessing xen guest image is very easy if the image is not lvm partitioned. But the main problem arise when the image is of lvm format and normal mount command cannot be used. Here I will show both the way. The first is when ext filesystem is used, and the second is when lvm is used.
To mount xen guest image (without lvm)
1. check the partition on the image
# fdisk -lu
The result will be something like this:
2. Mount using offset option
# mount -o loop,offset=106929152 /path/to/image /mnt
where 106929152=208846*512, 208846 is the start of the partition. Using this way, you only mount the second partition and not the whole image
3. You can now access your image at /mnt
To mount xen guest image (with lvm)
1. Check the partition on the image
# fdisk -lu /path/to/image
2. You have to install kpartx to handle lvm partiton
# yum install kpartx
3. Run kpartx on the image
# kpartx -av /path/to/image
4. Run vgscan to scan volume group available
# vgscan
5. Run vgchange to activate the volume group in the image
# vgchange -ay VolGroup00
6. Use lvs to see what is the name of your low volume group
# lvs
7. Mount the low volume group
# mount /dev/VolGroup00/LogVol01 /mnt
8. You can access your lvm image at the mounted directory which is /mnt
9. To unmount it, a few commands have to be executed (umount for unmounting, vgchange -an to deactivate volume group, kpartx -d to delete device map and losetup -d to delete loop device used)
# umount /mnt/
# vgchange -an VolGroup00
# kpartx -d /path/to/image
# losetup -d /dev/loop0
Hope this will be useful
Mounting is an easy process to do, provided the filesystem type you are using is supported. What happen when you have an LVM formatted disk, and you need to mount it because the disk cannot be booted and a hell lot of valuable data kept inside?? Do not worry, because the solution is here.......
1. Get a live cd, for example, Ubuntu. For this article, I use Ubuntu 6.06 (I cannot find any latest version of ubuntu at my place)
2. Boot using the live cd. Search for these tools: lvm2. If the cd do not have it, install it.
# apt-get install lvm2
3. To make sure the harddisk is recognised, you can use fdisk
# fdisk -lu
4. Once installed, run pvscan to scan all disks for physical volume. this to make sure your LVM harddisk is detected by Ubuntu
# pvscan
PV /dev/sda2 VG VolGroup00 lvm2 [74.41 GB / 32.00 MB free]
Total: 1 [74.41 GB] / in use: 1 [74.41 GB] / in no VG: 0 [0 ]
5. After that run vgscan to scan disks for volume groups.
# vgscan
Reading all physical volumes. This may take a while...
Found volume group "VolGroup00" using metadata type lvm2
6. Activate all volume groups available.
# vgchange -a y
2 logical volume(s) in volume group "VolGroup00" now active
7. Run lvscan to scan all disks for logical volume. You can see partitions inside the hard disk now active.
# lvscan
ACTIVE '/dev/VolGroup00/LogVol00' [72.44 GB] inherit
ACTIVE '/dev/VolGroup00/LogVol01' [1.94 GB] inherit
8. Mount the partition to any directory you want, usually to /mnt
# mount /dev/VolGroup00/LogVol00 /mnt
9. You can access the partition in the /mnt directory and can backup your data