Saturday, August 21, 2010

Moving Windows the easy way

(WARNING you need to take great care in this process and know which is your original disk. This example assumes the /dev/sda is the disk with your working operating system and data on and /dev/sdb is the one that is going to be the new one).

Having recently moved my back up disk to a larger sized disk thinking the old one was failing (even though it turns out that its the small electronics board that's playing up), I thought I'd move my old back up disk (250GB) into my laptop which had a 120GB drive and was telling me that it had a bad sector. So here's the problem (or rather not for someone who seriously uses Linux).

Partition 1 - Windows
Other partitions are Linux based file systems.

Most sites will tell you that you can't just move Windows from one disk to another without having to do an ASR (Automated Systems Recovery), well that's just a load of BS and comes from a bunch of I.T. people that really don't understand computer systems properly.

So here is how it's done without having to do anything apart from move the partitions from one disk to another using a real tool.

Preparing the disk
  1. First acquire a copy of Linux. Any one will do that will allow you to boot a command line via a rescue system. I used Fedora 12 DVD
  2. Boot the Fedora 12 DVD with only your main drive (still in the system) attached
  3. At the options menu select the Rescue option, but press the TAB key and delete the entire line
  4. Replace the line with linux rescue and press enter
  5. Accept the language you need and the keyboard layout
  6. Say no to the network being started
  7. Do not mount or search for existing systems
  8. Select the Shell option
  9. You will now have a bash Linux command line and some lovely utilities
  10. Plug in your external drive that will be your new O/S disk
  11. Use fdisk to set up the partitions, ensuring that the sizes are exactly the same between the disks. Use the fdisk -l option to list the disks and see the partition information for both disks. Keep typing fdisk -l until you see /dev/sda and /dev/sdb or at least 2 disks
  12. Your primary disk (the one with the operating system on it currently) should be /dev/sda if it's a SATA drive like mine. And the USB external drive will be /dev/sdb (if there are no other disks in your system)
  13. Type fdisk /dev/sdb to modify the layout of the external disk
    d will delete a partition
    n will create a new partition
    p will print the partition table
    w will save the partition table
    a will allow you to set the active boot partition
  14. You will need all of the above commands to set up your disk to be the new O/S disk
  15. Delete any partitions that are on the new disk (/dev/sdb)
  16. Hopefully the geometry of the disks will be similar so that when you create a new partition you should be able to use the same Start and End cylinders as the old disk. You can check by the number of blocks, which should be the same for each corresponding file system e.g.
    /dev/sda1 1 1912 15358108+ 7 HPFS/NTFS
    /dev/sdb1 1 1912 15358108+ 7 HPFS/NTFS
  17. Once you have set all of the partitions to their correct size and marked the relevant partition as the active boot (you can identify this by looking for the * against the device) then we can start copying the partitions between the disks
Copying the partitions
  1. We will use a command called dd to copy the partitions. This is a byte copying utility that does not care what your data is, it just does the job, unlike these Windows command tools which clearly don't.
  2. To copy the first partition type
    dd if=/dev/sda1 of=/dev/sdb1
  3. Wait for the command prompt to come back. There is no information displayed on the screen during the copy, but you can tell it to show information
  4. To see progress of the dd command press ATL+F2 to move to the secondary terminal window
  5. Type
    ps -ef | grep dd
  6. Identify the dd if=/dev.... process ID (PID)
  7. Type
    kill -USR1 PIDfromStep6
  8. Press ALT+F1 to return to the original screen where dd is running and notice the information on the screen. It will tell you how much has been transferred so far and how fast the transfer is going.
  9. Repeat the above steps for every partition on the original O/S drive
When the process of copying all the partitions has finished carefully swap the hard disks over. When you start your system you will only need to start it with the new disk.

Starting the system (if you have dual boot)
  1. Boot from the Fedora DVD
  2. Select the Linux rescue
  3. This time follow the instruction and tell it to search for the Linux system
  4. Tell it to mount the found Linux system to /mnt/sysimage
  5. Type
    cd /mnt/sysimage
  6. Type
    chroot /mnt/sysimage
  7. Now we need to ensure that the MBR is correctly written to the disk, so type
    grub-install /dev/sda
  8. Type
    reboot
Your system can now be started without the DVD and will boot to GRUB, from their you can then choose your operating systems as normal.

If Windows then the system should just boot with no further modifications.

This is all based on the size of your partitions being identical between both disks (even if the sizes of the physical disk are different).

Monday, August 16, 2010

Boot loaders and dual boot systems

Install order no longer matters
Once upon a time it used to matter which order you installed your operating systems when trying to dual boot Windows and Linux/Unix systems. Today that no longer matters and you can install your operating system in any order. The only thing you really need to remember is that your Windows operating system will almost certainly require at least 1 primary partition and so will your Linux system.

Re-instantiating GRUB
If you do decide to install or re-install Windows once you've installed Linux then you'll need to re-instantiate your GRUB boot loader as the primary boot loader. Here are the steps to perform this, for those of you like me who use Linux as their primary OS, but due to the rest of the world who seem to think they have to use Windows or MAC OS and you do work for these companies, here are the steps to recovering your boot loader after installing Windows without having to re-install Linux.
  1. Boot your system using the Linux installation disk
  2. On some versions of Linux, e.g. Fedora 12 you might want to change the rescue option so that it just has linux rescue instead of all the other options. To do this select the linux rescue option and press tab, then delete the line and type linux rescue
  3. Follow the instruction up to the point it asks you to find installed systems
  4. Tell it to skip
  5. Then select shell option for a command line
  6. Type fdisk -l to see the hard disks on your system
  7. Hopefully you remember which one contains GRUB (the /boot) directory since this is the one you now need to set to primary boot
  8. Type fdisk /dev/XXX where XXX is the name of the device you wish to change the boot flag
  9. If you can't remember the option to toggle the boot flag type m and press enter
  10. Type p to print the partition table and note which partition has the * as this is the current boot partition
  11. Type the letter a to change the boot flag
  12. Type the number of the partition that currently has the *
  13. Type a and then type the number of the partition that contains the GRUB boot loader
  14. Type w to save the partition table
At this stage you should reboot the system as normal to see if your grub boot loader re-appears. This should be enough to do it. If it does not then;
  1. Start the system from the Linux install media as above, but this time do tell the system to search for Linux installations
  2. If it successfully finds your Linux installation it will generally mount it to /mnt/sysimage
  3. cd /mnt/sysimage
  4. chroot /mnt/sysimage
  5. Now we wish to re-install the GRUB boot loader. To do this type grub-install /dev/XXX where XXX is the disk and partition number where GRUB should be installed, that is where the /boot directory is
  6. Provided that your GRUB boot loader is in /boot, if it's not then you will need to look at the man page to see what option you need
Once done your system will reboot with the GRUB boot loader, and as long as you know the GRUB options to make Windows boot you should be able to add this to your boot loader.

Typical grub.conf Windows settings are;
rootnoverify (hd0,0)
chainloader +1

Where hd0,0 is the disk number and partition number where Windows boot loader resides.