Re-sizing disk partition linux/vm

11,191

As already stated, you had first to resize the partition containing the logical volume, than you can proceed with LVM resize. Follow these steps:

  1. with fdisk -l -u /dev/sda take note of the current partition setting (especially the start sector)
  2. delete partition sda2 and recreate it. Use the very same start sector, or you will lose your data! Obviously, the end sector should be higher that before (fdisk will suggest to use the last available sector by default, so you can simply press ENTER here)
  3. now run partprobe /dev/sda or reboot the system. This will inform the kernel that partition size has changed
  4. run pvresize /dev/sda2. This will resize the physical volume structures
  5. execute pvs and vgs to check that the new size is correctly identified by LVM
  6. you can now resize your LVM volume with lvresize -l +100%FREE /dev/mapper/VolGroup-lv_root
  7. finally, resize the filesystem itself with resize2fs /dev/mapper/VolGroup-lv_root

These instruction are valid for increasing disk space (extending the volume)

Shrinking a volume is conceptually similar but you had to follow the steps backward, starting with filesystem shrink. As a side note, if you use XFS or other filesystems that can not be shrunk, you can not resize your LVM volume without a backup/restore process.

Share:
11,191

Related videos on Youtube

Tiffany Walker
Author by

Tiffany Walker

Updated on September 18, 2022

Comments

  • Tiffany Walker
    Tiffany Walker over 1 year

    I VM Player running a linux guest and I was wanting to know how do I expand the disk? In the VM player I gave more disk space but I am not sure how to mount/expand/connect the new disk space to the system.

    My old disk space was 14GB

    [root@localhost ~]# df -h /
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/mapper/VolGroup-lv_root
                           14G  4.5G  8.2G  36% /
    

    Then I expanded it and now I see sda2 which is the new space?

    [root@localhost ~]# fdisk -l
    
    Disk /dev/sda: 128.8 GB, 128849018880 bytes
    255 heads, 63 sectors/track, 15665 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk identifier: 0x000cd44d
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *           1          64      512000   83  Linux
    Partition 1 does not end on cylinder boundary.
    /dev/sda2              64        2611    20458496   8e  Linux LVM
    
    Disk /dev/mapper/VolGroup-lv_root: 14.5 GB, 14537457664 bytes
    255 heads, 63 sectors/track, 1767 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk identifier: 0x00000000
    
    
    Disk /dev/mapper/VolGroup-lv_swap: 6408 MB, 6408896512 bytes
    255 heads, 63 sectors/track, 779 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk identifier: 0x00000000
    

    Do I need to mount the new space first?

    resize2fs -p /dev/mapper/VolGroup-lv_root 108849018880
    resize2fs 1.41.12 (17-May-2010)
    The containing partition (or device) is only 3549184 (4k) blocks.
    You requested a new size of 1474836480 blocks.
    
     resize2fs -p /dev/mapper/VolGroup-lv_root 128849018880
    resize2fs 1.41.12 (17-May-2010)
    resize2fs: Invalid new size: 128849018880
    
    [root@localhost ~]# lvextend -L+90GB /dev/mapper/VolGroup-lv_root
      Extending logical volume lv_root to 103.54 GiB
      Insufficient free space: 23040 extents needed, but only 0 available
    
    [root@localhost ~]# lvextend -L90GB /dev/mapper/VolGroup-lv_root
      Extending logical volume lv_root to 90.00 GiB
      Insufficient free space: 19574 extents needed, but only 0 available
    

    EDIT: So after trying pvcreate/vgextend nothing has so far worked. I'm guessing the new disk space added from VM Player is not showing up?

    pvscan
      PV /dev/sda2   VG VolGroup   lvm2 [19.51 GiB / 0    free]
      Total: 1 [19.51 GiB] / in use: 1 [19.51 GiB] / in no VG: 0 [0   ]
    
    • Kishan K
      Kishan K over 9 years
      Could you please close this question in case one of the answers was ok? If something was wrong with them please let the people know.