Move root filesystem HOWTO
According to much research, the correct procedure to move the root filesystem from one partition to another on the same drive is described in my howto below (incomplete).
It doesn't work (despite much of it coming from searching this forum!).
Can anyone help filling in the blanks? It would be a great resource here once completed.
Here we go...
HOWTO move the root filesystem from one partition to another larger existing unused partition on the same drive (maybe we can remove some of these restrictions)
The major steps are:
A. Move the physical location of the entire root filesystem
B. Change the pointer in Stage 1 of the boot loader so it points to the new Stage 2 location
C. Change the pointers to your root filesystem in grub.cfg
D. Change the list of partitions to mount at boot
So, just four little things to do.
Step A: Move the root fs
A1. Boot from a live cd. The live cd need only have __________ on it, and it must leave you in a terminal. Almost any live CD will do.
1b. If your live cd does not make you root, then do this:
sudo -i (this is needed for the chroot later)
A2. Mount source and destination partitions
mount /dev/srcpartition /mnt/src
mount /dev/destpartition /mnt/dest
A3. Clean the destination
rm -rf *
A4. Confirm the destination is empty
The count shown should be ___________. It is not supposed to be zero because _____________
A5. Copy the files from src to dest, taking care to preserve attributes (tar is often suggested, or cp -a)
tar cfp - ./* |(cd /mnt/dest; tar xvfp -)
A6. Verify the copy
The counts provided in the two df commands above should be exactly the same
The counts provided by two du commands above should be the same or different by no more than _________ files. Minor differences are due to ____________, which we don't care about.
In my case, this went perfectly, according to df -i
Step B: Reinstall the Stage 1 boot loader so it points to the new location of Stage 2
B1. Reinstall stage 1, which lives in MBR of this drive.
By moving the root filesystem, you have moved the stage 2 file of the boot loader, leaving stage 1 pointing to the wrong place. The stage 2 file is going to be /boot/grub/__________ on the destination partition. Stage 2 also may load some of a bunch of other files, but we don't have to worry about that. They are all in /boot/grub of the destination partition, and since we have already copied them across, we can forget about them.
We're going to chroot, but will need a real /dev dir, and since /mnt/dest is not an active root fs, there is nothing real in its dev directory. The grub-install command needs a real /dev dir, so we must first make it available:
mount -o bind /dev /mnt/sys/dev
grub-install --boot-directory /mnt/dest/boot /dev/sda
This command will read /etc/defaults/grub or /etc/grub/grub.conf (depending on your distro) to get certain options, read all your partitions looking for a files called ___________, which it will consider to be canditates for the boot menu, then finally write into the MBR of the /dev/sda a small program that tells the BIOS where to look for the stage 2 of the boot loader (there is no stage 1.5 in grub2). Stage 2 of the bootloader is in a file called ____________ and must be located in ___________. You have already put it there when you copied the fs above.
If the output says the --boot-directory option is not recognized, do this ______________
In my case, the command exited with no errors printed, but when I reboot, it just goes straight into the old partition as if I had done nothing at all.
Step C: Fix the bootloader's config file
C1. Rewrite grub.cfg
grub.cfg is used by grub to present the boot menu and to tell grub how to get the systems listed in the boot menu to become your root directory and which kernel file to use (ie which OS you will have).
In grub.cfg, there are pointers to the partitions, so this file must be rewritten. You cannot do it by hand, it is mind-boggling complex (according to the devs) and needs a entire directory of scripts to create this file. However, there is nothing in the scripts that affect the references to the partitions, so you must ____________
Now run a command that does the rewrite for you:
In my case, the command exits with no printed errors, but there has been no change to grub.cfg (old or new), not even a change in its timestamp.
Step D: Fix the list of partitions to mount at boot
D1. Edit /etc/fstab
Change the line for the root fs from that horrible UUID to a simple /dev/sda1, or wherever you chose to put it.
Reboot. Confirm with cat /proc/mounts that your intended new location is mounted. Check also with df -h to see if you're in the bigger place.
In my case, it always boots into the old partition.
Steps you might have thougt we'd have to do, but do not have to do:
a. edit /etc/default/grub: there's nothing in here that concerns the location of the root fs or anything about partitions
b. edit any of the scripts in /etc/grub.d: ditto
c. Manually edit /boot/grub/grub.cfg: no need, in fact not allowed. Done by update-grub for you.
How to create a test environment like the one I used:
1. Remove all drives from the test machine and install one new blank drive. Or do whatever you like to get to this state (eg fdisk to remove all partitions).
2. Boot from Ubuntu 10.04.01-server (any other will also work)
3. Install. When the partition questions come up, use the whole drive and let it choose partitions and their sizes.
4. Confirm your machine boots into the new installation. Note where your root fs is (eg /dev/sda1), and touch a marker file, eg touch /var/run/thisisthefirstinstall, so you can be sure later which is which.
5. Boot again from the install CD
6. When the partition questions come up, choose to install into an existing partition other than the one your first root fs resides in, and let the partitioner do the rest on automatic (it will split your selected partition into at least two, one for root fs and one for swap).
7. Confirm your system boots into this new fs. Touch some marker file somewhere eg /var/run/thisisthesecondinstall.
Now, assume you want to keep the second, and move it to the larger partition, where the first resides. Follow the howto above.