LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   LinuxQuestions.org Member Success Stories (https://www.linuxquestions.org/questions/linuxquestions-org-member-success-stories-23/)
-   -   Sharing a swap file with linux and windows (https://www.linuxquestions.org/questions/linuxquestions-org-member-success-stories-23/sharing-a-swap-file-with-linux-and-windows-109511/)

Doug Hammond 10-28-2003 10:37 AM

Sharing a swap file with linux and windows
 
So, I read a while ago that it was considered generally a quite difficult thing to create a swap file (or partition) which could be shared by both windows and linux on a dual boot system.
WRONG. !!

After all you dont need large redundant files on your system do you ?

Here's how I have my machine set up:

Dual boot Win XP (other versions will work also), and RedHat linux 9.0 (other distros/versions _should_ work also. Feel free to post replies to this if specific variations on this theme are needed to make it work on other OS versions).

First off, you need a partition or drive which is visible to both windows and linux. This could be:
- Your windows partition. Although be warned that if this is NTFS, linux won't be able to write to it (at least my system doesn't) so that's a non-starter. You need to use an alternative.
- A dedicated FAT (16 or 32) swap partition.
- Another drive altogether (with FAT file system). This is the set-up I have.

I am not going to go into here what size swap you should be using (if any), nor do I intend to explain how to create/move/copy/resize partitions on your HD. But, here are the outline apporxiamte steps you need to take for both systems to be happy.

First things first: When installing linux, you should not specify a special linux swap partition. Ignore the warning about not using a swap partition for now. If you already have linux set up (probably most of you, but I did it from scratch), you could go about removing the current reference to the swap partition in /etc/fstab, and then convert your swap partition to FAT, so that you can share it with windows.

Boot windows and change the page/swap file settings to use a file on the shared space of your choice (on my machine this is D: second hard disk). Under Win XP/2K/NT this creates a file called pagefile.sys. On other Windows versions 95/98/ME etc i think it's called win386.swp. You will need to know the precise location and name of this file in order to set it up with linux. You should be able to find it quite easily because of it's enoumous size.

That's windows taken care of. You will probably have to reboot back into windows immediately so that it can create/move the appropriate files.

Configuring linux is a little more tricky (it always is, isn't it ?). There are a couple of things you need to change, and one of these is necessary at every boot of linux, however, this can be taken care of in the startup scripts.

The first thing to do, as a test before we modify the system is issue a couple of commands to check that linux has the appropriate permissions to use the swap file properly. Open up a console, and find the swap/page file that windows created. On my system, under linux this is /mnt/data/pagefile.sys.

Run a quick check with (you should be root to do this):
Code:

mkswap /mnt/data/pagefile.sys
swapon /mnt/data/pagefile.sys

The first command sets the file up for swap use with linux. The second activates it as a swap space.
Verify that linux is using swap by issuing a 'free' command. It should list the amount of used/free swap and memory. If on of these commands fails, you should check out the write attributes of your drives/partitions/files. Linux needs to write to this file, quite obviously.

Now, you want these things to happen automatically when you boot linux. The second part is easy, you need an entry in your /etc/fstab file which looks remarkably like the old one:
Code:

..
/mnt/data/pagefile.sys  swap    swap    defaults  0  0
..

Then, when the system calls for all swap spaces to activate, it sees this entry and all is good.
However, not much is going to happen if you booted windows in the meantime and overwrote the file with windows junk. So on every linux boot we need an mkswap command. I put mine in the /etc/rc.sysinit file. I'm not sure if it should really belong in here, but it works for me. A word of warning: Be VERY careful when editing this file. !!

In the file I found the very LAST occurrance of the command 'swapon -a', and just before it I put in:
Code:

..
mkswap /mnt/data/pagefile.sys
..

Now when linux boots, it runs through the rc startup script and automatically configures the use of your windows swap/page file for it's own use.

Sweet! That's all there is to it. I know I've written quite a lot here, but really there is nothing to it.

I hope it works for you.. really there's only a few things you need to change, and they're not difficult.

Doug Hammond.

rofranco 08-31-2004 01:32 AM

Hi,

I would like to share my swap partition (which I had already created and am using for Windows) between Win2K and Linux.
My question is: if I follow the procedure that you described, Windows 2000 will be able to use the swap file even though Linux has overwritten it ? I heard that if I didn't backup the swap partition and restored it before shutting down Linux and starting Windows, that Windows wasn't able to use it.

Thank you in advance for your answer.

Rodolfo.

psylocibe 10-03-2004 12:15 PM

i found an exhaustive explanation of the way to share the swap file at the url: http://www.mail-archive.com/inspiron.../msg00427.html

i think this link will be useful for you.

rofranco 10-04-2004 03:01 AM

Thank you for your reply. I think that the link you mention is an earlier version of the: Linux Swap Space Mini-HOWTO

I was a bit worried that maybe Windows wouldn't work if I messed the swap partition (which I did a couple of times before making it work) but I can tell you now that, luckily, Windows 2000 creates a temporary swap file in drive C in case it cannot use the partition that you assigned it.

The only thing that I could say about the tutorial is that nowadays, I think that most Linux distributions use the new style swap space that enables to use big swap partitions. So, you have to read the whole tutorial and beware of the following note:

"The new style swap space: Linux knows about two styles of swap areas, the old style and the new style. By default, you will be using the old style swap space with a signature "SWAP-SPACE". If you want to use the new style swap space, then first of all change "SWAP-SPACE" in all the above scripts to "SWAPSPACE2"."

Having done that, I had no problem to set up a shared swap partition between Linux and Windows 2000.

Another thing that I remember having a bit of trouble, was to rename the swap partition in Windows. Before starting the tutorial, my swap partition under Windows was called "SWAP". The label command didn't seem to really change the swap partitions name. Fortunatelly, I had a boot diskette from Partition Magic with which I was able to change the label and Linux saw it.

Thanks for your help, and hope that my comments can help someone else.

aecaudel 11-17-2004 01:02 AM

From your post regarding:

mkswap /mnt/data/pagefile.sys

I presume that you have to mount the pagefile.sys before the mkswap. Do you then have to have an fstab entry to mount the partition holding the pagefile.sys? Does this entry have to precede the swap entry in fstab?

The reason I ask this is "mkswap /dev/hda5/pagefile.sys" complains about it not being a directory.

/bin/bash 07-11-2005 06:06 AM

aecaudel

Yes you must mount the windows partition before running the mkswap command. Your /etc/fstab file might look something like this:

/dev/hda5 /mnt/data vfat defaults 0 0
/mnt/data/pagefile.sys swap swap defaults 0 0


All times are GMT -5. The time now is 04:04 AM.