summaryrefslogtreecommitdiff
path: root/i/pc104/initrd/create.sh
blob: 9bca54b8b3df0d48ccff6dd37dadfd2f5dbe5a0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/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 <file>] [-s <size>] [-z]"
	echo "  -o <file> 	: name of the initrd file"
	echo "  -s <size> 	: 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,proc,sbin,usr/{sbin,bin},var/lock,robot,initrd,mnt/localsys}
	# Create all the devices files we need
	create_dev
	# Fstab
	install -m 644 $INITRD_CONFIG_DIR/fstab $INITRD_TEMPDIR/etc/fstab
	# Udhcpc script
	install -m 755 $INITRD_CONFIG_DIR/udhcpc.script $INITRD_TEMPDIR/etc/udhcpc.script
	# Copy the linuxrc script
	install -m 755 $INITRD_CONFIG_DIR/linuxrc $INITRD_TEMPDIR/linuxrc
	# Copy the update script for autonome mode
	install -m 755 $INITRD_CONFIG_DIR/update_initrd.sh $INITRD_TEMPDIR/etc/update_initrd.sh
}

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 ? Useful ? XXX
	$makedev -v consoleonly
	#tty1 tty2 tty3 tty4 tty5
	# Serial
	$makedev -v ttyS0 ttyS1 ttyS2
	# Hard disk
	$makedev -v hda
	# 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 -9 $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