Resize Partition

Problem: A virtual machine has a data disk and it needs to be extended

  • Enlarge disk in VM tool; might be done online
  • New disk size is not seen in Linux yet. To avoid a reboot, rescan the disk:
# echo 1 > /sys/class/block/sdb/device/rescan
  • New size is now available:
# lsblk
[...]
sdb      8:16   0  2.5T  0 disk
└─sdb1   8:17   0    2T  0 part /home
  • Check with fdisk:
# fdisk -l /dev/sdb
Disk /dev/sdb: 2.5 TiB, 2684354560000 bytes, 5242880000 sectors
Disk model: Virtual Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x1451b487

Device     Boot Start        End    Sectors Size Id Type
/dev/sdb1        2048 4194303999 4194301952   2T 83 Linux
  • Parted also looks fine (with a GPT table it would offer me to fix sizes):
# parted /dev/sdb
GNU Parted 3.2
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: Msft Virtual Disk (scsi)
Disk /dev/sdb: 2684GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  2147GB  2147GB  primary  btrfs

(parted) quit
  • Ok, let’s extend
# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.33.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

The size of this disk is 2.5 TiB (2684354560000 bytes). DOS partition table format cannot be used on drives for volumes larger than 2199023255040 bytes for 512-byte sectors. Use GUID partition table format (GPT).

Command (m for help): q

Damn, this machine still uses MSDOS partition table and cannot grow beyond 2TB. Need to convert to GPT first:

  • Convert to GPT:
# gdisk /dev/sdb
GPT fdisk (gdisk) version 1.0.3

Partition table scan:
  MBR: MBR only
  BSD: not present
  APM: not present
  GPT: not present


***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by
typing 'q' if you don't want to convert your MBR partitions
to GPT format!
***************************************************************


Command (? for help): p
Disk /dev/sdb: 5242880000 sectors, 2.4 TiB
Model: Virtual Disk
Sector size (logical/physical): 512/4096 bytes
Disk identifier (GUID): 148332D1-AD46-47F7-B10A-BE80D0D1DD68
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 5242879966
Partitions will be aligned on 2048-sector boundaries
Total free space is 1048577981 sectors (500.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048      4194303999   2.0 TiB     8300  Linux filesystem

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.
  • Now extend partition (delete and recreate; first sector must be the same, last sector is more; do not wipe existing file system):
# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.33.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/sdb: 2.5 TiB, 2684354560000 bytes, 5242880000 sectors
Disk model: Virtual Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 148332D1-AD46-47F7-B10A-BE80D0D1DD68

Device     Start        End    Sectors Size Type
/dev/sdb1   2048 4194303999 4194301952   2T Linux filesystem

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Command (m for help): n
Partition number (1-128, default 1):
First sector (34-5242879966, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-5242879966, default 5242879966):

Created a new partition 1 of type 'Linux filesystem' and of size 2.5 TiB.
Partition #1 contains a btrfs signature.

Do you want to remove the signature? [Y]es/[N]o: n

Command (m for help): p

Disk /dev/sdb: 2.5 TiB, 2684354560000 bytes, 5242880000 sectors
Disk model: Virtual Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 148332D1-AD46-47F7-B10A-BE80D0D1DD68

Device     Start        End    Sectors  Size Type
/dev/sdb1   2048 5242879966 5242877919  2.5T Linux filesystem

Command (m for help): w
The partition table has been altered.
Syncing disks.
  • Check with lsblk again:
# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
[...]
sdb      8:16   0  2.5T  0 disk
└─sdb1   8:17   0  2.5T  0 part /home
  • Resize filesystem (it is a btrfs):
# btrfs fi resize max /home
Resize '/home' of 'max'
  • Check size:
# df -h /home
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1       2.5T  1.9T  610G  76% /home

Done