LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware
User Name
Password
Linux - Hardware This forum is for Hardware issues.
Having trouble installing a piece of hardware? Want to know if that peripheral is compatible with Linux?

Notices


Reply
  Search this Thread
Old 04-18-2008, 01:16 PM   #1
georgesdev
LQ Newbie
 
Registered: Oct 2007
Location: France
Distribution: Redhat4 Redhat5, Centos
Posts: 8

Rep: Reputation: 0
Question how to duplicate a partition using dd


Hello,

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?

Thanks a million!

Georges
 
Old 04-18-2008, 01:25 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Mineral, Virginia
Distribution: Debian 8
Posts: 7,877

Rep: Reputation: 318Reputation: 318Reputation: 318Reputation: 318
I suggest that you not use the dd command. Instead you will have a less error prone experience if you use:

mkfs.ext3 /dev/sda1

mount /dev/sda1 /mnt/d2/ -t ext3

cp -a /* /mnt/d2

-----------------
Steve Stites
 
Old 04-18-2008, 04:24 PM   #3
georgesdev
LQ Newbie
 
Registered: Oct 2007
Location: France
Distribution: Redhat4 Redhat5, Centos
Posts: 8

Original Poster
Rep: Reputation: 0
Question Thanks, but ...

Thanks Jailbait,

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?

Thanks,

Georges
 
Old 04-18-2008, 06:05 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,092

Rep: Reputation: 3323Reputation: 3323Reputation: 3323Reputation: 3323Reputation: 3323Reputation: 3323Reputation: 3323Reputation: 3323Reputation: 3323Reputation: 3323Reputation: 3323
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.
 
Old 04-18-2008, 06:38 PM   #5
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
You don't need to dd to a partition though

dd if=/dev/sda1 of=/data/image

and restore with

dd if=/data/image of=/dev/sda1

image is a file
 
Old 04-18-2008, 08:28 PM   #6
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Mineral, Virginia
Distribution: Debian 8
Posts: 7,877

Rep: Reputation: 318Reputation: 318Reputation: 318Reputation: 318
Quote:
Originally Posted by georgesdev View Post
Thanks Jailbait,

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.

----------------------
Steve Stites
 
Old 04-18-2008, 08:45 PM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,092

Rep: Reputation: 3323Reputation: 3323Reputation: 3323Reputation: 3323Reputation: 3323Reputation: 3323Reputation: 3323Reputation: 3323Reputation: 3323Reputation: 3323Reputation: 3323
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.
 
Old 04-19-2008, 11:03 AM   #8
SilverBear2006
Member
 
Registered: Oct 2006
Location: Great Lakes region, North America
Distribution: Mepis 7.0, Mepis 8.0, ArchLinux, Pardus 2008, Kubuntu 8.04, Ubuntu 8.10
Posts: 69

Rep: Reputation: 16
Hello, georgesdev.

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.

Bon chance!

--SilverBear
 
Old 04-19-2008, 11:24 AM   #9
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1194Reputation: 1194Reputation: 1194Reputation: 1194Reputation: 1194Reputation: 1194Reputation: 1194Reputation: 1194Reputation: 1194
Quote:
Originally Posted by SilverBear2006 View Post
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.
 
Old 04-19-2008, 11:44 AM   #10
SilverBear2006
Member
 
Registered: Oct 2006
Location: Great Lakes region, North America
Distribution: Mepis 7.0, Mepis 8.0, ArchLinux, Pardus 2008, Kubuntu 8.04, Ubuntu 8.10
Posts: 69

Rep: Reputation: 16
Quote:
Originally Posted by johnsfine View Post
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.
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.
 
Old 04-19-2008, 05:55 PM   #11
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,092

Rep: Reputation: 3323Reputation: 3323Reputation: 3323Reputation: 3323Reputation: 3323Reputation: 3323Reputation: 3323Reputation: 3323Reputation: 3323Reputation: 3323Reputation: 3323
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.
 
Old 04-20-2008, 09:58 AM   #12
georgesdev
LQ Newbie
 
Registered: Oct 2007
Location: France
Distribution: Redhat4 Redhat5, Centos
Posts: 8

Original Poster
Rep: Reputation: 0
Thumbs up it is working now. Thanks!

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!

Georges
 
Old 04-20-2008, 10:42 PM   #13
onebuck
Moderator
 
Registered: Jan 2005
Location: Summer Midwest USA, Central Illinois, Winter Central Florida
Distribution: SlackwareŽ
Posts: 13,609
Blog Entries: 34

Rep: Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790
Hi,

syg00 I think your talking about 'Learn The DD Command Revised'.
 
Old 04-21-2008, 09:46 AM   #14
SilverBear2006
Member
 
Registered: Oct 2006
Location: Great Lakes region, North America
Distribution: Mepis 7.0, Mepis 8.0, ArchLinux, Pardus 2008, Kubuntu 8.04, Ubuntu 8.10
Posts: 69

Rep: Reputation: 16
Quote:
Originally Posted by onebuck View Post
Hi,

syg00 I think your talking about 'Learn The DD Command Revised'.
Thanks for the link, onebuck!
That is an Awesome post by AwesomeMachine. I don't know why I haven't been aware of it.

It's in my permanent bookmarks now, as well as posted in the Links to "How-To" webpages on sblinux.org

An article like that is one of the things that make LQ.org the place it is!


thanks again,
SilverBear
 
  


Reply

Tags
grubinstall


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what is the Duplicate packet? saman123 Linux - Networking 1 09-26-2007 12:08 AM
Duplicate Thread jimmy512 LQ Suggestions & Feedback 4 08-21-2007 11:54 AM
Duplicate ACKs arunachalam Linux - Networking 3 11-07-2005 11:28 AM
Duplicate /boot partition, fail to mount oO0agentcow0Oo Linux - Newbie 1 07-27-2005 08:12 PM
Mount Duplicate TuxFreak Fedora 13 12-20-2004 07:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration