What's the correct way to specify a swap file for an lxc container
I'm creating an lxc container under CentOS 7.1 using virt-install. Everything works fine, except there is no mechanism provided in virt-install for specifying how much swap the container uses, just memory.
What I thought I'd do is just add a swap file in my container and create the appropriate entry in /etc/fstab to load the swap file when the container starts. My container fstab looks something like this: Code:
/dev/root / rootfs defaults 0 0 swapon -a and my swap file is then added as expected. I need this to happen automatically though, and if I can't put it in /etc/fstab, what's the correct way to automatically start a swap file when an lxc container starts? |
swapping to a file is ugly and doesn't work with all filesystems. Can you provide an lvm or disk partition instead?
|
Unfortunately no. Our containers live in an isolated environment without access to the raw disk devices of the host, just the space that has been given to them for their root fs. We're using ext4 and swap files are working fine. The issue is just how to enable the swap file in an automated manner.
|
Create the file at the root level (/swapfile) rather than at /var/tmp.
|
That doesn't solve the problem--everything is under one mount point and moving the swapfile to /swap instead of /var/tmp/swap doesn't resolve the root cause that / is not ready by the time the system wants to start the swapfile. The startup sequence looks like this:
Code:
# virsh console vm-02 Code:
# systemctl status swap.swap -l |
Based on some reading I've done, I thought I'd be able to add the line
After=local-fs.target to /usr/lib/systemd/system/swap.target in my container, making the swap target start after the local-fs target starts, but that didn't work--the swap file is still activated too soon, before the local file system is ready. I must be missing something here, I was sure this was the solution... |
Turns out to be not as simple as I was expecting, but for different reasons. With containers, there is only one kernel. If I do add swap space to a container, e.g., using the swapon command, the swap space actually gets added to the host, not the container. The lesson to learn here is that LXC containers are not VMs, and although they can be treated like a VM in many ways, in some cases they behave quite differently.
|
Thanks for the postback.
|
In the end the solution was to allocate the swap files themselves in the host, not the containers. Swap space for a container is controlled by the virsh memtune command:
virsh memtune <container> --hard-limit=<mem> --config virsh memtune <container> --swap-hard-limit=<mem+swap> --config So, if for example I wanted to give a container called "test" access to 6G of memory plus another 2G of swap, I'd run the commands virsh memtune test --hard-limit=6G --config virsh memtune test --swap-hard-limit=8G --config Note that the swap limit is in fact the sum of the physical memory plus how much swap is wanted. On the host itself, you need to allocate at least 2G of swap space, either using a partition or using swapfiles. You will probably also want to set vm.swappiness to 1 on the host so that the containers will only swap when they absolutely have to. |
All times are GMT -5. The time now is 10:13 PM. |