When running virtual machines there might be a need to add space your virtual machine when it’s running low, in this example we will add 10GB to the root filesystem of a virtual instance of CentOS.
- Shut down your virtual machine
- Change the setting “Provisioned Size” in VMware to the total size that you want to have available in the operating system of your virtual server:
- Start the virtual machine and log in as root to check that size on your disk has changed:
# fdisk -l /dev/sdaDisk/dev/sda: 107.4 GB, 107374182400 bytes255 heads, 63 sectors/track, 13054 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/Osize (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x000076f1Device Boot Start End Blocks Id System/dev/sda1* 1 17 131072 83 LinuxPartition 1 does not end on cylinder boundary./dev/sda217 8355 66976768 8e Linux LVMIn this example, the disk /dev/sda has been extended to 107.4 GB and there are only two primary partitions currently in use, this is good because there are room for two more primary partitions.
- What you need to do now is to create the new partition sda3 that disk size can be taken from when extending the logical volume later on. This will be done with fdiskThe default values here should be sane, just press enter after the command input.
# fdisk /dev/sdaWARNING: DOS-compatible mode is deprecated. It's strongly recommended toswitch off the mode (command'c') and change displayunitstosectors (command'u').Command (mforhelp): nCommand actione extendedp primary partition (1-4)pPartition number (1-4): 3First cylinder (8355-13054, default 8355):Using default value 8355Last cylinder, +cylinders or +size{K,M,G} (8355-13054, default 13054):Using default value 13054 - In the previous step, a new partition was created from the space that was added from VMware, and now we need to set the correct partition type before writing the partition table and saving the changes:
Command (mforhelp): tPartition number (1-4): 3Hex code (typeL to list codes): 8eChanged systemtypeof partition 3 to 8e (Linux LVM)Command (mforhelp): wThe partition table has been altered!Calling ioctl() to re-readpartition table.WARNING: Re-reading the partition table failed with error 16: Device or resource busy.The kernel still uses the old table. The new table will be used atthe next reboot or after you run partprobe(8) or kpartx(8)Syncing disks.The warning message above can be addressed in two ways:
1) Reboot the system
2) Re-scan the device and add the new partition.We will do the latter:
# partx -v -a /dev/sdadevice/dev/sda: start 0 size 209715200gpt: 0 slicesdos: 4 slices# 1: 2048- 264191 ( 262144 sectors, 134 MB)# 2: 264192-134217727 (133953536 sectors, 68584 MB)# 3: 134217728-209712509 ( 75494782 sectors, 38653 MB)# 4: 0- -1 ( 0 sectors, 0 MB)BLKPG: Device or resource busyerror adding partition 1BLKPG: Device or resource busyerror adding partition 2added partition 3The partx application will give some errors for the existing partitions, but that’s nothing to worry about. The important part is that partition 3 was successfully added.
If partition 3 isn’t added, reboot the machine to make the changes take effect. - Create a new physical volume that can be used for adding space to the volume group
# pvcreate /dev/sda3Physical volume"/dev/sda3"successfully created - Check what name your volume group your root filesystem uses:
# vgdisplay--- Volume group ---VG Name 1System IDFormat lvm2[..]In this case the Volume group is called 1
- Extend the volume group with the space we added with vgextend:
# vgextend 1 /dev/sda3Volume group"1"successfully extended - With the utility pvscan you can see what physical volumes are available, and how much free space there is within them. The free space can be used to extend your root partion, or other volumes that are short on space.
# pvscanPV/dev/sda2VG 1 lvm2 [63,87 GiB / 29,87 GiBfree]PV/dev/sda3VG 1 lvm2 [36,00 GiB / 36,00 GiBfree]Total: 2 [99,87 GiB] /inuse: 2 [99,87 GiB] /inno VG: 0 [0 ] - In this step we will add 10G to the root partition. First find out what logical volume that the root partition belongs to:
# df -hFilesystem Size Used Avail Use% Mounted on/dev/mapper/1-sys7,8G 1,4G 6,0G 20% /tmpfs 246M 0 246M 0%/dev/shm/dev/sda1120M 27M 88M 24%/boot/dev/mapper/1-opt7,8G 181M 7,2G 3%/opt/dev/mapper/1-tmp7,8G 19M 7,4G 1%/tmp/dev/mapper/1-var7,8G 475M 6,9G 7%/varThe root partition originates from the logical volume “sys”, and the total size is 7.8 GB.
- The following command will add 10 GB to the Logical Volume “sys” in the Volume Group “1” and the disk area will be taken from the Physical Volume sda3.
# lvextend -L+10G /dev/mapper/1-sys /dev/sda3Size of logical volume 1/syschanged from 8,00 GiB (2048 extents) to 18,00 GiB (4608 extents).Logical volume sys successfully resized. - Run the following command to expand the ext4 filesystem inside of the Logical Volume:
# resize2fs /dev/mapper/1-sysresize2fs 1.41.12 (17-May-2010)Filesystem at/dev/mapper/1-sysis mounted on /; on-line resizing requiredold desc_blocks = 1, new_desc_blocks = 2Performing an on-line resize of/dev/mapper/1-systo 4718592 (4k) blocks.The filesystem on/dev/mapper/1-sysis now 4718592 blocks long. - If you get error after this command, try:
xfs_growfs /dev/centos/root
And volià! Now your root partition is extended with 10 GB:
# df -hFilesystem Size Used Avail Use% Mounted on/dev/mapper/1-sys18G 1,5G 16G 9% /tmpfs 246M 0 246M 0%/dev/shm/dev/sda1120M 27M 88M 24%/boot/dev/mapper/1-opt7,8G 181M 7,2G 3%/opt/dev/mapper/1-tmp7,8G 19M 7,4G 1%/tmp/dev/mapper/1-var7,8G 475M 6,9G 7%/var
