...making Linux just a little more fun!

### 2-cent Tip: Copying a partition

Kapil Hari Paranjape [kapil at imsc.res.in]

Mon, 19 Mar 2007 22:19:34 -0700

Hello,

Here is a way to copy a partition which is mounted without copying all the other partitions that are under it.

Let /patha be the mountpoint of the partition from which you want to copy (it could even be the root path /).

Let /dev/new be the device to which you want to copy the data.

    	mkdir /var/tmp/src
    	# The bind mount is the crucial thing!
    	mount --bind /patha /var/tmp/src
    	mkdir /var/tmp/target
    	mount /dev/new /var/tmp/target
    	cd /var/tmp/src
    	# I find this a neat way to copy all files
    	# and permissions
    	find . | cpio -pdum /var/tmp/target
    	umount /dev/new
    	umount /var/tmp/src
    	rmdir /var/tmp/{src,target}

And you are done! I hope the above commands are self-explanatory. If not, then I can explain further.

Kapil. --

Top Back

Neil Youngman [ny at youngman.org.uk]

Tue, 20 Mar 2007 08:57:17 +0000

On or around Tuesday 20 March 2007 05:19, Kapil Hari Paranjape reorganised a bunch of electrons to form the message:

    > Hello,
    >
    > Here is a way to copy a partition which is mounted without copying
    > all the other partitions that are under it.
    >
    > Let /patha be the mountpoint of the partition from which you want to
    > copy (it could even be the root path /).
    >
    > Let /dev/new be the device to which you want to copy the data.
    >
    > 	mkdir /var/tmp/src
    > 	# The bind mount is the crucial thing!
    > 	mount --bind /patha /var/tmp/src
    > 	mkdir /var/tmp/target
    > 	mount /dev/new /var/tmp/target
    > 	cd /var/tmp/src
    > 	# I find this a neat way to copy all files
    > 	# and permissions
    > 	find . | cpio -pdum /var/tmp/target
    > 	umount /dev/new
    > 	umount /var/tmp/src
    > 	rmdir /var/tmp/{src,target}
    >
    > And you are done!
    > I hope the above commands are self-explanatory. If not, then I can
    > explain further.

I'm not sure what this does that wouldn't be done by 'cp -ax'?

Neil Youngman

Top Back

Thomas Adam [thomas.adam22 at gmail.com]

Tue, 20 Mar 2007 09:09:30 +0000

On Tue, 20 Mar 2007 08:57:17 +0000 Neil Youngman <ny at youngman.org.uk> wrote:

    > I'm not sure what this does that wouldn't be done by 'cp -ax'?

Lots. cp -ax wouldn't handle non-regular files, and would suffer the same file limit restructions as shell globbing would, hence you'd have to mitigate it with xargs, etc.

Note that the tar equivalent would be something like:

    (tar cf - *) | ( cd /target; tar xfp -)

Plus various different incantations thereof.

-- Thomas Adam

Top Back

Ben Okopnik [ben at linuxgazette.net]

Tue, 20 Mar 2007 09:20:39 -0500

On Tue, Mar 20, 2007 at 09:09:30AM +0000, Thomas Adam wrote:

    > On Tue, 20 Mar 2007 08:57:17 +0000
    > Neil Youngman <ny at youngman.org.uk> wrote:
    >
    > > I'm not sure what this does that wouldn't be done by 'cp -ax'?
    >
    > Lots.  cp -ax wouldn't handle non-regular files, and would suffer the
    > same file limit restructions as shell globbing would, hence you'd have
    > to mitigate it with xargs, etc.
    >
    > Note that the tar equivalent would be something like:
    >
    > ```
    > (tar cf - *) | ( cd /target; tar xfp -)
    > '''
    >
    > Plus various different incantations thereof.

...e.g., '-mount' or '-xdev', which tell 'find' not to descend into directories on mounted FSes. I've never used either of these options, so I may be off, but I recalled seeing them in the 'find' man page and thinking "oooh, nifty tool."

    --
    * Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *

Top Back

Kapil Hari Paranjape [kapil at imsc.res.in]

Tue, 20 Mar 2007 07:43:07 -0700

On Tue, 20 Mar 2007, Neil Youngman wrote:

    > I'm not sure what this does that wouldn't be done by 'cp -ax'?

On Tue, 20 Mar 2007, Thomas Adam wrote:

    > Note that the tar equivalent would be something like:
    >
    > ```
    > (tar cf - *) | ( cd /target; tar xfp -)
    > '''

Plus the added dis-incentive that this "-x" switch is something handled by libc rather than the kernel. This means: (0) There may be "hidden" directories that have been mounted over which are seen by the "--bind" approach but are not seen by the libc approach. (a) It may not work with "diet" libraries. (b) It may not work with "busybox" versions. (c) since it involves system calls it may be slower. These assertions are untested but I believe (0) must logically be true!

Regards,

Kapil. --

Top Back