cloning disk using cp - no special devices for target disk
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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
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
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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 ?
Distribution: Red Hat 8.0, Slackware 8.1, Knoppix 3.7, Lunar 1.3, Sorcerer
Posts: 771
Rep:
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.
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
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.