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
Earlier:
/dev/hdc7 / ext3 noatime 0 1
Now:
/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)
root (hd0,7)
kernel /boot/kernel-genkernel-x86-2.6.14-gentoo-r5 root=/dev/ram0 init=/linuxrc ramdisk=8192 real_root=/dev/hdc6 udev
initrd /boot/initramfs-genkernel-x86-2.6.14-gentoo-r5
savedefault
bootI 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.
Related Posts:
Readers who viewed this page, also viewed:
- Setting Up Rails Development Environment on Fedora GNU/Linux
- Installing Sun Java SE 6, Apache Maven 2 and Tomcat 5.5 on Ubuntu GNU/Linux
- Step 3(a): Merging(Hardcoding) Subtitles With AVI(DivX/XviD) Files Using AviRecomp
- Another system rescue story, using Ubuntu LiveCD
- Maemo SDK VMWare Appliance 0.4 Released With Lot of Goodies
If you installed your system on top of the logical volume manager (LVM) you can even move your running system to different hard disks.
QuoteComment by Patrick Kursawe — September 25, 2006 @ 5:04 pm
The classic way to do this (before the introduction of the new, generalized “tar”s) was something like:
tar -p -m cf - foo | (cd bar; tar xf - )
FWIW
QuoteComment by Carlie Coats — September 25, 2006 @ 6:01 pm
I recently was researching the best way to do this very same thing. I first thought about cp, but I wasn’t sure how well it would copy the file permissions and all that good stuff. I stumbled across a how-to using tar, funny thing is, it was another Gentoo user. I also happen to be an avid Gentoo user too, have been for two+ years now.
It basically works the same, not sure if it is faster. I would assume not since you then have to un tar it, but I am sure you could just pipe it if you would like too. That or don’t use a tar file. Either way, the -p is to keep file permissions.
cd /mnt/oldgentoo
tar -cvpf /mnt/newgentoo/sys.tar .
cd /mnt/newgentoo
tar -xvpf sys.tar
For me, I also have to copy over my boot folder, which I always put on another partition. It is done the same way.
It worked for me, sort of. If it weren’t for the 40 GB drive I was going to deciding to fail, it would have. ;)
I am looking at getting a 250 GB to dedicate to my main Gentoo system, so I will be trying this again. Maybe I will play with it, see what works the best. I could even time it to do a better compare.
QuoteComment by zargon — September 25, 2006 @ 6:22 pm
Why are you using a Genkernel? They’re HUGE! If you can do this (and understand it) you can easily make your own kernel.
Quote-Aaron
Comment by Aaron Lynch — September 25, 2006 @ 6:45 pm
This works well if the Grub bootloader remains on the same drive and thus the machine remains booting from the same drive. I’ve been looking for help when the boot drive changes (or is removed in my case due to a failing drive.) Anyone know how to handle moving (or reinstalling) the Grub bootloader on a new drive? That way I could (a) move the boot partition per the post above to a good drive, (b) reinstall the boot loader with the right config, and then (c) remove the old drive. Thanks, Jason
QuoteComment by Jason — September 25, 2006 @ 7:47 pm
I have not restored grub on a PC just yet, but this should be simple.
I am trying to think here if the LiveCD of Gentoo has grub or not, I would think it does. If that is the case, just parition the drive like you normally would. If you have a mix of SATA and IDE drives, I would look up which acts as drive 0, 1, 2, etc.. over the other. I do not know if sda would be drive 0 over hda.
Run the command: grub
Next you need to tell grub where to go, run: root hd(0,0)
(The first number stands for the drive, hda = 0, hdb = 1, hdc = 2, etc.. The next number stands for the partition, hda1 = 0, hda2 = 1, hdb3 = 2, etc… If you follow the Gentoo Installation Handbook, it would be 0,0]
Next you need to tell it what drive to use to install grub in the MBR, run: setup (hd0)
[Like the previous command, the number means which drive it is, hda = 0, hdb = 1, hdc = 2, etc…).
The quit, run: quit
From here, you will want to edit your grub.conf file in /boot/grub/grub.conf. Edit this how ever you need to for your system.
That should be it, if your old drive is dead and no longer works, this new drive should be hda or sda if SATA so there should be no problem. Just make sure that you use the right drive or else the system won’t boot.
QuoteComment by zargon — September 25, 2006 @ 8:28 pm
tabrez on September 25, 2006 at 8:41 pm said:
This was the first Gentoo that I had ever installed, so I went completely by the instructions in the Gentoo manual. From the second time onwards though, I have always compiled my kernel manually as genkernel doesn’t allow too many customisations to be done very easily. Even on this particular system I have got many kernels that I have built without genkernel, but for some reason they are all giving me one or the other problem. I hope to get to a less-experimental and more stable kernel very soon.
QuoteComment by tabrez — September 25, 2006 @ 8:41 pm
tabrez on September 25, 2006 at 8:58 pm said:
It shouldn’t be difficult to reinstall the GRUB boot-loader on a different hard drive(LILO makes it slightly more complicated). Just follow the above procedure to move the entire GNU/Linux system to the new hard drive first, and then while making changes to the /boot/grub/menu.lst file(on the new hard drive), enter the details of the new disk and partition number([hd0, 0] for first partition of first disk, [hd3, 7] for 8th partition of 4th disk etc). Then you just need to run the following command(by getting into the system using a Live CD or some other way):
sh# grun-install /dev/hda
hda might be hdb, hdc, sda etc depending on the configuration of your computer.
QuoteComment by tabrez — September 25, 2006 @ 8:58 pm
next time use rsync it’s safer
QuoteComment by jorge.vargas — September 26, 2006 @ 5:17 am
Additional tip:
You could use your regular system to copy. Just mount your (root) with the bind option:
#mount -t bind / /mnt/oldroot
and then, copy. If you are so worry with the RW:
#mount -o remount,ro /
QuoteComment by Winckler — September 26, 2006 @ 9:00 am
[...] Every Flavour Beans » Moving A GNU/Linux Installation To A Different Partition (tags: linux howto partition tutorial installation sysadmin moving reference unix tutorials install image backup article boot) [...]
QuotePingback by its about time» Blog Archive » links for 2006-09-26 — September 27, 2006 @ 5:48 am
[...] Moving A GNU/Linux Installation To A Different Partition (tags: linux howto partition tutorial sysadmin moving installation) [...]
QuotePingback by links for 2006-09-29 « Where Is All This Leading To? — September 29, 2006 @ 6:25 am
[...] Moviendo GNU/Linux a otra partición: Si te acabas de comprar un nuevo disco duro y no quieres perder la instalación de tu sistema, con este artículo podrás hacerlo. [...]
QuotePingback by Kernel Source » Artículos de la semana (25-30 Sept 2006) — October 2, 2006 @ 2:06 am
[...] Instructions on how to move a working GNU/Linux operating system to a new partition, then boot from it and continue to use it as nothing has ever happened to it! Using just a simple ‘cp’ command! Beat that Microsoft Windows read more | digg story [...]
QuotePingback by EveryDigg » Blog Archive » Moving A GNU/Linux Installation To A Different Partition — October 4, 2006 @ 5:48 pm
[...] Instructions on how to move a working GNU/Linux operating system to a new partition, then boot from it and continue to use it as nothing has ever happened to it! Using just a simple ‘cp’ command! Beat that Microsoft Windows read more | digg story [...]
QuotePingback by The Inveterate Observer » Blog Archive » Moving A GNU/Linux Installation To A Different Partition — October 12, 2006 @ 4:25 pm