Skip to content

Instantly share code, notes, and snippets.

@wooyey
Last active April 15, 2022 19:44
Show Gist options
  • Save wooyey/398ba31d7b60633d01dc10068759cb20 to your computer and use it in GitHub Desktop.
Save wooyey/398ba31d7b60633d01dc10068759cb20 to your computer and use it in GitHub Desktop.
Linux partition resize
qemu-img resize image.qcow2 +20G

New to delete and create both: extended and lvm partition where new LVM starts on same sector as old one:

fdisk /dev/sda


Command (m for help): p

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders, total 83886080 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: 0x000a975f

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      499711      248832   83  Linux
/dev/sda2          501758    41940991    20719617    5  Extended
/dev/sda5          501760    41940991    20719616   8e  Linux LVM


Command (m for help): d
Partition number (1-5): 5

Command (m for help): d
Partition number (1-5): 2

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): e
Partition number (1-4, default 2):
Using default value 2
First sector (499712-83886079, default 499712):
Using default value 499712
Last sector, +sectors or +size{K,M,G} (499712-83886079, default 83886079):
Using default value 83886079

Command (m for help): n
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (501760-83886079, default 501760):
Using default value 501760
Last sector, +sectors or +size{K,M,G} (501760-83886079, default 83886079):
Using default value 83886079

Command (m for help): x

Expert command (m for help): b
Partition number (1-5): 5
New beginning of data (499713-83886079, default 501760): 501760

Expert command (m for help): r

Command (m for help): t
Partition number (1-5): 5
Hex code (type L to list codes): 8e
Changed system type of partition 5 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders, total 83886080 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: 0x000a975f

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      499711      248832   83  Linux
/dev/sda2          499712    83886079    41693184    5  Extended
/dev/sda5          501760    83886079    41692160   8e  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
partprobe /dev/sda
pvresize /dev/sda5
lvextend -l +100%FREE /dev/mapper/ubuntu-root
resize2fs /dev/mapper/ubuntu-root
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment