If you use lvm on a virtual machine, you may need to extend logical volume.
For example, I have preconfigured lvm on disks:
[root@centos7 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 8G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 7G 0 part
├─centos_centos7-root 253:0 0 6,2G 0 lvm /
└─centos_centos7-swap 253:1 0 820M 0 lvm [SWAP]
sdb 8:16 0 8G 0 disk
└─sdb1 8:17 0 8G 0 part
├─docker-data 253:2 0 4G 0 lvm
└─docker-var_lib_docker 253:3 0 4G 0 lvm
sr0 11:0 1 1024M 0 rom
and I want to extend logical volume var_lib_docker on the volume group docker
Volume group docker doesn't have any free space:
parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 8589MB 8588MB primary lvm
So, I've stopped the virtual machine, extended virtual disk sdb and checked it:
(it can do without stopping virtual machine: extend disk, end refresh disk configuration: echo 1 |> /sys/class/block/sda/device/rescan )
[root@centos7 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 8G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 7G 0 part
├─centos_centos7-root 253:0 0 6,2G 0 lvm /
└─centos_centos7-swap 253:1 0 820M 0 lvm [SWAP]
sdb 8:16 0 10G 0 disk
└─sdb1 8:17 0 8G 0 part
├─docker-data 253:2 0 4G 0 lvm /data
└─docker-var_lib_docker 253:3 0 4G 0 lvm /var/lib/docker
sr0 11:0 1 1024M 0 rom
Ok, disk sdb became 10G size instead 8G, but physical volume and volume group still have old size:
[root@centos7 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos_centos7 lvm2 a-- <7,00g 0
/dev/sdb1 docker lvm2 a-- <8,00g 0
Let fix it:
[root@centos7 ~]# parted /dev/sdb print
Error: The backup GPT table is not at the end of the disk, as it should be. This might mean that another operating system believes the disk is smaller. Fix,
by moving the backup to the end (and removing the old backup)?
Fix/Ignore/Cancel? Fix
Warning: Not all of the space available to /dev/sdb appears to be used, you can fix the GPT to use all of the space (an extra 4194304 blocks) or continue with
the current setting?
Fix/Ignore? Fix
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 10,7GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 8589MB 8588MB primary lvm
(parted) resizepart
Partition number? 1
End? [8589MB]? 10240
(parted) print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 10,7GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 10,2GB 10,2GB primary lvm
(parted) q
Information: You may need to update /etc/fstab.
It would be better to umount logical volumes:
[root@centos7 ~]# umount /var/lib/docker/
[root@centos7 ~]# umount /data
Now resize phisycal volume:
[root@centos7 ~]# pvresize /dev/sdb1
Physical volume "/dev/sdb1" changed
1 physical volume(s) resized or updated / 0 physical volume(s) not resized
[root@centos7 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos_centos7 lvm2 a-- <7,00g 0
/dev/sdb1 docker lvm2 a-- 9,53g <1,54g
Now resize volume group:
[root@centos7 ~]# lvextend -l+100%FREE /dev/mapper/docker-var_lib_docker
Size of logical volume docker/var_lib_docker changed from 4,00 GiB (1024 extents) to <5,54 GiB (1417 extents).
Logical volume docker/var_lib_docker successfully resized.
And check it:
[root@centos7 ~]# e2fsck -f /dev/mapper/docker-var_lib_docker
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/mapper/docker-var_lib_docker: 6226/262144 files (0.1% non-contiguous), 89172/1048576 blocks
Then resize the file system:
[root@centos7 ~]# resize2fs /dev/mapper/docker-var_lib_docker
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/mapper/docker-var_lib_docker to 1451008 (4k) blocks.
The filesystem on /dev/mapper/docker-var_lib_docker is now 1451008 blocks long.
And mount logical volumes again:
[root@centos7 ~]# mount -a
check:
[root@centos7 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 8G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 7G 0 part
├─centos_centos7-root 253:0 0 6,2G 0 lvm /
└─centos_centos7-swap 253:1 0 820M 0 lvm [SWAP]
sdb 8:16 0 10G 0 disk
└─sdb1 8:17 0 9,5G 0 part
├─docker-data 253:2 0 4G 0 lvm /data
└─docker-var_lib_docker 253:3 0 5,5G 0 lvm /var/lib/docker
sr0 11:0 1 1024M 0 rom
the logical volume var_lib_docker was resized up to 5,5G.