HackInstall: hackinstall.sh

File hackinstall.sh, 6.6 KB (added by rysiek, 3 years ago)

HackInstall script, ver. 0.1beta

Line 
1#!/bin/bash
2#
3# hackable1 automagic installer
4#
5# (c) 2009 Mike 'rysiek' Woźniak <rysiek@brama.elka.pw.edu.pl>
6#     licensed under GPL ver.3 or any later
7#
8# installs hackable1 from given tarballs to a given uSD card device
9#
10# usage:
11#   hackinstall.sh /dev/some_device /path/to/fat_kernel.tar.gz /path/to/ext2_partition.tar.gz [/path/to/optional_fix.tar.gz]
12#
13# WARNING: this script WILL re-partition a given device, and WILL cause data loss on that device!
14#
15
16# ToDo:
17#  sanity checks!
18#  - file existance;
19#  - permissions;
20#  - tools availability;
21#  cleanup after fsckup!
22#  tar file format discovery (gz/bz2)
23#  wget support (hell, yeah!)
24
25# variables
26HI_DEVICE="$1"
27HI_FATTGZ="$2"
28HI_EXTTGZ="$3"
29
30# UUID, meh ;)
31HI_UUID="$( date "+%s.%N" )"
32
33# mount points
34HI_FAT_MP="/tmp/fat.$HI_UUID"
35HI_EXT_MP="/tmp/ext.$HI_UUID"
36
37# self identification
38HI_ID="Hackable1 uSD Automated Installer"
39HI_AUTHOR="(c) 2009 Michał 'rysiek' Woźniak <rysiek@brama.elka.pw.edu.pl>"
40HI_GPL="    licensed under GPL ver.3 or any later"
41
42# utils
43function hi_usage() {
44        echo
45        echo "$HI_ID"
46        echo "$HI_AUTHOR"
47        echo "$HI_GPL"
48        echo
49        echo "usage:"
50        echo -e " $0 /dev/some_device fat_kernel.tar.gz ext2_partition.tar.gz [optional_fix.tar.gz]"
51        echo
52}
53
54function hi_exit_with_error() {
55        echo -e "errors encountered:\n$1"
56        exit $2
57}
58
59# do we have the optional fix?
60if [[ -n "$4" ]]; then
61        HI_FIXTGZ="$4"
62fi
63
64# sanity checks
65if [[ "$1" == "-h" || "$1" == "--help" || -z "$HI_DEVICE" || -z "$HI_FATTGZ" || -z "$HI_EXTTGZ" ]]; then
66        hi_usage
67        exit 0
68fi
69
70# identify yourself
71echo
72echo "$HI_ID"
73echo "$HI_AUTHOR"
74echo "$HI_GPL"
75echo
76
77# is the user *sure*?
78echo "If you choose to proceed, ALL AND ANY data on the $HI_DEVICE device"
79echo "WILL BE IRREVERSIBLY DELETED! Choose wisely!"
80echo
81echo -n "proceed? (y/N): "
82read HI_AGREE
83if [[ "$HI_AGREE" != "y" && "$HI_AGREE" != "Y" ]]; then
84        echo "aborted."
85        exit 0
86fi
87echo "proceeding..."
88echo
89
90# partition the card
91echo "+- re-partitioning $HI_DEVICE:"
92HI_DBG_OUTPUT="$( (
93        echo "set send_human {.1 .3 1 .05 2}"
94        # spawning...
95        echo "spawn fdisk \"$HI_DEVICE\""
96        echo "expect \"Command (m for help):\""
97        # clearing the partition table...
98        echo "send_error \"   - clearing the partition table... \""
99        echo "send -h \"o\r\""
100        echo "expect \"Command (m for help):\""
101        echo "send_error \"done\n\""
102        # new partition for fat...
103        echo "send_error \"   - creating the fat partition... \""
104        echo "send -h \"n\r\""
105        echo "expect \"primary partition (1-4)\""
106        echo "send -h \"p\r\""
107        echo "expect \"Partition number (1-4):\""
108        echo "send -h \"1\r\""
109        echo "expect \"First cylinder\""
110        echo "send -h \r"
111        echo "expect \"Last cylinder or +size or +sizeM or +sizeK\""
112        echo "send -h \"+8M\r\""
113        echo "expect \"Command (m for help):\""
114        echo "send -h \"t\r\""
115        echo "expect \"Hex code (type L to list codes)\""
116        echo "send -h \"4\r\""
117        echo "expect \"Command (m for help):\""
118        echo "send_error \"done\n\""
119        # new partition for ext...
120        echo "send_error \"   - creating the ext partition... \""
121        echo "send -h \"n\r\""
122        echo "expect \"primary partition (1-4)\""
123        echo "send -h \"p\r\""
124        echo "expect \"Partition number (1-4):\""
125        echo "send -h \"2\r\""
126        echo "expect \"First cylinder\""
127        echo "send -h \r"
128        echo "expect \"Last cylinder or +size or +sizeM or +sizeK\""
129        echo "send -h \r"
130        echo "expect \"Command (m for help):\""
131        echo "send_error \"done\n\""
132        # writing to disk
133        echo "send_error \"   - writing changes to disk... \""
134        echo "send -h \"w\r\""
135        echo "expect eof"
136) | expect - )" 2>&1 || hi_exit_with_error "$HI_DBG_OUTPUT" 2
137echo "done."
138
139# finding partition device files
140echo "+- discovering partition devices:"
141HI_DEVICE_EXT="$( ls -1 "$HI_DEVICE"* | egrep -v "^$HI_DEVICE$" )"
142HI_DEVICE_FAT="$( echo -ne "$HI_DEVICE_EXT" | head -n 1 )"
143HI_DEVICE_EXT="$( echo -ne "$HI_DEVICE_EXT" | tail -n 1 )"
144# fat partition
145if [[ -z "$HI_DEVICE_FAT" ]]; then
146        echo "   - fat partition device: not found! aborting..."
147        exit 3
148else
149        echo "   - fat partition device: $HI_DEVICE_FAT"
150fi
151# ext partition
152if [[ -z "$HI_DEVICE_EXT" ]]; then
153        echo "   - ext partition device: not found! aborting..."
154        exit 3
155else
156        echo "   - ext partition device: $HI_DEVICE_EXT"
157fi
158# not the same file, I assume?..
159if [[ "$HI_DEVICE_FAT" == "$HI_DEVICE_EXT" ]]; then
160        echo "    - fat and ext partition device paths must differ! aborting..."
161        exit 3
162fi
163
164# create filesystems...
165# ...fat...
166echo "+- formatting:"
167echo -n "   - $HI_DEVICE_FAT as vfat... "
168HI_DBG_OUTPUT="$( mkfs.vfat "$HI_DEVICE_FAT" 2>&1 )" || hi_exit_with_error "$HI_DBG_OUTPUT" 4
169echo "done."
170# ...ext2...
171echo -n "   - $HI_DEVICE_EXT as ext2... "
172HI_DBG_OUTPUT="$( mkfs.ext2 "$HI_DEVICE_EXT" 2>&1 )" || hi_exit_with_error "$HI_DBG_OUTPUT" 4
173echo "done."
174
175# create mount points...
176# ...fat...
177echo "+- creating mount points:"
178echo -n "   - $HI_FAT_MP... "
179HI_DBG_OUTPUT="$( mkdir "$HI_FAT_MP" 2>&1 )" || hi_exit_with_error "$HI_DBG_OUTPUT" 5
180echo "done."
181# ...ext2...
182echo -n "   - $HI_EXT_MP... "
183HI_DBG_OUTPUT="$( mkdir "$HI_EXT_MP" 2>&1 )" || hi_exit_with_error "$HI_DBG_OUTPUT" 5
184echo "done."
185
186# mount the partitions...
187# ...fat...
188echo "+- mounting:"
189echo -n "   - $HI_DEVICE_FAT to $HI_FAT_MP... "
190HI_DBG_OUTPUT="$( mount "$HI_DEVICE_FAT" "$HI_FAT_MP" 2>&1 )" || hi_exit_with_error "$HI_DBG_OUTPUT" 6
191echo "done."
192# ...ext2...
193echo -n "   - $HI_DEVICE_EXT to $HI_EXT_MP... "
194HI_DBG_OUTPUT="$( mount "$HI_DEVICE_EXT" "$HI_EXT_MP" 2>&1 )" || hi_exit_with_error "$HI_DBG_OUTPUT" 6
195echo "done."
196
197# untaring...
198echo "+- untaring:"
199# ...fat kernel...
200echo -n "   - $HI_FATTGZ to $HI_FAT_MP... "
201HI_DBG_OUTPUT="$( tar -C "$HI_FAT_MP" -xomzf "$HI_FATTGZ" 2>&1 )" || hi_exit_with_error "$HI_DBG_OUTPUT" 7
202echo "done."
203
204# ...ext partition...
205echo -n "   - $HI_EXTTGZ to $HI_EXT_MP... "
206HI_DBG_OUTPUT="$( tar -C "$HI_EXT_MP" -xzf "$HI_EXTTGZ" 2>&1 )" || hi_exit_with_error "$HI_DBG_OUTPUT" 8
207echo "done."
208
209# ...optionally, the fix...
210if [[ -n "$HI_FIXTGZ" ]]; then
211        echo -n "   - $HI_FIXTGZ to $HI_EXT_MP... "
212        HI_DBG_OUTPUT="$( tar -C "$HI_EXT_MP" -xzf "$HI_FIXTGZ" 2>&1 )" || hi_exit_with_error "$HI_DBG_OUTPUT" 9
213        echo "done."
214fi
215
216# umount
217# ...fat...
218echo "+- unmounting:"
219echo -n "   - $HI_DEVICE_FAT... "
220HI_DBG_OUTPUT="$( umount "$HI_DEVICE_FAT" 2>&1 )" || hi_exit_with_error "$HI_DBG_OUTPUT" 10
221echo "done."
222# ...ext2...
223echo -n "   - $HI_DEVICE_EXT... "
224HI_DBG_OUTPUT="$( umount "$HI_DEVICE_EXT" 2>&1 )" || hi_exit_with_error "$HI_DBG_OUTPUT" 10
225echo "done."
226
227# cleanup
228echo "+- removing mountpoints:"
229echo -n "   - $HI_FAT_MP... "
230HI_DBG_OUTPUT="$( rm -rf "$HI_FAT_MP" 2>&1 )" || hi_exit_with_error "$HI_DBG_OUTPUT" 11
231echo "done."
232echo -n "   - $HI_EXT_MP... "
233HI_DBG_OUTPUT="$( rm -rf "$HI_EXT_MP" 2>&1 )" || hi_exit_with_error "$HI_DBG_OUTPUT" 11
234echo "done."
235
236# AOK, all done!
237echo "all done."
238exit 0