Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Transferring linux from one hd to another

I did this to transfer an OpenSuSE (Leap 42.1) system but should basically work for others. Partly going off the instructions here but deliberately not modifying the drives' uuid because I disagree with that approach (you might want to continue using the old drive for something else for one thing).

Boot into rescue system/live cd with current/old disk and new one both in the system. I create a directory for the old system and mount whatever partition looks like the right one to it.

mkdir old
mount /dev/sdc2 old

Double-check the contents to make sure it looks right. Also repeat these instructions as needed if you have multiple partitions used by the same system but for me I just had root and swap. Now run fdisk to create the partitions as needed on the new disk. Then create filesystems as needed.

fdisk /dev/sdb
# Edit as needed
mkfs.ext4 /dev/sdb2
mkswap /dev/sdb1

Mount the new partition(s) and copy files from old to new:

mkdir new
mount /dev/sdb2 new
cp -afv old/. new/

/etc/fstab and the grub installation need the ids of the disk. Actually you could do things the old-fashioned way in fstab but it's more robust to specify the exact disk so adding/removing disks doesn't stop it from booting properly. So run

ls -la /dev/disk/by-id/
cd new # let's switch here to make the commands more easier

And look at what entries symbolically link to the new disk you want switch to. Then edit /etc/fstab as needed to point to the new disk. Since I don't have X, I use tab completion of the exact disk ids, then edit the command into a statement that writes to the end of /etc/fstab so I don't have to write it down.

echo '/dev/disk/by-id/XXXXXXXXXX-part1' >> etc/fstab

I ran it twice for me for / and for swap and edited the file to look right. I then also copy this id into the grub config file, boot/grub2/grub.cfg. You will also need the uuid for the disk as a whole to replace the current value in grub.cfg.

ls -la /dev/disk/by-uuid/

Now search and replace everything in grub.cfg that uses the old drive id (you can't miss it, it's a big ugly uuid) with that for the new drive and replace references to partitions by id with the new ones as you did in fstab. I use vim and just use its search and replace (:%s/search/replace/g) for both of these replacements. Save the file, get out and write the new bootloader. You'll need access to devfs and so forth but also chroot, so mount the directories as needed:

mount --bind /dev dev 
mount --bind /proc proc 
mount --bind /sys sys
chroot .
grub2-install /dev/sdb

Then get out and unmount everything just to make sure.

umount dev proc sys
cd ..
umount old new

Then reboot and don't boot into the live cd of course. You may need to change to boot to the new disk in the bios as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.