I can't imagine why a swap file needs to be fixed size. Why not let it resize dynamically, like the hard drive image file for a virtual box?
It doesn't look like Linux supports dynamic swap file sizing (at any rate, I couldn't get it to detect that a swap file had changed size without a
Presumably it is easier to assume that a swap file doesn't change file size dynamically when writing code for supporting it. I don't see any great use for it either. Since you can use multiple swap files, nothing's preventing you from creating more swap files as needed.
Also note this paragraph from the
The swap file implementation in the kernel expects to be able to write to the file directly, without the assistance of the filesystem. This is a problem on preallocated files (e.g. fallocate(1)) on filesystems like XFS or ext4, and on copy-on-write filesystems like btrfs.
I'd think the same problem would apply to a dynamically-sized swap file.
SwapSpace is a utility that will allow you to make dynamically sized swap if that is what you are looking for. I do believe it is available in Ubuntu.
Swap space can be added to a running system by using the
swapon command. It needs an existing (unused) swapfile/partition. To create one, use the
dd command to allocate a contiguous file, and then
mkswap to add the correct control information to the file / partition.
To remove a swapfile, use
swapoff; the file can then be deleted.
I suspect that is what
SwapSpace dæmon that @user231695 mentioned does.
Note that swapoff can be a slow operation if the swapfile is in use; all the data has to be transferred to another swap area.
There appears to be a program named
swapspace. Available from Ubuntu 18.04 onward.
The corresponding GitHub page. They recommend using a static swap file if you want to suspend and resume work. They also do not recommend using it for systems that need to be responsive.