Linux Swap Space Mini-HOWTO: The Procedure

Next Previous Contents

## 6. The Procedure

NOTE: This procedure has been written keeping in mind RedHat Linux 6.0 Although this procedure in general is applicable for all Linux distributions, the details may vary. You are welcome to add the details for your distribution. Many users will already have a swap partition devoted to Linux. I assume you have one.

## 6.1 Turn off swapping and create a DOS partition

- Boot Linux. Turn off swapping by editing the /etc/fstab file and commenting the line that describes your swap partition.

- Using fdisk under Linux, delete the swap partition in order to create free space on the disk.

- Boot DOS. Create a DOS partition (using FDISK) the size = the size swap space you want. If you cannot create the partition (probably because DOS FDISK says that all the space in extended partition is allocated to the logical drive), you can use fdisk or cfdisk under Linux to create the partition. You can even just change the type of your swap partition from 82h (Linux Swap) to 06h (FAT16). In that case you don't need to delete the swap partition, just change the type.

- Once you have created the partition, Boot DOS if you are not already in DOS. DOS will be assigned a drive letter to your new partition. Use that drive letter instead of X whenever these instructions lists a command like "LABEL X:" or "COPY FOO X:DUMMY.DAT"

- Format this partition using the DOS FORMAT command.

  > `

  >     C:\>FORMAT X:

  > `

- Set the volume label on this partition to "SWAP SPACE" using the DOS LABEL command. Verify it by the DIR command. Please do this as a separate step. Some versions of FORMAT do not seem to put the volume label in the boot sector as it should. [Note: some people has written me saying the volume label is stored in the root directory. Yes, but at least since DOS 5.0 it has also been in the boot sector.]

  > `

  >     C:\>LABEL X:
  >     C:\>DIR X:

  > `

- At this stage, you have a DOS partition ready for use for swapping.

## 6.2 Tell Windows the location of the new swap file

### For Windows 3.1 users

- Start Windows. Go to the Control Panel, select "386 Enhanced". Select "Virtual Memory" and create a Windows Permanent swap file on drive X: of maximum size (Windows will tell you the maximum size). Windows may complain saying it will not use a swap file that big. Ignore the message and create the file anyway.

- Exit Windows.

### For Windows 95/98 users

- Start Windows. Go to the Control Panel (Start | Settings | Control panel). Double click the System icon. In the resulting System Properties dialog box, select the Performance tab. Now click the Virtual Memory button. This will bring up the Virtual Memory dialog box. Select "Let me specify my own virtual memory settings". Select the drive you intend to share as swap space with Linux (X:). Keep the Maximum and Minimum values as selected by Windows. Click OK. Windows will prompt you to restart your computer. Do it.

- When you restart Windows, you will see that the win386.swp file has been created on the X: drive. In addition, there is also the Recycled folder. Since you don't intend to store any other files on this drive and also a single file on this partition will be convienent, tell Windows not to maintain Recycle Bin for this (X:) drive. On the Desktop, left click the Recycle Bin icon, and in the resulting pop-up menu, click Properties. This will bring up the Recycle Bin Properties dialog box. Click the Global tab and select "Configure drives independently". Now click the tab for X: drive (which will have the label SWAP SPACE). In this dialog box, move the slider for "Maximum size of Recycle Bin" to 0 percent. The "Space Reserved" entry should now read as 0 bytes. Click OK. Restart your computer.

- Now that you are not maintaining the Recycle Bin for drive X:, ideally the `RECYCLED` folder from X: must be deleted by Windows. Not only Windows doesn't does that, but also does not allow you to delete the folder. But as I said earlier, this folder is now unnecessary and we want drive X: to contain only the win386.swp file. For that purpose, first start your computer in MS-DOS mode. Select Start | Shut Down | Restart your computer in MS-DOS mode, or if you are booting Windows, press F8 at the start, and then from the startup menu, select Command Prompt only. Once you are in MS-DOS mode, type the following commands:

  > `

  >     C:\>X:
  >     X:\>ATTRIB -R -H -S RECYCLED
  >     X:\>DELTREE RECYCLED

  > `

  Now you have drive X: containing only win386.swp. You can verify this using the various options for the `dir`command. This does not mean that the `RECYCLED` folder will not be there on drive X:. When Windows boots up and finds that there is no `RECYCLED` folder on X:, it will create it automatically. However, when we will be restoring X: from Linux, it will simply contain win386.swp file. If anyone is aware of a method to prevent Windows from creating the `RECYCLED` folder, please e-mail me.

