======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. //
- Backup your data first, then continue with these steps!
- Check the size of the current disk:lsblk
- 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
- // In this example we'll be resizing the 600 GB disk /dev/sdb. //
- Enlarge the virtual disk in the hypervisor to the desired size.
- Check which virtual disks are present in the VM:ls -l /sys/class/scsi_disk/
- 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
- // In this example we are resizing the 2:0:1:0 disk. //
- 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'
- Check if the extra disk space is now visible:lsblk
- 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
- // You can see that the sdb device is now 700 GB in size, but sdb1 is still 600 GB. //
- // 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). //
- Now the sdb1 partition needs to be enlarged.
- Start fdisk on sdb:sudo fdisk /dev/sdb
- You should now see the following fdisk prompt:Command (m for help):
- Press 'p' and enter to print the current partition layout:Command (m for help): p
- 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):
- Now we need to:
- Delete sdb1.
- Create a new primary partition using the full size of the disk.
- Write the changes.
- 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
- Refresh the partition table in the kernel (this wil fail if you are editing the root disk, a reboot will be required):partprobe
- Check if the resized partition is visible:lsblk
- 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
- // In the example output the sdb1 is now also 700 GB in size, but the lv_data is still 600 GB. //
- Resize the LVM physical volume (PV) on sdb1:sudo pvresize /dev/sdb1
- Check that the pvresize worked:lsblk
- 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
- // In the example output the vg_root-lv_data is now also 700 GB. //
- Resize the LVM logical volume (LV) to fill all available free space:sudo lvextend -l +100%FREE /dev/vg_root/lv_data
- Check that the logical volume has been resized:sudo lvs
- 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
- // This example output shows that the lv_data logical volume is now 700 GB. //
- Resize the filesystem on the logical volume:
- For an ext4 formatted filesystem:sudo resize2fs /dev/mapper/vg_root-lv_data
- For an xfs formatted filesystem:sudo xfs_growfs /dev/mapper/vg_root-lv_data
- Check that the ext4 volume is now larger:df -h
- 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
- // The example output now shows vg_root-lv_data as 689 GB or close to 700 GB. //