How to copy single partition?

I presently backup my multi-OS multi-paritition boot drive (fedora core 4/ext3, WinXPServer/NTFS, WinXPHome/FAT32) with the command:

telinit 1; cp /dev/sda /dev/sdb

And this works.

Is there a command to only copy a single partition instead of an entire device?

And what about the grub stuff. Is that in a partition? My grub stuff is corrupted or the grub.conf file is bad. I can boot windows manually via grub but not fedora. How can I restore the grub and linux partitions but not the windows partition?

Thanks,
siegfried

Partitions for /dev/sda are going to be like /dev/sda1, /dev/sda2, /dev/sda3, etc. You can copy them too.

The master boot record is outside of all partitions. It is the very, very first sector on the hard drive. You can copy just the boot sector like this:

dd if=/dev/hda bs=512 count=1 of=bootsector

A corrupt grub can't boot anything. If you can boot windows with grub, then grub is not corrupted, but maybye your partitions are, or grub.conf, and maybye you can't remember which partition number you need etc.

I'd boot the system with some sort of linux livecd to see what's going on with your partitions, and edit your grub.conf if need be.

As for how to restore a selected partition from that giant everything-backup file, I don't know. There doesn't seem to be anything like a loop device for splitting a file into partitions the way linux handles hard drives... I'll do some experimenting and see.

You can restore your boot sector easily, though, if that's the problem.

dd if=awesome-ginormous-all-in-one-universal-backup-file.img of=/dev/hda bs=512 count=1

Be sure to back it up on a flash drive or something before you do that of course.

Wait. Your backup is a drive too, isn't it. :eek:

It should have partitions /dev/sdb1 /dev/sdb2 etc. You might need to do this to get them to become visible on /dev/sdb however, since linux needs to be told when the partition table changes. Either that, or reboot after you do that cp thing to see the new partitions.

# fdisk /dev/sdb

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
#

DO NOT type 'w' if you get anything but a blank message! All you want to do is write back the SAME boot sector to make fdisk tell Linux to reread it's partition table.

Yes -- my backup is a triple *bootable* drive (thank goodness -- I can verify that everything still works).

So you gave me several dd commands but not one that would copy an entire partition from one disk to another. Could you give me an example of that? On your dd commands, how do you select a block size? Where do those other numeric values come from?

So if I'm getting a grub prompt but grub cannot find the grub.conf file (which I believe is in a linux partition) that could indicate the linux partition is corrupted.

I wonder if grub could do a dd for me? hmmm... if not, then knoppix probably could.

Let's suppose I book with knoppix. Would fdisk -l enumerate my drives?

I don't understand the dd command for copying a boot sector. Where is the destination and source specified?

Thanks,
Siegfried

I was able to boot SuSE live via a DVD I burned and mount the damaged partition. It mounted fine and all the files seemed to be intact!

fdisk -l did not complain about anything.

parted's "check 5" command gave this strange error message

Error: File system has an incompatable feature enabled.

What does that mean?

Any suggestions has to how to make this partition compatible with grub so I can boot fedora again?

Thanks,
Siegfried

Here is the scoop: /dev/sdb is the good disk which boots fedora and /dev/sda is the disk with the bad fedora partition. When I try to boot this latter partition with grub it says: Error 17 Cannot mount selected partition.

How do I restore the partition attributes so grub will boot from it again?

Thanks,
Sieg

Here is what parted says:

linux:~ # parted
Using /dev/sda
(parted) print
Disk geometry for /dev/sda: 0kB - 160GB
Disk label type: msdos
Number Start End Size Type File system Flags
1 32kB 126GB 126GB primary ntfs type=07
3 126GB 136GB 10GB primary ntfs boot, type=07
2 136GB 160GB 24GB extended lba, type=0f
5 136GB 157GB 21GB logical ext3 type=83
6 157GB 159GB 2089MB logical linux-swap type=82
(parted) select /dev/sdb
Using /dev/sdb
(parted) print
Disk geometry for /dev/sdb: 0kB - 160GB
Disk label type: msdos
Number Start End Size Type File system Flags
1 32kB 126GB 126GB primary ntfs boot, type=07
2 126GB 160GB 34GB extended lba, type=0f
5 126GB 136GB 10GB logical ntfs type=07
6 136GB 157GB 21GB logical ext3 type=83
7 157GB 159GB 2089MB logical linux-swap type=82
(parted)

