Linux Swap Space Mini-HOWTO

Rahul Sundaram rahulsundaram@yahoo.co.in

v1.6, 2002-12-27
This Linux Mini-HOWTO describes how to share your Linux swap partition with Windows.

1. Introduction

Many people use both Linux and MS-Windows. The ability to do so is an important part of "the Linux revolution"; i.e. letting people experiment with (and get hooked on) Linux while still being able to run their off-the-shelf software. Since both Linux and MS-Windows use virtual memory with swap to disk, a frequently occurring question in comp.os.linux.setup is how to share swap spaces, in order to reduce the amount of disk space needed.

There are several methods for sharing swap spaces, the one described in this document is probably the most complicated one but is the only one I have encountered that allows maximum performance for both environments without the risk of trashing a disk partition. If you are using DOS, then many DOS applications manage their own swap files. You can also share the Linux swap partition with these applications.

2. What you need

This procedure have a few requirements that need to be filled. I strongly recommend that you fill these requirements *anyway*, as there are several problems with older versions.

  1. MS-DOS 5.0 or newer and MS-Windows 3.1/3.11 OR MS-Windows 95/98
  2. A shutdown/init that knows to run a file on shutdown. (The SysVinit-2.50 package can do this, for example. SysVinit-2.50 is available from sunsite.unc.edu in /pub/Linux/system/Daemons. Almost all current distributions use this init package.)

3. Recent Changes and versions

Modifications by Rahul U. Joshi

Date: 26 Nov 1999

4. Administrative

4.1 Copyright

This mini-HOWTO is Copyright Rahul Sundaram.All rights reserved.This document is licensed under the Linux Documentation Project license.I welcome any kind of commercial distrubution but I would like to receive information regarding this.I would also help anyone willing to translate this document.If you require any exceptions to the licensing terms please contact me Rahul Sundaram. The latest version of this document is always available at the Linux Documentation website at http://tldp.org/HOWTO/mini/Automount.html.

4.2 Disclaimer

Although I have tried my best to bring out various facts, the author is not responsible for any damage due to the actions taken based upon the information contained in this document. It is impossible to test the things under all the configurations, so probably some of the hints given in this document may be buggy and may not work on your system. In case you trace the bug, let me know it first !

This document is provided ``as is''. I put great effort into writing it as accurately as I could, but you use the information contained in it at your own risk. In no event shall I be liable for any damages resulting from the use of this work.

In case you are planning to include this HOWTO on some distribution medium or in print, I will like to have an acknowledgement e-mail (just for my record). In that case, I may also be able to send you the latest version of the document.

5. Before you begin

