Importdisk with resize or bring libguestfs-tools to life

VasoVV

Active Member
Jun 12, 2019
1
0
41
39

Goals​

Painless creating VM from cloud image with resizing of root partition

Expected implementation​

* it is expected that qm importdisk has option to resize partitions of disk images while importing
like this: qm importdisk --resize partition=/dev/sdaN,size=[+]<size>


Bash:
# Setting variables
export VMID=2000
export STORAGE=local-lvm
export DISK=/dev/pve/vm-$VMID-disk-1
export IMGURL=https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img
export IMG=`basename $IMGURL`

# Getting disk image
wget $IMGURL
---
# Creating VM
qm create $VMID --memory 2048 --net0 virtio,bridge=vmbr0 --bios ovmf --efidisk0 $STORAGE:1


# Importing disk image with resizing root partition
qm importdisk $VMID --resize partition=/dev/sda1,size=10G $IMG $STORAGE
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                    !!! It is a Feature request and not implemented yet !!!

# Attach imported disk
qm set $VMID --virtio0 $STORAGE:vm-$VMID-disk-1

## Set boot order
qm set $VMID --boot order=virtio0

Real life​

Bash:
# Setting variables
---
# Getting disk image
---
# Creating VM
qm create $VMID --memory 2048 --net0 virtio,bridge=vmbr0 --bios ovmf --efidisk0 $STORAGE:1

# Importing disk image
qm importdisk $VMID $IMG $STORAGE

# Attach imported disk
qm set $VMID --virtio0 $STORAGE:vm-$VMID-disk-1

## Set boot order
qm set $VMID --boot order=virtio0

# Resize disk
qm resize $VMID virtio0 10G

# Get GPT information and define root partition number
fdisk -l $DISK
  …
  Device                      Start     End Sectors  Size Type
  /dev/pve/vm-2000-disk-1p1  227328 4612062 4384735  2,1G Linux filesystem    <----- Root partition has number 1
  /dev/pve/vm-2000-disk-1p14   2048   10239    8192    4M BIOS boot
  /dev/pve/vm-2000-disk-1p15  10240  227327  217088  106M EFI System

# Fixing GPT after resizing
sgdisk -e $DISK

# Deleting root partition
sgdisk -d 1 $DISK

# Recreating root partition
sgdisk -N 1 $DISK

# Create mapping of our disk to make it's partitions available
kpartx -a $DISK

# Checking filesystem before resizing
e2fsck -f /dev/mapper/pve-vm--$VMID--disk--1p1

# Resizing filesystem
resize2fs /dev/mapper/pve-vm--$VMID--disk--1p1

Alternative way with libguestfs-tools​

The idea is to resize the root partition in image before importing
Bash:
# Get the root partition number from image
virt-filesystems --long -h -a $IMG          
  Name        Type        VFS   Label            Size  Parent
  /dev/sda1   filesystem  ext4  cloudimg-rootfs  2,1G  -      <----- It is here (/dev/sda1)
  /dev/sda15  filesystem  vfat  UEFI             106M  -

# Allocate space for new image
truncate -s 10G new.img

# Resizing
virt-resize --expand /dev/sda1 $IMG new.img

# Checking up result:
virt-filesystems --long -h -a new.img       
  Name       Type        VFS   Label            Size  Parent
  /dev/sda2  filesystem  vfat  UEFI             106M  -
  /dev/sda3  filesystem  ext4  cloudimg-rootfs  9,9G  -      <----- Looks good, but partition numbering
                                                                    has changed, so BIOS boot won't work
                                                                    until `update-grub && grub-install /dev/sda`,
                                                                    but EFI boot works perfect

# Creating VM
qm create $VMID --memory 2048 --net0 virtio,bridge=vmbr0 --bios ovmf --efidisk0 $STORAGE:1

# Importing disk image
qm importdisk $VMID new.img $STORAGE

# Attach imported disk
qm set $VMID --virtio0 $STORAGE:vm-$VMID-disk-1

## Set boot order
qm set $VMID --boot order=virtio0

But one more problem: libguestfs-tools doesn't work in pve:
Bash:
LIBGUESTFS_DEBUG=1 LIBGUESTFS_TRACE=1 virt-filesystems -a $IMG  
libguestfs: trace: set_verbose true
libguestfs: trace: set_verbose = 0
libguestfs: create: flags = 0, handle = 0x5619f08c58e0, program = virt-filesystems
libguestfs: trace: add_drive "jammy-server-cloudimg-amd64.img" "readonly:true"
libguestfs: creating COW overlay to protect original drive content
libguestfs: trace: get_tmpdir
libguestfs: trace: get_tmpdir = "/tmp"
libguestfs: trace: disk_create "/tmp/libguestfsPC4LC3/overlay1.qcow2" "qcow2" -1 "backingfile:/tmp/jammy-server-cloudimg-amd64.img"
libguestfs: command: run: qemu-img
libguestfs: command: run: \ create
libguestfs: command: run: \ -f qcow2
libguestfs: command: run: \ -o backing_file=/tmp/jammy-server-cloudimg-amd64.img
libguestfs: command: run: \ /tmp/libguestfsPC4LC3/overlay1.qcow2
qemu-img: /tmp/libguestfsPC4LC3/overlay1.qcow2: Backing file specified without backing format
Detected format of qcow2.libguestfs: error: qemu-img: /tmp/libguestfsPC4LC3/overlay1.qcow2: qemu-img exited with error status 1, see debug messages above
libguestfs: trace: disk_create = -1 (error)
libguestfs: trace: add_drive = -1 (error)
libguestfs: trace: close
libguestfs: closing guestfs handle 0x5619f08c58e0 (state 0)
libguestfs: command: run: rm
libguestfs: command: run: \ -rf /tmp/libguestfsPC4LC3

Conclusions​

For happy life we need qm importdisk --resize feature or whether libguestfs-tools working
 
Feature requests should be filed in https://bugzilla.proxmox.com/ , this is more of a support forum.

On topic - since you are using a cloud-init capable image wouldn't it be easier to see if you can utilize processes that already exist and are not PVE specific?
https://cloudinit.readthedocs.io/en/latest/topics/examples.html?highlight=resize#grow-partitions
https://cloudinit.readthedocs.io/en/latest/topics/modules.html?highlight=resize#resizefs

The "ideal" process you have described seems to be extremely configuration/OS/storage/layout dependent.

Further, I've successfully installed and used libguestfs tools on PVE host for a small surgery I needed to perform on an image. The error you listed is not fatal, first google search result: https://github.com/GNS3/gns3-server/issues/1964



Blockbridge: Ultra low latency all-NVME shared storage for Proxmox - https://www.blockbridge.com/proxmox