So far we’ve built u-boot from scratch, built the Linux kernel and built the u-boot SPL so we don’t have to use the Xilinx SDK if we don’t want to. Our main goal here is to create a embedded Linux system on our Zybo. Our secondary goal is going to be to add the Xenomai RT patches and create a real time Linux system. One step that we haven’t gone over yet is creating a root filesystem.
We have a couple choices when it comes to root filesystems, depending on where our embedded system is going to be deployed. Some smaller system will use a RAM disk as it’s root filesystem. A ramdisk is a filesystem that is loaded into memory every time the system is started. This type of file system is not persistent. This means that any changes or modifications that are made do not survive a reboot.
There are two ramdisks that are commonly used in Linux systems. The first is the initial ram disk (commonly called initrd). This is an older method but it’s still supported in the Linux kernel. When the kernel boots up it will decompress the ramdisk and use it as the root filesystem. Some Linux systems (included embedded ones) may use this file system to perform some initialization and the pivot to the real root filesystem. You can google “pivot_root” to see exactly how this is accomplished. Some embedded systems will continue to use the initial ramdisk instead of loading a persistent one. Any filesystem changes that we make will be lost on a reboot. This can be good or bad depending on what we are trying trying to accomplish. The initrd requires a synthetic block device of fixed size, which restricts the file system from growing without creating a new block device and starting from scratch again. One draw back of using any RAM disk is that the more libraries and utilities we need in our file system the larger the file system will grow and hence the more RAM it will use.
The initramfs is the preferred (or the most recent) way of creating a ramdisk for your Linux system. Traditionally initramfs can be built into the kernel itself which makes it very quick and compact. We don’t need to create a block device to create it which makes it much easier to build. One draw back is that we don’t want to include anything in the initramfs that can’t fall under the GPL license. This is because we can include the initramfs into our kernel build therefore making it fall under the GPL. One way around that is to use the initramfs filesystem but include it externally using the initrd hooks.
The more common approach in hobby based boards is to use the sdcard (or part of it) as the root filesystem and have persistent storage. This method is much easier when add utilities, libraries and executables to our system. For learning purposes I’ve chosen to use a ramdisk for my zybo system. In a later blog post we will also go over how to use a Ubuntu (or Arch) based root filesystem which will be much bigger but give use more flexibility and ease when it comes to included third party libraries.
Moving on to creating or RAM based root filesystem. Xilinx provided an example of building an initrd file system on there website here, which seems fairly old, so for our purposes we will use the initramfs method and use the initrd hooks to have it included outside of our kernel image. I’ve taken a lot of information from the above page so it’s still worth a read.
First we will need the Busybox source. We can download it here, at the time of writing the latest Busybox is 1.26.2. We can uncompress the tar file into it’s own directory.
tar xvjf busybox-1.26.2.tar.bz2 cd busybox-1.26.2 make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- defconfig
So now we’ve uncompressed busybox, and configured it. The next step is to add any custom configuration that we may need using menuconfig. If your build environment hasn’t used menuconfig before make sure you have ncurses installed or we will see errors when running this next command.
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig
Next we need to set up where we are going to place the busybox executable and the symlinks that go along with it. Once we are in the menuconfig screen go to busybox settings, installation options and specify a location for busybox installation prefix. For me I placed this in a directory called zynq_ram_rootfs, and make sure to specify the full path. Lastly exit menuconfig and save our changes. Next let’s build our executable.
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- install
We should see some build output, once the build is done we can cd into our install directory and see the the symlinks that were made by the build process.
mkdir dev mkdir etc etc/init.d mkdir mnt opt proc root mkdir sys tmp var var/log
Next we need to remove linuxrc, we are doing this because linux looks for an executable called init when the first process starts up. We will need to link this to our busybox executable. Remember for this to work we need to be in the install directory for our root filesystem.
rm ./linuxrc ln –s ./bin/busybox ./init
If Linux can’t find the init script is should fall back to using an older method of starting up the first user process. This should include calling linuxrc but I prefer to make the init symlink. Next we need to create some configuration files that will help get Linux setup using our root filesystem. First we need to create a file named /etc/fstab.
LABEL=/ / tmpfs defaults 0 0 none /dev/pts devpts gid=5,mode=620 0 0 none /proc proc defaults 0 0 none /sys sysfs defaults 0 0 none /tmp tmpfs defaults 0 0
This file contains information about all the partitions, block devices and remote file systems. Here we are mounting each of these directories at startup.
Next we need to create the /etc/inittab file, this file controls what happens whenever a system is booted or when a run level is changed.
::sysinit:/etc/init.d/rcS # /bin/ash # # Start an askfirst shell on the serial ports ttyPS0::respawn:-/bin/ash # What to do when restarting the init process ::restart:/sbin/init # What to do before rebooting ::shutdown:/bin/umount -a -r
This file is from this Xilinx tutorial, it’s pretty straight forward. We spawn and ash shell (busybox uses the ash shell) on the UART0 which is ttyPS0 and then we have actions to perform on shutdown and restart.
We also need to create the /etc/init.d/rcS file, this file is the second main boot script. The rcS file is the run-level file for single user mode. Because our system only has the root user we are a single user system.
#!/bin/sh echo "Starting rcS..." echo "++ Mounting filesystem" mount -t proc none /proc mount -t sysfs none /sys mount -t tmpfs none /tmp echo "++ Setting up mdev" echo /sbin/mdev > /proc/sys/kernel/hotplug mdev -s mkdir -p /dev/pts mkdir -p /dev/i2c mount -t devpts devpts /dev/pts echo "rcS Complete"
We also need to set this script as executable or we won’t be able to run this script and our linux system won’t be able to do anything useful.
chmod 755 <path_to_rootfs>/etc/init.d/rcS
One of the last steps here in creating our root file system is the create a password file for the root user. We will need to create the etc/passwd file.
This file maintains the information about each user that can use the system. If you want to know the meaning of the above line checkout this page.
We now have a basic root filesystem but we are missing libraries that our programs will need to run. We could go download glibc and build it and install it in our root filesystem. Or we can copy the libraries from our toolchain, the easiest way to do this (IMHO) is to use the sysroot that is provided by the toolchain vendor. In our case we can download that from the Linaro site here.
I downloaded the file sysroot-glibc-linaro-2.21-2017.05-arm-linux-gnueabihf.tar.xz. We can uncompress this file and then install it into our rootfs.
Let’s decompress, move the file into our development directory
tar xf sysroot-glibc-linaro-2.21-2017.05-arm-linux-gnueabihf.tar.xz
We now see a folder called sysroot-glibc-linaro-2.21-2017.05-arm-linux-gnueabihf which contains all of our libraries will need for our system. Just as a warning, this is throwing the entire kitchen sink into your rootfs, so any lib that the compiler provides and it expects to be in a live system is here. If you aren’t using some libraries you may want to remove them. For example if you aren’t using fortran then you may want to consider removing those from the libraries you install in your rootfs. If you are just writing C programs, libc, libm and a couple of gcc dependencies may be all you need.
When I do the following step I add about 256MB of files to my rootfs, which is great for prototyping, but it isn’t good for a live system that wants to use RAM for program data (remember how a RAM disk works). By doing this next step we may defeat the purpose of using busybox to create a minimal rootfs. There are some simple steps we can take to size down the libraries that we are deploying. These steps may save us 200MB of space in our root filesystem. Let’s first copy over everything.
cp -rf ./sysroot-glibc-linaro-2.21-2017.05-arm-linux-gnueabihf/* <path_to_busy_box_rootfs>
So if we check the size of the directory that contains our rootfs we should see something close to 256MB for it’s size. This is very very large and in some cases it may be too large to fit into RAM. So we are going to need to start reducing it’s size. The first thing we can do is strip out all the debug symbols. Using the following command will strip all the debug symbols in the libraries of our rootfs.
arm-linux-gnueabihf-strip <path_to_rootfs>/lib/* arm-linux-gnueabihf-strip <path_to_rootfs>/usr/lib/*
Since I’m not using fortran I removed it from my lib directory and I also removed the debug directory from lib/ also.
rm <path_to_rootfs>/lib/libgfortran.* rm -r <path_to_rootfs>/lib/debug
Now we should see out rootfs is about 65MB, these is much more manageable. If you would like to slim it down even further look through the directories and see if there is anything else that you don’t need and remove it as needed. Once we have that done we can move on with compressing it.
We need to compress our rootfs and get into the proper cpio format.
cd <path_to_rootfs> find . | cpio -o --format=newc > <path_to_file>/rootfs.img
Almost done, the last step is to add the u-boot header that u-boot needs to load the rootfs image. We will also be changing the name since xilinx u-boot will be looking to load a file called uramdisk.image.gz
mkimage -A arm -T ramdisk -C gzip -d rootfs.img uramdisk.image.gz
You’ll need to make sure that you have the mkimage utility installed. Check with what package this is included in for your distribution. For example if you are using Ubuntu you would install
sudo apt-get install u-boot-tools
One last thing we need to do is update our linux kernel config with the new size of the ramdisk. So we’ll have to recompile the kernel once we do this but it’s pretty straight forward. Open arch/arm/configs/xilinx_zynq_defconfig modify the following line
CONFIG_CMDLINE="console=ttyPS0,115200n8 root=/dev/ram rw initrd=0x00800000,65M earlyprintk mtdparts=physmap-flash.0:512K(nor-fsbl),512K(nor-u-boot),5M(nor-linux),9M(nor-user),1M(nor-scratch),-(nor-rootfs)"
I changed mine from 16M to 65M, there’s no reason that if you are just running C code that you’d need all the libs we included, so you could skip this step and slim down your rootfs. If you’d like to continue with the larger rootfs with all it’s features then do the above modification (replacing 16M with the size of your rootfs in megabytes) and then follow my previous tutorial on compiling the linux kernel.
That’s it, you should now have a busy box based file system that we can add libraries and utilities to as needed.
Where to next?? If you’ve followed my blog posts you should now have all we need to create a custom embedded Linux distribution. Next blog post we will put all of our steps together and then boot our system and finally get to building Xenomai 3 patched kernel for Zynq.