Resize VM root disk inside Alpine image

Is it possible to create an Alpine VM from an Alpine Container?

Because i am trying to increase /dev/sda2 space/size since the Alpine VM creates with alower space size ev2n after giving it a size: 50GB attribute in the profile.

But In Ubuntu VM when i change the size attribute its reflected but not the same behaviour with alpine.

So i thought of creating a regular container which is created with enough size which then i can create a VM by copy the Container into a VM.

Not easily. As containers don’t have their own kernel and VMs require this.

Rather than doing that it sounds like you need to resize the Alpine VM’s disk space.

Can you show me what you have done so far?

The reason the disk space resizes automatically for Ubuntu VMs is because they have cloud-init installed which detect when the VM’s disk is grown and then also grows the filesystem in response.

I just duplicated thedefault profile and added the size: 50GB attribute. Then i createdhe VM using this profile for both the Alpine VM and Ubuntu VM but the Alpine VM didnt create with this 50GB though.

It likely did, its just the filesystem inside the VM didn’t detect and resize on first boot.

LXD does not have control of the filesystems inside a VM so cannot resize them itself (unlike containers).

This is how to resize the root disk inside an Alpine VM:

lxc init images:alpine/3.16 v1 --vm -c security.secureboot=false
lxc config device override v1 root size=50GiB
lxc start v1
lxc shell v1
v1:~# df -h # root partition /dev/sda2 is too small.
Filesystem                Size      Used Available Use% Mounted on
devtmpfs                 10.0M         0     10.0M   0% /dev
shm                     490.3M         0    490.3M   0% /dev/shm
/dev/sda2                 3.7G    177.2M      3.5G   5% /
tmpfs                   196.1M    260.0K    195.9M   0% /run
/dev/sda1                98.4M    266.0K     98.2M   0% /boot/efi
config                  231.5G     38.8G    180.9G  18% /run/lxd_config/drive

Lets resize it:

v1:~# growpart /dev/sda 2
CHANGED: partition=2 start=206848 old: size=8181727 end=8388574 new: size=104650719 end=104857566
v1:~# resize2fs /dev/sda2
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/sda2 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 7
The filesystem on /dev/sda2 is now 13081339 (4k) blocks long.

v1:~# df -h # Now root /dev/sda2 is correct size.
Filesystem                Size      Used Available Use% Mounted on
devtmpfs                 10.0M         0     10.0M   0% /dev
shm                     490.3M         0    490.3M   0% /dev/shm
/dev/sda2                48.3G    182.7M     48.1G   0% /
tmpfs                   196.1M    260.0K    195.9M   0% /run
/dev/sda1                98.4M    266.0K     98.2M   0% /boot/efi
config                  231.5G     38.8G    180.9G  18% /run/lxd_config/drive

This is similar to How can I expand the size of VM - #4 by tomp

1 Like