This is not a step by step HOWTO. The reader is expected to first read the entire document so that he (or she) becomes aware of the possible pitfalls and performance bottlenecks. After having a fair idea as to what steps are to be taken, he (or she) can then follow the next section in a step by step manner. Since you will be playing as a root and that too with the system initialization files, be alert as to what you are doing. Write in those files comments for those sections that you added, and when you want to remove a section, do not delete it. Instead just comment it out. (In shell scripts comments begin with a pound(#) sign). In case things go wrong, you may restore your original configuration by removing the changes you did. Although you are not required to know shell scripting since I have given all those that would be needed, some knowledge may be advantageous both for understanding as well for debugging.

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

6.2 Tell Windows the location of the new swap file

For Windows 3.1 users

For Windows 95/98 users

>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

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

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.

7. A couple of notes

8. What are we exactly doing ?

9. The swapinit.sh script

This shell script initializes the swap space on the partition. The code for signature detection has been repeated 6 times because many a times the signature is properly detected only on the second or the third try.

(Adapted from the original Swap-Space-HOWTO by H. Peter Anvin)

 
#!/bin/sh 
# 
# swapinit.sh - activate the swap partition 
# 
# written by Rahul U. Joshi 
# Verify and initialize swap space 
# 
 
echo -n 'Verifying swap space... ' 
 
loopcount=0 
 
# flag to indicate whether the partition has been activated or not 
activated=0 
 
# check for signatures 6 times before giving up 
while [ $loopcount -lt 6 ] 
  do 
 
  if [ "`/bin/dd 2>/dev/null if=/dev/winswap bs=1 count=10  skip=4086`" = 'SWAP-SPACE' ]; then 
    echo "Linux signature found, iteration $loopcount" 
    echo "Activating swap partitions"  
    swapon /dev/winswap 
    activated=1 
    break 
 
  elif [ "`/bin/dd 2>/dev/null if=/dev/winswap bs=1 count=11 skip=43`" = 'SWAP SPACE ' ]; then    
    echo "DOS signature found, iteration $loopcount" 
    echo "Making swap partition"   
    mkswap /dev/winswap YYYYY 
    echo "Activating swap partitions"  
    swapon /dev/winswap 
    activated=1 
    break 
 
  else    
   let loopcount=loopcount+1 
  fi   
 
  done 
 
 
if [ $activated -ne 1 ] ; then 
  echo "Swap signature not found after $loopcount tries" 
  echo "No swapping partitions activated" 
  exit 1 
fi 

10. The swaphalt.sh script

This script first checks the Linux swap signature and then restores the Windows file system on it.

(Adapted from the original Swap-Space-HOWTO by H. Peter Anvin)

 
#!/bin/sh 
# 
# swaphalt.sh   This file is executed through the /etc/rc.d/init.d/halt 
#               script after swapping and accounting has been turned off. 
# 
# Author:       Rahul U. Joshi 
# 
 
# check swap partition signature and restore Windows swap info 
 
loopcount=0 
 
# flag to indicate whether the swap info has been restored or not 
restored=0 
 
# check for swap signature 3 times before giving up 
while [ $loopcount -lt 3 ] 
  do 
 
  if [ "`/bin/dd 2>/dev/null if=/dev/winswap bs=1 count=10 skip=4086`" = 'SWAP-SPACE' ]; then 
    echo "Restoring DOS/Windows swap info , iteration $loopcount"  
    /bin/zcat /etc/winswap.gz  >  /dev/winswap 
    restored=1 
    break 
  else 
    loopcount=loopcount+1 
  fi 
 
  done 
 
if [ $restored -ne 1 ] ; then 
   echo "Swap signature not found after $loopcount tries" 
   echo "Skipping restoring" 
fi 
 

11. The msinfo.sh script

This shell script analyses the boot sector of the given partition and displays some information along with the "Total Special Sectors" in a message box. It assumes that the filesystem on the given partition is a FAT16. If not, it will print an error message and exit. Invoke it as

 
# msinfo <partition name> 
 

To run this script, you will need the "dialog" program that displays dialog boxes. You can get it from here.

#!/bin/sh 
# 
# msinfo.sh           This shell script displays the boot sector of the 
#                     given partition.
# 
# Author:             Rahul U. Joshi 
# 
# Modifications       Removed the use of expr and replaced it by the let 
#                     command. 
# 
# ------------------------------------------------------------------------
# This program is a free software, you can redistribute it and/or modify
# it under the eterms of the GNU General Public Liscence as published by
# the Free Software Foundation; either version 2 or (at your option) any
# later version.
#
# This program is being distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY without even the implied warranty of
# MERCHANTIBILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public Liscence for more details.
# -------------------------------------------------------------------------
 
 
# check for command line arguments 
if [ $# -ne 1 ]; then 
   echo "Usage: msinfo <partition name>" 
   exit 1 
fi 
 
# check whether the input name is a block device 
if [ ! -b $1 ]; then 
   echo "msinfo: $1 is not a block device" 
   exit 1 
fi 
 
# create two temporary files for use 
TMPFILE=`mktemp -q /tmp/$0.XXXXXX` 
if [ $? -ne 0 ]; then 
    echo "msinfo: Can't create temp file, exiting..." 
    exit 1 
fi 
 
TXTFILE=`mktemp -q /tmp/$0.XXXXXX` 
if [ $? -ne 0 ]; then 
   echo "msinfo: Can't create temp file, exiting..." 
   rm -f $TMPFILE 
   exit 1 
fi 
 
backtitle="`printf "%78s" "msinfo, Information about FAT16 filesystem -- Rahul Joshi"`" 
 
dialog --title "Boot sector of $1" --backtitle "$back_title" \ 
       --infobox "\nAnalysing boot sector for $1\nPlease wait..."  14 60 
 
# truncate TXTFILE to zero length 
echo > $TXTFILE 
 
# get Formatting DOS version 
dd 2>/dev/null if=$1  bs=1 count=8 skip=3 | dd 2>/dev/null of=$TMPFILE 
printf >>$TXTFILE "%30s : %s\n" "Formatting DOS version" "`cat $TMPFILE`" 
 
 
# get file system 
dd 2>/dev/null if=$1  bs=1 count=8 skip=54 | dd 2>/dev/null of=$TMPFILE 
printf >>$TXTFILE "%30s : %s\n" "Filesystem" "`cat $TMPFILE`" 
 
# check if filesystem in a FAT16 
if [ "`cat $TMPFILE`" != "FAT16   " ]; then 
  dialog --title "Boot sector of $1" --backtitle "$back_title" \
         --infobox  "\nCan't find a FAT16 filesystem on $1"  14 60 
  exit 2 
fi 
 
# get volume label in boot sector 
dd 2>/dev/null if=$1  bs=1 count=11 skip=43 | dd 2>/dev/null of=$TMPFILE 
printf >>$TXTFILE "%30s : %s\n" "Volume label in boot sector" "`cat $TMPFILE`" 
  
 
# get Sector size 
dd 2>/dev/null if=$1  bs=1 count=2 skip=11| od -An -tdS | dd 2>/dev/null of=$TMPFILE 
printf >>$TXTFILE "%30s : %d\n" "Sector size" `cat $TMPFILE` 
sector_size=`cat $TMPFILE` 
 
 
# get Reserved sectors 
dd 2>/dev/null if=$1  bs=1 count=2 skip=14| od -An -tdS | dd 2>/dev/null of=$TMPFILE 
printf >>$TXTFILE "%30s : %d\n" " Reserved sectors" `cat $TMPFILE` 
reserved_sectors=`cat $TMPFILE` 
 
 
# get FAT sectors 
dd 2>/dev/null if=$1  bs=1 count=1 skip=16| od -An -tdS | dd 2>/dev/null of=$TMPFILE 
fat_count=`cat $TMPFILE` 
 
dd 2>/dev/null if=$1  bs=1 count=2 skip=22| od -An -tdS | dd 2>/dev/null of=$TMPFILE 
sectors_per_fat=`cat $TMPFILE` 
 
# calculate the no of sectors allocated for FAT's 
let fat_sectors=fat_count*sectors_per_fat 
 
printf >>$TXTFILE "%30s : %u (%u x %u) \n" "FAT sectors" "$fat_sectors" \
        "$fat_count" "$sectors_per_fat" 
 
 
# get root directory sectors 
dd 2>/dev/null if=$1  bs=1 count=2 skip=17| od -An -tdS | dd 2>/dev/null of=$TMPFILE 
root_sectors=`cat $TMPFILE` 
 
# calculate the no of sectors allocated for root directory 
let root_sectors=root_sectors*32/sector_size 
 
printf >>$TXTFILE "%30s : %u\n" "Root directory sectors" "$root_sectors" 
 
 
# get Total special sectors 
let total=reserved_sectors+fat_sectors+root_sectors 
printf >>$TXTFILE "%30s : %u\n" "Total special sectors" "$total" 
 
 
# display the information
dialog --title "Boot sector of $1"  --backtitle "$back_title"  --msgbox "`cat $TXTFILE`" 14 60 
  
# delete temporary files 
rm -f $TMPFILE 
rm -f $TXTFILE 
 
# end of msinfo.sh 

12. The original msinfo file

Here is the msinfo file required to find the "Total Special Sectors". It was included by the original author of this HOWTO in an encoded form. First copy the text from the line "begin 755 msinfo.gz" to the line "end" into a temporary file say temp.uu. Then uudecode this file, and finally gunzip to get msinfo file.

 
# uudecode temp.uu 
# gunzip  msinfo.gz  

Now run the program using the command

    
# ./msinfo /dev/winswap 
The program displays the boot sector information for given partition as well as the "Total Special Sectors" for the drive.
 
begin 755 msinfo.gz 
M'XL(`$$YNRT"`Y557TQ;51@_MW]HZ0AM'"$U-.28W([..>84%1%D2QH>',9* 
M2S&VC);V=FV]T*;WWBD+1I)K'YH;DOJF#YH80GPR^J""D"R8-K('MS2;#Q*S 
M%Y,E-P&592A$V:[?.;?M"FP/?DG/G^_[?7_Z_3G7PL313TT(?8P0<J"CI':8 
M$5H^#:=5<KT^MQ`^UAQ1O*QUH0<.]B7S/FC[U!98Y;))]<*N^AG"^LB((J%E 
MAFIM[A9U0O`;%:*7N#[L%G!_-IH34V(J,WUZ.CK%O6I#N4DTE,E-144Q-7T) 
M>]_PX\M<3@!`'ZZ26[`-I7A.F!%$;JK.;21`!#.\-,5A/CK)\=B3FL:3F8PH 
M<#$QDSO91Q%^>L%"Z@KW*"-NR3;""5SN,A?'NI[0=P0Q=#[P&&$5@3UNZ3VW 
M=-(V`NYQ/)6CX)DC2F`KD!&C/!:R7"Q%]L-6`8'D+=]8T*^$V8@RR\;5\P9( 
M^;\&T:F]E`Q`EI,_6YHCZBT#BJ09M0VV9!:XVKLF]0NX%'Z4R];\W](3A-%J 
MI:`_&12!>@33QC0S'V8]?O5+0`;3UC23K)!"?@97N8R#:29MF/>R)PI>UJF) 
MK%/-4QR39OSJE8,8MHZ)UC#`?1ZX'N!ZU`L'X;@.?X["C<1DUT%,9QW32C'' 
M"<98PQR#8#=(L'>96NQWR/4W1@>H3\'!OC3,MMN_"[,=A1OVK^XEH($[$S^H 
MY@28=MB78-$/`=8)(L=\P*$5*I_LS@)_G7!=\Z9V@F"``Y$XQM[T!T>(WHNU 
M/T9CH)YIZI("&9ZK32AR=DTN/QNZ."&7?27H_<(=_^=Q"U1N3Y.>!-E`$BY( 
M;(4ZG)/[\_0F_54HY7\5[<!33)\2M%:Y(.THX_N%K?%2L9&(O=4>8F^@IV9( 
M,<HEDU:17T'23M)GA#ANW-<F'J5-]>6!G2;BU+YN)@<RL.JBIFD4[:<###UG 
MO3Z77WO?L?O+@@G\+(-<"X&QT"&#_N7@_\2;JGAEF&U1`JR#ZCD/Z]7HJ'ZH 
M09_JNAZGJ^N?X5.39_AXMY!!?3@6G>X2,9^)QG%\!IZ@5`SSJ>EWN!SN>HCK 
MLJ%F08R*J5B4YV=T0-R&J#UYJW>4#"7/#D,0/GG`0ZI@__`;6->]K!>C[<GD 
M31/IQP>:!B]D?DT,*<;Y6=9Y]3:P][^W`?+>8F(1!*ZY7B1:BN=@D_XY9?0I 
M'?ZT0=V@BBWIIV$47#Y@7-,9Q>0<*=7;NMW-NS2>9#_X3V)8()?A$;!`ALRW 
M#&\%RO_^@9F6TP<]'M8J@'AFA"[JM]0(WCQ!`[.826!F&A@HG4JWU5R_KKL& 
M9GNQF$;J[?NZ\YUZ?B].C,OEWI*\94J`:6LB?U,RK^2.([0QRQK(W!D2[K5M 
M,G>&0B7A'K03F"'17*&L#9XU@+U2L9K4-GCIG(K(NE:SM,>SM,?;X,T:&U6, 
MT./E%NAUTNE_$,^E(A0M!B7K[D'>X6'\FC25Q3W=+V3YLR^CXJW.S0>#54*# 
M#ZE^AI+N-35^_>![U4!?@VQ%ET<L!BC>MC5"\GH->"T6'4/2NS>((N0,DT>_ 
9K6^!S%65KRS`,`,UVB43\!]-BKG]B`<``#5" 
` 
end 

13. Acknowledgements, feedback and dedication

This Mini-HOWTO has been largely derived from the Swap-Space Mini HOWTO by H. Peter Anvin. Rahul.U.Joshi added the Windows 95/98 specific details as well as a few Red Hat Linux specific details. The msinfo program given in the original HOWTO didn't work on the Joshi's Red Hat Linux 6 system, so he created a similar program. However, He has also included the original msinfo program.It is now maintained by Rahul Sundaram.

I rely on you, the readers, to make this HOWTO useful. If you have any suggestions, comments, corrections, requests, flames, etc., feel free to contact Rahul Sundaram. I dedicate this document to my friends.