📄 288-290.html
字号:
<HTML>
<HEAD>
<TITLE>Special Edition Using Linux, Fourth Edition:Managing File Systems</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0789717468//-->
<!--TITLE=Special Edition Using Linux, Fourth Edition//-->
<!--AUTHOR=Jack Tackett//-->
<!--AUTHOR=Jr.//-->
<!--AUTHOR=Steve Burnett//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Que//-->
<!--CHAPTER=14//-->
<!--PAGES=288-290//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="286-288.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch15/293-296.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>You’re better off using a swap partition instead of a swap file. All access to a swap file is performed through the normal Linux file system. The disk blocks that make up the swap file are probably not contiguous and, therefore, performance isn’t as good as it is with a swap partition. I/O to swap partitions is performed directly to the device, and disk blocks on a swap partition are always contiguous. Also, by keeping the swap space off a normal file system, you reduce the risk of corrupting your regular file system if something bizarre happens to your swap file.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading17"></A><FONT COLOR="#000077">Creating a Swap Partition</FONT></H4>
<P>To create a swap partition, you must have created a disk partition by using <TT>fdisk</TT> and tagged it as type 82, Linux swap. After you create the swap partition, you have two additional steps to follow to make the swap partition active.</P>
<P>First, you must prepare the partition in a manner similar to creating a file system. Instead of <TT>mkfs</TT>, the command for preparing the partition is <TT>mkswap</TT>. The syntax of the <TT>mkswap</TT> command is as follows:</P>
<!-- CODE SNIP //-->
<PRE>
mkswap [-c] <I>device size-in-blocks</I>
</PRE>
<!-- END CODE SNIP //-->
<P>where <I>device</I> is the name of the swap partition, such as /dev/hda2, and <I>size-in-blocks</I> is the size of the target file system in blocks. You can get the size in blocks by running <TT>fdisk</TT> and looking at the partition table. In the example in the section “Making Sure the Sizes Are Correct,” the size of /dev/hda2 was 19,159 blocks. Linux requires that swap partitions be between 9 and 65,537 blocks in size. The <TT>-c</TT> argument tells <TT>mkswap</TT> to check the file system for bad blocks when creating the swap space, which is a good idea.</P>
<P>Following the example in “Making Sure the Sizes Are Correct,” the command for setting up a swap partition on /dev/hda2 is this:</P>
<!-- CODE SNIP //-->
<PRE>
mkswap -c /dev/hda2 19159
</PRE>
<!-- END CODE SNIP //-->
<P>After you run <TT>mkswap</TT> to prepare the partition, you must make it active so that the Linux kernel can use it. The command to make the swap partition active is <TT>swapon</TT>. The syntax for the <TT>swapon</TT> command is as follows:</P>
<!-- CODE SNIP //-->
<PRE>
swapon <I>filesys</I>
</PRE>
<!-- END CODE SNIP //-->
<P>where <TT><I>filesys</I></TT> is the file system that you want to make available as swap space. Linux makes a call to <TT>swapon</TT> <TT>-a</TT> during boot, which mounts all available swap partitions listed in the /etc/fstab file.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>Remember to put an entry for any swap partitions or swap files that you create into the /etc/fstab file so that Linux can automatically access them at boot time.<HR></FONT>
</BLOCKQUOTE>
<H4 ALIGN="LEFT"><A NAME="Heading18"></A><FONT COLOR="#000077">Creating a Swap File</FONT></H4>
<P>Swap files can be useful if you need to expand your swap space and can’t allocate disk space to create a dedicated swap partition. Setting up a swap file is almost identical to creating a swap partition. The main difference is that you have to create the file before you can run <TT>mkswap</TT> and <TT>swapon</TT>.</P>
<P>To create a swap file, you use the <TT>dd</TT> command, which is used for copying large chunks of data. For a full description of this command, see the man page for <TT>dd</TT>. The main things that you have to know before creating the file are the name of the swap file you want to create and its size in blocks. A block under Linux is 1,024 bytes. For example, to create a 10MB swap file named <TT>/swap</TT>, enter</P>
<!-- CODE SNIP //-->
<PRE>
<B># dd if=/dev/zero of=/swap bs=1024 count=10240</B>
</PRE>
<!-- END CODE SNIP //-->
<P><TT>of=/swap</TT> specifies that the file to be created is named <TT>/swap</TT>, and <TT>count=10240</TT> sets the size of the output file to be 10,240 blocks, or 10MB. You then use <TT>mkswap</TT> to prepare the file as a swap space:</P>
<!-- CODE SNIP //-->
<PRE>
<B># mkswap /swap 10240</B>
</PRE>
<!-- END CODE SNIP //-->
<P>Remember that you have to tell <TT>mkswap</TT> how big the file is. Before you run <TT>swapon</TT>, you need to make sure that the file is completely written to disk by using the /etc/sync command.</P>
<P>Now you’re ready to make the swap file active. Like with the swap partition, you use the <TT>swapon</TT> command to make the file active; for example,</P>
<!-- CODE SNIP //-->
<PRE>
<B># swapon /swap</B>
</PRE>
<!-- END CODE SNIP //-->
<P>If you need to get rid of a swap file, you must make sure that it’s not active. Use the <TT>swapoff</TT> command to deactivate the swap file, as in</P>
<!-- CODE SNIP //-->
<PRE>
<B># swapoff /swap</B>
</PRE>
<!-- END CODE SNIP //-->
<P>You can then safely delete the swap file.
</P>
<H3><A NAME="Heading19"></A><FONT COLOR="#000077">From Here…</FONT></H3>
<P>In this chapter, you’ve looked at many different aspects of the Linux file system, from a tour of the basic directory structure to mounting and unmounting file systems. You’ve explored accessing remote file systems with NFS and looked in detail at how to create file systems and prepare them for use. Finally, this chapter discussed the creation of swap partitions and swap files.
</P>
<P>You can find more information about systems administration in the following chapters:</P>
<DL>
<DD><B>•</B> Chapter 7, “Understanding System Administration,” introduces you to common systems administration tasks.
<DD><B>•</B> Chapter 10, “Managing User Accounts,” describes how to set up and manage user accounts on your Linux system.
<DD><B>•</B> Chapter 11, “Backing Up Data,” discusses how to plan and implement plans for data backups.
</DL>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="286-288.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch15/293-296.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -