#!/bin/bash # Create an empty initrd # This script require to be run under root privilege... set -u set -e # Parameters # Initrd size in Mo INITRD_SIZE=16 # The output file for the initrd INITRD_OUTPUT="initrd.test" # Save the name of a temporary directory INITRD_TEMPDIR="" # Directory with stuff for the script INITRD_CONFIG_DIR="conf" # Busybox source location INITRD_BUSYBOX="$INITRD_CONFIG_DIR/busybox" # Initrd compression (activate by default) INITRD_COMPRESS=0 help_initrd () { echo "Usage: $0 [-o ] [-s ] [-z]" echo " -o : name of the initrd file" echo " -s : size of the initrd (in Mb)" echo " -z : disable compression" echo " -h : this message" exit 1 } # Create initrd file create_initrd () { # Create an empty file dd if=/dev/zero of=$INITRD_OUTPUT bs=1M count=$INITRD_SIZE # Format in ext2 with block size 1024, force and nothing reserved for # root ! Be quiet ! /sbin/mkfs -t ext2 -b 1024 -F -m 0 -q $INITRD_OUTPUT } mount_initrd () { # Create a temporary directory INITRD_TEMPDIR=`mktemp -d -t initrd_robot.XXXXXX` # Mount the initrd on the temporary directory mount -o loop -t ext2 $INITRD_OUTPUT $INITRD_TEMPDIR } create_empty_struct () { # Create an empty root structure mkdir -p $INITRD_TEMPDIR/{bin,dev,etc/{robot,init.d},proc,sbin,usr/{sbin,bin},robot,initrd,mnt/localsys} # Create all the devices files we need create_dev # Install the inittab install -m 644 $INITRD_CONFIG_DIR/inittab $INITRD_TEMPDIR/etc/inittab # Copy the rcS script install -m 755 $INITRD_CONFIG_DIR/rcS $INITRD_TEMPDIR/etc/init.d/rcS # Fstab install -m 644 $INITRD_CONFIG_DIR/fstab $INITRD_TEMPDIR/etc/fstab # Copy the update script for autonome mode install -m 755 $INITRD_CONFIG_DIR/update_initrd.sh $INITRD_TEMPDIR/etc/robot/update_initrd.sh # Udhcpc script install -m 755 $INITRD_CONFIG_DIR/udhcpc.script $INITRD_TEMPDIR/etc/udhcpc.script # Library ln -s /robot/lib $INITRD_TEMPDIR/lib } create_dev () { # Where is MAKEDEV ? makedev="/dev/MAKEDEV" # We have to move to the dev directory... cur_dir=`pwd` cd $INITRD_TEMPDIR/dev # Create device $makedev -v std # Create the console for logging ? $makedev -v consoleonly tty1 tty2 tty5 # tty5 -> log # Serial $makedev -v ttyS0 ttyS1 ttyS2 # Hard disk $makedev -v hda # For initrd #mknod -m 600 initctl p # XXX Complete... # Back to the originated directory cd $cur_dir } # XXX Usefull ? compil_busybox () { # Compile a busybox for the initrd # First, put the configuration file #cp $INITRD_CONFIG_DIR/config-busybox $INITRD_BUSYBOX/ # Compile busybox make -C $INITRD_BUSYBOX all 2>/dev/null # Move it cp $INITRD_BUSYBOX/{busybox,busybox.links} $INITRD_CONFIG_DIR/ } fill_struct_busybox () { # Copy busybox in bin install -m 755 $INITRD_BUSYBOX/busybox $INITRD_TEMPDIR/bin/busybox # Now we can create all the links provided by busybox # for c in $($INITRD_TEMPDIR/bin/busybox | # sed -n -e '/,/s/[,\[]/ /gp') # New version from applets/install.sh from busybox for c in $(sort $INITRD_BUSYBOX/busybox.links | uniq) do # Get the directory name and the rights links case "$(dirname $c)" in /) bb_path="bin/busybox" ;; /bin) bb_path="busybox" ;; /sbin) bb_path="../bin/busybox" ;; /usr/bin|/usr/sbin) bb_path="../../bin/busybox" ;; *) echo "Unknown installation directory: $appdir" exit 1 ;; esac echo "$c -> $bb_path" ln -s $bb_path $INITRD_TEMPDIR/$c done # We need, in ANY cases, the glibc, even in static (for gethostname # function for example) # In fact, even if after the compilation it said that we need the lib, # we do not need it... #cp /lib/libc.so.6 $INITRD_TEMPDIR/lib/ } umount_initrd () { # Umount the initrd directory umount $INITRD_TEMPDIR # Remove temporary files rmdir $INITRD_TEMPDIR # Need a compression ? if (( $INITRD_COMPRESS == 0 )) then gzip -f -4 $INITRD_OUTPUT fi } if [ $# -gt 5 ] then help_initrd fi while getopts "o:s:zh" option do case $option in o) INITRD_OUTPUT="$OPTARG";; s) INITRD_SIZE="$OPTARG";; z) INITRD_COMPRESS=1;; h) help_initrd;; \?) help_initrd;; esac done create_initrd mount_initrd || umount_initrd create_empty_struct || umount_initrd fill_struct_busybox || umount_initrd umount_initrd exit 0