Thursday, July 10, 2008

Build your own ultimate boot disc

Everything Linux and Open Source

Build your own ultimate boot disc

June 25, 2008 (4:00:00 PM) - 2 weeks, 1 day ago

By: Kurt Edelbrock

turn on your trusty old Linux box, and things are going well as you
pass through the boot loader, until the disk check reveals that your
hard drive partition table is corrupt, and you are unable to access
your machine. You need a good rescue disk -- and the best way to get
one is to create your own.

You can
customize an Ubuntu 8.04 Hardy Heron live CD to make a good bootable
utilities disk by adding and removing packages from the standard
installation. Specifically, you can remove most of the Ubuntu
applications and install antivirus, a partition recover tool, a few
disk utilities, and a rootkit checker, among other things. I'm going to
create the live CD within an Ubuntu installation, but the directions
should work for most Debian-based operating systems, and can be easily
ported elsewhere. This guide largely follows the community documentation article
on the Ubuntu customization process, which is a good place to look for
more advanced information and troubleshooting support, while the wiki is the best place to look for customized directions.

To create and use the Ubuntu-based boot CD, you'll need a computer
with at least 3GB of disk space and 512MB RAM. 1GB of swap is
recommended, though I did it with 512 MB.

Create the live CD environment

The first step is to download
the Ubuntu 8.04 live CD ISO file for your system type. You can get it
from the Web site, or you can use wget on the command line:

wget -v

To work with the image, you'll need to install a few packages to
support the squashfs filesystem format, and mkisofs, the utility to
create ISO images. On Ubuntu, you can install them with the command sudo apt-get install squashfs-tools mkisofs.

Now, load the squashfs module, then copy, mount, and extract the contents of the ISO file in order to customize the contents:

