summaryrefslogtreecommitdiff
path: root/scripts/install.sh
blob: 79bfa36bf94a08c4a111650aba3834bfec967a31 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env bash
if [ $(id -u) -ne 0 ];then
	printf "Installation is only possible as root\n"
	exit 1
fi

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

if [ ! -z $sfdisk -a -x $sfdisk ];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 mke2fs is installed"
mke2fs=$(which mke2fs)

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

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

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

cfgfs=1
rb532=0
while getopts "nr" option
do
	case $option in
		n)
			cfgfs=0
			;;
		r)
			rb532=1
			;;
		*)
			printf "Option not recognized\n"
			exit 1
			;;
	esac
done
shift $(($OPTIND - 1))


if [ -z $1 ];then
	printf "Please give your compact flash or USB device as first parameter\n"
	exit 1
else
	if [ -z $2 ];then
		printf "Please give your install tar archive as second parameter\n"
		exit 2
	fi
	if [ -f $2 ];then
		printf "Installing $2 on $1\n"
	else
		printf "$2 is not a file, Exiting\n"
		exit 1
	fi
	if [ $rb532 -eq 1 ];then
		if [ -z $3 ];then
			printf "Please give the kernel as third parameter\n"
			exit 2
		fi
		if [ -f $3 ];then
			printf "Installing $3 on $1\n"
		else
			printf "$3 is not a file, Exiting\n"
			exit 1
		fi
	fi
	if [ -b $1 ];then
		printf "Using $1 as CF/USB disk for installation\n"
		printf "This will destroy all data on $1, are you sure?\n"
		printf "Type "y" to continue\n"
		read y
		if [ "$y" = "y" ];then
			$sfdisk -l $1 2>&1 |grep 'No medium'
			if [ $? -eq 0 ];then
				exit 1
			else
				printf "Starting with installation\n"
			fi
		else
			printf "Exiting.\n"
			exit 1
		fi
	else
		printf "Sorry $1 is not a block device\n"
		exit 1
	fi
fi
	

if [ $(mount | grep $1| wc -l) -ne 0 ];then
	printf "Block device $1 is in use, please umount first.\n"
	exit 1
fi


if [ $($sfdisk -l $1 2>/dev/null|grep Empty|wc -l) -ne 4 ];then
	printf "Partitions already exist, should I wipe them?\n"
	printf "Type y to continue\n"
	read y
	if [ $y = "y" ];then
		printf "Wiping existing partitions\n"
		dd if=/dev/zero of=$1 bs=512 count=1 >/dev/null 2>&1
	else
		printf "Exiting.\n"
		exit 1
	fi
fi

case $2 in
	wrap*)
		speed=38400
		;;
	*)
		speed=115200
		;;
esac

if [ $rb532 -ne 0 ];then
	printf "Create partition and filesystem for rb532\n"
	rootpart=${1}2
	$parted -s $1 mklabel msdos
	sleep 2
	maxsize=$(env LC_ALL=C $parted $1 -s unit cyl print |awk '/^Disk/ { print $3 }'|sed -e 's/cyl//')
	rootsize=$(($maxsize-2))

	$parted -s $1 unit cyl mkpart primary ext2 0 1
	$parted -s $1 unit cyl mkpart primary ext2 1 $rootsize
	$parted -s $1 unit cyl mkpart primary fat32 $rootsize $maxsize
	$parted -s $1 set 1 boot on
	$sfdisk --change-id $1 1 27
	$sfdisk --change-id $1 3 88
	sleep 2
	$mke2fs ${1}2
	sync
	dd if=$3 of=${1}1 bs=2048
	sync
else
	rootpart=${1}1
	if [ $cfgfs -eq 0 ];then
		printf "Create partition and filesystem without cfgfs\n"
$sfdisk $1 << EOF
,,L
;
;
;
y
EOF
		$mke2fs ${rootpart}
	else
		printf "Create partition and filesystem with cfgfs\n"
		$parted -s $1 mklabel msdos
		sleep 2
		maxsize=$(env LC_ALL=C $parted $1 -s unit cyl print |awk '/^Disk/ { print $3 }'|sed -e 's/cyl//')
		rootsize=$(($maxsize-2))

		$parted -s $1 unit cyl mkpart primary ext2 0 $rootsize
		$parted -s $1 unit cyl mkpart primary fat32 $rootsize $maxsize
		$parted -s $1 set 1 boot on
		$sfdisk --change-id $1 2 88
		$mke2fs ${1}1
	fi
fi

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

sleep 2
$tune2fs -c 0 -i 0 -m 1 ${rootpart} >/dev/null
if [ $? -eq 0 ];then
	printf "Successfully disabled filesystem checks on ${rootpart}\n"
else	
	printf "Disabling filesystem checks failed, Exiting.\n"
	exit 1
fi	

tmp=$(mktemp -d)
mount -t ext2 ${rootpart} $tmp
printf "Extracting install archive\n"
tar -C $tmp -xzpf $2 
printf "Fixing permissions\n"
chmod 1777 $tmp/tmp
chmod 4755 $tmp/bin/busybox

if [ $rb532 -eq 0 ];then
	printf "Installing GRUB bootloader\n"
	mkdir -p $tmp/boot/grub
	mount -o bind /dev $tmp/dev
	chroot $tmp mount -t proc /proc /proc
	chroot $tmp mount -t sysfs /sys /sys
cat << EOF > $tmp/boot/grub/grub.cfg
set default=0
set timeout=1
serial --unit=0 --speed=$speed
terminal_output serial 
terminal_input serial 

menuentry "GNU/Linux (OpenADK)" {
	insmod ext2
	set root=(hd0,1)
	linux /boot/vmlinuz-adk root=/dev/sda1 ro init=/init console=ttyS0,$speed console=tty0 panic=10
}
EOF
	chroot $tmp grub-install $1
	umount $tmp/proc
	umount $tmp/sys
	umount $tmp/dev
fi

printf "Creating device nodes\n"
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

umount $tmp

printf "Successfully installed.\n"
exit 0