- Disk Cleanup troubles you ?

  If you have Disk Cleanup installed, then ocassionally Windows comes out with a "Low disk space on drive X:". Since we are using the entire X: drive for swapping, there is no need to have any free space on X:. To prevent this dialog box from appearing, start Disk Cleanup (point to Start | Programs | Accessories | System Tools | Disk Cleanup). In the resulting dialog box, select the X: drive and click OK. The "Disk Cleanup for drive X:" dialog box appears. Click the "Settings" tab and uncheck the "If this drive runs low on disk ... blah blah blah" check box.

>From this stage onwards, Windows will assume that it's swap file is on drive X:. So the drive X: must be intact each time you boot Windows. If you are using some additional system utilities like Norton Utilities for Windows 95, then you should probably consult the online help or the manuals to keep them informed of the changes in the settings, otherwise they may come out with an error message.

## 6.3 Back up the Total Special Sectors

- Boot Linux, then log in as root.

- Use the fdisk command to find the name of the partition and its size in blocks. Create a symbolic link from `/dev/winswap` to this partition. If the partition is hda7, then type:

  > `

  >     # ln -s /dev/hda7 /dev/winswap

  > `

  [NOTE TO PURISTS: Please use a symlink. The name of this partition is going to go into several configuration files and inconsistencies could be fatal.]

- Find the "Total Special Sectors" for the swap drive. These are nothing but the total number of sectors required for the boot sector, FAT and root directory. There are a number of ways to find this number. Simplest copy the msinfo.sh file using your favourite editor. Give the following commands at the shell prompt

  > `

  >     # cp Swap-Space-HOWTO msinfo.sh
  >     # chmod +x msinfo.sh

  > `

  (Much easier if you use one of the editors with KDE or GNOME)

  Now, open the msinfo.sh file in the editor and delete everything except the msinfo.sh file in section 8. Now run this file as

  > `

  >     # ./msinfo.sh /dev/winswap

  > `

  The program will print the some information about the swap partition. Take note at the number saying "Total special sectors", and verify that the volume label says "SWAP SPACE". If it does not, reboot DOS and re-do the LABEL command.

- Note: You can also find the "Total Special Sectors" using tools that can display the Boot sector like the Norton Disk Editor or minfo from mtools under Linux. For Norton Disk Editor, select "Boot Sector" from the Object menu. Note down the following entries :

  > `

  >     Reserved Sectors at beginning : r
  >     FAT Copies                    : f
  >     Sectors per FAT               : s
  >     Root directory entries        : r

  > `

  Now the "Total Special Sectors" is given by

  T = r + (s * f) + (d / 16)

  However, using the above program is more convienent.

  Let us denote these Total Special Sectors by XXX.

- [Optional step] Windows may occasionally leave some space on the partition, even if it is told not to. Don't attempt to use this space, since it will be erased any time you run Linux. If you want to avoid accidentally using it (and lose data), you can create a dummy file that fills that space by using the following commands:

  > `

  >     mkdir /mnt
  >     mount -t msdos /dev/winswap /mnt
  >     dd if=/dev/zero of=/mnt/dummy.fil
  >     umount /mnt

  > `

  The dd command will report "No space left on device". This is exactly what you want.

- Type on the shell prompt:

  > `

  >     # dd if=/dev/winswap bs=512 count=XXX | gzip -9 > /etc/winswap.gz
  >                                       ^^^

  > `

  ... where XXX is replaced with the "Total special sectors" number.

  Here we are saving the Total Special Sectors in a compressed form in the file /etc/winswap.gz

## 6.4 Modify the initialization and shutdown scripts to handle our new configuration

- Now, we will modify our initalization scripts so that swap space will be create on the partition each time Linux starts and the DOS/Windows special sectors will be restored each time Linux shut downs.

