<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML ><HEAD ><TITLE >Setting Up Swap Space</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Linux Partition HOWTO" HREF="index.html"><LINK REL="PREVIOUS" TITLE="Recovering a Deleted Partition Table" HREF="recovering.html"><LINK REL="NEXT" TITLE="Appendix" HREF="appendix.html"></HEAD ><BODY CLASS="sect1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Linux Partition HOWTO</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="recovering.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="appendix.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="setting_up_swap" ></A >9. Setting Up Swap Space</H1 ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="swap_partitions" ></A >9.1. Swap Files</H2 ><P > Normally, there are only two steps to setting up swap space, creating the partition and adding it to /etc/fstab. A typical fstab entry for a swap partition at /dev/hda6 would look like this: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><FONT COLOR="#000000" ><PRE CLASS="programlisting" > /dev/hda6 swap swap defaults 0 0 </PRE ></FONT ></TD ></TR ></TABLE ><P > The next time you reboot, the initialization scripts will activate it automatically and there's nothing more to be done. </P ><P > However, if you want to make use of it right away, you'll need to activate it maually. As root, type: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><FONT COLOR="#000000" ><PRE CLASS="programlisting" > <TT CLASS="userinput" ><B ><B CLASS="command" >mkswap</B > -f <TT CLASS="filename" >/dev/hda6</TT ></B ></TT > <TT CLASS="userinput" ><B ><B CLASS="command" >swapon</B > <TT CLASS="filename" >/dev/hda6</TT ></B ></TT > </PRE ></FONT ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="swapfiles" ></A >9.2. Swap Files</H2 ><P > There might be times when you've run out of swap space and it is not practical to repartition a drive or add a new one. In this case, you can use a regular file in an ordinary partition. All you have to do is create a file of the size you want </P ><P > <TT CLASS="userinput" ><B ><B CLASS="command" >dd</B > if=/dev/zero of=/var/my_swap bs=1024 count=131072</B ></TT > </P ><P > and activate it </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><FONT COLOR="#000000" ><PRE CLASS="programlisting" > <TT CLASS="userinput" ><B ><B CLASS="command" >mkswap</B > -f /var/my_swap</B ></TT > <TT CLASS="userinput" ><B ><B CLASS="command" >swapon</B > /var/my_swap</B ></TT > </PRE ></FONT ></TD ></TR ></TABLE ><P > This invocation creates a file called <TT CLASS="filename" >my_swap</TT > in <TT CLASS="filename" >/var</TT >. It is 128 Mb long (128 x 1024 = 131072). Initially, it is filled with zeros. However, <B CLASS="command" >mkswap</B > marks it as swap space and <B CLASS="command" >swapon</B > tells the kernel to start using it as swap space. When you are done with it, </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><FONT COLOR="#000000" ><PRE CLASS="programlisting" > <TT CLASS="userinput" ><B ><B CLASS="command" >swapoff</B > /var/my_swap</B ></TT > <TT CLASS="userinput" ><B ><B CLASS="command" >rm</B > /var/my_swap</B ></TT > </PRE ></FONT ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="multiple_swap_areas" ></A >9.3. Multiple Swap Areas</H2 ><P > More than one swap partition can be used on the same system. Consider an example fstab where there is a single swap partition: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><FONT COLOR="#000000" ><PRE CLASS="programlisting" > /dev/hda5 / ext3 defaults 1 1 /dev/hda1 /boot ext2 defaults 1 2 none /dev/pts devpts gid=5,mode=620 0 0 none /proc proc defaults 0 0 /dev/hda7 /usr ext3 defaults 1 2 /dev/hda6 swap swap defaults 0 0 </PRE ></FONT ></TD ></TR ></TABLE ><P > Imagine replacing the entry for the swap partition with these three lines: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><FONT COLOR="#000000" ><PRE CLASS="programlisting" > /dev/hda6 none swap sw,pri=3 0 0 /dev/hdb2 none swap sw,pri=2 0 0 /dev/hdc2 none swap sw,pri=1 0 0 </PRE ></FONT ></TD ></TR ></TABLE ><P > This configuration would cause the kernel to use /dev/hda6 first. it has the highest priority assigned to it (pri=3). The maximum priority can be 32767 and the lowest 0. If that space were to max out, the kernel would start using /dev/hdb2, and on to /dev/hdc2 after that. Why such a configuration? Imagine that the newest (fastest) drives are given the highest priority. This will minimize speed loss as swap space usage grows. </P ><P > It is possible to write to all three simulataneously. If each has the same priority, the kernel will write to them much like a RAID, with commensurate speed increases. </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><FONT COLOR="#000000" ><PRE CLASS="programlisting" > /dev/hda6 none swap sw,pri=3 0 0 /dev/hdb2 none swap sw,pri=3 0 0 /dev/hdc2 none swap sw,pri=3 0 0 </PRE ></FONT ></TD ></TR ></TABLE ><P > Notice that these three partitions are on separate drives, which is ideal in terms of speed enhancement. </P ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="recovering.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="appendix.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Recovering a Deleted Partition Table</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" > </TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Appendix</TD ></TR ></TABLE ></DIV ></BODY ></HTML >