Changing distribution remotely -- HOWTO
How to completely change your distribution on a remote box without a boot disk, with only ssh access, using only one partition, and with only one reboot at the very end of the conversion process. And by completely, I mean completely. You can delete every single file of the old distro if you want.
Why would you ever need to do something like that?
Well, a lot of hosting companies offer only the software configuration of their choice. On top of that, most of them would not agree to send someone to the datacenter to be on the phone with you while inserting/changing CDs to help you install a distro of your choice.
It's easier to completely switch a linux distribution if the original configuration includes a swap partition. That partition can be reformatted to contain a fully-working system. Then, it can be used for booting up to let you free up the main partition(s).
But if there is no "spare" partition for you to use, of if it's too small for you to be comfortable in the future, or if you want to completely change the partition table, then here is how to do it.
For this example, I am going to assume one large root partition, but you can just as easily perform the switch with multiple partitions.
The original distribution is not important, and the new distribution I picked is Debian.
You can use any distribution you want, but I used Debian because it has a debootstrap package, which can be used to create a basic installation in a chroot.
I'm assuming that you have your distro prepared in a file called newinstall.tar. It can either be prepared elsewhere and uploaded to your remote box or prepared right on the box.
Turn the swapping off and make sure it's off.
# swapoff -a
# free
free should show swap 0. You only need to turn the swap off if you plan on changing something about your existing swap partition or if your swap is a file on the root partition.
Shut down everything you can except for sshd (and except for system processes of course).
You need to kill running processes until you can remount the root in read-only mode.
Keep killing processes that you don't need until you can successfully run this command:
# mount -o remount,ro /
If you get
mount: / is busy
it means some process has a file open for writing on /.
You can use fuser and lsof to see which files are being accessed.
Once you have successfully remounted root in read-only mode and made sure there are no active processes writing to files on the root partition, remount it back in the read-write mode and move on to the next step.
# mount -o remount,rw /
Create a directory for your new distribution and put it on a temp drive. I'll use /newinstall and assume that the new distro is in /newinstall.tar.
# mkdir /newinstall
# mount -t tmpfs -o size=230M tmpfs /newinstall
You should use a temp drive so that you can safely mess with your root partition. If you don't want to change the file system type of the root partition and prefer to selectively delete directories of the original distro then you should be fine without using a temp drive.
Extract your new distro into the newinstall directory.
# tar xfp newinstall.tar
Chroot into your new distro.
# chroot /newinstall
# mount -t proc none /proc
Once inside chroot, update your /etc/fstab to what it's supposed to be once your new distro is running. In the case of a single large root, you should have something like:
proc /proc proc defaults 0 0
/dev/hda1 / ext3 defaults,errors=remount-ro 0 1
/dev/hda2 none swap sw 0 0
Set up the kernel, network and disk drivers, boot loader, initrd and anything else that you need to make sure your new distro can boot. I'm not going to cover the details here because each of those tasks is well-documented elsewhere.
If your kernel uses initrd then make sure to include the correct modules for your disk drives, the network card and run mkinitrd.
If you are using grub then run grub-install and edit menu.lst.
One everything is ready and you are sure your new distro can boot, exit from chroot.
Try to remount root in read-only mode again just to be safe and remount it back to read-write.
Here comes the magic.
Change the running root of your system to your new distro.
# mkdir /newinstall/oldroot
# pivot_root /newinstall /newinstall/oldroot
# mount -t proc none /proc
At this point, your new root is in /newinstall and your old root (the real /) is in /oldroot.
If you want to change anything on your root partition (besides simple rm) then fix up your /etc/mtab with the data from /proc/mounts adjusted for your oldroot. If /dev/hda1 was / then put it in mtab as /dev/hda1 /oldroot.
Go to oldroot and remove everything you don't need.
In my case, I removed everything except for /proc and /sys. You might also want to leave /dev/shm alone. Everything else can be removed at this point.
Once your oldroot (which is the real /) is empty or in the state you want it to be, copy your new distro out into the oldroot.
You might want to specify all directories separately to avoid error messages of oldroot not being copied onto itself.
# cp -a -i /bin /etc /dev /usr ....... /oldroot/
Do not include /proc and /sys in your copying command. They should still exist on the oldroot. If you changed your file system and it's now empty then create those directories.
If you left more than a few things (like /dev/shm) on the oldroot and don't care about overwriting them then don't use -i with cp.
Once you are done copying, verify that your settings are correct and the system is indeed ready to boot. Make sure sshd is configured in init.d to start at boot and your networking configuration is correct.
Reboot.
# shutdown -r now
If the unimaginable happened and your box had booted successfully then consider yourself lucky. You are now running your new distro on a "clean" install.
Disclaimer: Use any information on this page at your own risk. There is a good chance that your system will become non-bootable and/or you will lose your data. As a matter of fact, if I had to bet money on the outcome of your distro change, I would bet against you. This is a very simple yet dangerous procedure. You have been warned.
THIS INFORMATION IS PROVIDED TO YOU "AS-IS". NO WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED, ARE MADE TO YOU AS TO THE CONTENT OF THIS PAGE OR ANY MEDIUM IT MAY BE ON, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
I DISCLAIM ALL RESPONSIBILITY FOR THE ACCURACY AND RELIABILITY OF INFORMATION ON THIS PAGE.
Tuesday, September 18, 2007
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment