Skip to content

Instantly share code, notes, and snippets.

@Himura2la
Last active July 17, 2023 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Himura2la/bc8106baa1700d8b745e2661ecd67eef to your computer and use it in GitHub Desktop.
Save Himura2la/bc8106baa1700d8b745e2661ecd67eef to your computer and use it in GitHub Desktop.
How to grow a mounted partition
  1. Grow the disk in your hypervisor (Hyper-V: Edit Disk in Actions panel)

  2. Apply the disk size change (you can do echo 1 | sudo tee /sys/class/block/sda/device/rescan instead of partprobe)

    $ sudo partprobe /dev/sda
    
    $ sudo dmesg | tail
    ...
    [4688278.217348] sd 2:0:0:0: Capacity data has changed
    [4688278.217583] sd 2:0:0:0: [sda] 83886080 512-byte logical blocks: (42.9 GB/40.0 GiB)
    [4688278.217661] sda: detected capacity change from 32212254720 to 42949672960
    
    $ sudo parted -l
    Warning: Not all of the space available to /dev/sda appears to be used, you can
    fix the GPT to use all of the space (an extra 20971520 blocks) or continue with
    the current setting?
    Fix/Ignore? fix
    
    Disk /dev/sda: 42.9GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags:
    
    Number  Start   End     Size    File system  Name  Flags
    ...
     2      538MB   32.2GB  31.7GB  ext4
    
  3. Use growpart (from cloud-guest-utils package) to resize the required partition and write changes (alternatives: resizepart, cfdisk, parted).

     $ sudo growpart --verbose /dev/sda 2
     CHANGED: partition=2 start=1050624 old: size=61863903 end=62914527 new: size=82835423 end=83886047
    
  4. Apply the partition size change (using resize2fs).

    $ sresize2fs /dev/sda2
    sresize2fs 1.46.2 (28-Feb-2021)
    Filesystem at /dev/sda2 is mounted on /; on-line resizing required
    old_desc_blocks = 4, new_desc_blocks = 5
    The filesystem on /dev/sda2 is now 10354427 (4k) blocks long.
    
  5. Verify.

    $ sudo fdisk -l
    Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 sectors
    ...
    
    Device       Start      End  Sectors  Size Type
    /dev/sda1     2048  1050623  1048576  512M EFI System
    /dev/sda2  1050624 83886046 82835423 39.5G Linux filesystem
    
    $ df -h
    Filesystem      Size  Used Avail Use% Mounted on
    ...
    /dev/sda2        39G   25G   13G  67% /
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment