4

I would like to shrink the main disk of a LXC container on a Proxmox VE 4.2, from 30GB to 20GB for instance. Shrinking seems not to be supported yet :

$ pct resize <VMID> rootfs 20G
unable to shrink disk size

Any ideas on how to accomplish this ?

3 Answers 3

12

You probably figured it out already, but that seems to be a limitation of LXC containers, at least for now. From the pct man page :

 <size> \+?\d+(\.\d+)?[KMGT]?
       The new size. With the + sign the value is added to the actual size of the volume and without it,
       the value is taken as an absolute one. Shrinking disk size is not supported.

If you truly want to shrink a container, I guess you'd have to perform a backup, then restore it with the --rootfs local:<newsize> option, like so :

pct stop <id>
vzdump <id> -storage local -compress lzo
pct destroy <id>
pct restore <id> /var/lib/lxc/vzdump-lxc-<id>-....tar.lzo --rootfs local:<newsize>

Of course, you can't perform this sort of resizing online, so I wouldn't call it a great solution, but it works if you have no other choice.

Good luck,

6
  • I have used zerofree to determine on many data can be saved, used it on a raw image of a stopped container... but it still miss shrinking. As virtualbox can do with: "VBoxManage modifyhd --compact disk.vdi"
    – ıɾuǝʞ
    May 5, 2017 at 15:34
  • found it : you can use "fallocate -v --dig-holes /.../file.raw"
    – ıɾuǝʞ
    May 5, 2017 at 16:01
  • 3
    Thank you it worked, but with Proxmox 5 I had to use this syntax to resize the image to 40G: --rootfs 40 Feb 15, 2018 at 0:12
  • 1
    You may as well want to append -unprivileged
    – Qian Chen
    Apr 4, 2019 at 8:46
  • 1
    Warning! DO NOT ``` pct destroy <id> ``` when you atteched a volume mount disk. In this case your data / disk is gone. Proxmox only say "delete uses data". But deleting mount points is fail in design.
    – ZerP
    Apr 12, 2020 at 21:11
3

This is now much easier and can be done without backup and restore.

root@pve:~# lvdisplay | grep "LV Path\|LV Size"
  LV Path                /dev/pve/swap
  LV Size                8.00 GiB
  LV Path                /dev/pve/root
  LV Size                96.00 GiB
  LV Size                1.67 TiB
  LV Path                /dev/pve/vm-100-disk-0
  LV Size                40.00 GiB
  LV Path                /dev/pve/vm-101-disk-0
  LV Size                8.00 GiB
  LV Path                /dev/pve/vm-103-disk-0
  LV Size                508.00 GiB
  LV Path                /dev/pve/vm-104-disk-0
  LV Size                8.00 GiB
  LV Path                /dev/pve/vm-105-disk-0
  LV Size                1000.00 GiB
  LV Path                /dev/pve/snap_vm-105-disk-0_vzdump
  LV Size                500.00 GiB
root@pve:~# e2fsck -fy /dev/pve/vm-103-disk-0
e2fsck 1.46.5 (30-Dec-2021)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/pve/vm-103-disk-0: 59084/33292288 files (0.6% non-contiguous), 3465123/133169152 blocks
root@pve:~# resize2fs /dev/pve/vm-103-disk-0 20G
resize2fs 1.46.5 (30-Dec-2021)
Resizing the filesystem on /dev/pve/vm-103-disk-0 to 5242880 (4k) blocks.
The filesystem on /dev/pve/vm-103-disk-0 is now 5242880 (4k) blocks long.

root@pve:~# lvreduce -L 20G /dev/pve/vm-103-disk-0
  WARNING: Reducing active logical volume to 20.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce pve/vm-103-disk-0? [y/n]: y
  Size of logical volume pve/vm-103-disk-0 changed from 508.00 GiB (130048 extents) to 20.00 GiB (5120 extents).
  Logical volume pve/vm-103-disk-0 successfully resized.

Then just update the LXC config file to reflect the new size.

1
  • 2
    Perhaps superfluous, but the LXC config is in /etc/pve/lxc/103.conf, with the line rootfs: local-lvm:vm-103-disk-0,size=508G being the one to update (to size=20G in this case)
    – Tim
    Jan 28, 2023 at 13:36
0

since I was also stuck at this place many times and @ThisGuy's solution did not work, I used an alternative way.

Warning: If you mess this up, your might overflow your created volume! LXC does not like this!

  1. stop the container
  2. edit /etc/pve/lxc/XXX.conf
  3. change the disk size for your mount point to something different (this will be used in step 5 by Proxmox to create the actual volume)
  4. backup your LXC
  5. restore your LXC

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .