1#!/bin/bash
2#
3# mkrootfs.sh - creates a root file system
4#
5
6# TODO: need to add checks here to verify that busybox, uClibc and bzImage
7# exist
8
9
10# command-line settable variables
11BUSYBOX_DIR=..
12UCLIBC_DIR=../../uClibc
13TARGET_DIR=./loop
14FSSIZE=4000
15CLEANUP=1
16MKFS='mkfs.ext2 -F'
17
18# don't-touch variables
19BASE_DIR=`pwd`
20
21
22while getopts 'b:u:s:t:Cm' opt
23do
24	case $opt in
25		b) BUSYBOX_DIR=$OPTARG ;;
26		u) UCLIBC_DIR=$OPTARG ;;
27		t) TARGET_DIR=$OPTARG ;;
28		s) FSSIZE=$OPTARG ;;
29		C) CLEANUP=0 ;;
30		m) MKFS='mkfs.minix' ;;
31		*)
32			echo "usage: `basename $0` [-bu]"
33			echo "  -b DIR  path to busybox direcory (default ..)"
34			echo "  -u DIR  path to uClibc direcory (default ../../uClibc)"
35			echo "  -t DIR  path to target direcory (default ./loop)"
36			echo "  -s SIZE size of root filesystem in Kbytes (default 4000)"
37			echo "  -C      don't perform cleanup (umount target dir, gzip rootfs, etc.)"
38			echo "          (this allows you to 'chroot loop/ /bin/sh' to test it)"
39			echo "  -m      use minix filesystem (default is ext2)"
40			exit 1
41			;;
42	esac
43done
44
45
46
47
48# clean up from any previous work
49mount | grep -q loop
50[ $? -eq 0 ] && umount $TARGET_DIR
51[ -d $TARGET_DIR ] && rm -rf $TARGET_DIR/
52[ -f rootfs ] && rm -f rootfs
53[ -f rootfs.gz ] && rm -f rootfs.gz
54
55
56# prepare root file system and mount as loopback
57dd if=/dev/zero of=rootfs bs=1k count=$FSSIZE
58$MKFS -i 2000 rootfs
59mkdir $TARGET_DIR
60mount -o loop,exec rootfs $TARGET_DIR # must be root
61
62
63# install uClibc
64mkdir -p $TARGET_DIR/lib
65cd $UCLIBC_DIR
66make INSTALL_DIR=
67cp -a libc.so* $BASE_DIR/$TARGET_DIR/lib
68cp -a uClibc*.so $BASE_DIR/$TARGET_DIR/lib
69cp -a ld.so-1/d-link/ld-linux-uclibc.so* $BASE_DIR/$TARGET_DIR/lib
70cp -a ld.so-1/libdl/libdl.so* $BASE_DIR/$TARGET_DIR/lib
71cp -a crypt/libcrypt.so* $BASE_DIR/$TARGET_DIR/lib
72cd $BASE_DIR
73
74
75# install busybox and components
76cd $BUSYBOX_DIR
77make distclean
78make CC=$BASE_DIR/$UCLIBC_DIR/extra/gcc-uClibc/i386-uclibc-gcc
79make CONFIG_PREFIX=$BASE_DIR/$TARGET_DIR install
80cd $BASE_DIR
81
82
83# make files in /dev
84mkdir $TARGET_DIR/dev
85./mkdevs.sh $TARGET_DIR/dev
86
87
88# make files in /etc
89cp -a etc $TARGET_DIR
90ln -s /proc/mounts $TARGET_DIR/etc/mtab
91
92
93# other miscellaneous setup
94mkdir $TARGET_DIR/initrd
95mkdir $TARGET_DIR/proc
96
97
98# Done. Maybe do cleanup.
99if [ $CLEANUP -eq 1 ]
100then
101	umount $TARGET_DIR
102	rmdir $TARGET_DIR
103	gzip -9 rootfs
104fi
105