old-www/HOWTO/Swap-Space-6.html

464 lines
16 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9">
<TITLE>Linux Swap Space Mini-HOWTO: The Procedure </TITLE>
<LINK HREF="Swap-Space-7.html" REL=next>
<LINK HREF="Swap-Space-5.html" REL=previous>
<LINK HREF="Swap-Space.html#toc6" REL=contents>
</HEAD>
<BODY>
<A HREF="Swap-Space-7.html">Next</A>
<A HREF="Swap-Space-5.html">Previous</A>
<A HREF="Swap-Space.html#toc6">Contents</A>
<HR>
<H2><A NAME="s6">6. The Procedure </A></H2>
<P>
<EM>NOTE:</EM>
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.
<H2><A NAME="ss6.1">6.1 Turn off swapping and create a DOS partition </A>
</H2>
<P>
<UL>
<LI><P>Boot Linux. Turn off swapping by editing the /etc/fstab file and commenting
the line that describes your swap partition.
<P>
</LI>
<LI><P>Using fdisk under Linux, delete the swap partition in order to create free
space on the disk.
<P>
</LI>
<LI><P>
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.
<P>
</LI>
<LI><P>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"
<P>
</LI>
<LI><P>Format this partition using the DOS FORMAT command.
<BLOCKQUOTE><CODE>
<PRE>
C:\>FORMAT X:
</PRE>
</CODE></BLOCKQUOTE>
<P>
</LI>
<LI><P>
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.]
<BLOCKQUOTE><CODE>
<PRE>
C:\>LABEL X:
C:\>DIR X:
</PRE>
</CODE></BLOCKQUOTE>
<P>
</LI>
<LI><P>
At this stage, you have a DOS partition ready for use for swapping.
<P>
<P>
<P>
</LI>
</UL>
<H2><A NAME="ss6.2">6.2 Tell Windows the location of the new swap file </A>
</H2>
<H3><B>For Windows 3.1 users</B></H3>
<P>
<UL>
<LI><P>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.
<P>
<P>
</LI>
<LI><P>Exit Windows.
<P>
<P>
</LI>
</UL>
<H3><B> For Windows 95/98 users </B></H3>
<P>
<UL>
<LI><P>
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.
<P>
<P>
<P>
</LI>
<LI><P>
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.
<P>
</LI>
<LI><P>Now that you are not maintaining the Recycle Bin for drive X:, ideally the
<CODE>RECYCLED</CODE> 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:
<BLOCKQUOTE><CODE>
<PRE>
C:\>X:
X:\>ATTRIB -R -H -S RECYCLED
X:\>DELTREE RECYCLED
</PRE>
</CODE></BLOCKQUOTE>
<P>Now you have drive X: containing only win386.swp. You can verify this
using the various options for the <CODE>dir</CODE>command. This does not mean
that the <CODE>RECYCLED</CODE> folder will not be there on drive X:. When
Windows boots up and finds that there is no <CODE>RECYCLED</CODE> 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 <CODE>RECYCLED</CODE> folder,
please e-mail me.
<P>
<P>
</LI>
<LI>
<B> Disk Cleanup troubles you ? </B>
<P>
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.
<P>
<P>
</LI>
</UL>
<P>
>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.
<P>
<H2><A NAME="ss6.3">6.3 Back up the Total Special Sectors </A>
</H2>
<P>
<UL>
<LI><P>
Boot Linux, then log in as root.
<P>
<P>
</LI>
<LI><P>
Use the fdisk command to find the name of the partition and its size in blocks.
Create a symbolic link from <CODE>/dev/winswap</CODE> to this partition. If the
partition is hda7, then type:
<BLOCKQUOTE><CODE>
<PRE>
# ln -s /dev/hda7 /dev/winswap
</PRE>
</CODE></BLOCKQUOTE>
<P>[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.]
<P>
<P>
<P>
</LI>
<LI><P>
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
<A HREF="Swap-Space-11.html#msinfo">msinfo.sh</A> file using your favourite editor. Give the
following commands at the shell prompt
<BLOCKQUOTE><CODE>
<PRE>
# cp Swap-Space-HOWTO msinfo.sh
# chmod +x msinfo.sh
</PRE>
</CODE></BLOCKQUOTE>
<P>(Much easier if you use one of the editors with KDE or GNOME)
<P>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
<BLOCKQUOTE><CODE>
<PRE>
# ./msinfo.sh /dev/winswap
</PRE>
</CODE></BLOCKQUOTE>
<P>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.
<P>
</LI>
<LI><P>
<B>Note:</B> 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 :
<BLOCKQUOTE><CODE>
<PRE>
Reserved Sectors at beginning : r
FAT Copies : f
Sectors per FAT : s
Root directory entries : r
</PRE>
</CODE></BLOCKQUOTE>
<P>Now the "Total Special Sectors" is given by
<P>T = r + (s * f) + (d / 16)
<P>However, using the above program is more convienent.
<P>Let us denote these Total Special Sectors by XXX.
<P>
<P>
</LI>
<LI><P>
[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:
<BLOCKQUOTE><CODE>
<PRE>
mkdir /mnt
mount -t msdos /dev/winswap /mnt
dd if=/dev/zero of=/mnt/dummy.fil
umount /mnt
</PRE>
</CODE></BLOCKQUOTE>
<P>The dd command will report "No space left on device". This is exactly what
you want.
<P>
<P>
</LI>
<LI><P>
Type on the shell prompt:
<BLOCKQUOTE><CODE>
<PRE>
# dd if=/dev/winswap bs=512 count=XXX | gzip -9 > /etc/winswap.gz
^^^
</PRE>
</CODE></BLOCKQUOTE>
<P>
<P>... where XXX is replaced with the "Total special sectors" number.
<P>Here we are saving the Total Special Sectors in a compressed form in the file
/etc/winswap.gz
<P>
<P>
</LI>
</UL>
<H2><A NAME="ss6.4">6.4 Modify the initialization and shutdown scripts to handle our new configuration </A>
</H2>
<P>
<UL>
<LI>
<P>
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.
<P>
<P>
<P>
</LI>
<LI><P>
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,
<BLOCKQUOTE><CODE>
<PRE>
# cp Swap-Space-HOWTO /etc/rc.d/init.d/swapinit.sh
# cp Swap-Space-HOWTO /etc/rc.d/init.d/swaphalt.sh
</PRE>
</CODE></BLOCKQUOTE>
<P>and then using your favourite editor, delete all execpt the required part. Now
make these file "executable" by giving the following commands
<BLOCKQUOTE><CODE>
<PRE>
# chmod +x swapinit.sh
# chmod +x swaphalt.sh
</PRE>
</CODE></BLOCKQUOTE>
<P>
<P>
</LI>
<LI><P>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.
<P>
<P>
<P>
</LI>
<LI><P>
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.
<BLOCKQUOTE><CODE>
<PRE>
# Runlevel 0 means shut down the system
l0:0:wait:/etc/brc
</PRE>
</CODE></BLOCKQUOTE>
<P>(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 !
<P>For the remainder of this file, I will assume the filename was
/etc/rc.d/init.d/halt (halt for short).
<P>
<P>
<P>
</LI>
<LI>
<P>Check the name of your system initialization file. For SysVinit, this is the
file listed in the following line of /etc/inittab.
<BLOCKQUOTE><CODE>
<PRE>
# System initialization
si::sysinit:/etc/rc.d/rc.sysinit
</PRE>
</CODE></BLOCKQUOTE>
<P>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)
<P>
<P>
<P>
</LI>
<LI>
<P>
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.
<P>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.
<BLOCKQUOTE><CODE>
<PRE>
# -----------------------------------------------------------------------
# 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
# ----------------------------------------------------------------------
</PRE>
</CODE></BLOCKQUOTE>
<P>
<P>
</LI>
<LI>
<P>
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.
<P>
<P>
<P>
</LI>
<LI>
<P>
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.
<P>
<P><EM>Note for RedHat Linux 6.1 : </EM> 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 <B>not</B> replace the <CODE>swapoff
$SWAPS</CODE> comamnd by the <CODE>swapoff -a</CODE> command. In that case, just
add the <CODE>/etc/rc.d/init.d/swaphalt.sh</CODE> command to the halt file
<P>
<BLOCKQUOTE><CODE>
<PRE>
# ------------------------------------------------------------------------
# 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 ] &amp;&amp; /sbin/accton
# ------------------------------------------------------------------------
# ------------------------------------------------------------------------
# Added by yourname
# check swap signature and restore Windows swap info
/etc/rc.d/init.d/swaphalt.sh
# ------------------------------------------------------------------------
</PRE>
</CODE></BLOCKQUOTE>
<P>
<P>
</LI>
</UL>
<H2><A NAME="ss6.5">6.5 Reenable swapping </A>
</H2>
<P>
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.
<P>
<HR>
<A HREF="Swap-Space-7.html">Next</A>
<A HREF="Swap-Space-5.html">Previous</A>
<A HREF="Swap-Space.html#toc6">Contents</A>
</BODY>
</HTML>