- Instead of placing the command for handling the details directly in the initialization scripts, we will prepare two separate files called swapinit.sh and swaphalt.sh. One sample of these files that should work under Red Hat Linux is given in section at the end. You can choose to place these files under any directory, preferably /etc/rc.d/init.d. Create the two files in this directory and copy into then the scripts given in sections at the end. One simple way is to copy this HOWTO in these files as,

  > `

  >     # cp Swap-Space-HOWTO  /etc/rc.d/init.d/swapinit.sh
  >     # cp Swap-Space-HOWTO  /etc/rc.d/init.d/swaphalt.sh

  > `

  and then using your favourite editor, delete all execpt the required part. Now make these file "executable" by giving the following commands

  > `

  >     # chmod +x swapinit.sh
  >     # chmod +x swaphalt.sh

  > `

- Run fdisk and find the size of the swap partition in blocks. In both the above files (swap????.sh) replace the YYYYY by this number.

- Check the name of the shutdown file. For SysVinit this is the file listed in the following line of /etc/inittab; add it if you don't have it.

  > `

  >     # Runlevel 0 means shut down the system
  >     l0:0:wait:/etc/brc

  > `

  (Under Red Hat Linux, this file probably will be /etc/rc.d/init.d/halt) If you are not in a position to find it out, browse through the /etc directory and find the initialization file that contains the "swapon -a" command. Only this much for now !

  For the remainder of this file, I will assume the filename was /etc/rc.d/init.d/halt (halt for short).

- Check the name of your system initialization file. For SysVinit, this is the file listed in the following line of /etc/inittab.

  > `

  >     # System initialization
  >     si::sysinit:/etc/rc.d/rc.sysinit

  > `

  In case of your distribution, your file may be different. I assume that the file is /etc/rc.d/rc.sysinit (rc.sysinit for short)

- Add the following piece of code to your initialization file (rc.sysinit), in place of the "swapon -a" command. You may consider not deleting but just commenting the original commands so that if something goes wrong, we can restore the script to it's initial state.

  If your swapon is in /etc, replace /sbin/swapon with /etc/swapon. If it is in /bin, replace with /bin/swapon. Do the same for mkswap.

  > `

  >     # -----------------------------------------------------------------------
  >     # removed by yourname
  >     # Start up swapping.
  >     # action "Activating swap partitions" swapon -a
  >     # -----------------------------------------------------------------------

  >     # ----------------------------------------------------------------------
  >     # added by yourname
  >     # Verify and initialize swap space
  >     #
  >     /etc/rc.d/init.d/swapinit.sh
  >     # ----------------------------------------------------------------------

  > `

- A simple way to paste the text is to start two Virtual Consoles. Log as a root in both cases. Use your favourite editor to open this HOWTO on one console and the script to be modified on other console. Now select the above code with your mouse and switch to the other console. Place the cursor at the desired position to paste and press the right mouse button. The selected text will be copied.

- Add the following piece of code to your shutdown file (halt).Put this after any command that might need swap to be in place. Normally, your halt file will have the "swapoff -a" command. First replace the -a by /dev/winswap. Then immediately after that section, add the following commands.

  Note for RedHat Linux 6.1 : It seems that Red Hat Linux 6.1 halt script automatically finds all the swap partitions and turns of swapping. So, RedHat Linux 6.1 users need not replace the `swapoff $SWAPS` comamnd by the `swapoff -a` command. In that case, just add the `/etc/rc.d/init.d/swaphalt.sh` command to the halt file

  > `

  >     # ------------------------------------------------------------------------
  >     # Modified by yourname
  >     # replaced "swapoff -a" by "swapoff /dev/winswap"
  >     #
  >     # Turn off swap, then unmount file systems.
  >     runcmd "Turning off swap and accounting" swapoff /dev/winswap
  >     [ -x /sbin/accton ] && /sbin/accton
  >     # ------------------------------------------------------------------------

  >     # ------------------------------------------------------------------------
  >     # Added by yourname
  >     # check swap signature and restore Windows swap info
  >     /etc/rc.d/init.d/swaphalt.sh
  >     # ------------------------------------------------------------------------

  > `

## 6.5 Reenable swapping

Uncomment the line in /etc/fstab that you commented earlier. (Not really necessary, since we now do not refer to fstab for swap partitions). Reboot Linux. You should now have swapping on the new swap device.

Next Previous Contents