Skip to content

Instantly share code, notes, and snippets.

@HugoPoi
Last active June 22, 2024 19:14
Show Gist options
  • Save HugoPoi/a817233aee0e4eb0b72fb6bdf60bfa90 to your computer and use it in GitHub Desktop.
Save HugoPoi/a817233aee0e4eb0b72fb6bdf60bfa90 to your computer and use it in GitHub Desktop.
How to resize virtual machine partition after extend the disk

Resize a VM system disk

Introduction

  • You have a VM with small disk
  • You have GPT partition table
  • You NOT USE LVM (you should)
  • You need to extend a partition on the main drive
  • You use Debian 8.x aka Jessie

Extend the disk

  • Open your VM hypervisor config and resize the disk
  • The VM main disk will now be 40G in place of 20G
  • Your partition is still based on the 20G disk
  • You need to reboot to notify the VM of the change of disk size ( lsblk or fidsk you will still see 20G without rebooting )
  • Maybe you can hot reload the disk metadata in kernel with partprobe

Edit/fix the partition table

  • fdisk or cfdisk WILL NOT WORK FOR THIS, and FAIL with Value out of range during creation on partition exciding the original disk size ( on Debian 8 )
  • Use parted it will fix the GPT partition table
  1. Unmount swap if this is your last partition swapoff /dev/sda3
  2. parted /dev/sda
  3. (parted) print
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 41943040 blocks) or continue with the
current setting?
Fix/Ignore? Fix

And then you will have something like that

Model: ATA BHYVE SATA DISK (scsi)
Disk /dev/sda: 42,9GB
Sector size (logical/physical): 512B/16384B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system     Name  Flags
 1      1049kB  538MB   537MB   fat32                 boot, esp
 2      538MB   20,9GB  20,4GB  ext4
 3      20,9GB  21,5GB  535MB   linux-swap(v1)
  1. Optionnaly delete your swap partition (parted) rm 3
  2. (parted) resizepart 2
Warning: Partition /dev/sda2 is being used. Are you sure you want to continue?
Yes/No? Yes
  1. (parted) End? [20,9GB]? 37GB
  2. Optionnaly recreate the swap partition at the end of disk with (parted) mkpart
Partition name?  []? swap
File system type?  [ext2]? swap
parted: invalid token: swap
File system type?  [ext2]? linux-swap
Start? 37GB
End? 42,9GB
  1. (parted) print
Number  Start   End     Size    File system     Name  Flags
 1      1049kB  538MB   537MB   fat32                 boot, esp
 2      538MB   37,0GB  36,5GB  ext4
 3      37,0GB  42,9GB  5949MB  linux-swap(v1)  swap
  1. You can (parted) quit

Extend/Rebuild the FileSystems

  1. Launch mkswap /dev/sda3
  2. Launch resize2fs /dev/sda2
  3. Then blkid /dev/sda3
  4. Report the new UUID in /etc/fstab
  5. Remount your swap with swapon /dev/sda3
  6. You are ALL GOOD TO GO (or not)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment