39

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.

1

3 Answers 3

35

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.

3
  • 5
    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, 2016 at 7:54
  • 1
    I needed to to a sudo grub-install /dev/sda before the sudo update-grub Nov 3, 2016 at 3:09
  • You forgot update-initramfs -u (initramfs contains the resume partition UUID).
    – rustyx
    Mar 19, 2023 at 12:16
7

My final solution to this was a combination of a number of techniques:

  1. I connected the dying drive and its replacement to the computer simultaneously.
  2. The new drive was smaller than the old, so I shrank the partitions on the old using GParted.
  3. After doing that, I copied the partitions on the old drive, and pasted them on the new (also using GParted).
  4. 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.

After all this was done, things seem to work.

2
  • Finally, this solved it for me. I had a Virtualbox disk (vdi file) that I needed to move to a smaller disk. However Virtualbox does not support shrinking a vdi file, so I had to create a new virtual disk and copy over the linux installation onto this new disk. I've spent two days trying to get it to boot.
    – j.karlsson
    Dec 19, 2019 at 9:48
  • This is the way. GParted's copy-partition feature is awesome.
    – Josh M.
    Oct 30, 2023 at 23:01
6

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.

2
  • 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, 2010 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, 2010 at 8:31

You must log in to answer this question.

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