<div class="code">sudo modprobe squashfs<br />mkdir rescue<br />mv ubuntu-8.04-desktop-i386.iso rescue<br />cd rescue<br />mkdir mnt<br />sudo mount -o loop ubuntu-8.04-desktop-i386.iso mnt<br />mkdir extract-cd<br />rsync --exclude=/casper/filesystem.squashfs -a mnt/ extract-cd<br />mkdir squashfs<br />sudo mount -t squashfs -o loop mnt/casper/filesystem.squashfs squashfs<br />mkdir edit<br />sudo cp -a squashfs/* edit/<br /></div></pre>

<p>You'll want to customize the CD in a chroot environment. Chroot
changes the root directory of the environment, allowing you to access
the files and applications inside the CD directly, which you must do in
order to use tools like apt-get. In order to use a network connection
inside chroot, which you'll probably want to do to add new packages,
you'll need to copy in the hosts and resolv.conf files to configure
your network settings. You can achieve this with the following:</p>

<pre><div class="code">sudo cp /etc/resolv.conf edit/etc/<br /><br />sudo cp /etc/hosts edit/etc/</div></pre>

<p>Once you've completed these steps, you can start to work inside the
live CD. Mount the live CD to the edit/dev mountpoint, then change your
root directory into the newly mounted volume. You'll need to mount
/proc and /sys volumes to work with the kernel, and export your
settings to avoid locale and GPG problems later on:</p>

<pre><div class="code">sudo mount --bind /dev/ edit/dev<br />sudo chroot edit<br />mount -t proc none /proc<br />mount -t sysfs none /sys<br /><br />export HOME=/root<br />export LC_ALL=C<br /></div></pre>

<h4>Free space by removing unneeded packages</h4>

<p>You can configure the packages that are included with the live CD
using apt-get or Aptitude. You'll want to free up some space to add the
rescue applications; even though the data is compressed, all of it
needs to fit on a 700MB CD or on a higher-capacity DVD. You can remove
packages and applications that aren't useful for the recovery. I chose
to remove the suite, the GNOME games set, Ekiga,
Ubiquity, Evolution, and the GIMP, saving me around 200MB. If you are
comfortable without a command-line environment, you might want to get
rid of GNOME and Xorg; if you do that, you need not install GParted and
the other graphical tools in the next section. In any case, the goal is
to get rid of large applications. To sort all of the installed packages
by size, run the following command in the chrooted environment:</p>

<p><code>dpkg-query -W --showformat='${Installed-Size} ${Package}\n' | sort -nr | less</code></p>

<p>You can use apt-get to remove a package. Use it with the <code>--purge</code> argument to get rid of configuration files. The sudo command won't work in the chroot, and therefore should be omitted:</p>

<p><code>apt-get remove --purge <em>package-name</em></code></p>

<div class="sidebar">
<p><strong>Prebuilt Linux rescue CDs</strong></p>

<p>You don't need to build a custom rescue disk to get a great bootable
utility CD. Here are a few pre-built rescue CDs you can try.</p>

<ul><li><a href="">Parted Magic</a> -- This 45MB boot
CD uses GParted, the GNOME partition editor, to handle partition table
management for an extensive list of filesystems, including ext2/3,
NTFS, and HFS+. Parted Magic uses the Xfce desktop environment to
provide a variety of tools, including Firefox, Thunar, and ISO tools.
It also has a USB version to use from a thumb drive.</li><li><a href="">SystemRescueCd</a> -- The 191MB
CD features partition, archive, and networking tools, along with a slew
of editors and file browsers. This is probably the easiest system boot
CD, and is recommended for less advanced Linux users. It also has a
rootkit checker, virus scan, and CD burning utilities. It includes an X
interface through Xfce.</li><li><a href="">Trinity Rescue Kit</a> -- The
129MB Trinity Rescue Kit is designed for the rescue and recovery of
Windows machines, but it will work for Linux as well. It includes a few
virus scan applications, a Windows password reset tool, Samba, SSH,
rootkit removal tools, and partition and backup tools. It is based on
Mandriva Linux.</li></ul>

<h4>Add rescue applications</h4>

<p>Once you have removed all of the unneeded applications from the live
CD you can start to add rescue and recovery applications. Generally,
rescue CDs include a variety of disk utilities and security tools, as
well as networking tools to find support and access outside machines.
You may not want all of the applications I mention, and you can add
some that I don't. This is your personal boot CD, and should be
configured as you see fit. For ideas about what to include on your CD,
you might want to check out some of the prebuilt rescue distributions
mentioned in the sidebar.</p>

<p>You can install packages from the repositories using apt-get, but
you must add the multiverse repository to your /etc/apt/sources.list

<pre><div class="code"><br />deb hardy main multiverse<br />deb-src hardy main multiverse<br /></div></pre>

<!-- Image: partimage_thumb.png - Cutline: Partimage is an essential program for a rescue disk. Use it to copy and restore data. -->

<p>A disk partition tool is the staple of a mature boot disk. Fortunately, the Ubuntu live CD comes with <a href="">GParted</a>,
the GNOME Partition Editor, so adding a package isn't required. If you
chose to forgo a graphical environment, you should make sure that
parted is installed instead to handle partition tables from the command
line. If you accidentally delete a partition, installing a program like
<a href="">testdisk</a> can help
you recover it, as well as provide a few other basic disk tools. If you
are using the ext2 filesystem type and you accidentally delete a file,
you'll find the <a href="">e2undel</a> package helpful in recovering it. If you need to copy an entire partition from a dying disk, or just want to make a backup, <a href="">partimage</a> is the way to go. You can also use it to restore a partition with a previously made backup.</p>

<!-- Image: GParted_thumb.png - Cutline: Use GParted to configure your partition tables for your hard disk -->

<p>If you plan to use this disc with Windows machines, you will want to install antivirus and rootkit tools. <a href="">Clamscan</a> provides quick and easy virus scan with a command-line-based update tool. <a href="">Chkrootkit</a> is a scanner to find and remove rootkits that could be hiding in your computer. You can use <a href="">sleuthkit</a> to conduct analysis of your filesystem and browse through hidden files.</p>

<p>After you finish adding packages, clean up your temporary data and unmount the environment:</p>

<pre><div class="code">apt-get clean<br />rm -rf /tmp/*<br />rm /etc/resolv.conf<br />umount /proc<br />umount /sys<br />exit<br />sudo umount edit/dev<br /></div></pre>

<p>Now, regenerate the manifest (which is basically a list of installed packages) and copy in into the correct directory:</p>

<pre><div class="code"><br />chmod +w extract-cd/casper/filesystem.manifest<br />sudo chroot edit dpkg-query -W --showformat='${Package} ${Version}\n' > extract-cd/casper/filesystem.manifest<br />sudo cp extract-cd/casper/filesystem.manifest extract-cd/casper/filesystem.manifest-desktop<br />sudo sed -i '/ubiquity/d' extract-cd/casper/filesystem.manifest-desktop<br /></div></pre>

<p>Compress the filesystem to squeeze it onto a disc:</p>

<pre><div class="code"><br />sudo rm extract-cd/casper/filesystem.squashfs<br />sudo mksquashfs edit extract-cd/casper/filesystem.squashfs -nolzma<br /></div></pre>

<p>And finally, create the ISO file:</p>
<pre><div class="code"><br />cd extract-cd<br />sudo mkisofs -r -V "$IMAGE_NAME" -cache-inodes -J -l -b isolinux/isolinux.bin -c isolinux/ -no-emul-boot -boot-load-size 4 -boot-info-table -o ../ubuntu-8.04-desktop-i386.iso<br /></div></pre>

<p>Once the image file is created, you need to burn it to a disc. You
can do that pretty easily with K3b or Brasero. If you want, you can do
it from the command line:</p>

<pre><div class="code"><br />cdrecord dev=/dev/cdrom ubuntu-8.04-desktop-i386.iso<br /></div>

Once the CD is finished burning, you should be able to put it into
your optical drive and boot into the environment you just created.

This should give you more than enough information to start building
your ultimate custom rescue CD. Add the packages and tools you need,
and hopefully you'll never be at a loss the next time your computer has
a problem during startup.

Read in the original layout at:

No comments: