Moving A GNU/Linux Installation To A Different Partition
When I had first installed the Gentoo GNU/Linux operating system, my initial plan was to use it just for a couple of weeks before overwriting it by installing some other new distribution from Distrowatch. But I started to like it so much in less than a month’s time that I replaced Debian Sid with Gentoo as the primary operating system on my personal desktop(the server still runs Debian Sarge). Almost an year now, I haven’t regretted the decision ever. What I did regret a few days ago, though, was the amount of disk space that I had allotted for Gentoo during its installation – a meagre 11GB partition! Mortals like me lack the foresight required in such situations.
I had never considered ‘moving’ an installed operating system to a different location until then – I thought it to be such a lousy way to lose the oppurtunity for a fresh installation of the OS from the scratch(avoiding the mistakes that were committed the last time). But installing a Gentoo system from the sratch! Heavens forbid. It had taken 3 days for me to get the a bare minimum Gentoo system up and running, nothing more than the GNOME desktop manager. Another 4-5 days to install the most basic applications. I was not prepared at all to go through similar 1 week of downloading, compiling and installing the packages again by starting from the scratch. I decided to move my existing Gentoo system to a new, bigger partition. Not just the data but the entire bootable operating system.
None of my reservations regarding moving an installed system to a new location turned out to be true after a quick google search about them: all one needs to accomplish this task is a new, empty partition and atleast a GNU/Linux Live CD to run the basic unix commands. I confirmed that this is indeed correct information and won’t result in an unusable system by getting help from folks on the linux IRC channels. I had only two things to confirm:
- If there is a way to ‘cp’ files which preserves the file permissions for the copied files, right? Yes. ‘cp -a’
- If /proc kind of directories which store special, non-data files will be re-created at the boot time and won’t create any problems while they are copied(or if they are skipped from the copy operation)? Yes.
Thanks to the folks on the IRC channels, both my doubts were clarified to my satisfaction(one must appreciate the level of depth that they are ready to go to explain such stuff, I got more than what I had asked for :) ) and I was well set to finally begin the process of moving my Gentoo system from the 11GB partition(/dev/hdc7) to a 40GB one(/dev/hdc6, which I had earlier formatted with ext3 filesystem).
- I could have tried to use GParted Live CD to resize the Gentoo partition if there were any free space available *after* my Gentoo partition. Unfortunately, /dev/hdc6 was the last partition on my third hard disk.
- Another way to accomplish the same task would be to use the dd command. But remember that it copies the empty(non-data) space also and hence would be very inefficient if you have very less occupied space on the partition. This was not the case with me, but I still thought that cp is a better bet for my case.
I booted into my Kubuntu system(can be done from any Live CD) and followed the procedure listed below to complete the process of moving my Gentoo Installation from /dev/hdc7 to /dev/hdc6:
- Unmounted /dev/hdc7 from /media/gentoo and re-mounted it with read-only option, just to be on the safe side(those who are attempting to do something like this *must* take a complete backup of their entire partition before beginning).
sh# sudo umount /media/gentoo
sh# sudo mount /dev/hdc7 /media/gentoo -o ro
- Mounted the partition to which I want to move the Gentoo system.
sh# sudo mkdir /media/newgentoo
sh# sudo mount /dev/hdc6 /media/newgentoo
- Copied the files from the old partition to the new one.
sh# sudo cp -a /media/gentoo/* /media/newgentoo/
The ‘-a’ option tries to preserve as much structure of the filesystem as possible(ownerships, permissions, symbolic links etc). Consider if ‘-x’ is also required in your case.
- Did one modification to the /etc/fstab file in /media/newgentoo(/ should now be /dev/hdc6).
sh# sudo emacs -nw /media/newgentoo/etc/fstab
/dev/hdc7 / ext3 noatime 0 1
/dev/hdc6 / ext3 noatime 0 1
- Modified the GRUB configuration file to add an entry for the new Gentoo partition. Did not remove the entry of the old Gentoo system for now and decided to remove it once my new system boots correctly.
NOTE: The grub installed on my MBR belongs to my Kubuntu installation. All the other GNU/Linux systems and the Windows operating systems have their boot loaders on their respective partitions whose entries I have added to the grub configuration file present in Kubuntu.sh# sudo emacs -nw /boot/grub/menu.lst
title Gentoo GNU/Linux(New)
kernel /boot/kernel-genkernel-x86-2.6.14-gentoo-r5 root=/dev/ram0 init=/linuxrc ramdisk=8192 real_root=/dev/hdc6 udev
I actually made a copy of my previous entry and just changed the value of ‘real_root’ field from ‘/dev/hdc7 udev’ to ‘/dev/hdc6 udev’. The strange looking values of the rest of the fields owe to the fact that I have my kernel and initramfs images copied in /boot directory of my Ubuntu system(and hence needed no modifications). The Grub boot loader reads these files from there and then mounts /dev/hdc6 as the ‘real root’ device.
That’s it. I booted into the new Gentoo system by selecting its entry from the boot-loader menu, and soon the all familiar Gentoo desktop I had gotten used to for the past one year was ready to go. No hiccups, no troubleshooting and no tweaking was required. The only observable difference from my old Gentoo system was that I had a lot more free disk space now! I will continue to use the old system for some more time to do couple of testings(which I was afraid to do earlier) before erasing it to install another GNU/Linux operating system over it. With RC5 already available, Slackware 11.0 seems to be just around the corner ;)
For those who don’t have another GNU/Linux OS installed on their computers or don’t have the luxury to boot from a Live CD(for remote systems, for example) to perform the copying operations, the copying can be done from the running system(the OS you want to move) too – with a litttle more caution. A lot of information on this can be extracted from the Gentoo wiki pages(applies to all distributions, not just Gentoo) – Google search should take you there.