cannot add swap with fallocate on EC2
Matthew Barrera
I'm trying to setup the swap in /etc/rc.local this way:
sudo fallocate -l 8192M /mnt/swapfile1
sudo mkswap /mnt/swapfile1
sudo swapon /mnt/swapfile1This however returns an error:
fallocate: /mnt/swapfile.sys: fallocate failed: Operation not supportedmy partitions (EC2) are this:
ubuntu@ip-10-0-0-52:~$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 7.8G 2.8G 4.6G 38% /
none 4.0K 0 4.0K 0% /sys/fs/cgroup
udev 3.7G 8.0K 3.7G 1% /dev
tmpfs 752M 204K 752M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 3.7G 0 3.7G 0% /run/shm
none 100M 0 100M 0% /run/user
/dev/xvdb 414G 71M 393G 1% /mntwith dd it works, but it's kinda slow, I wanted to make sure swap is present on boot.
1 Answer
As of the Linux Kernel v2.6.31, the fallocate system call is supported by the btrfs, ext4, ocfs2, and xfs filesystems.
You can confirm which file system you have with this command (assuming the device name given above):
file -sL /dev/xvdb Unfortunately, it appears that in some cases the instance storage is provided formatted at ext3 instead of ext4.
Assuming your instance storage is at /dev/svdb and you want to make a swap file at /mnt/swap that's 16GB in size (huge!), Putting this is /etc/rc.local will make that happen every time at boot. You should comment out the line for /dev/xvdb in /etc/fstab in that case, because now the device will be mounted by /etc/rc.local late in the boot process instead of early.
umount /dev/xvdb
mkfs.ext4 /dev/xvdb
mount /dev/xvdb /mnt
fallocate -l 16GB /mnt/swap
chmod 600 /mnt/swap
mkswap /mnt/swap
swapon /mnt/swap