How to embiggen a raw disk partition using libvirt/kvm/qemu

home | blog | Terrible people and places | Covid-19 links | Teh Internet | guest blog |rants | placeholder | political | projects | Gwen and Liam | Citadel patched | Tools | Scouts




DONT DO THIS ON DATA YOU CARE ABOUT
YOU WILL MAKE A MISTAKE AND LOOSE ALL OF YOUR DATA
YOU HAVE BEEN WARNED.

Do back up / snapshot / test on a non-production filesystem.
Do not try this, these are my notes...
If you are foolhardy enough to give it a try:
Make a copy of your vm image (lvm snapshot and snag a copy).
Edit your new vm in virsh (nuke the mac address line so you don't bump heads with the live one).
Edit your ip address in your vm (mount it) see other page on this blog.
- hint blog/01256151999 check for the mount bit.
Ok, now on to the fun:
before all this - add 10GB to the disk image

(turn off the vm first):

on the host find the vm disk image and resize it:
 qemu-img resize vmdiskfile +10G

boot up the vm and extend the partition table:

root@vmguest:~# fdisk /dev/vda

---> trust, but verify:

Command (m for help): p

Disk /dev/vda: 53.7 GB, 53687091200 bytes
16 heads, 63 sectors/track, 104025 cylinders, total 104857600 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: 0x000702ec

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048      499711      248832   83  Linux
/dev/vda2          501758    83884031    41691137    5  Extended
/dev/vda5          501760    83884031    41691136   8e  Linux LVM

---> The disk /dev/vda was 42.9 GB before the qemu-resize (above)

---> delete the lvm partition (re-create later as larger)

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

---> delete the extended partition (re-create later as larger)

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

---> trust, but verify:

Command (m for help): p

Disk /dev/vda: 53.7 GB, 53687091200 bytes
16 heads, 63 sectors/track, 104025 cylinders, total 104857600 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: 0x000702ec
   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048      499711      248832   83  Linux

---> re-create the extended partition (now will be larger)

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-104857599, default 499712):
Using default value 499712
Last sector, +sectors or +size{K,M,G} (499712-104857599, default 104857599):
Using default value 104857599

---> trust, but verify:

Command (m for help): p

Disk /dev/vda: 53.7 GB, 53687091200 bytes
16 heads, 63 sectors/track, 104025 cylinders, total 104857600 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: 0x000702ec

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048      499711      248832   83  Linux
/dev/vda2          499712   104857599    52178944    5  Extended

---> re-create the logical partition (now will be larger)

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-104857599, default 501760):
Using default value 501760
Last sector, +sectors or +size{K,M,G} (501760-104857599, default 104857599):
Using default value 104857599

---> trust, but verify:

Command (m for help): p
Disk /dev/vda: 53.7 GB, 53687091200 bytes
16 heads, 63 sectors/track, 104025 cylinders, total 104857600 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: 0x000702ec

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048      499711      248832   83  Linux
/dev/vda2          499712   104857599    52178944    5  Extended
/dev/vda5          501760   104857599    52177920   83  Linux

---> re-create the partition types to match what was:

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)

---> trust, but verify:

Command (m for help): p

Disk /dev/vda: 53.7 GB, 53687091200 bytes
16 heads, 63 sectors/track, 104025 cylinders, total 104857600 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: 0x000702ec

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048      499711      248832   83  Linux
/dev/vda2          499712   104857599    52178944    5  Extended
/dev/vda5          501760   104857599    52177920   8e  Linux LVM

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.

---> Say a little prayer:

root@vmguest:~# reboot

Broadcast message from root@vmguest
        (/dev/pts/0) at 15:31 ...

The system is going down for reboot NOW!
root@vmguest:~# Connection to vmguest closed by remote host.
Connection to vmguest closed.

---> ssh to the vmguest (hope it booted):

root@vmguest's password:
Welcome to blah

---> important part to note (we are almost full captain!):

................
  => / is using 91.9% of 33.10GB
................

---> lets check out what the volume groups look like:

root@vmguest:~# vgdisplay
  --- Volume group ---
  VG Name               vmguest
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               39.76 GiB
  PE Size               4.00 MiB
  Total PE              10178
  Alloc PE / Size       10178 / 39.76 GiB
  Free  PE / Size       0 / 0
  VG UUID               VJjvr3-Xan6-0Erq-Lo9y-QvT2-gQ4l-KR4y0w

---> lets check out what the logical volumes look like:

root@vmguest:~# lvdisplay
  --- Logical volume ---
  LV Name                /dev/vmguest/root
  VG Name                vmguest
  LV UUID                syduCb-TDzz-Bow8-FonV-ePg7-Zfz8-2VDdvy
  LV Write Access        read/write

  LV Status              available
  # open                 1
  LV Size                33.76 GiB
  Current LE             8642
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0

  --- Logical volume ---
  LV Name                /dev/vmguest/swap_1
  VG Name                vmguest
  LV UUID                a6ibbL-70hx-6tFN-2ygT-yOjg-8hxk-saioHr
  LV Write Access        read/write
  LV Status              available
  # open                 2
  LV Size                6.00 GiB
  Current LE             1536
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:1

---> lets check out what the partitions look like:

root@vmguest:~# mount
/dev/mapper/vmguest-root on / type ext4 (rw,errors=remount-ro)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
none on /sys/fs/fuse/connections type fusectl (rw)
none on /sys/kernel/debug type debugfs (rw)
none on /sys/kernel/security type securityfs (rw)
udev on /dev type devtmpfs (rw,mode=0755)
devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620)
tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755)
none on /run/lock type tmpfs (rw,noexec,nosuid,nodev,size=5242880)
none on /run/shm type tmpfs (rw,nosuid,nodev)
/dev/vda1 on /boot type ext2 (rw)

---> Roll for sanity (i.e. check your partions before getting them wrong):

root@vmguest:~# fdisk /dev/vda
Command (m for help): p

Disk /dev/vda: 53.7 GB, 53687091200 bytes
16 heads, 63 sectors/track, 104025 cylinders, total 104857600 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: 0x000702ec

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048      499711      248832   83  Linux
/dev/vda2          499712   104857599    52178944    5  Extended
/dev/vda5          501760   104857599    52177920   8e  Linux LVM

Command (m for help): q

---> If you feel lucky, extend the physical volume on the partion that has the lvm

root@vmguest:~# pvresize /dev/vda5
  Physical volume "/dev/vda5" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized
root@vmguest:~# pvdisplay
  --- Physical volume ---
  PV Name               /dev/vda5
  VG Name               vmguest
  PV Size               49.76 GiB / not usable 2.81 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              12738
  Free PE               2560
  Allocated PE          10178
  PV UUID               wZlP0L-UIcw-qfHM-QMga-HL0G-sYkU-f3zfz8

---> If you still feel lucky, extend the logical volume on the partion that has the lvm

root@vmguest:~# lvextend /dev/vmguest/root /dev/vda5
  Extending logical volume root to 43.76 GiB
  Logical volume root successfully resized
root@vmguest:~#
root@vmguest:~# lvdisplay
  --- Logical volume ---
  LV Name                /dev/vmguest/root
  VG Name                vmguest
  LV UUID                syduCb-TDzz-Bow8-FonV-ePg7-Zfz8-2VDdvy
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                43.76 GiB
  Current LE             11202
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0

  --- Logical volume ---
  LV Name                /dev/vmguest/swap_1
  VG Name                vmguest
  LV UUID                a6ibbL-70hx-6tFN-2ygT-yOjg-8hxk-saioHr
  LV Write Access        read/write
  LV Status              available
  # open                 2
  LV Size                6.00 GiB
  Current LE             1536
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:1

root@vmguest:~#

---> If that worked, why not try to resize /?

root@vmguest:~# resize2fs /dev/vmguest/root
resize2fs 1.42 (29-Nov-2011)
Filesystem at /dev/vmguest/root is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 3
The filesystem on /dev/vmguest/root is now 11470848 blocks long.

--- ok now only 75% full, not 97%!------------
root@vmguest:~# df -h
Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/vmguest-root      43G   31G   11G  75% /
udev                         3.0G  4.0K  3.0G   1% /dev
tmpfs                        598M  260K  597M   1% /run
none                         5.0M     0  5.0M   0% /run/lock
none                         3.0G     0  3.0G   0% /run/shm
/dev/vda1                    228M  158M   59M  73% /boot

Nice, pat yourself on the back.



[æ]