User Tools

Site Tools


qLinux
… a distribution study


<< Prev
Make the System Bootable

Next >>
GRUB-2.06


Create an Initramfs

Preface

Using a statically linked binary of busybox (musl) make our life easier. However, not all commands of busybox are suitable for us.

Part I

Create a directory  image  and change into it. The next steps will be done inside this directory. Create the directory layout:

cd image
mkdir -pv qlnx/{bin,etc,dev,lib,prc,sys}
ln -sv qlnx/{bin,etc,dev,lib,prc,sys} .
mv -v /prc /proc
mkdir -v /run

Copy the required tools and the related shared libraries into the image directory. Starting with  bash :

$ cp /qlnx/bin/bash /qlnx/bin/sh qlnx/bin
$ ldd /qlnx/bin/bash
        /qlnx/lib/ld-musl-x86_64.so.1 (0x7f115adc4000)
        libreadline.so.8 => /qlnx/lib/libreadline.so.8 (0x7f115ad6e000)
        libhistory.so.8 => /qlnx/lib/libhistory.so.8 (0x7f115ad61000)
        libncursesw.so.6 => /qlnx/lib/libncursesw.so.6 (0x7f115acf8000)
        libc.so => /qlnx/lib/ld-musl-x86_64.so.1 (0x7f115adc4000)

Copy the libc and the loader. This is required one time only:

$ cp -vnd /qlnx/lib/libc.so* /qlnx/lib/ld-musl-x86_64.so.1 qlnx/lib

Copy the other listed listed libraries including symlinks:

$ cp -vnd /qlnx/lib/qlnx/lib/libreadline.so* \
$ cp -vnd /qlnx/lib/qlnx/lib/libhistory.so* \
$ cp -vnd /qlnx/lib/qlnx/lib/libncursesw.so* qlnx/lib

Repeat this for the following tools:

bash
cut
echo
killall
ln
ls
mkdir
mknod
modprobe
mount
setsid
sh     <-- link to bash
switch_root

Put  busybox  to the image and create some recommended symlinks:

cd <image-dir>/bin
cp -vnd busybox .
ln -s busybox poweroff
ln -s busybox reboot

It is highly recommended to create symlinks or make copies of  busybox . The issue is that all hard links will change if one of them will be overwritten. Thus if you copy a regular qlnx binary to the image and overwrite a hard link  busybox  itself will be overwritten.

Create some devices:

mknod -m 644 dev/console c 5 1
mknod -m 666 dev/null c 1 3

Create a  fstab  file:

echo "devtmpfs        /qlnx/dev       devtmpfs        mode=0755       0 0" > /qlnx/etc/fstab

Create a file  init  in the root of the image directory

#!/qlnx/bin/sh

PATH="/qlnx/bin:/bin"

mount -t proc  none /qlnx/prc
mount -t sysfs none /qlnx/sys

[ ! -e /dev/console ] && mknod /dev/console c 5 1
[ ! -e /dev/null ]    && mknod /dev/null c 1 3
# The next devices are not required if we mount /dev (see below)
[ ! -e /dev/tty ]     && mknod /dev/tty c 5 0
[ ! -e /dev/tty0 ]    && mknod /dev/tty0 c 4 0
[ ! -e /dev/tty1 ]    && mknod /dev/tty1 c 4 1
[ ! -e /dev/ttyS0 ]   && mknod /dev/ttyS0 c 4 64
[ ! -e /dev/ttyS1 ]   && mknod /dev/ttyS1 c 4 65
[ ! -e /dev/random ]  && mknod /dev/random c 1 8
[ ! -e /dev/urandom ] && mknod /dev/urandom c 1 9
[ ! -e /dev/zero ]    && mknod /dev/zero c 1 5

mount devtmpfs

echo -e "\nBoot took $(cut -d' ' -f1 /qlnx/prc/uptime) seconds\n"

exec /qlnx/bin/setsid /qlnx/bin/sh

setsid
    This prevents an error, that “job control is turned off” in an interactive shell.

Create the initramfs image itself. We use the  cpio  from busybox:

cd image
find . | busybox cpio -ov -H newc | gzip -9 >../initrd.img

Test It

If you don't want to test the step until here then jump to Part II.

qemu-system-x86_64 -kernel bzImage -nographic -append 'console=ttyS0' -initrd initrd.img

-nographic -append 'console=ttyS0'
    Run in the current terminal.

The initramfs part starts with a line:

Unpacking initramfs...

Now you can play a little bit around. To exit simply run

poweroff -f

Part II

Remove the last line from [init  and add the following lines (replace  /dev/sdb1  with the correct device):

mkdir /newroot
rootdev="/qlnx/dev/sdb1"
mount $rootdev /newroot

mount --move /qlnx/sys /newroot/qlnx/sys
mount --move /qlnx/prc /newroot/qlnx/prc
mount --move /qlnx/dev /newroot/qlnx/dev

# Now switch to the new filesystem, and run /qlnx/bin/sinit out of it. Use
# "exec" here, because you want the new init program to inherit PID 1.
exec switch_root /newroot /qlnx/bin/sinit

Some people will  umount  the filesystems instead of a  mount –move . There might be situations where a new mount after  switch_root  is more clean, but most commonly moving the mount points is ok.

Recreate the initramfs image. We use the  cpio  from busybox:

cd image
find . | busybox cpio -ov -H newc | gzip -9 >../initrd.img

Copy the  initrd.img  image and and the kernel  bzImage  to your boot directory. Adjust your bootloader (here is a simple reference).


<< Prev
Make the System Bootable

        

Next >>
GRUB-2.06