summaryrefslogtreecommitdiff
path: root/scripts/create-image.sh
blob: bbebcf4d2855d828ecbe0a4bb9c310283ebf7ed1 (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
#!/usr/bin/env bash

grubinstall=1

while getopts ":tin" option
do
	case $option in
		t)
		emul=$OPTARG
		;;
		i)
		initramfs=1
		;;
		n)
		grubinstall=0
		;;
		*)
		printf "Option not recognized\n"
		exit 1
		;;
	esac
done
shift $(($OPTIND - 1))

if [ $(id -u) -ne 0 ];then
	printf "Installation is only possible as root\n"
	exit 1
fi

printf "Checking if grub is installed"
grub=$(which grub)

if [ ! -z $grub -a -x $grub ];then
	printf "...okay\n"
else
	printf "...failed\n"
	exit 1
fi

printf "Checking if parted is installed"
parted=$(which parted)

if [ ! -z $parted -a -x $parted ];then
	printf "...okay\n"
else
	printf "...failed\n"
	exit 1
fi

printf "Checking if qemu-img is installed"
qimg=$(which qemu-img)

if [ ! -z $qimg -a -x $qimg ];then
	printf "...okay\n"
else
	printf "...failed\n"
	exit 1
fi

if [ -z $1 ];then
	printf "Please give the name of the image file\n"
	exit 1
fi	

if [ -z $initramfs ];then
	if [ -z $2 ];then
		printf "Please give the name of the openadk archive file\n"
		exit 1
	fi	
else
	if [ -z $2 ];then
		printf "Please give the full path prefix to kernel/initramfs\n"
		exit 1
	fi
fi


printf "Generate qemu image\n"
$qimg create -f raw $1 300M

printf "Create partition and filesystem\n"
$parted -s $1 mklabel msdos
$parted -s $1 mkpart primary ext2 0 300
$parted -s $1 set 1 boot on
$parted -s $1 mkfs 1 ext2

if [ $? -eq 0 ];then
	printf "Successfully created partition\n"
	$parted $1 print
else
	printf "Partition creation failed, Exiting.\n"
	exit 1
fi


tmp=$(mktemp -d)

mount -o loop,offset=16384 -t ext2 $1 $tmp

if [ -z $initramfs ];then
	printf "Extracting install archive\n"
	tar -C $tmp -xzpf $2 
	printf "Fixing permissions\n"
	chmod 1777 $tmp/tmp
	chmod 4755 $tmp/bin/busybox
else
	printf "Copying kernel/initramfs\n"
	mkdir $tmp/boot $tmp/dev
	cp $2-kernel $tmp/boot/kernel
	cp $2-initramfs $tmp/boot/initramfs
fi

if [ $grubinstall -eq 1 ];then
printf "Copying grub files\n"
mkdir $tmp/boot/grub
cp /boot/grub/stage1 $tmp/boot/grub
cp /boot/grub/stage2 $tmp/boot/grub
cp /boot/grub/e2fs_stage1_5 $tmp/boot/grub

if [ -z $initramfs ];then
cat << EOF > $tmp/boot/grub/menu.lst
serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1
terminal --timeout=2 serial console
timeout 2
default 0
hiddenmenu
title linux
root (hd0,0)
kernel /boot/kernel root=/dev/sda1 init=/init console=ttyS0,115200 console=tty0 panic=10 rw
EOF
else
cat << EOF > $tmp/boot/grub/menu.lst
serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1
terminal --timeout=2 serial console
timeout 4
default 0
hiddenmenu
title linux
root (hd0,0)
kernel /boot/kernel root=/dev/sda1 console=ttyS0,115200 console=tty0 rw
initrd /boot/initramfs
EOF
fi

printf "Installing Grub bootloader\n"
$grub --batch --no-curses --no-floppy --device-map=/dev/null >/dev/null << EOF
device (hd0) $1
root (hd0,0)
setup (hd0)
quit
EOF

fi

printf "Creating device nodes\n"
mknod -m 666 $tmp/dev/zero c 1 5
mknod -m 666 $tmp/dev/null c 1 3
mknod -m 622 $tmp/dev/console c 5 1
mknod -m 666 $tmp/dev/tty c 5 0
mknod -m 666 $tmp/dev/tty0 c 4 0
mknod -m 660 $tmp/dev/hda b 3 0
mknod -m 660 $tmp/dev/hda1 b 3 1
mknod -m 666 $tmp/dev/ttyS0 c 4 64

umount $tmp

printf "Successfully installed.\n"
exit 0