Categories
FreeBSD tech

Create a rescue USB on FreeBSD

In this post, I document how to create a bootable rescue USB stick on FreeBSD. Having such a stick is handy in case you want to quickly inspect a broken or suspect system.

There are many ways to Create a rescue USB on FreeBSD. One can also use the FREEBSD official installer to choose to get into a live shell.

I use mfsBSD as the contents of this rescue disk. Normally, one can directly use mfsBSD as a rescue risk but below I specifically create an MBR based USB image in order to boot on those older hardware. Many a time, older hardware is unable to boot using GPT.

WARNING: USE ALL INSTRUCTIONS BELOW WITH CAUTION. THEY CAN SCRAP YOUR COMPUTER IF JUST EXECUTED WITHOUT CHECKING.

Preparing the device

  1. Download the mfsBSD ISO of interest. In this case, it is mfsbsd-13.0-RELEASE-amd64.iso
  2. Have the USB stick ready. In this example we use an archaic 256MB stick
  3. Ensure that the USB stick does not have any data. This example assumes that it doesn’t have any partitions as well.
  4. Caution: Use the right device id. Identify the device id – you can find this after plugging in the USB stick and checking dmesg. In this example, we use da1
  5. Setup the device partitions on da1. We keep a second partition which can be used as a writable scratch area which would be useful during recovery.
# gpart create -s MBR da1
da1 created

# gpart add -t freebsd da1
da1s1 added

# gpart create -s BSD da1s1
da1s1 created

# gpart add -t freebsd-ufs -s 150M da1s1
da1s1a added

# gpart add -t freebsd-ufs da1s1
da1s1b added

# gpart bootcode -b /boot/boot0 da1
gpart bootcode -b /boot/boot0 da1

# newfs /dev/da1s1a
/dev/da1s1a: 150.0MB (307200 sectors) block size 32768, fragment size 4096
       using 4 cylinder groups of 37.53MB, 1201 blks, 4864 inodes.
super-block backups (for fsck_ffs -b #) at:
 192, 77056, 153920, 230784

# newfs /dev/da1s1b
/dev/da1s1b: 95.0MB (194520 sectors) block size 32768, fragment size 4096
        using 4 cylinder groups of 23.75MB, 760 blks, 3072 inodes.
super-block backups (for fsck_ffs -b #) at:
 192, 48832, 97472, 146112

Setting up the rescue stick

# mdconfig -a -f mfsbsd-13.0-RELEASE-amd64.iso
md0
# mount -t cd9660 /dev/md0 /mnt

# mkdir /mnt2

# mount /dev/da1s1a /mnt2

# cd /mnt

# find . -print -depth| cpio -dump /mnt2
177244 blocks

Cleaning up

So, we have now copied all the files from the mfsBSD ISO file to the USB stick root partition. Now it is time to cleanup and then we are ready to use our freshly created rescue stick.

# cd

# umount /mnt2

# umount /mnt

# mdconfig -d -u md0

# rmdir /mnt2

And that’s it. You now know how to create a rescue USB on FreeBSD.