CRUX-ARM : Home

Home :: Documentation :: Download :: Development :: Community :: Ports :: Packages :: Bugs :: Links :: About :: Donors
Merge branch '2.7' into 2.8
[initrd.git] / mkinitrd.sh
1 #!/bin/sh
2 #
3 # mkinitrd.sh: Shell script to make an initial initrd image
4 # by Jose V Beneyto, sepen at crux dot nu
5
6 msgUsage() {
7 echo "Usage: $APPNAME <options>"
8 echo "Where options are:"
9 echo " --name=NAME Name for the result image (default: initrd.gz)"
10 echo " --size=SIZE Size expecified in bytes (default: 4096)"
11 echo " --type=TYPE Filesystem type for the image (default: ext2)"
12 echo "Example:"
13 echo " $APPNAME --name=myinitrd.gz --size=8192"
14 exit 0
15 }
16
17 msgError() {
18 echo "Error, $@" 2>&1
19 exit 1
20 }
21
22 checkRoot() {
23 [ "$(id -u)" != "0" ] && msgError "you need to be root to do this."
24 }
25
26 parseArgs() {
27 [ $# -lt 1 ] && msgUsage
28 for arg in $@; do
29 case $arg in
30 --name=*) IMG_NAME=${arg##*=} ;;
31 --size=*) IMG_SIZE=${arg##*=} ;;
32 --type=*) IMG_TYPE=${arg##*=} ;;
33 *) msgUsage ;;
34 esac
35 done
36 }
37
38 main() {
39 rm -rf $TMP_PATH $MNT_PATH
40 install -d $TMP_PATH $MNT_PATH
41 pushd $TMP_PATH && \
42 rm -f initrd
43 dd if=/dev/zero of=initrd bs=1024 count=$IMG_SIZE && \
44 mkfs.$IMG_TYPE -F -m 0 -b 1024 initrd $IMG_SIZE && \
45 mount -v -o loop -t $IMG_TYPE initrd $MNT_PATH && \
46 rm -rf $MNT_PATH/lost+found && \
47 install -d -m 0755 $MNT_PATH/{mnt,media,etc,dev,sys,proc,lib,usr,var/{log,lock,run},tmp} && \
48 mknod $MNT_PATH/dev/console c 5 1 && chmod 666 $MNT_PATH/dev/console && \
49 mknod $MNT_PATH/dev/null c 1 3 && chmod 666 $MNT_PATH/dev/null && \
50 mknod $MNT_PATH/dev/tty c 5 0 && chmod 666 $MNT_PATH/dev/tty && \
51 mkdir $MNT_PATH/dev/rd && mknod $MNT_PATH/dev/rd/0 b 1 0
52 mknod $MNT_PATH/dev/ram0 b 1 0 && chmod 600 $MNT_PATH/dev/ram0
53 for i in 0 1 2 3 4 5 6 7; do
54 mknod $MNT_PATH/dev/tty$i c 4 $i && chmod 666 $MNT_PATH/dev/tty$i
55 done && \
56 umount -v $MNT_PATH && \
57 gzip -v initrd && \
58 popd && \
59 mv $TMP_PATH/initrd.gz $IMG_NAME
60 }
61
62 APPNAME="$(basename $0)"
63
64 TMP_PATH="$(cd $(dirname $APPNAME); pwd)/.tmp"
65 MNT_PATH="$(cd $(dirname $APPNAME); pwd)/.mnt"
66
67 IMG_SIZE=4096 # default image size = 4MB
68 IMG_TYPE=ext2 # default filesystem type = ext2
69
70 export PATH=$PATH:/sbin:/usr/sbin
71
72 checkRoot
73 parseArgs $@ && \
74 main || msgError "$APPNAME failed"
75
76 umount -v $MNT_PATH
77 rm -rf $TMP_PATH $MNT_PATH
78
79 # End of file