I decided to generally upgrade some of the hardware in my home-server, which consisted mostly of salvaged parts. This required me to move the root partition to another harddrive. Sounds like a daunting task, but it actually isn’t such a hassle.
Here is how I moved my Linux root from one drive to another in Ubuntu Server 12.04 LTS, and was able to (finally) throw out my old IDE drive.
Disclaimer: I cannot guarantee that this procedure will work for you as it did for me. You could lose data or break your system! I can recommend virtualizing your server, which will eliminate the need to move the root partition since you can just move the image of the server. If you have the time – look into virtualization now and skip this moving partitions nonsense.
Once the new harddisk is installed, check which drive you are going to partition before you partition it, if you mix them up you might destroy important data. Most of the commands below require root access, so you might want to run sudo -i
beforehand to save you some time. Just make sure you don’t make any mistakes
First, create a partition table on the target device (replace X with the drive letter) using parted
(recommended) or fdisk
(doesn’t support GPT tables for drives >2TB):
parted /dev/sdX
Setup the partition table as you like (I have 3 partitions: /
, /home
and swap
). When you are done, create the filesystems:
mkfs.ext4 /dev/sdX1 mkfs.ext4 /dev/sdX2 mkswap /dev/sdX3
Create a temporary mountpoint for the home partition (in my case /mnt/home
) and mount it. Then copy over the home directories using rsync
. (skip this if you don’t have a separate partition for /home
)
rsync -avP /home /mnt/home
Create a temporary mountpoint for the new root partition (in my case /mnt/newroot
) and mount it.
Clone special directories using rsync
(alternatively you can just create empty dirs with mkdir
)
rsync -vd /sys /mnt/newroot/sys rsync -vd /proc /mnt/newroot/proc rsync -vd /dev /mnt/newroot/dev rsync -vd /tmp /mnt/newroot/tmp
Mount special dirs
mount -t proc proc /mnt/newroot/proc mount -t sysfs sysfs /mnt/newroot/sys mount -t devtmpfs devtmpfs /mnt/newroot/dev
Copy root using rsync
, excluding special dirs and /mnt
.
We need to exclude /mnt
or else we will recursively copy the drive into itself until it is full. Also we exclude /home
since we already copied that to another partition.
rsync -avP / /mnt/newroot --exclude /mnt --exclude /proc --exclude /sys --exclude /dev --exclude /tmp --exclude /home
wait…
Now we have create any custom mountpoints that we excluded manually. /home
for a first, and I also had one in /mnt
.
mkdir /mnt/newroot/home mkdir -p /mnt/newroot/mnt/3t0disk
Now chroot
to the new root dir, this is the first sign that things are going well
chroot /mnt/newroot
/etc/fstab
To edit /etc/fstab
, we first need the UUIDs of the partitions (Note that these change whenever we re-partition a drive). We get those by running the following command (again, replace X with your drive letter):
blkid /dev/sdX*
Example Result:
/dev/sdb1: UUID="e9864048-28bb-4a90-91be-9fbb12d33454" TYPE="ext4" /dev/sdb2: UUID="9faebeef-a442-4d8d-a1dc-a563bcdc29ad" TYPE="ext4" /dev/sdb3: UUID="3f076b89-763c-4624-a30f-370649e21275" TYPE="swap"
Now change /etc/fstab
entries to reflect UUID changes in an editor of your choice.
Bootloader
To enable booting from the new disk, install grub bootloader on it (note: --recheck
is not recommended, use only if required):
grub-install --recheck /dev/sdb update-grub
If this fails, check out my issues section further down.
Finishing off
Shut down the server, unplug the old drive and try booting it up again. I recoomend keeping the old drive for a while and checking if everything is still working correctly. That way you can always revert to the old setup.
If everything works fine, thats great! If not – continue below
Issues:
Initramfs error -> No such UUID
For some reason – and maybe someone can clear this up for me – grub did not update the menu.lst
with the new UUID for the root=
parameter. An incorrect UUID causes an initramfs
error. All I had to do was replace the incorrect UUID with the correct one and I was good to go.
Just open up /boot/grub/menu.lst
in your favorite editor and change the UUID to the one of your root drive.
Installing Grub fails:
Maybe it was the faulty motherboard (needed it for the old IDE root disk) but if you have any issues installing the bootloader, try using a livecd to install grub on the new disk. There is a good step-by-step guide on the ubuntu community help: https://help.ubuntu.com/community/Grub2/Installing#via_ChRoot
Links:
Here are some older articles that I used as a reference in my research:
- http://www.techrepublic.com/article/solutionbase-moving-linux-from-one-disk-to-another/
- https://forums.virtualbox.org/viewtopic.php?f=3&t=39292
- http://riceball.com/d/content/moving-your-linux-root-partition
Thank you for this article. I was able to move my Ubuntu Server 14.04.1 LTS to a different HDD with this guide.
I’m glad it worked for you too
Thanks for this tutorial. I only wanted to point out regarding
Sorry, disregard my comment. Here it definitely is necessary to exclude it.
rsync has this very useful option
-x, –one-file-system don’t cross filesystem boundaries
So your copy command could be just
rsync -avPx / /mnt/newroot