6

This Linux Swap Space Mini-HOWTO describes how to share swap space between Windows and Linux. **Do these instructions still apply to Ubuntu in 2011? How should I modify the steps for Ubuntu?

Is there a better approach to sharing swap space?**

Based on the HOWTO, it seems best to create a dedicated NTFS swap partition:

  • Dedicated so the swap file will be contiguous and remain unfragmented.
  • NTFS so both Windows and Ubuntu can read/write to it. (Or is FAT32 better for this purpose?)

Then, configure Ubuntu to prepare the swap space for use by Linux on start up; by Windows on shut down.

I want to dual boot Ubuntu and Windows 7 on my X301 laptop. However, my laptop only has a 64 GB SSD, so I would like to conserve as much disk space as possible.


update: There is an alternate method using a special driver for Windows that let you use a Linux swap partition for temporary storage like a RAM-disk, but it doesn't seem to be as good...

3
  • You can use neither NTFS nor FAT32 for the swap disk. The swap partition has an own file system called "swap", which can be used only for this particular purpose. But you may try to juse a swap-file instead. What I could imagine of would be to use a swap-file and auto-delete the swap file of the other OS on startup. But this would require some tricks with the startup scripts, where I've absolutely no idea. – FUZxxl Feb 15 '11 at 19:08
  • 2
    The linked "Linux Swap Space Mini-HOWTO" deals with 3.1 and 95/98. I'm not watching MS very carefully, but maybe that's not really up to date. AFAIK, mkswap only marks the file/partition as swap, this is just a security measure so you don't swap in your documents. No idea what Windows does, but I could imagine it relying on the filename and maybe its attributes. I'd go and try it out, if I were you. – maaartinus Feb 15 '11 at 19:50
  • 1
    Just for future reference, Linux can indeed use a file for Swap. – nanofarad Oct 10 '12 at 10:26
3

Windows' swap space is typically a pagefile.sys file stored on the drive. It is given an arbitrary size, and can use no more than that size.

Ubuntu and Linux require a dedicated 'swap' partition or designated swap space. However, the swap space between Linux and Windows are not formatted correctly for each system to understand the other's swap space. This causes the limitation in the ability to share swap space. However, you don't need to share swap space. It acts on the premise of RAM: each bit of memory is filled with data and allocated as its needed. When the data there is not needed, it is marked as being able to be written over. This then means that some other program can come by and overwrite the last allocated area with new data. This cycle then continues.

2

Not possible. The format of pagefile.sys is proprietary and unknown.

2
  • 3
    The actual pagefile.sys file does not need to be shared. Both Windows and Ubuntu ignore the initial contents of their respective swap spaces at startup, so the actual contents do not have to be preserved. They just have to be 'ready.' – Leftium Feb 15 '11 at 19:19
  • 2
    @Leftium: No, Linux looks for a valid swap header. That is why you have to format a swap partition with mkswap before you can use it. I'm pretty sure Windows is similar. – psusi Feb 15 '11 at 20:11
1

It is still possible to use Windows pagefile.sys as a swap file in Linux and not so complicated after all.

First you need to auto-mount your Windows partition at start-up. Add this line to /etc/fstab:

UUID=<MY_UUID> /mnt/Windows_C auto auto 0 0

Then create a script that will format the swap file if necessary and mount it. For example home/<username>/swap.sh:

#!/bin/bash
pagefile=/mnt/Windows_C/pagefile.sys
type=$(blkid -s TYPE -o value $pagefile)
if [[ $type != swap && $type != swsuspend ]]; then
    mkswap $pagefile
fi
swapon $pagefile

Make the script executable and create a service to launch it after the Windows partition has been mounted: create the file /etc/systemd/system/swap.service containing

[Unit]
Description=Use Windows swap file
After=local-fs.target

[Service]
Type=simple
ExecStart=/home/<username>/swap.sh

[Install]
WantedBy=multi-user.target

Start the service to check it's working:

sudo systemctl start swap

If the script is working, the command swapon should return something like:

NAME                        TYPE SIZE USED PRIO
/mnt/Windows_C/pagefile.sys file 8,5G   0B   -2

If not, try to do systemctl status swap.service to see what happened.

If everything went OK you can enable the service:

sudo systemctl enable swap

Now you're mostly done. If you want to avoid some insecure permissions warnings on pagefile.sys, you need to set up a permission mapping between Windows and Linux.

To do so, unmount the Windows partition then generate a user mapping file:

sudo ntfsusermap /dev/disk/by-uuid/<UUID>

Remount the partition and move the user mapping file to a new folder named .NTFS-3G:

sudo mkdir /mnt/Windows_C/.NTFS-3G
sudo mv UserMapping /mnt/Windows_C/.NTFS-3G/

From what I saw, you do not need to do anything on the Windows side, the reformating of pagefile.sys is automatic at start-up.

https://linuxize.com/post/create-a-linux-swap-file/

Run script after fstab

How do I use 'chmod' on an NTFS (or FAT32) partition?

2
  • 1
    Nice answer. Did you test this? Also does this allow suspend to disk (hibernate)? or does it work in the same way as the swap file in Ubuntu and only allow suspend (sleep)? – Sasuke Uchiha Jun 19 '20 at 8:46
  • Yes I tried it. I edited the script swap.sh to avoid reformatting the swap file after waking up from hibernate. It works like a swap file in Ubuntu so it's possible to hibernate following those instructions: askubuntu.com/questions/6769/…. Obviously that will not work if you go to Windows when Ubuntu is in hibernation (the swap file will need to be reformatted). – Jean Paul Jun 19 '20 at 23:03

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.