Linux - HardwareThis forum is for Hardware issues.
Having trouble installing a piece of hardware? Want to know if that peripheral is compatible with Linux?
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.
I am trying to duplicate a partition using dd so that I can later use it if I mess with the original.
I am doing this on redhat 4 update 4 (can't use higher version of Redhat because of support contract).
I try to copy /dev/sda2 to /dev/sda1
The partitions look good to me (even is /dev/sda1 is bigger than /dev/sda2). Here is output of fdisk -l:
Disk /dev/sda: 320.0 GB, 320072933376 bytes
255 heads, 63 sectors/track, 38913 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 3000 24097468+ 83 Linux
/dev/sda2 * 9126 10400 10241437+ 83 Linux
Then I did an mkfs (although since I will do a dd from sda2 to sda1, I don't know why I have to):
# mkfs.ext3 /dev/sda1
Then I did the dd command itself:
# dd if=/dev/sda2 of=/dev/sda1
Then I try to mount the partition, and that fails:
# mount /dev/sda1 /mnt/d2/ -t ext3
mount: wrong fs type, bad option, bad superblock on /dev/sda1,
or too many mounted file systems
Then I doublechecked the original filesystem in /dev/sda2 was really ext3:
# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext3 9.7G 3.4G 5.9G 37% /
So, can someone kindly tell me what I have done wrong?
The thing is I forgot to mention that the partition I am trying to copy is the / filesystem of my machine.
So it includes many exotic files such as:
- device files for the cdrom, the audio, the disks, etc ...
- files from several different users
- links
- etc ...
So I really want to use something stronger than just cp.
So what do you think, What have I done wrong in my original post above?
Or if cp is really my solution, what options must I use to match my requirements above?
Unmount sda2 before attempting the "dd" - or "cp" for that matter.
Yep - that means do this from a rescue system - or liveCD or such. If you want a valid image, make sure there are no un-committed journal entries.
The thing is I forgot to mention that the partition I am trying to copy is the / filesystem of my machine.
So it includes many exotic files such as:
- device files for the cdrom, the audio, the disks, etc ...
- files from several different users
- links
- etc ...
So I really want to use something stronger than just cp.
Or if cp is really my solution, what options must I use to match my requirements above?
cp copies everything. You need the -a option to meet your requirements.
syg00 said:
Quote:
Unmount sda2 before attempting the "dd" - or "cp" for that matter.
You need to umount sda2 before doing dd. But cp will not work if you umount sda2.
Having them mounted from a liveCD isn't an issue (and needed for "cp" as you suggest).
What I was really trying to suggest was that trying to backup an active root isn't on - you could never trust the result.
I have to agree with syg00: this operation needs to be done from a LiveCD, with no partitions mounted on the HD drive you're copying from, and copying to.
Using dd:
I have successfully moved root partitions formatted in ext3 and even in ntfs, with the dd command. If I were moving your's I would use:
Code:
dd if=/dev/sda2 of=/dev/sda1 bs=32256
The bs=32256 is based on 63 sectors of 512 bytes, as indicated in your fdisk -l output. That might correct a problem that might have occurred. You can try it. Adding the bs= will also usually result in a faster data dump procedure -- if you are certain you know the bytes per sector of the format.
A possible problem is that the two partitions are not the same size. As long as the destination partition [sda1] is larger, it should still work. The worst that might happen is that you would lose the extra space in sda --it might become unusable. I have read that. But I always used dd to move partitions to destinations that are exactly the same size, so I do not know from experience if it is true what they say.
Using cp:
Daniel Robbins, founder of Gentoo, is of the opinion that you can use the cp command to move system files. Again, you need to be in a LiveCD, since the OS you are moving cannot be running! That command would be:
Code:
cp -a /dev/sd2 /dev/sda1
The -a switch is the same as -dpR. This preserves all the links, file attributes and copies files and subdirectories recursively. See the man page for cp: http://www.ss64.com/bash/cp.html
Comparing the methods:
The essence of dd is you get an exact byte-for byte copy, including any fragmentation or other errors that exist on the original partition. But if it works as /dev/sda2, it should work after being exactly copied to /dev/sda1.
The essence of cp is that you get all the correct files, but re-written without fragmentation in a new filesystem. The new filesystem should be a copy of the old filesystem. dd copies bytes, cp copies files.
Most important: as syg00 also said, you cannot have the OS you are copying running!
dd -- all partitions unmounted
cp -- partitions mounted, but not being actively used by any process.
Daniel Robbins, founder of Gentoo, is of the opinion that you can use the cp command to move system files. Again, you need to be in a LiveCD, since the OS you are moving cannot be running! That command would be:
Code:
cp -a /dev/sd2 /dev/sda1
What about Grub?
I don't really understand how Grub stage 1 finds Grub stage 1.5, but I would expect that process would be broken by the cp method you just described. So I expect you need to reinstall Grub after copying via cp a partition that includes Grub, assuming you want the copy to be bootable.
Under some conditions, a partition copied with dd could be bootable without reinstalling Grub.
I believe Grub stage 1.5 finds everything else using the directory structure, so once Grub stage 1 has found Grub stage 1.5, the rest should be immune to the sort of changes that occur when you copy a partition with cp.
I don't really understand how Grub stage 1 finds Grub stage 1.5, but I would expect that process would be broken by the cp method you just described. So I expect you need to reinstall Grub after copying via cp a partition that includes Grub, assuming you want the copy to be bootable.
Under some conditions, a partition copied with dd could be bootable without reinstalling Grub.
I believe Grub stage 1.5 finds everything else using the directory structure, so once Grub stage 1 has found Grub stage 1.5, the rest should be immune to the sort of changes that occur when you copy a partition with cp.
Good point!
GRUB is already installed in the MBR of sda in Georges's case, so all that should need doing is to change the /boot/grub/menu.lst entries designating the /dev/sda1 as the booting root. Taking my file as an example:
Code:
# (0) Arch Linux
title Arch Linux
root (hd0,0)
kernel /vmlinuz26 root=/dev/sda2 ro vga=771
initrd /kernel26.img
One would need to change it to:
Code:
# (0) Arch Linux
title Arch Linux
root (hd0,0)
kernel /vmlinuz26 root=/dev/sda1 ro vga=771
initrd /kernel26.img
If this is done on both sda1 and sda2 versions, the first reboot after the LiveCD session will put Georges into his sda1 system. After verifying that he is indeed running his OS installed on sda1, this should eliminate any further need for sda2:
as root:
Code:
grub-install /dev/sda
Anybody who knows differently, please correct me if I'm wrong.
I have plenty of spare partitions on my system right now and haven't had to do this kind of operation in well over a year. But this looks correct to me.
As this is only a "backup" in case of mess-up, it is sufficient to merely do the copy.
If the original system has to be recovered, grub would need to be re-installed after the "cp" back. During recovery (from a liveCD) it could/should be done from that at the end of the "cp".
"dd" suffers in comparison with the way it (doesn't) handle errors - especially executed like that. There is a large thread here on using "dd"; covers most of the issues.
I like "cp" in that I can change filesystem if I choose to - generally not a consideration in a recovery of course.
Using bs option did the trick. Thanks silverbear for the suggestion.
Quote:
[root@shuttle2 ~]# dd if=/dev/sda2 of=/dev/sda1 bs=32256
325125+0 records in
325125+0 records out
[root@shuttle2 ~]# mount /dev/sda1 /mnt/d2/
[root@shuttle2 ~]# ls !$
ls /mnt/d2/
bin dev initrd lost+found mnt root srv usr
boot etc lib media opt sbin sys var
content-april home lib64 misc proc selinux tmp
I'll now play with grub-install, and also with cp -a
Thanks to all who posted, it is a real community here!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.