Tuesday, August 31, 2010

Add SystemRescueCD capability to your USB stick

For Linux users: Creating a bootable SystemRescueCD USB stick. The SystemRescueCD is an invaluable resource and should be in every techs toolkit. Kudos and much thanks to the developers!

This is a modified version of section D) at:
How to install SystemRescueCd on a USB stick
Please see this page for other details.

Also this assumes grub and not grub2 is in use on your system. You most likely will have to modify for a grub2 install. One big difference is that partition numbers in grub2 are "1" based and not "0" based as they are in grub.

One reason for this additional material is that the grub-install method did not work at all for me, I needed to manually run grub. The other is that I wanted most of the USB stick to be usable for transferring files between various devices/OS's - the common file system for such purposes is FAT32.

Unlike Linux, Windows, myopic as it is, can only see the first partition on a USB stick (out of the box). And for added safety I wanted the SystemRescuCD code to reside on a non-Windows usable file-system, in this case ext4. So we make the first partition FAT32, for portability, and the second partition, which Windows doesn't see anyway, ext4.

WARNING - running these commands will destroy data - be absolutely sure you are working with the proper device!

When you see "sde", substitute the "e" with the drive letter of your target device, do not blindly type it in as is. When you see hd4 substitute the "4" with the corresponding 0 based number as e is to 4 (notes below). In my actual system, with 4 hard drives (sda, sdb, sdc, sdd), the flash drive is sde and grub's syntax refers to it as hd4.

Become root or use sudo, whatever your style is.

Prepare (erase) the flash drive by writing all "ones" (not zeroes) to it:
  • tr '\000' '\377' < /dev/zero | dd bs=16384 of=/dev/sde
* The "erased state" for flash is when it is filled with all 1's. People regularly make the mistake of filling flash based storage devices with all zeros (as is typically done with real disks) without every realizing what they are doing. See: Bootable Installation From USB Flash Sticks

Write a new blank partition table (if your distro's fdisk doesn't turn DOS compatibility mode off, turn it off as well - the "c" command):
  • fdisk -H 224 -S 56 /dev/sde
  • o
  • w
Create 2 new partions, a primary of type "c" (FAT32 LBA) for data sharing, sized to leave about 1.5GB remaining at end of drive, example is for an 8GB flash drive:
  • fdisk -H 224 -S 56 /dev/sde
  • n
  • p
  • 1
  • (accept default)
  • +6500M (~capacity of your stick minus 1500M)
  • n
  • p
  • 2
  • (accept default)
  • +800M
  • t
  • 1
  • c
  • a
  • 2
  • w
The above will leave a bit of free space on the device (don't fret). The ending sequences: t,1, c - set's partition 1 to FAT32 LBA and a, 2 toggles the bootable flag for partition 2.

Make sure your OS sees the new partition layout:
  • fdisk -l /dev/sde
If not run "partprobe" or remove/reinsert the USB stick and try again.

Format partition 1:
  • mkfs.vfat /dev/sde1
Format partition 2 (disabling journal):
  • mkfs.ext4 -O ^has_journal /dev/sde2
Check the file system integrity:
  • fsck /dev/sde1
  • fsck /dev/sde2
If above is fine then mount this /dev/sde2 to /media/usbstick (as example - if your OS automounts you can use that mountpoint - but stay consistent) and copy the files from your SystemRescueCD into this directory. (cf normal instructions)
  • mount -t ext4 /dev/sde2 /media/usbstick
  • cp -a /where.your.System.RescueCD.files.are/* /media/usbstick/*
Copy GRUB staging files from an existing GRUB installation:
  • mkdir -p /media/usbstick/boot/grub
  • cp -a /lib/grub/<distro-specific>/* /media/usbstick/boot/grub
Create a menu.lst in /media/usbstick/boot/grub/. Note that we use hd0 here instead of hd4 as grub will see the usb stick as hd0 when we boot from it.
===============================================
timeout 10

title SystemRescueCD 32bit
kernel (hd0,1)/isolinux/rescuecd
initrd (hd0,1)/isolinux/initram.igz

title SystemRescueCD 64bit
kernel (hd0,1)/isolinux/rescue64
initrd (hd0,1)/isolinux/initram.igz
===============================================

Original page recommends to run:
  • grub-install --root-directory=/media/usbstick/ /dev/sde
  • **(this did not work for me)
Alternate to above "grub-install" (properly replace hd4 with the correct data, see below):
  • grub
  •  grub> find /boot/grub/menu.lst  (will verify the proper root location)
  • grub> root (hd4,1)    (specify where your /boot partition resides)
  • grub> setup (hd4)     (install GRUB in the USB stick's MBR)
  • grub> quit
Grub's syntax is zero based, drive sda would be hd0, drive sdb would be hd1 and so on, therefore drive sde is hd4. The same goes for partition numbering, the first partition is 0, the second is 1. Our SystemRescueCD data is on the 2nd partition of sde, hence (hd4,1) in the example.

You should now be able to boot into the SystemRescue OS as well as use the first partition for transferring files between various OS's.

Please comment on whether you find this information useful or what might be done to improve it. Thank you!

10 comments:

  1. grub-install --root-directory=/media/usbstick/ /dev/sde
    didn't for me too but

    grub-install --recheck --root-directory=/media/usbstick/ /dev/sde

    seemed to fix (my) problem

    ReplyDelete
  2. Good to know. Might have worked in my case as well but I prefer the manual method over grub-install which even the docs refer to as "definitely less safe": http://goo.gl/3YoRu

    ReplyDelete
  3. Thanks for this guide, as `grub-install` didn't work for me, too.

    One thing that seems odd to me: you advice running `find /boot/grub/menu.lst`; of course, that's menu.lst on my hard disk, not the one I just placed on my USB device. So I tried `find /mnt/usb/boot/grub/menu.lst`, but that failed (though my device really was mounted there).

    After that, I tried your advice (`find /boot/grub/menu.lst`) and Grub responded with (hd1,1), where I would expect it to respond with (hd0,1) (where my /boot resides). Can you elaborate on that?

    I already looked at the info pages (`info grub`), but they say:

    grub> find /boot/grub/stage1

    This will search for the file name `/boot/grub/stage1' and show the devices which contain the file.

    ReplyDelete
  4. @Marcel
    Did you miss the step:
    Create a menu.lst in /media/usbstick/boot/grub/ ?

    ReplyDelete
  5. just a question: Doesn't the grub hd number change from computer to computer depending on the number of harddrives available? If, so, it will work on MY computer it was created on, but what about others?
    I want a system rescue stick I can boot/use on any computer I want to fix, not just my own, like using the system rescue CD. How do i accomplish this?

    ReplyDelete
  6. Yeah, my Thinkpad 4 yrs old laptop (nc6120 I think) boots from the 2nd ext4 partition.
    Windoze XP installed on that Thinkpad laptop sees and can copy files on 1st vfat partitin.
    So, all is well and working... but NOT for my 6 yrs old AT8-32X MBO (Abit) based PC systems.
    The MBO must be the reason.
    It boots fine (once I talk it into it, a lil' persuasion needed) if I install sysresccd on one vfat partition USB, nothing else on the stick, the easy automated install.
    That seems to be what I will have to stick with.
    Of course, I am now also looking into a way to have an ext4 partion aside, as 2nd partition on the USB drive, for Linux stuff on the stick, but the booting must, I guess, be from vfat... from the Billy and the Gang's fatty bloaty stuff of their ugly OS grown and spread on ignorance and greed of the world.
    So, I guess my mileage to follow now is, regular auto install, the recommended one on the page:
    http://www.sysresccd.org/Sysresccd-manual-en_How_to_install_SystemRescueCd_on_an_USB-stick#Execute_the_installer
    And then, whatever works to get another partition... Shrink vfat first, or what?
    I don't know yet... But have an ext3/ext4 partition, as 2nd partiotion on the stick for better things which won't stink of the Corrupting Gang's stuff.
    This page was useful. Thanks!
    God bless you!

    ReplyDelete
  7. Why the "-H 224 -S 56", though?

    ReplyDelete
  8. It booted just fine but I'm getting "Cannot find /sysrcd.dat on devices" error message and an incomplete boot that drops me to a mini shell. /sysrcd.dat file is right there in the root. I also tried these instructions with an older version of the iso that I know worked with this particular hardware just fine and it ends up the same way. I guess this guide is missing a step or two.

    ReplyDelete

Note: Only a member of this blog may post a comment.