Ubuntu – Is it possible to create an Ubuntu swapfile on an NTFS partition

dual-bootlenovontfsswapusb

As the Windows 7 end-of-life is fast approaching, I am testing Ubuntu 18.04.3 LTS on my ancient (2013) laptop. Unfortunately, dual-booting seems nigh-impossible, so I am using a persistent live USB thanks to askubuntu's @sudodus and @C.S.Cameron. I am typing this on Ubuntu and can run LibreOffice and a grand strategy game!

However, I only have 4GB of RAM (which has to double up as video RAM) and the system is grinding to a halt when I reach 100% RAM usage while playing the game. Some of this is probably due to a long-standing memory bug in gnome-software, but I think the game will eventually fill 4GB anyway.

So the next challenge is trying to add some swap. It doesn't need to be available all the time, just when I run games. It's possible to run a swap file on a USB stick, but it's very bad for the stick. So using my internal hard drive would be cheaper and might be faster.

Although I probably can't create a swap partition on my hard drive, it occurred to me that Ubuntu now defaults to using swapfiles. Most HOWTOs assume that swapfile is in an ext2/3/4 partition. But could I, and how would I, create a swapfile in an NTFS partition? The Arch Linux forum has a method for creating an a swapfile on an NTFS USB stick. But that worked on a different distro six years ago and suggests writing a systemd service, which seems ambitious for an Ubuntu beginnner.

In addition, I would be doing this in a NTFS partition that contains hidden files that are required by Windows.

BTW this is not a duplicate of the various answers on how to use a Linux swap partition as a Windows swapfile.

Best Answer

I'm not quite sure whether a swap file on an NTFS partition is the best solution for you. You may be better off creating an actual swap partition on your hard drive and use that. You could use a tool such as gparted to resize your NTFS partition, reducing it by the size you want your swap file to be, say, 2 GB. Then you could allocate a 2 GB swap partition on the newly freed space on your hard drive.

(Of course that assumes you have at least 2 GB of free space on your NTFS partition. But that's just the same space a swap file would consume as well.)

The reason I'm saying that a swap file on your NTFS partition may not be the right solution is because it may impact performance significantly, for two reasons:

  • Your NTFS partition is not fresh, it's been used for a long time, and your 2 GB swap file will likely be fragmented. Hence, reading and writing to that swap file may be much slower since it is not a contiguous memory region (as opposed to a swap partition). See this excellent answer on Server Fault for some more information.

  • On top of that, the NTFS drivers are rather slow, as mentioned in the Arch Linux forums in the thread that you linked to yourself.

If you are nevertheless determined to create a swap file on your NTFS partition, that's easy. All you have to do is this:

  1. Ensure your NTFS partition is mounted somewhere. For the sake of example, let's assume the mountpoint for your NTFS partition is /media/windows.

  2. Create a swap file with 2 GB on your NTFS partition like so:

dd if=/dev/zero of=/media/windows/swapfile.img bs=1M count=2048
mkswap /media/windows/swapfile.img
  1. Enable the swap with
swapon /media/windows/swapfile.img

That's it really. Those are exactly the instructions from that Arch Linux thread, I merely changed the size of the swap file to 2 GB.

The whole rest of the Arch Linux thread only deals with the issue that the OP wants to have his swap file enabled automatically at boot, so that they don't have to run that swapon command every time after rebooting the system. For that to work, you have to somehow make sure that the NTFS partition is mounted before the swap is enabled, since the swap file is on the NTFS partition, and that can get a bit messy, which is why someone there suggests creating a systemd service to enable the swap after the NTFS partition is mounted.

But if you only want to test how a swap file on your NTFS partition solves the problems with your game, you don't need to worry about that right now. You can just go ahead with the above steps. It simply means that, every time you reboot your system, you have to re-run steps (1) and (3) above, i.e., make sure your NTFS partition is mounted (hint: put it in your /etc/fstab and have it mounted at boot time) and run that swapon command. Note that you only have to perform step (2) once: Once the swap file is created, you don't need to create it anew. Unless you want to change its size.

Then, if that works for you and you're really happy with the results and you can play your game and one day you're annoyed that you have to run that swapon command every time you reboot your system, then you can worry about writing a systemd service to run that swapon command every time automatically at boot. And then, if you have trouble with that, you're welcome to ask another specific question about that problem here. Or perhaps, by then you will like Ubuntu so much that you decide to get rid of Windows 7 and install Ubuntu for good. ;)

Related Question