20080103

Building a big endian ufs image on a little endian system

Let's say you want to create a ram disk for a kernel, or a ufs image for an emulator. To make it interesting, the image needs to be in big endian format and you just have an intel x86 machine which is little endian. How do you proceed? If you were building a little endian image, the answer is simple: just newfs an md partition, mount it and copy. But that won't work for a big endian image. Fortunately, the answer is straight forward:
  1. create a tree of files you want in the image
  2. install the makefs port
  3. use makefs to create your image.
You'll need a /tmp file system of at least 2GB to follow these steps exactly. If you don't have one this big, then you'll need to use a different path below to use a different partition.

Create a tree of files

You'll need to create an image of what you want. Let's assume that it is a powerpc root disk for FreeBSD. For the sake of simplicity, I'm assuming that the kernel comes from elsewhere. If not, then you'll need to add the buildkernel/installkernel targets
  • setenv TARGET powerpc
  • setenv MAKEOBJDIRPREFIX /tmp/imp/obj
  • make buildworld
  • sudo make installworld DESTDIR=/tmp/imp/ppc-root
  • sudo make distrib-dirs DESTDIR=/tmp/imp/ppc-root
  • sudo make distribution DESTDIR=/tmp/imp/ppc-root
  • echo "hostname=ppc-qemu" > /tmp/imp/ppc-root/etc/rc.conf
  • echo ifconfig_DEFAULT=DHCP >> /tmp/imp/ppc-root/etc/rc.conf
  • echo /dev/ad0a / ufs rw 1 1 > /tmp/imp/ppc-root/etc/fstab
The above steps build the FreeBSD world, and create a directory that could be used as a root directory. It can be used via NFS, or placed on a flash or in a ram disk.

Installing the makefs port

NetBSD created a tool to create file systems. Colin Percival ported this to FreeBSD and make a FreeBSD port to boot. He's no longer maintaining the port, but I try to keep it compiling. Installing the port is done in the usual FreeBSD way:
  • cd /usr/ports/sysutils/makefs
  • make all
  • sudo make install
  • make clean
Making the image

Once you have makefs installed, creating the image is the next step. You'll need to figure out how big you wish to make the image. For me, I think that 500MB is a good size. The base system is about 250MB, and that leaves room left over for testing programs in the emulator.
  • makefs -B big -s 500m /tmp/imp/ppc-root-image /tmp/imp/ppc-root
The resulting file, /tmp/imp/ppc-root-image is going to be 500MB in size.

1 comment:

Dylan Cochran said...

Thanks for the tip!

This helps me quite a bit, too bad porting the netbsd endian neutral filesystem code isn't as straightforward as it could be.