Swap file not mounting on boot

I had trouble mounting multiple swap FILES residing on different partitions but solved like this on the command line:

CREATE SWAP AS A FILE ON FIRST DISK DRIVE:

dd if=/dev/zero of=/mnt/sda1/swapfile bs=1M count=256
chmod 600 /mnt/sda1/swapfile
mkswap /mnt/sda1/swapfile

CREATE ANOTHER ON SECOND DISK DRIVE:

dd if=/dev/zero of=/mnt/sdb1/swapfile bs=1M count=256
chmod 600 /mnt/sdb1/swapfile
mkswap /mnt/sdb1/swapfile

FOOLISH THINGS:

uci set fstab.@global[0].anon_swap='1'
uci set fstab.@global[0].auto_swap='1'

CLEAN HOUSE:

while uci -q delete fstab.@swap[-1]; do :; done

AUTOSTART ENTRY 1 WITH A FAKE LABEL:

uci add fstab swap
uci set fstab.@swap[-1].enabled='1'
uci set fstab.@swap[-1].device='/mnt/sda1/swapfile'
uci set fstab.@swap[-1].label='foo'

AUTOSTART ENTRY 2 WITH A FAKE LABEL:

uci add fstab swap
uci set fstab.@swap[-1].enabled='1'
uci set fstab.@swap[-1].device='/mnt/sdb1/swapfile'
uci set fstab.@swap[-1].label='bar'

COMMIT AND REBOOT FOR PROOF:

uci commit fstab
reboot

AND WE HAVE SUCCESS:

free
Swap: 524280

w00h00!
Now you can plug and unplug hard drives as either sda1 or sdb1 while the power is off, and as long as one of them contains a swapfile file then it'll mount at bootup.

2 Likes