The situation is as follows.

  1. I have a Linux partition on a primary drive (modestly-sized SSD, and sharing it with Windows).
  2. I have another Linux (ext4) partition on a hard drive. It is permanently mounted in /etc/fstab.
  3. I don't want to make a swap file on a root drive to save space.

Thus I want to make a swap file on the hard drive partition. I've successfully created and enabled a swap file, but I have trouble enabling it permanently in /etc/fstab. Should it be mounted under /dev/ (where the drive is mounted), or under /mnt/ (where the file system is mounted)?

  • Under /mnt but i think it will not work, as it depends on the other disk or partition to be mounted first because your swap file is not in the main partition. I think it will try to mount the two in the same time when the system reads the fstab, and only the disk will be mounted. In the past the swap mount script are separeted from the disk auto-mount. But today i think is not like this anymore. In my opinion it will be better if you remove the swap auto-mount from the fstab and mount it on a shell script, like /etc/rc.local with swapon por example. But this depend on your linux distro. – Luciano Andress Martini Jan 6 '17 at 11:43

In your case the /etc/fstab entry and preceding steps for a swap file looks like as follows.

dd if=/dev/zero of=/mnt/<UUID>/swapfile bs=1M count=512
mkswap /mnt/<UUID>/swapfile
chmod 600 /mnt/<UUID>/swapfile
echo "/mnt/<UUID>/swapfile none swap defaults 0 0" >> /etc/fstab

So the entry in the /etc/fstab should look like

/mnt/<UUID>/swapfile none swap defaults 0 0

and should be below the line that mounts /mnt/<UUID>.

Then you should be able to activate it with the command as follows.

swapon -a

Concerning the question from your comment, mounting the swap file with the UUID created during mkswap, no it is no possible. You have to specify the full path to the file.

  • "I don't want to make a swap file on a root drive" – Kusalananda Jan 6 '17 at 11:44
  • Sorry, Under which path is your second HDD mounted? I can update my answer accordingly. – Thomas Jan 6 '17 at 11:46
  • The HDD entry in fstab goes as follows: /dev/disk/by-uuid/<UUID> /mnt/<UUID> auto nosuid,nodev,nofail,x-gvfs-show 0 0 Also, I've noticed that during the creation of swapfile a UUID was generated, although it was created as a file and not as partition. So it might be possible to mount this file under UUID as well? – Silent Inky Jan 6 '17 at 13:37
  • Updated my answer... – Thomas Jan 6 '17 at 13:56

If you had any other non-root device(hard drive partition), since you already created a swap file, make an entry in the partition(you don't require /mnt), mount your (/dev/<hard_drive_partiton>) to other directory /swap, and then create your swap files over there.

add an entry for your swap file in /etc/fstab:

/swap/swapfile swap swap defaults 0 0 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.