Cheat on increasing partitions and lvm

Home

0.1 Increase kvm disk image size

This assumes that the vm is a kvm virtual machine with a qcow2 image type.

On the kvm host, issue this command to increase the size of the qcow2 image by 1 Gigabyte: sudo qemu-img resize /var/lib/libvirt/images/vm5.qcow2 +1G

Note that the size of the actual qcow2 file won't change at this point.

0.2 Boot the kvm vm

On booting you should see that the disk drive has increased in size by 1G however, nothing has changed on the partitions. I used lsblk command.

root@vm5~[1007] $
lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0    9G  0 disk 
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0    7G  0 part 
  ├─cl-root 253:0    0  6.2G  0 lvm  /
  └─cl-swap 253:1    0  820M  0 lvm  [SWAP]
sr0          11:0    1 1024M  0 rom  
root@vm5~[1008] $

In my example above, I have expanded the /dev/sda drive from 8G to 9G

Similarily, the command fdisk -l /dev/sda shows that you now have 9G of space on the /dev/sda drive:

root@vm5~[1005] $
fdisk -l /dev/sda
Disk /dev/sda: 9 GiB, 9663676416 bytes, 18874368 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
Disklabel type: dos
Disk identifier: 0xb23213cb

Device     Boot   Start      End  Sectors Size Id Type
/dev/sda1  *       2048  2099199  2097152   1G 83 Linux
/dev/sda2       2099200 16777215 14678016   7G 8e Linux LVM
root@vm5~[1006] $

0.3 Create a new primary partition using fdisk

The command to use is fdisk /dev/sda which manages the partition table on the drive /dev/sda

root@vm5~[1008] $
fdisk /dev/sda

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


Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table

I then used the command n to create a new partition, and p to make it a primary partition. The rest I just used the defaults as that used up all the available space I had added.

Command (m for help): n
Partition type
   p   primary (2 primary, 0 extended, 2 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (3,4, default 3): 
First sector (16777216-18874367, default 16777216): 
Last sector, +sectors or +size{K,M,G,T,P} (16777216-18874367, default 18874367): 

Created a new partition 3 of type 'Linux' and of size 1 GiB.

Command (m for help):

I then saved and quit the fdisk session with the w command:

Command (m for help): w
The partition table has been altered.
Syncing disks.

Then I confirmed that I had a new partition from the 1G of added space using the lsblk command again:

root@vm5~[1009] $
lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0    9G  0 disk 
├─sda1        8:1    0    1G  0 part /boot
├─sda2        8:2    0    7G  0 part 
│ ├─cl-root 253:0    0  6.2G  0 lvm  /
│ └─cl-swap 253:1    0  820M  0 lvm  [SWAP]
└─sda3        8:3    0    1G  0 part 
sr0          11:0    1 1024M  0 rom  
root@vm5~[1010] $

You can see that my new partition is sda3.

You can also check what the partitions are with the fdisk -l command:

root@vm5~[1010] $
fdisk -l
Disk /dev/sda: 9 GiB, 9663676416 bytes, 18874368 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
Disklabel type: dos
Disk identifier: 0xb23213cb

Device     Boot    Start      End  Sectors Size Id Type
/dev/sda1  *        2048  2099199  2097152   1G 83 Linux
/dev/sda2        2099200 16777215 14678016   7G 8e Linux LVM
/dev/sda3       16777216 18874367  2097152   1G 83 Linux




Disk /dev/mapper/cl-root: 6.2 GiB, 6652166144 bytes, 12992512 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 /dev/mapper/cl-swap: 820 MiB, 859832320 bytes, 1679360 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
root@vm5~[1011] $

0.4 Change the Partition Type to be a LVM

The partition should be changed to a logical volume, so use fdisk again:

root@vm5~[1016] $
fdisk /dev/sda

Welcome to fdisk (util-linux 2.32.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/sda: 9 GiB, 9663676416 bytes, 18874368 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
Disklabel type: dos
Disk identifier: 0xb23213cb

Device     Boot    Start      End  Sectors Size Id Type
/dev/sda1  *        2048  2099199  2097152   1G 83 Linux
/dev/sda2        2099200 16777215 14678016   7G 8e Linux LVM
/dev/sda3       16777216 18874367  2097152   1G 83 Linux

Command (m for help): t
Partition number (1-3, default 3): 3
Hex code (type L to list all codes): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden or  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi ea  Rufus alignment
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         eb  BeOS fs        
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ee  GPT            
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        ef  EFI (FAT-12/16/
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f0  Linux/PA-RISC b
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f1  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f4  SpeedStor      
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      f2  DOS secondary  
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fb  VMware VMFS    
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fc  VMware VMKCORE 
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fd  Linux raid auto
1c  Hidden W95 FAT3 75  PC/IX           bc  Acronis FAT32 L fe  LANstep        
1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot    ff  BBT            
Hex code (type L to list all codes): 8e

Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): 

And just confirm that sda3 is now LVM by using p command to print the table:

Command (m for help): p
Disk /dev/sda: 9 GiB, 9663676416 bytes, 18874368 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
Disklabel type: dos
Disk identifier: 0xb23213cb

Device     Boot    Start      End  Sectors Size Id Type
/dev/sda1  *        2048  2099199  2097152   1G 83 Linux
/dev/sda2        2099200 16777215 14678016   7G 8e Linux LVM
/dev/sda3       16777216 18874367  2097152   1G 8e Linux LVM

Command (m for help): w
The partition table has been altered. 
Syncing disks.

root@vm5~[1017] $

Don't forget to w write the changes to disk:

0.5 run fsck on the new partition

This is the file system check, fsck command.

This will give you an error, as this partition does NOT yet have a filesystem on it. i.e. You have not formatted that partition yet.

0.6 Add the new partition to the root logical volume, thus extending its size

At this stage I can format the new partition as another mount point, say /data or something like that

1 Options to extend the partition (non disruptively)

1.1 Resizing using growpart (cloud-utils-growpart)

I had success on several kvm based VMs using the earlier approach in my org file here Resizing a guest disk space I recommend using this approach.

1.2 Blog (not attempted)

This blog shows how, if you are careful, you can extend the size of the partition without destroying the data on it, but I did NOT do this. Maybe next time. My biggest roadblock is that you would have to unmount the partition, and this is the root partition / that we are talking about, so I did not do this.

1.3 Home