34

I am upgrading the internal SATA hard drive on my laptop from a 40G drive to a 160G drive. I have a Linux/Ubuntu desktop which has a SATA card. I would actually like to do the same thing for a couple CentOS & FreeBSD boxes at work, and it seems this would have the same solution.

I've heard that I can use DD to mirror the 40G partition to the 160G drive, or that I can save the 40G partition as an image on my local system, and then copy that 40G image to the 160G drive.

Can anyone describe how I may do this? Do I need any other utilities, such as gparted

2
  • 7
    Ahha... This is the first question!!
    – Harshit
    Nov 24, 2015 at 9:50
  • 7
    Question Number One! I'm also the proposer for this site, and I thought I'd kick off the site with a fairly technical question. I was rewarded with a T-shirt and some swag. Nov 24, 2015 at 19:35

7 Answers 7

18

Normally I would suggest a solution such as "hook up the 2nd hard drive using an external enclosure, boot from a linux CD, then use a command such as dd if=/dev/sda of=/dev/sdb bs=1G, but since you want to use the same technique for work, I have what may be a better solution.

All of my servers and laptops get imaged at work using Clonezilla. There are two ways of using it...one of which uses a dedicated server and is probably overkill for you, and another which utilizes a boot CD and external hard drive.

The idea is that you boot in with the Clonezilla CD and have a largish (bigger than the source drive) external USB drive. Clonezilla walks you through making an image of the existing drive, after which you power down the machine, replace the drive, then boot back into Clonezilla, and it walks you through restoring the data.

This gives you the opportunity to A) put the image on a bigger drive, and B) retain a backup of the data.

7

Your first task would be to connect both disks to an existing Linux system or connect the new disk to the original system.

You must be very careful since it is very simple to copy the blank disk on top of the good disk!

To end up with the boot sectors and all, you would do something like:

dd if=/dev/hdx of=/dev/hdy

Where hdx is your 40G disk and hdy is your 160G disk. You will notice there are no partition numbers like /dev/hdx1. This copies the entire disk, partition info and all.

Your new disk will just like the old disk, 40G allocated. It should boot right up when placed back in your laptop. Hope you used LVM? Otherwise hope you did not use all the partitions? Getting past this point requires a lot more info.

Another solution is to dump each individual partition. This requires a lot more situation awareness since you will need to recreate the boot information.

All of this is best used for cloning computers, not upgrading hard disks. It is much better to restore to a new installation using your backups.

1
  • so only the first 40G is copied to 160G disk? I can't exactly understand what you mean in this sentence : "Hope you used LVM? Otherwise hope you did not use all the partitions? Getting past this point requires a lot more info." I guess if we can use gparted to see exactly to what segments the partitions were placed, then, we could create a new partition in the new 160G disk from the new segment(after the existing 40G segments).
    – Chan Kim
    Oct 21, 2020 at 11:56
6

While you can use dd to copy a disk like that, doing so has a number of drawbacks:

  1. The destination must be exactly the same size or larger than the source
  2. After copying, you will need to resize the partitions to use any additional space
  3. You will waste time copying free space
  4. Any fragmentation present in the old disk is preserved

Using an imaging program like Ghost4Linux, partclone, or clonezilla at least takes care of numbers 2 and 3. You can also simply format the new disk, mount it, and copy all of the files over with cp -ax ( as root ), and then reinstall the boot loader on the new drive. This method does not suffer from any of the above drawbacks.

1
  • hi, can you tell me how to reinstall the boot loader on the new drive? (assuming the first partition has the kernel image, say..) I know in UEFI, the bootloader is located in the MBR + (space between MBR and the first partition).
    – Chan Kim
    Oct 21, 2020 at 12:03
3

To simply copy the partition, you can use dd if=/dev/srcDrive of=/dev/dstDrive or something like this. I would recommend you to read its man page.

2

You asked how to do it with dd, but I had better success piping the output of dump into restore. Given the source ad1s1a and the target ad2s1a:

$ mount /dev/ad2s1a /target
$ cd /target
$ dump -0Lauf - /dev/ad1s1a  | restore -rf -

I tried this on FreeBSD, actually I found it on the FreeBSD Forum

1

One simple example is this:

dd if=/dev/sda of=/dev/sdb

But if you have some special needs, you really should read the manpage (man dd) or search on Google.

Another idea could be the use of rsync (don't forget to set the right options, like -az [packages the files instead of copy one file after another] or --numeric-ids [uses the uid/gid instead of names like "root"] and maybe some others). The link contains many examples.

If the new HDD doesn't have partitions, you can use gparted or palimpset.

When you're unsure I would format the HDD and then sync the data with rsync.

1

Well, I have done migrations similar to this by booting with both disks attached and a Live cd.

  1. You recreate the partition info of the first disk in the second one, possibly making some partitions bigger to use the extra space, and so.
  2. Then suppose you have /dev/sda{1,2,3,4} and want to copy them to /dev/sdb{1,2,3,4}, you mkdir /mnt/sd{a,b}{1,2,3,4} and mount each partition in each dir (or if you want to do it one by one you can make a /mnt/origin /mnt/destination and mount/umount each pair)
  3. Then you do a cp -avr /mnt/origin/. /mnt/destination/ (or a cp -avr /mnt/sda1/. /mnt/sdb1/
  4. Wait while you browse the web :)
  5. Remember yourself to reinstall grub, lilo or whatever bootmanager you use.

Doing it that way you will defragment the files as they are copied, you can also change the filesystems in the partitions (migrate from reiserfs or ext3 to ext4, etc.), but please, remember to edit /etc/fstab after copying to suit the new situation.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .