OS - Linux - Resize disk

Resize separate data disk

Tested to work on an Ubuntu 14.04.4 LTS VM on VMWare ESXi 5.5, and CentOS 8 on VMware ESXi 6.7.

Scenario: the ext4 partition on the LVM logical volume on the LVM physical volume on partition sdb1 of sdb needs to be expanded. If you are using a physical volume directly on a disk without a partition in between, skip the fdisk steps.

  1. Backup your data first, then continue with these steps!
  2. Check the size of the current disk:
    lsblk
    1. Example output:
      NAME                           MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
      sda                              8:0    0    16G  0 disk
      +-sda1                           8:1    0   285M  0 part /boot
      +-sda2                           8:2    0  15.7G  0 part
        +-vg_root-lv_root (dm-0) 252:0    0  15.3G  0 lvm  /
        +-vg_root-lv_swap (dm-1) 252:1    0   480M  0 lvm  [SWAP]
      sdb                              8:16   0   600G  0 disk
      +-sdb1                           8:17   0   600G  0 part
        +-vg_root-lv_data (dm-2) 252:2    0   600G  0 lvm  /data
      sr0                             11:0    1  1024M  0 rom
    2. In this example we'll be resizing the 600 GB disk /dev/sdb.
  3. Enlarge the virtual disk in the hypervisor to the desired size.
  4. Check which virtual disks are present in the VM:
    ls -l /sys/class/scsi_disk/
    1. Example output:
      total 0
      lrwxrwxrwx 1 root root 0 Nov  1 08:27 2:0:0:0 -> ../../devices/pci0000:00/0000:00:15.0/0000:03:00.0/host2/target2:0:0/2:0:0:0/scsi_disk/2:0:0:0
      lrwxrwxrwx 1 root root 0 Nov  1 08:27 2:0:1:0 -> ../../devices/pci0000:00/0000:00:15.0/0000:03:00.0/host2/target2:0:1/2:0:1:0/scsi_disk/2:0:1:0
    2. In this example we are resizing the 2:0:1:0 disk.
  5. Force a rescan of the 2:0:1:0 disk:
    sudo sh -c 'echo 1 > /sys/class/scsi_disk/2\:0\:1\:0/device/rescan'
  6. Check if the extra disk space is now visible:
    lsblk
    1. Example output:
      NAME                           MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
      sda                              8:0    0    16G  0 disk
      +-sda1                           8:1    0   285M  0 part /boot
      +-sda2                           8:2    0  15.7G  0 part
        +-vg_root-lv_root (dm-0) 252:0    0  15.3G  0 lvm  /
        +-vg_root-lv_swap (dm-1) 252:1    0   480M  0 lvm  [SWAP]
      sdb                              8:16   0   700G  0 disk
      +-sdb1                           8:17   0   600G  0 part
        +-vg_root-lv_data (dm-2) 252:2    0   600G  0 lvm  /data
      sr0                             11:0    1  1024M  0 rom
    2. You can see that the sdb device is now 700 GB in size, but sdb1 is still 600 GB.
  7. If your physical volume does not reside on a partition (i.e. if there is no sdb1 between sdb and the lvm volume in lsblk output) skip to step 15 (and drop the number suffix to the device in the pvresize command).
  8. Now the sdb1 partition needs to be enlarged.
  9. Start fdisk on sdb:
    sudo fdisk /dev/sdb
  10. You should now see the following fdisk prompt:
    Command (m for help):
  11. Press 'p' and enter to print the current partition layout:
    Command (m for help): p
    1. Example output:
      Disk /dev/sdb: 644.2 GB, 644245094400 bytes
      12 heads, 25 sectors/track, 4194304 cylinders, total 1258291200 sectors
      Units = sectors of 1 * 512 = 512 bytes
      Sector size (logical/physical): 512 bytes / 512 bytes
      I/O size (minimum/optimal): 512 bytes / 512 bytes
      Disk identifier: 0x6d1297f2
      
         Device Boot      Start         End      Blocks   Id  System
      /dev/sdb1            2048  1258291199   629144576   83  Linux
      
      Command (m for help):
  12. Now we need to:
    1. Delete sdb1.
    2. Create a new primary partition using the full size of the disk.
    3. Write the changes.
    4. Use the following one-letter commands within fdisk (lines prefixed with # are comments):
      # Delete the partition
      d
      # If there is only one partition on the disk, as in this example, 
      # it will be deleted. If there are more specify the partition number to delete:
      1
      # Create a new partition
      n
      # Choose a primary partition
      p
      # Assign a number to the partition
      1
      #
      # Press Enter twice to accept the default values for the starting and ending of the partition.
      #
      # Print the partition table, it should now list the new partition
      p
      # Set partition type
      t
      # Select partition number to set type on
      2
      # Set it to LVM (8e) or Linux (83)
      # On CentOS 8: LVM (31) or Linux (20)
      8e
      # Write the new partition table to disk
      w
      # Exit fdisk
      q
  13. Refresh the partition table in the kernel (this wil fail if you are editing the root disk, a reboot will be required):
    partprobe
  14. Check if the resized partition is visible:
    lsblk
    1. Example output:
      NAME                           MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
      sda                              8:0    0    16G  0 disk
      +-sda1                           8:1    0   285M  0 part /boot
      +-sda2                           8:2    0  15.7G  0 part
        +-vg_root-lv_root (dm-0) 252:0    0  15.3G  0 lvm  /
        +-vg_root-lv_swap (dm-1) 252:1    0   480M  0 lvm  [SWAP]
      sdb                              8:16   0   700G  0 disk
      +-sdb1                           8:17   0   700G  0 part
        +-vg_root-lv_data (dm-2) 252:2    0   600G  0 lvm  /data
      sr0                             11:0    1  1024M  0 rom
    2. In the example output the sdb1 is now also 700 GB in size, but the lv_data is still 600 GB.
  15. Resize the LVM physical volume (PV) on sdb1:
    sudo pvresize /dev/sdb1
  16. Check that the pvresize worked:
    lsblk
    1. Example output:
      NAME                           MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
      sda                              8:0    0    16G  0 disk
      ├─sda1                           8:1    0   285M  0 part /boot
      └─sda2                           8:2    0  15.7G  0 part
        ├─vg_root-lv_root (dm-0) 252:0    0  15.3G  0 lvm  /
        └─vg_root-lv_swap (dm-1) 252:1    0   480M  0 lvm  [SWAP]
      sdb                              8:16   0   700G  0 disk
      └─sdb1                           8:17   0   700G  0 part
        └─vg_root-lv_data (dm-2) 252:2    0   700G  0 lvm  /data
      sr0                             11:0    1  1024M  0 rom
    2. In the example output the vg_root-lv_data is now also 700 GB.
  17. Resize the LVM logical volume (LV) to fill all available free space:
    sudo lvextend -l +100%FREE /dev/vg_root/lv_data
  18. Check that the logical volume has been resized:
    sudo lvs
    1. Example output:
        LV      VG          Attr      LSize   Pool Origin Data%  Move Log Copy%  Convert
        lv_data vg_root -wi-ao--- 700.00g
        lv_root vg_root -wi-ao---  15.25g
        lv_swap vg_root -wi-ao--- 480.00m
    2. This example output shows that the lv_data logical volume is now 700 GB.
  19. Resize the filesystem on the logical volume:
    1. For an ext4 formatted filesystem:
      sudo resize2fs /dev/mapper/vg_root-lv_data
    2. For an xfs formatted filesystem:
      sudo xfs_growfs /dev/mapper/vg_root-lv_data
  20. Check that the ext4 volume is now larger:
    df -h
    1. Example output:
      Filesystem                       Size  Used Avail Use% Mounted on
      udev                             5.9G  4.0K  5.9G   1% /dev
      tmpfs                            1.2G  836K  1.2G   1% /run
      /dev/dm-0                         15G  5.6G  8.6G  40% /
      none                             4.0K     0  4.0K   0% /sys/fs/cgroup
      none                             5.0M     0  5.0M   0% /run/lock
      none                             5.9G   12K  5.9G   1% /run/shm
      none                             100M     0  100M   0% /run/user
      /dev/mapper/vg_root-lv_data      689G  517G  142G  79% /data
      /dev/sda1                        276M   69M  194M  26% /boot
    2. The example output now shows vg_root-lv_data as 689 GB or close to 700 GB.