Monday, September 10, 2012

NetBSD on a stick

This is not really a new idea but after a couple of upgrade "oopses" that rendered my machine unbootable due to init getting upset about not finding MAKEDEV I decided I needed a safety net that was a little bit more portable than the USB hard disk enclosure that had saved me in the past.

I had a spare 4Gb USB memory stick which is more than ample to hold a full NetBSD install including X and a few other bits and pieces so I decided to put the memory stick to good use.  The advantage of using a memory stick is that it is easy to update, we have the entire NetBSD toolset available, not some cut down version.  The downside is that the memory stick does have a limited number of writes so one wouldn't last very long for full time day to day use but as a portable rescue device or even just to be used to test new hardware before buying it can be useful.

On my laptop the memory stick appeared as sd0 so I will use this device in all my examples, for those following along at home you may need to adjust your device naming.

Firstly, I needed to adjust the mbr.  The memory stick had a single MSDOS partition on it, this needed to be changed to be a NetBSD one (type 169).  To do this I ran fdisk:

fdisk -u /dev/sd0d

The -u flag makes fdisk interactive, prompting you to update certain parameters as you go along.  For my purposes I just accepted the defaults to everything until I was prompted to change a partition.  At this point I just selected the only partition there and changed the sysid to 169 and set a bootmenu label of NetBSD, all other parameters were left at their default.  Once I had finished editing the partitions I accepted the default "none" at the partition prompt to move on.  Fdisk prompted me to update the mbr to which I said yes and also said yes to it updating the mbr type to bootsel since I had a boot menu.

Once the mbr was set it was time to edit the disklabel:

disklabel -e /dev/sd0d

This opens up the disk label in an editor.  I left most of the settings as is only editing the e: partition, I changed the e: into an a: and change the fstype to "4.2BSD", set the fsize to 2048, the pgsize to 16384 and the cpg/sgs to 0.  The line ended up looking like:

  a:   7883696      1104     4.2BSD   2048 16384     0  # (Cyl.      0*-   3849)

Then I just saved the changes and quit the editor.  With the new disklabel in place I could then create the filesystem:

newfs /dev/sd0a

This took a while to do but eventually completed without problem.  Once the file system was built I mounted it:

mount /dev/sd0a /mnt

Then unpacked the installation sets onto the memory stick.  I had created my own release files using "build.sh release" but I could have easily just downloaded the official sets, the files I needed were under the binary/sets directory.  I just cd'ed to that directory and did:

cdir=`pwd`
for f in *.tgz
do
 (cd /mnt && tar zxpf ${cdir}/${f})
done

and waited for a long time, the unpacking was very slow.  Once this was done I picked the kernel-GENERIC.gz file from binary/kernel, uncompressed this and copied as /mnt/netbsd.

To make the memory stick bootable I did:

cp /usr/mdec/boot
installboot -fv /dev/sd0d /usr/mdec/bootxx_ffsv1

I had to use the -f flag on installboot because I was getting an error "Old BPB too big", the -f flag forced installboot to continue anyway, the result seemed to work fine.

I created a new fstab:

 /dev/sd0a       /               ffs     rw,log  1 1 

and edited /mnt/etc/rc.conf to change "rc_configured" to "YES" so the machine would come up multiuser.

After this it was just a matter of rebooting my machine, the BIOS picked up a USB device and started a boot from it.  I was presented with a boot menu, when I selected the only option my machine slowly booted from the memory stick.  I was able to log in and do everything you would expect to be able to do on a clean NetBSD install.

No comments: