Skip to content

Swap in kvm

How does cloud-image manage swap for kvm virtuals? Swap uses to be a disk partition, but, for VMs, it's not a concept by itself at PVE level, as it is for LXE containers. We create VMs by building some cloud-image PVE template, that already comes with it's /etc/fstab and eventually partitionning, considering a swap or not.

When creating a debian bullseye vm with cielito.proxmox.create_kvm, it's ok, it has a swap, 1Gb:

root@alfa:~# free
               total       usado       libre  compartido   búf/caché   disponible
Mem:         4008196     2133752      137776       26588     2045800     1874444
Inter:       1048572      390920      657652

There is no swap partition (disk has been grown during installation, does this work with several partitions?):

root@alfa:~# parted /dev/sda print
Model: QEMU QEMU HARDDISK (scsi)
Disk /dev/sda: 18,3GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  Flags
14      1049kB  4194kB  3146kB                     bios_grub
15      4194kB  134MB   130MB   fat16              boot, esp
 1      134MB   18,3GB  18,1GB  ext4

There is a swap file:

# /etc/fstab: static file system information
UUID=xxxxxxxx-xxxx-yyyy-8b01-eed78dc14d62 / ext4 rw,discard,errors=remount-ro,x-systemd.growfs,usrquota,grpquota,usrjquota=aquota.user,grpjquota=aquota.group,jqfmt=vfsv0 0 1
UUID=zzzz-DB16 /boot/efi vfat defaults 0 0
/swapfile	none	swap	sw	0	0

root@alfa:~# du -h /swapfile 
1,1G	/swapfile

However, with bookworm c-i image, there is no longer such file, no such configuration, so no swap:

root@iota:~# free
               total       usado       libre  compartido   búf/caché   disponible
Mem:         2014432      455720      149968       42276     1638872     1558712
Inter:             0           0           0
root@iota:~# cat /etc/fstab
# /etc/fstab: static file system information
UUID=xxxxxxxx-yyyy-zzzz-bc19-e76bcc9aeca6 / ext4 rw,discard,errors=remount-ro,x-systemd.growfs 0 1
UUID=tttt-B4DC /boot/efi vfat defaults 0 0

I'll check our c-i images origin and c-i/PVE templates process creation (that maybe we should also document here in english), but shouldn't we also consider in the role the creation of a swap when the template doesn't have it?