#! /bin/bash set -e usage=\ "Usage: `basename $0` blocks inodes rootdir target Example: $0 1024 600 root rootimg" function usage() { if [ "$1" != "" ]; then echo "$1" 2>&1 fi echo "$usage" 2>&1 echo -n "" 2>&1 exit 10 } function INThandler() { if [ "$0" = "INT" ]; then echo -n "Interrupt! " 2>&1 fi if [ -n "$tmpfile" ] && [ -e "$tmpfile" ]; then df $tmpfile 2>&1 > /dev/null && { # echo -n "Unmounting ${tmpfile}. " 2>&1 ; umount $tmpfile ; } echo -n "Removing ${tmpfile}. " 2>&1 ; rm $tmpfile [ -n "$tmpmount" ] && [ -d "$tmpmount" ] && { echo -n "Removing $tmpmount." 2>&1 ; rmdir $tmpmount ; } fi echo "" 2>&1 exit 0 } function make_nodes { local devicedir=$1 local do_rm if [ "$devicedir" = "" ]; then echo -e "Emptry dedicedir. Cannot continue\a" 1>&2 exit 10 fi pushd "$devicedir" > /dev/null || { \ echo -e "Cannot change to the directory $devicedir\a" 1>&2; exit 10; } if [ `pwd` = "/dev" ]; then echo -e "Aieee! We are in device directory."\ "Something is terribly wrong!\a" 1>&2 exit 10 fi echo "$devicedir" | grep "/dev$" >/dev/null; if [ "$?" -ne "0" ]; then echo -e "Illegal devicedir $devicedir\a" 1>&2 exit 10 fi do_rm=no for node in $dir/* ; do if [ "$node" != "$dir"'/*' ]; then if [ -b "$node" -o -c "$node" -o -l "$node" ]; then do_rm=yes else echo -e "File $node not a node or link in directory $dir."\ "Refusing to continure.\a" 1>&2 exit 10 fi fi done if [ "$do_rm" = "yes" ]; then rm $dir/* fi mknod console c 5 1 mknod full c 1 7 # mknod hda b 3 0 # mknod hdb b 3 64 # mknod hdc b 22 0 # mknod hdd b 22 64 mknod kmem c 1 2 mknod mem c 1 1 mknod mtd0 c 90 0 mknod mtd1 c 90 2 mknod mtd2 c 90 4 mknod mtd3 c 90 6 mknod mtd4 c 90 8 mknod mtd5 c 90 10 mknod mtd6 c 90 12 mknod mtd7 c 90 14 mknod mtd8 c 90 16 mknod mtd9 c 90 18 # mknod mtd10 c 90 20 # mknod mtd11 c 90 22 # mknod mtd12 c 90 24 # mknod mtd13 c 90 26 # mknod mtd14 c 90 28 # mknod mtd15 c 90 30 # mknod mtd16 c 90 32 mknod mtdblock0 b 31 0 mknod mtdblock1 b 31 1 mknod mtdblock2 b 31 2 mknod mtdblock3 b 31 3 mknod mtdblock4 b 31 4 mknod mtdblock5 b 31 5 mknod mtdblock6 b 31 6 mknod mtdblock7 b 31 7 mknod mtdblock8 b 31 8 mknod mtdblock9 b 31 9 # mknod mtdblock10 b 31 10 # mknod mtdblock11 b 31 11 # mknod mtdblock12 b 31 12 # mknod mtdblock13 b 31 13 # mknod mtdblock14 b 31 14 # mknod mtdblock15 b 31 15 # mknod mtdblock16 b 31 16 mknod mtdr0 c 90 1 mknod mtdr1 c 90 3 mknod mtdr2 c 90 5 mknod mtdr3 c 90 7 mknod mtdr4 c 90 9 mknod mtdr5 c 90 11 mknod mtdr6 c 90 13 mknod mtdr7 c 90 15 mknod mtdr8 c 90 17 mknod mtdr9 c 90 19 # mknod mtdr10 c 90 21 # mknod mtdr11 c 90 23 # mknod mtdr12 c 90 25 # mknod mtdr13 c 90 27 # mknod mtdr14 c 90 29 # mknod mtdr15 c 90 31 # mknod mtdr16 c 90 33 mknod null c 1 3 mknod port c 1 4 mknod ppp c 108 0 mknod ram0 b 1 0 mknod ram1 b 1 1 ln -s ram0 ram mknod random c 1 8 mknod tty c 5 0 mknod tty0 c 4 0 mknod tty1 c 4 1 # mknod tty2 c 4 2 # mknod tty3 c 4 3 # mknod tty4 c 4 4 mknod ttyAM0 c 204 16 ln -s ttyAM0 ttyS0 mknod urandom c 1 9 mknod vcs0 c 7 0 mknod vcs1 c 7 1 mknod vcsa0 c 7 128 mknod vcsa1 c 7 129 mknod zero c 1 5 popd >/dev/null; } id=`whoami` [ "$id" != "root" ] && { \ echo -e "You must be root to run \"$0 $*\"\a" 1>&2; \ exit 10; \ } realid=`who am i | perl -lne 'print $1 if(/!(\S+)/)'` [ "$#" -lt 4 ] && usage size=$1 inodes=$2 dir=$3 realtarget=$4 [ "$inodes" -gt 1 ] || usage "Inodes count \"$inodes\" too small" [ -d "$dir" ] || usage "Cannot find directory $dir" for subdir in bin sbin etc dev; do [ -d "$dir/$subdir" ] || \ usage "Cannot find directory $subdir in $dir" done pid=$$ trap INThandler INT EXIT tmpfile=/tmp/tmp.minix.$$ tmpmount=/tmp/tmp.mount.$$ if [ "${realtarget}X" = "X" ]; then if [ "$realid" != "" ]; then target=/tmp/root_fs_$realid else target=/tmp/root_fs fi else target=$realtarget fi [ -e $tmpfile ] && usage "Cannot create $tmpfile: file exists" touch $tmpfile || { echo "Cannot create $tmpfile" test -e $tmpfile rm $tmpfile 2>&1 >/dev/null exit 10 } dd if=/dev/zero of=$tmpfile bs=1k count=$size 2> /dev/null mkdir $tmpmount || { echo "Cannot create $tmpmount" rm -f $tmpfile exit 10 } echo "Creating minix filesystem" /sbin/mkfs.minix -i $inodes $tmpfile mount $tmpfile $tmpmount -o loop cp -a $dir/* $tmpmount || { echo "Cannot copy files to $tmpmount" exit 10; } make_nodes $tmpmount/dev chown -R root:root $tmpmount echo "Blocks:" df $tmpmount echo "" echo "Inodes:" df -i $tmpmount echo "" echo "Filesystem root directory:" ls $tmpmount chown -R root:root $tmpmount echo "Press enter to unmount and copy ramdisk image to $target" read line umount $tmpmount if [ "$error" != "yes" ]; then if [ "${realidX}" != "X" -a "${realtarget}X" != "X" ]; then sudo -u $realid test -f $target && sudo -u $realid rm -f $target sudo -u $realid cp $tmpfile $target sudo -u $realid ls -l $target else test -f $target && rm -f $target cp $tmpfile $target chmod 666 $target ls -l $target fi # perl -e '$size='$size'*1024; # print sprintf("initrd_start=0x%08x initrd_end=0xc0800000\n" . # "Lauterbach root load address: 0x%08x\n", # 0xc0800000 - $size, 0x10800000 - $size);' fi rm $tmpfile rm -r $tmpmount found=no for file in /tmp/tmp.mount.* /tmp/tmp.minix.*; do if [ -e $file ]; then if [ "$found" = "no" ]; then echo -e "\n\n\aOld tmp files found, please clean them up." found=yes fi ls -ld $file fi done