LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   LinuxQuestions.org Member Success Stories (https://www.linuxquestions.org/questions/linuxquestions-org-member-success-stories-23/)
-   -   Creating rescue CDs for the computer illiterate (https://www.linuxquestions.org/questions/linuxquestions-org-member-success-stories-23/creating-rescue-cds-for-the-computer-illiterate-714800/)

openSauce 03-27-2009 05:52 AM

Creating rescue CDs for the computer illiterate
 
I recently had to install Win XP on a friend's computer, as it had apparentlty accumulated various mystery errors over the years. This being a bit of a hassle, and since the box didn't ship with a rescue cd (or even drivers), I decided to create my own. It's basically a Fedora Live CD image, written to a DVD, along with an image of the hard drive containing a fresh install of XP+drivers, and a script that writes the image to the hard drive, so that a trained monkey can use it if I'm not around. No doubt there are tools around that will do this automatically, but with DIY you learn more and have more control (and, of course, it's more fun ;))

This is for others who want to do the same thing (it took a few experiments and wasted DVDs to get everything right), and to invite comments for improvements. You are of course free to copy the small script below and modify as you see fit - I've tested it as far as I can, but it hasn't actually been used in anger, so use at your own risk!

First thing to know to make this easier: Windows can store user files and settings on a separate 'home' partition, Linux-style. This means you can keep the system partition reasonably small, so that the compressed image will fit on a DVD, and the rescue DVD can be used without overwriting user data. This Knowledge Base article tells you how:
http://support.microsoft.com/kb/314843/en-us

For a single-user system, you just have to change one registry entry. Haven't tried it for moving the whole Documents and Settings dir for all users.

Once the OS is installed as you want it for the rescue image, boot your live cd of choice and take the image. There are plenty of online tutorials on how to do this, I've used this one:
http://www.inference.phy.cam.ac.uk/s...artitions.html

The actual commands I used were
Code:

dd if=/dev/sda of=/media/usb-hdd/mbr.bak bs=512 count=1  # back up MBR (inc. partition table)
dd if=/dev/sda1 bs=1k conv=sync,noerror | gzip > /media/usb-hdd/sda1.img.gz  # back up first partition

A handy tip from the above link, to make the image smaller, is to zero the unused space before taking the image, so that it can be better compressed. Obviously if you're backing up a Windows partition you need a live cd that can write to NTFS for this.

Code:

mount /dev/sda1 /mnt/sda1
dd if=/dev/zero of=/mnt/sda1/delme bs=8M
rm /mnt/sda1/delme
umount /mnt/sda1

Now you need to modify a live cd image so that our hypothetical monkey can use it without messing around with dd and other such dangerous tools. Credits go to homey for the commands here, which are for the Fedora 8 live cd.

Find the squashfs image on the live image and unsquash it to get a 4GB ext3 image:

Code:

/usr/sbin/unsquashfs /pathtocd/LiveOS/squashfs.img  # creates directory squashfs-root in the current dir
mount -o loop squashfs-root/LiveOS/ext3fs.img /mnt/cdimage # mounts the / filesystem of the live cd

Now modify as you see fit. I added a script to write the hdd image to the hdd, and a text file of instructions. The user fedora doesn't actually exist on the Fedora live cd until it boots - it's created in an init script - so for maximum usability I modified an init script to copy them to /home/fedora after the directory is created. The obvious way to do that is to have the files elsewhere on the filesystem and just copy them over, but for some reason the init scripts couldn't access them, saying they didn't exist. I went with the kludge of creating them by redirecting the output of echo, maybe someone has a better way.

This is the modified /etc/rc.d/rc.local I put on the new live cd. It creates the restore script and the instructions file, and sets permissions etc as they should be.

Code:

#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local

# normally Gnome creates the Desktop dir when user first logs in.
# create it here instead so we can put stuff on it
mkdir /home/fedora/Desktop
chown fedora.fedora /home/fedora/Desktop

echo 'To restore your C: drive, go to the Applications menu in the top-left and select System Tools -> Terminal

In the window that appears, simply type:
restore

and hit enter.  This is case-sensitive: do not use any capital letters.

WARNING: this will overwrite your C: drive with a fresh install of Windows.  After that, you can reboot into Windows, and need to switch on Windows Update and install any programs you want to use.  "restore" is written so that it will not touch anything on your D: drive, but it is best to have everything backed up just in case!
' > /home/fedora/Desktop/instructions.txt

echo '
# add . to the path so that script will execute according to instructions.txt, without prepending ./
PATH=.:$PATH
' >> /home/fedora/.bash_profile

echo '#!/bin/bash

# restore MBR, partition table and partition 1.
# intended to be run from modified Live DVD with backup files
# on /restore of the DVD.

# calls a separate script in order for exit status to be correct
# (using a single script with su -c "..." means $EXIT_STATUS cannot
# be set and read inside the "...")

su -c root-script.sh
' > /home/fedora/restore

echo '#!/bin/bash

# restore MBR, partition table and partition 1.
# intended to be called by the script "restore"

logfile=/home/fedora/Desktop/restore.log
imgdir=/mnt/live/restore


terminate () {
    exit $1
}

{
    { [ -r $imgdir/sda1.img.gz ] && [ -w . ] ; } || \
        { echo "Error: cannot read $imgdir/sda1.img.gz or cannot write to current directory."
        terminate 1
        } &&
   

    echo -n "Restoring MBR and partition table... " &&
    dd if=$imgdir/mbr.bak of=/dev/sda >> $logfile 2>>$logfile  &&
    echo "OK." &&

    echo -n "Re-reading partition table... " &&
    /sbin/partprobe /dev/sda >> $logfile 2>>$logfile &&
    echo "OK." &&

    echo -n "Restoring C: drive (this will take several minutes)... " &&
    gunzip -c $imgdir/sda1.img.gz | dd of=/dev/sda1 bs=1k >> $logfile 2>>$logfile  &&
    echo "OK." &&

    echo -e "\n Restore completed successfully.\n" \
        "Activity logged to $logfile.\n" \
        "Please reboot your computer." &&
    dd if=$imgdir/mbr.bak of=/dev/sda >> $logfile 2>>$logfile  &&
    echo "OK." &&

    echo -n "Re-reading partition table... " &&
    /sbin/partprobe /dev/sda >> $logfile 2>>$logfile &&
    echo "OK." &&

    echo -n "Restoring C: drive (this will take several minutes)... " &&
    gunzip -c $imgdir/sda1.img.gz | dd of=/dev/sda1 bs=1k >> $logfile 2>>$logfile  &&
    echo "OK." &&

    echo -e "\n Restore completed successfully.\n" \
        "Activity logged to $logfile.\n" \
        "Please reboot your computer." &&

    EXIT_STATUS=$?
} ||

{
    EXIT_STATUS=1
    echo "There was an error.  Please save $logfile on a memory stick."
}

terminate $EXIT_STATUS
' > /home/fedora/root-script.sh

chown fedora.fedora /home/fedora/{restore,root-script.sh} /home/fedora/Desktop/instructions.txt
chmod a+x /home/fedora/{restore,root-script.sh}

Now create the new squashfs from the modified ext3fs.img:
Code:

umount /mnt/cdimage
/sbin/mksquashfs squashfs-root squashfs.img

Careful: different versions of squashfs are incompatible with one another. Make sure you're using a version compatible with the one on the live CD - the simplest solution is to get a live cd which matches the distro you're working on.

Finally, make the new ISO, with the hdd image in the place expected by the restore script (/restore in the version above):
Code:

# The volume label "Fedora-8-Live-i686" should match the isolinux.cfg entry.
sudo mkisofs -R -l -L -D -b isolinux/isolinux.bin -c isolinux/boot.cat -o /mnt/data/rescue.iso -no-emul-boot -boot-load-size 4 -boot-info-table -V "Fedora-8-Live-i686" /path-to-cd-files

And you're done! Oh, it's also a good idea to include an instructions.txt on the root of the DVD, which explains how to boot from the optical drive for that particular computer, and also includes the instructions above.


All times are GMT -5. The time now is 12:20 PM.