25

I have two drives on my computer that have the following configuration:

Drive 1: 160GB, /home
Drive 2: 40GB, /boot and /

Unfortunately, drive 2 seems to be dying, because trying to write to it is giving me errors, and checking out the SMART settings shows a sad state of affairs.

I have plenty of space on Drive 1, so what I'd like to do is move the / and /boot partitions to it, remove Drive 2 from the system, replace Drive 2 with a new drive, then reverse the process.

I imagine I need to do some updating to grub, and I need to move some things around, but I'm pretty baffled how to exactly go about this. Since this is my main computer, I want to be careful not to mess things up so I can't boot.

29

You'll need to boot from a live cd. Add partitions for them to disk 1, copy all the contents over, and then use sudo blkid to get the UUID of each partition. On disk 1's new /, edit the /etc/fstab to use the new UUIDs you just looked up.

Updating GRUB depends on whether it's GRUB1 or GRUB2. If GRUB1, you need to edit /boot/grub/device.map

If GRUB2, I think you need to mount your partitions as they would be in a real situation. For example:

sudo mkdir /media/root
sudo mount /dev/sda1 /media/root
sudo mount /dev/sda2 /media/root/boot
sudo mount /dev/sda3 /media/root/home

(Filling in whatever the actual partitions are that you copied things to, of course)

Then bind mount /proc and /dev in the /media/root:

sudo mount -B /proc /media/root/proc
sudo mount -B /dev /media/root/dev
sudo mount -B /sys /media/root/sys

Now chroot into the drive so you can force GRUB to update itself according to the new layout:

sudo chroot /media/root
sudo update-grub

The second command will make one complaint (I forget what it is though...), but that's ok to ignore.

Test it by removing the bad drive. If it doesn't work, the bad drive should still be able to boot the system, but I believe these are all the necessary steps.

  • 1
    FYI to anyone viewing this these days, this does not apply to EFI setups. You need to mount /media/root/boot/efi, among other things. – wjandrea Sep 10 '16 at 7:54
  • I needed to to a sudo grub-install /dev/sda before the sudo update-grub – William Mortada Nov 3 '16 at 3:09
5

If you replace the drive right away you can use dd (tried it on my server some months ago, and it worked like a charm).

You'll need a boot-CD for this as well.

  1. Start boot-CD
  2. Only mount Drive 1
  3. Run dd if=/dev/sdb1 of=/media/drive1/backuproot.img - sdb1 being your root (/) partition. This will save the whole partition in a file.
    • same for /boot
  4. Power off, replace disk, power on
  5. Run dd if=/media/drive1/backuproot.img of=/dev/sdb1 - write it back.
    • same for /boot

The above will create 2 partitions with the exact same size as they had before. You might need to adjust grub (check macos post).

If you want to resize your partitions (as i did):

  1. Create 2 Partitions on the new drive (for / and /boot; size whatever you want)
  2. Mount the backup-image: mount /media/drive1/backuproot.img /media/backuproot/
  3. Mount the empty / partition: mount /dev/sdb1 /media/sdb1/
  4. Copy its contents to the new partition (i'm unsure about this command, it's really important to preserve ownership, cp -R won't do it!) cp -R --preserve=all /media/backuproot/* /media/sdb1
    • same for /boot/

This should do it.

  • It turns out that the new "40GB" drive I'm trying to install is smaller than my current "40GB" drive. I have both of them connected, and I'm booted into a liveCD. Is there an easy way to just dd from the old one to the new one, and call it a done deal? – mlissner Sep 4 '10 at 3:02
  • Actually, I sorted this question out. The partition that was too big was an ext4 partition, so I booted a liveCD, and used gparted to shrink it a bit. – mlissner Sep 4 '10 at 8:31
4

My final solution to this was a combination of a number of techniques. I connected the dyring drive and its replacement to the computer simultaneously. The new drive was smaller than the old, so I shrank the partitions on the old using gparted. After doing that, I copied the partitions on the old drive, and pasted them on the new (also using gparted). Next, I added the boot flag to the correct partition on the new drive, so it was effectively a mirror of the old drive.

This all worked well, but I needed to update grub2 per the instructions here: https://help.ubuntu.com/community/Grub2/Installing#Reinstall_from_the_LiveCD

After all this was done, things seem to work. Thanks to everybody for their help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.