old-www/LDP/LG/issue64/tag/12.html

271 lines
11 KiB
HTML

<!--startcut ======================================================= -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<META NAME="generator" CONTENT="lgazmail v1.3E.q">
<TITLE>The Answer Gang 64: Duping a Drive Under Linux</TITLE>
</HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000"
LINK="#3366FF" VLINK="#A000A0">
<!-- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -->
<P> <hr>
<CENTER>
<!-- *** BEGIN navbar *** -->
<!-- *** END navbar *** -->
</CENTER>
</p>
<P> <hr> <P>
<!-- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -->
<center>
<H1><A NAME="answer">
<img src="../../gx/dennis/qbubble.gif" alt="(?)"
border="0" align="middle">
<font color="#B03060">The Answer Gang</font>
<img src="../../gx/dennis/bbubble.gif" alt="(!)"
border="0" align="middle">
</A></H1>
<BR>
<H4>By Jim Dennis, Ben Okopnik, Dan Wilder,
the Editors of Linux Gazette...
and You!
<br>Send questions (or interesting answers) to
<a href="mailto:linux-questions-only@ssc.com">linux-questions-only@ssc.com</a>
</H4>
</center>
<p><hr><p>
<!-- endcut ======================================================= -->
<!-- begin 12 -->
<H3 align="left"><img src="../../gx/dennis/qbubble.gif"
height="50" width="60" alt="(?) " border="0"
>Duping a Drive Under Linux</H3>
<p><strong>From Leon
</strong></p>
<p align="right"><strong>Answered By The Linux Gazette Answer Gang
<br></strong></p>
<!-- sig -->
<!-- ::
Duping a Drive Under Linux
~~~~~~~~~~~~~~~~~~~~~~~~~~
:: -->
<P><STRONG>
I need to move my entire redhat 6.2 installation to another hard drive.
It currently resides on a portion of DRIVE-2. I want to move it to
DRIVE-3 and use the whole drive for the system. After I move the files
I intend to remove the partition from drive 2. DRIVE-3 has no files and
has not been formatted.
</STRONG></P>
<P><STRONG>
I have files installed in the system that I need and it would take days
to re-install and reconfigure them.
</STRONG></P>
<P><STRONG>
In windows, Laplink enables you to transfer or move files and their
subdirectories across drives or computers.
</STRONG></P>
<P><STRONG>
Do you know of a utility that can do this?
</STRONG></P>
<P><STRONG>
Leon
</STRONG></P>
<BLOCKQUOTE><IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
> [JimD]
Linux comes with several utilities for archiving your files.
</BLOCKQUOTE>
<BLOCKQUOTE>
In the simplest case you can simply use the GNU cp command
with the -a (--archive) and possibly the -x (--one-file-system)
options.
</BLOCKQUOTE>
<BLOCKQUOTE>
Here's an example. Assume that you are using <TT>/dev/hda</TT> now
and that you've adding a new <TT>/dev/hdc</TT> to your system. (I'll
assume that <TT>/dev/hdb</TT> is your CD-ROM, not that it matters).
Let's say that your Linux system is on <TT>/dev/hda5</TT>, <TT>/dev/hda6</TT>
and <TT>/dev/hda7</TT> (and that OS/2, Windows or whatever is on
<TT>/dev/hda{1</TT>,2,3}). Let's assume that you have <TT>/dev/hda5</TT> mounted on
<TT>/</TT>, <TT>/dev/hda6</TT> mounted on <TT>/usr</TT>, and <TT>/dev/hda7</TT> on <TT>/home</TT> (a minimal
but reasonable fs arrangement).
</BLOCKQUOTE>
<BLOCKQUOTE>
First I'd suggest partitioning <TT>/dev/hdc</TT> (using fdisk or cfdisk)
making your new filesystems (using mke2fs -c --- check for bad
blocks just in case; I know they're quite rare on modern hard
drives: but if the drive remaps out enough to spares it will have to
show some of them). You can make the new filesystems any
size you like; though you should probably make them at least as
large as your existing filesystems. Perhaps you'd like to
also move <TT>/var</TT> off of <TT>/</TT> and onto it's own filesystem (often a
good idea). So you might make a one or two hundred Mb /
on <TT>/dev/hdc1</TT>, a 127 Mb swap on hdc2, an "extended" container
for hdc3, and three filesystems (5, 6, and 7) for <TT>/usr</TT>, <TT>/var</TT>,
and <TT>/home</TT> restectively. So you create all of those. (<TT>/dev/hdc4</TT>
is unused in this example, since it is a "primary" partition and
we didn't need three primaries; in fact we could have just
created one primary for the <TT>/</TT> or <TT>/boot</TT> filesystem and put all of
the rest, including our swap partition into the extended container
as "logical" partitions from 5 through whatever).
</BLOCKQUOTE>
<BLOCKQUOTE>
Because I'm so studly at the command prompt I could (after
writing my new partition table) make all of these filesystems
with a short and a long command like:
</BLOCKQUOTE>
<BLOCKQUOTE><BLOCKQUOTE><CODE>
mkswap /dev/hdc2
<BR>for i in 1 5 6 7; do mke2fs -c /dev/hdc$i; done
</CODE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
... and I can then mount them like so:
</BLOCKQUOTE>
<BLOCKQUOTE><BLOCKQUOTE><CODE>
mount /dev/hdc1 /mnt &amp;&amp; cd /mnt &amp;&amp; mkdir proc usr var home &amp;&amp; mount /dev/hdc5 usr &amp;&amp; mount /dev/hdc6 var &amp;&amp; mount /dev/hdc7 home
</CODE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
(all those &amp;&amp; thingies ensure that each previous step was completed
without an error before going to the next step. The backslash just
marks a line continuation. Of course we can enter each command
one at a time and just manually make sure that it went O.K for
ourselves. Sometimes I'm just too studly for my own good).
</BLOCKQUOTE>
<BLOCKQUOTE>
Now, I'd have all of my new filesystems mounted under <TT>/mnt</TT> in
a way that corresponds to their intended arrangement on <TT>/</TT> (the
root directory). In other words, if <TT>/mnt</TT> were my root directory
then *it's* <TT>/usr</TT>, <TT>/var</TT>, and <TT>/home</TT> would be properly mounted.
</BLOCKQUOTE>
<BLOCKQUOTE>
Thus my target disk space is prepared and arranged for a full
copying. Here's how:
</BLOCKQUOTE>
<BLOCKQUOTE><BLOCKQUOTE><CODE>
cp -ax / /usr /home /mnt
</CODE></BLOCKQUOTE></BLOCKQUOTE>
<BLOCKQUOTE>
That's all! We're directing the GNU cp command to "archivally" copy
(<TT>-a</TT>) everything below <TT>/</TT>, <TT>/usr</TT>, and <TT>/home</TT> (including all of their
subdirectories; which is one of the implications of the -a option)
but <EM>excluding</EM> any <EM>external</EM> filesystems. That's why we have to
list / <EM>and</EM> <TT>/usr</TT> <EM>and</EM> <TT>/home</TT> (our three mounted local filesystems).
</BLOCKQUOTE>
<BLOCKQUOTE>
This command will not copy <TT>/proc</TT> (which is a special, virtual filesystem
that represents the kernel's PROCess state as if they were files
and directories). It will also skip any devfs, <TT>/dev/pts</TT>, and other
virtual filesystems as well as any NFS or other filesystems that we
didn't explicitly list on our command line.
</BLOCKQUOTE>
<BLOCKQUOTE>
Now you might understand why I spent so many paragraphs "setting up"
this example. If you had 11 filesystems (<TT>/</TT>, <TT>/usr</TT>, <TT>/var</TT>, <TT>/home</TT>,
<TT>/opt</TT>, <TT>/var/spool</TT>, <TT>/var/cache</TT>, <TT>/var/spool/news</TT>, <TT>/var/spool/mail</TT> and
<TT>/tmp</TT>, and <TT>/usr/local</TT>) then you'd probably want to list all of them
on your cp command.
</BLOCKQUOTE>
<BLOCKQUOTE>
If you left off the -x and just did a <tt>cp -a / /mnt</TT> then you'd
have two problems. You'd be making a copy of <TT>/proc</TT> to <TT>/mnt/proc</TT>
(which would waste lost of disk space and be hard to track down
after you rebooted and mount the virtual <TT>/proc</TT> over the
<TT>/proc</TT> containing "regular" files). Of course you could simply
follow your cp command with an '<tt>rm -fr /mnt/proc</TT>' --- but
that's a waste of time and CPU cycles; and an extra opportunity of
mess things up (particularly since you're doing all of this as
'root' where any mistake can destroy the whole system). Worse,
you'd copy some things from <EM>twice</EM> (once into <TT>/mnt</TT> and again
into <TT>/mnt/mnt</TT>). That's a classic recursion problem. Again,
you <EM>could</EM> follow up such a mistake with an rm -fr command
--- but you still might cause problems (you might fill up your
<TT>/mnt</TT> filesystem with duplicate <TT>/mnt/mnt</TT> and <TT>/proc</TT> junk).
</BLOCKQUOTE>
<BLOCKQUOTE>
So, it's wise to use -x (limit the cp command's recursion or
"descent through subdirectories" to those filesystems that the
cp command "started on").
</BLOCKQUOTE>
<BLOCKQUOTE>
There are similar ways to accomplish this task using any of
tar, cpio, rsync or the dump/restore pair of utilities. In fact
it's even possible using these with ssh to security duplicate your
system or any of its directory trees across a network to another
system. However, I'll defer discussion of that for a more
comprehensive "Recovery and Backup HOWTO."
</BLOCKQUOTE>
<!-- sig -->
<!-- sig -->
<!-- end 12 -->
<!--startcut ======================================================= -->
<P> <hr> </p>
<!-- *** BEGIN copyright *** -->
<H5 align="center">This page edited and maintained by the Editors
of <I>Linux Gazette</I>
<a href="http://www.linuxgazette.com/copying.html"
>Copyright &copy;</a> 2001
<BR>Published in issue 64 of <I>Linux Gazette</I> March 2001</H5>
<H6 ALIGN="center">HTML script maintained by
<A HREF="mailto:star@starshine.org">Heather Stern</a> of
Starshine Technical Services,
<A HREF="http://www.starshine.org/">http://www.starshine.org/</A>
</H6>
<!-- *** END copyright *** -->
<P> <hr>
<!-- begin tagnav ::::::::::::::::::::::::::::::::::::::::::::::::::-->
<p align="center">
<table width="100%" border="0"><tr>
<td align="right" valign="center" rowspan="3"
><IMG ALT="" SRC="../../gx/navbar/left.jpg"
WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="middle" border="0">
<A HREF="../lg_answer64.html"
><IMG SRC="../../gx/dennis/answertoc.jpg" align="middle"
ALT="[ Answer Guy Current Index ]" border="0"></A></td>
<td align="center" valign="center"><A HREF="../lg_answer64.html#greeting"><img align="middle"
src="../../gx/dennis/smily.gif" alt="greetings" border="0"></A> &nbsp;
<A HREF="1.html">1</A> &nbsp;
<A HREF="2.html">2</A> &nbsp;
<A HREF="3.html">3</A> &nbsp;
<A HREF="4.html">4</A> &nbsp;
<A HREF="6.html">6</A> &nbsp;
<A HREF="7.html">7</A> &nbsp;
<A HREF="9.html">9</A></td>
<td align="left" valign="center" rowspan="3"><A HREF="../../tag/kb.html"
><IMG SRC="../../gx/dennis/answerpast.jpg" align="middle"
ALT="[ Index of Past Answers ]" border="0"></A>
<IMG ALT="" SRC="../../gx/navbar/right.jpg" align="middle"
WIDTH="14" HEIGHT="45" BORDER="0"></td></tr>
<tr>
<td align="center" valign="center"><A HREF="11.html">11</A> &nbsp;
<A HREF="12.html">12</A> &nbsp;
<A HREF="15.html">15</A> &nbsp;
<A HREF="16.html">16</A> &nbsp;
<A HREF="18.html">18</A></td>
</tr><tr>
<td align="center" valign="center"><A HREF="20.html">20</A> &nbsp;
<A HREF="22.html">22</A> &nbsp;
<A HREF="24.html">24</A> &nbsp;
<A HREF="25.html">25</A> &nbsp;
<A HREF="26.html">26</A> &nbsp;
<A HREF="28.html">28</A> &nbsp;
<A HREF="29.html">29</A></td>
</tr></table>
</p>
<!-- end tagnav ::::::::::::::::::::::::::::::::::::::::::::::::::::-->
<P> <hr>
<CENTER>
<!-- *** BEGIN navbar *** -->
<!-- *** END navbar *** -->
</CENTER>
</p>
<!-- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -->
</BODY></HTML>
<!--endcut ========================================================= -->