3

I have a VM template that I use for building other VMs using virt-clone / KVM. The VM template is 4GB to save space. The storage for VMs that I build from this are on iSCSI targets or LVM volumes (depending on function), and their filesystem sizes differ depending on the role of the machine.

After creating a new VM from the template, I have to resize the root partition if I'm building something that will need more than 4GB of disk. This works fine while using parted interactively, but not scripted. When trying to remove the file system, I'm asked if I want to continue despite using '-s'

The output below shows a failed script attempt and a working interactive session to achieve this.

What's the best way about resizing my root partition post-clone that can be simply scripted ?

Failed parted script attempt

Partition after cloning

# parted /dev/vda p
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  4294MB  4293MB  primary  ext4         boot

Script removal attempt

# parted /dev/vda -s rm 1
Warning: Partition /dev/vda1 is being used. Are you sure you want to continue?
#

Partition after failed removal

# parted /dev/vda p
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  4294MB  4293MB  primary  ext4         boot

Working interactive resize (followed by reboot)

Partition before removal

# parted /dev/vda p
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  4294MB  4293MB  primary  ext4         boot

Removal and creation of new partition that uses all of disk

# parted /dev/vda
(parted) rm 1
Warning: Partition /dev/vda1 is being used. Are you sure you want to continue?
Yes/No? y
Error: Partition(s) 1 on /dev/vda have been written, but we have been unable to
inform the kernel of the change, probably because it/they are in use.  As a
result, the old partition(s) will remain in use.  You should reboot now before
making further changes.
Ignore/Cancel? I
(parted) mkpart p ext4 1 -1
(parted)

# parted /dev/vda p
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  10.7GB  10.7GB  primary  ext4

Filesystem resize

# resize2fs /dev/vda1
resize2fs 1.42.9 (4-Feb-2014)
Filesystem at /dev/vda1 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vda1 is now 2621184 blocks long.

# df -kh  .
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1       9.8G  1.6G  7.9G  17% /
Anonymouslemming
  • 801
  • 2
  • 14
  • 25

1 Answers1

2

I've figured out how to do this. The key is kpartx to make the LVM usable by parted outside the VM (so on the Hypervisor host). Then you modify the partition size, then you boot the guest and increase the filesystem.

So if you have a guest named TESTVM that has its storage at /dev/VMS/VIRT-TESTVM, you'd do the following on the hypervisor host:

# kpartx -a /dev/VMS/VIRT-TESTVM
# parted /dev/VMS/VIRT-TESTVM rm 1
# parted /dev/VMS/VIRT-TESTVM mkpart -a optimal p ext4 0% 100% 
# kpartx -d /dev/VMS/VIRT-TESTVM

Then simply start the machine, login and do

# resize2fs /dev/vda1 

Reboot again just to be on the safe side.

Anonymouslemming
  • 801
  • 2
  • 14
  • 25