You don't need special commands, the ones you already have will work quite fine. Linux gives you special files for partitions that you can use the same way you use the special files it gives you for disks. I'd always use dd instead of cp for copying whole drives or partitions but GNU cp is apparently smart enough to handle nearly anything.

For the record, copying from disk A partition 1 to disk B partition 1:

dd if=/dev/sda1 of=/dev/sdb1 conv=noerror,sync
# I'd reccomend dd over cp, dd can be told to not die on bad sectors as in above
# cp /dev/sda1 /dev/sdb1

The "conv=noerror,sync" is something important that cp does not have as far as I know... in the case of error, it will skip that sector in the input and write a sector full of zeroes on the output. cp would probably just stop on any errors.

That's what I'd think.

knoppix could, yes. Though a full-on graphical interface is probably overkill. :wink: I prefer Gentoo liveCDs, or those old little Redhat sysadm disks.

...apparently it does. I did not know that. :slight_smile:

The input file is specified with the "if=something", and the output file is specified with "of=something". If they are not given, they are assumed to be standard input and standard output. the "bs=512" tells it to assume 512 bytes per sector. The count=1 tells it to copy one and ONLY one sector. So, it copies the first and ONLY the first sector from the hard drive to file. tadah! :smiley:

If I use dd or cp, will they only copy the contents?

The contents of the misbehaving partition seem to be perfectly intact. I can boot SuSE live from a DVD and mount the partition. The problem is that grub won't recognize it as a boot partition.

Is there a way I can only change the attributes of the partition? will dd or cp change the partition attributes?

Thanks,
Siegfried

Hmm. cp or dd on a partition won't set a partition bootable, no, that's defined in the partition table, i.e. sector zero.

Something like this will set Partition 1 bootable:

# fdisk /dev/hda

The number of cylinders for this disk is set to 36483.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): a
Partition number (1-4): 1

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

# 

But are you certain that the partition you see is the boot partition? On some systems, like mine, /boot is a partition on it's own, not the root partition.

Is it true that grub.conf lives in an ext2 or ext3 and not in a windows partition?

Assume that is true: grub works but cannot find grub.conf because it cannot mount that partition. Therefor, the boot partition must be good and the fedora partition is a seperate partition that is not bootable.

Siegfried

Apparently partition magic changed the fedora partition from #5 to #4. When I type in the commands

root (hd0,4)
kernel /boot/vmlinuz-2.6.11-1.1369_FC4smp ro root=LABEL=/1 rhgb quiet
initrd /boot/initrd-2.6.11-1.1369_FC4smp.img

Everything boots fine! Hurray.

Now, why cannot grub automatically find /boot/grub/grub.conf?

When I manually issue the grub command "find /boot/grub.conf" it finds it on (hd0,4).

How can I make grub automatically find /boot/grub/grub.conf?

Thanks,
Siegfried

Grub lives in none of the above. Grub lives in the MBR itself, Sector Zero. It loads stuff like grub.conf from another partition, but the MBR is the prime mover.

Grub doesn't mount partitions since it happens outside of any OS. It can grab individual files but not much beyond that.

Or it may be looking for files in a slightly different place than you expect. What does your partition structure look like? Is boot a partition by itself? Does it have a symlink like

# ls -l /boot/boot
lrwxrwxrwx  1 root root       1 Apr 12  2005 boot -> .
#

I know mine does, and when I decided it was useless and deleted it, my system stopped booting :stuck_out_tongue:

And if that doesn't work, maybye try running grub-install again. It'll probe BIOS devices and figure out which BIOS device goes with which Linux device and hopefully configure the bootstrap code from there.