LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   cloning disk using cp - no special devices for target disk (http://www.linuxquestions.org/questions/linux-general-1/cloning-disk-using-cp-no-special-devices-for-target-disk-320508/)

vulture99 05-05-2005 04:20 PM

cloning disk using cp - no special devices for target disk
 
I plan to clone one disk to another nightly, using cp:

cp /dev/hda /dev/hde

This works well, successfully copying the partition table and data. However, I am unable to mount the /dev/hde partitions since none of the associated special devices exist:

$ sudo mkdir /test
$ sudo mount /dev/hde1 /test
mount: special device /dev/hde1 does not exist

$ ls -la /dev/hd*
brw-rw---- 1 root disk 3, 0 2005-05-05 00:43 /dev/hda
brw-rw---- 1 root disk 3, 1 2005-05-05 00:43 /dev/hda1
brw-rw---- 1 root disk 3, 2 2005-05-05 00:43 /dev/hda2
brw-rw---- 1 root disk 3, 3 2005-05-05 00:43 /dev/hda3
brw-rw---- 1 root disk 3, 4 2005-05-05 00:43 /dev/hda4
brw-rw---- 1 root cdrom 22, 0 2005-05-05 00:44 /dev/hdc
brw-rw---- 1 root disk 33, 0 2005-05-05 00:43 /dev/hde

Should I manually create the /dev/hde special devices (to match those on /dev/hda, since /dev/hde will be a clone) using mknod? Or is there another approach? My goal is to have a bootable copy of the system disk.

Thanks. Here's the detailed disk info:


$ sudo fdisk -l /dev/hda

Disk /dev/hda: 40.0 GB, 40020664320 bytes
255 heads, 63 sectors/track, 4865 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/hda1 * 1 1216 9767488+ 83 Linux
/dev/hda2 1217 1824 4883760 83 Linux
/dev/hda3 1825 4256 19535040 83 Linux
/dev/hda4 4257 4865 4891792+ 82 Linux swap / Solaris

$ sudo fdisk -l /dev/hde

Disk /dev/hde: 40.0 GB, 40020664320 bytes
255 heads, 63 sectors/track, 4865 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/hde1 * 1 1216 9767488+ 83 Linux
/dev/hde2 1217 1824 4883760 83 Linux
/dev/hde3 1825 4256 19535040 83 Linux
/dev/hde4 4257 4865 4891792+ 82 Linux swap / Solaris

acid_kewpie 05-05-2005 05:22 PM

you can't just copy a whole disk with cp, not a chance. cp copies files, and /dev/ entries are not files... to clone a disk outright, you'd use dd, but you need to unmount all relevant partitions in order to do it safely, which means using a boot disk or something to avoid mounting any part of your primary master drive

vulture99 05-05-2005 06:13 PM

You're right that dd actually *clones* a disk, empty space and all. But cp can definitely be used to copy the entire contents of a raw device.

My goal is to have a bootable copy (or clone) of the system disk as part of a backup plan. As long as it boots and functions, I don't care if a few log files change while the disk is being copied. What I'm not sure about is how to handle the special devices.

I understand your point about files changing during a copy, but take a look at what other folks have to say about using cp:

Quote:

Are you using identical disks? If so, here is what I do:
# cp /dev/hda /dev/hdb
Does the same thing as dd. cp understands that I am referencing raw disk
devices rather than file systems, and behaves correctly. I did not
partition the disk prior to the copy. This copied my MBR, extended
partitions, Linux partitions, swap partition, and Windoze partitions,
and the resulting disk boots and runs just like the original one. As I
recall, it took me 35 minutes to copy a 20G disk.
comp.os.linux.misc post
Quote:

Using dd to copy a 'live' system disk is likely to be bad news. It is
not a problem using dump/cpio/tar/cp/etc. since these are work at the FS
level and won't be affected by (normal) low-level changes to the
filesystem -- all that would be lost would be randomly updated system
files (eg log files), which won't affect the runablity of the cloned
system disk.
comp.os.linux.misc post

jlliagre 05-05-2005 06:50 PM

cp and dd are doing here the very same things, actually dd gives you more control about the process and would probably run faster.

Anyway, cloning disks with mounted partitions is madness, unless you are in a very very quiest state: single user mode with no file write during the copy.

About you issue with missing devices, did you reboot or run MAKEDEV to have them created ?

nxny 05-05-2005 07:12 PM

If the kernel is not letting you mount for instance say /dev/hde4, it might be due to the fact that the kernel is still using the old partition table. Read somethere that tools such as partx and GNU Parted can make the kernel reread the partition table when the new PT is written by the said tool. fdisk lacks this feature, not to mention dd and cp. I took the easy way out by rebooting.

If your device files are not present, run MAKEDEV, but beware that mknod will complain about existing devices.

vulture99 05-06-2005 05:10 PM

Hmmm, ok. Thanks for the input. I did `cp /dev/hda /dev/hde` and then tried MAKEDEV but couldn't get it to work. The command output indicated MAKEDEV created all the /dev/hde* files properly, but they did not appear. I ended up rebooting and that created the device files properly.

Point taken about dropping to single user mode. I am thinking of doing something like this (untested):

Code:

#!/bin/bash
/sbin/init 1
for i in 1 2 3 4;
  do
    /bin/umount /dev/hda$i
  done
/bin/cp /dev/hda /dev/hde  # or: dd if=/dev/hda of=/dev/hde bs=512
/bin/mount -a
/sbin/init 3
# done


jlliagre 05-08-2005 05:36 PM

Some comments:
- I'm not sure your script won't be interrupted by the state change (init 1).
- you won't be able to umount the partition where / stays (hda1 I guess).
- bs=512 is a very poor dd tuning, try using something like bs=1024k to optimize you I/Os.


All times are GMT -5. The time now is 08:10 AM.