Building an initial ramdisk without root permission

Introduction

Building an initrd as described in our Linux guide needs root permission to mount an empty image, set correct ownership and create special files. This is inconvenient and not always possible. This guide describes how you can build root filesystems without the need for root permission.

Requirements

You need the following tools:

  • genext2fs
  • fakeroot

Procedure

Installation of the needed tools

On Debian and derived distributions just enter

apt-get install genext2fs fakeroot

with root rights.

Generating an initrd from a complete directory structure

If you already have all the files you need for the rootfs in a directory, use the following command:

genext2fs -d rootfs -b 8192 initrd.img

This creates an 8MiB sized ext2 image called initrd with the contents of the directory rootfs. If you need the root squashing feature (making all files owned by root as described in the Linux guide) add the option “-U”

genext2fs -d rootfs -b 8192 -U initrd.img

Do not use this feature, if the contents already have the correct ownership, i.e. you used the instructions from the next section to build the rootfs contents or create the image from the contents of an SD card/USB flash drive using the OpenEmbedded based system.

Now just compress the image with gzip:

gzip -9 < initrd.img > initrd.bin

You can now flash the image initrd.bin.

Changing permissions and create special files without root rights

The last section has shown you, how to create an image from files which already have correct ownership or just making everything owned by root. But this does not work, when you need finer permissions or special files in your rootfs. The last one is needed, if you build an rootfs for the kernels later than 2.6.12. To do this you have to start the tool fakeroot. Just enter

fakeroot

From now on, everything you do looks to the tools you are using as if you had root rights. So all files you create are owned by root, you can create special files and so on. The files you create are not really owned by root, fakeroot just overrides some system calls to track file creation and permission changes and fakes these for all programs. This means that after leaving and reentering the fakeroot environment, special files aren't special files anymore and files with an ownership different from root are now owned by root. So using this method, you have to recreate these files or their permissions after each invocation of fakeroot or use parameters -i and -s to save and restore the environment on each invocation of fakeroot. For further details consult the fakeroot man page.

After you have finished your work in the fakeroot environment, leave it with Ctrl-D or the exit command.

Bringing it all together

So a complete workflow to build a rootfs from scratch without root rights would look like that:

  1. Enter the fakeroot environment
  2. Fill your rootfs directory with the contents
  3. Adjust the permission with chown and chmod
  4. Generate the image with genext2fs and gzip
  5. Leave the fakeroot environment