-
Notifications
You must be signed in to change notification settings - Fork 8
/
ubuntuCoreSetup.sh
executable file
·267 lines (228 loc) · 8.1 KB
/
ubuntuCoreSetup.sh
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/bin/bash
# This script attempts to download the release image
# for a particular Ubuntu Core distribution and
# architecture.
#
# It is strongly advised to run the script in the root folder
# where you want your Ubuntu Core filesystem to be extracted to.
#
# Parameters:
# 1 - Is the Ubuntu core distribution name
# e.g.: oneiric, precise, etc...
# 2 - Is the Ubuntu core architecture name
# e.g.: armel, or armhf (precise onwards only).
function abort()
{
echo "Aborting..."
exit 1
}
# First verify the script is run with sudo
# privileges.
if [ $(id -u) -ne 0 ]; then
echo "You don't have sufficient privileges to run this script."
abort
fi
# Verify parameters are set
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Please specify a distribution name in 1st parameter."
echo "......... and an architecture name in 2nd parameter."
abort
else
DISTRIB="$1"
ARMARCH="$2"
fi
distros=([1]=oneiric [2]=precise [3]=quantal)
archis=([1]=armel [2]=armhf)
# Validate parameters
success_distros=false
success_archis=false
for element in $(seq 1 ${#distros[@]})
do
if [ "${distros[$element]}" = "$DISTRIB" ]; then
success_distros="true"
fi
done
for element in $(seq 1 ${#archis[@]})
do
if [ "${archis[$element]}" = "$ARMARCH" ]; then
success_archis="true"
fi
done
if [ "$success_distros" = "false" ]; then
echo "Invalid distribution specified in parameter one!"
abort
fi
if [ "$success_archis" = "false" ]; then
echo "Invalid architecture specified in parameter two!"
abort
fi
current_path=`pwd`
# Verify path where script is executed
if [ "$current_path" = "/" ]; then
echo "Script does not allow to run in /"
abort
fi
while true; do
read -p "Do you wish to run this script in $current_path ?" yn
case $yn in
[Yy]* ) break;;
[Nn]* ) abort;;
* ) echo "Please answer yes or no.";;
esac
done
# DISTNUM needs an array or something for future versions
# Today only oneiric and precise are handled.
# Hoping Canonical will continue with the precise naming
# scheme too...
DISTNUM="12.04.2"
# if an extracted filesystem is already there, wipe it out!
if [ -d "usr" ] && [ -d "etc" ]; then
echo "An existing filesystem seems to be already present."
while true; do
read -p "Do you wish to wipe out everything in $current_path ?" yn
case $yn in
[Yy]* ) break;;
[Nn]* ) abort;;
* ) echo "Please answer yes or no.";;
esac
done
echo "Cleaning up previous core filesystem..."
rm -rf *
fi
# Verify 'wget' is installed
hash wget 2>&- || { echo >&2 "Script requires wget utility but it's not installed."; abort; }
# Verify 'head' is installed
hash head >&- || { echo >&2 "Script requires head utility but it's not installed."; abort; }
# Verify 'grep' is installed
hash grep 2>&- || { echo >&2 "Script requires grep utility but it's not installed."; abort; }
# Attempt to download the requested ubuntu core image
# If it doesn't exist, downloads the current daily image
# for the particular distribution and architecture. If not
# available, just fails.
if [ "$DISTRIB" = "oneiric" ]; then
DISTNUM="11.10"
elif [ "$DISTRIB" = "quantal" ]; then
DISTNUM="12.10"
fi
core_image="ubuntu-core-$DISTNUM-core-$ARMARCH.tar.gz"
core_path="http://cdimage.ubuntu.com/ubuntu-core/releases/$DISTNUM/release"
echo "Trying to download $core_path/$core_image ..."
wget -q -O "$core_image" "$core_path"/"$core_image"
if [ $? -ne 0 ]; then
echo "Requested $DISTRIB distribution for architecture $ARMARCH doesn't exist. Trying daily image..."
core_path="http://cdimage.ubuntu.com/ubuntu-core/daily/current"
core_image="$DISTRIB-core-$ARMARCH.tar.gz"
echo "Trying to download $core_path/$core_image ..."
wget -q -O "$core_image" "$core_path"/"$core_image"
if [ $? -ne 0 ]; then
echo "Failed to find any Ubuntu Core image for $DISTRIB distribution on $ARMARCH !!!"
abort
fi
fi
echo "Successfully downloaded $core_image"
# Extracting the Ubuntu Core filesystem
# in the folder where this script is run.
if [ -f "$core_image" ]; then
echo "Extracting $core_image filesystem in $current_path"
tar xfz "$core_image"
# Don't keep the tarball image.
rm "$core_image"
fi
echo "Setting up the Ubuntu Core Image for TI network..."
##
## Create a serial console on the UART.
##
echo "Setting up serial console on UART..."
sh -c "cat > etc/init/serial-auto-detect-console.conf << EOF
# serial-auto-detect-console - starts getty on serial console
#
# This service starts a getty on the serial port given in the console kernel argument.
#
start on runlevel [23]
stop on runlevel [!23]
respawn
exec /bin/sh /bin/serial-console
EOF
"
sh -c 'cat >bin/serial-console << EOF
for arg in \$(cat /proc/cmdline)
do
case \$arg in
console=*)
tty=\${arg#console=}
tty=\${tty#/dev/}
case \$tty in
tty[a-zA-Z]* )
PORT=\${tty%%,*}
# check for service which do something on this port
if [ -f /etc/init/\$PORT.conf ];then continue;fi
tmp=\${tty##\$PORT,}
SPEED=\${tmp%%n*}
BITS=\${tmp##\${SPEED}n}
# 8bit serial is default
[ -z \$BITS ] && BITS=8
[ 8 -eq \$BITS ] && GETTY_ARGS="\$GETTY_ARGS -8 "
[ -z \$SPEED ] && SPEED="115200,57600,38400,19200,9600"
GETTY_ARGS="\$GETTY_ARGS \$SPEED \$PORT"
exec /sbin/getty \$GETTY_ARGS
esac
esac
done
EOF
'
chmod a+x bin/serial-console
#
# Remove root password has it is not known anyway
# This is recommended to change it at first boot
# as well as create a new user.
#
echo "First user is root with no password..."
sh -c "sed -i 's/root:\*/root:/' etc/shadow"
#
# Configures the network to run inside
# your premises. Gathering information
# from your PC.
#
echo "Setting up the network for dhcp..."
sh -c "cat >etc/network/interfaces << EOF
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
EOF
"
echo "Gathering network information from your PC..."
cat /etc/resolv.conf | grep -v "#" >etc/resolv.conf
echo "http_proxy=\"$http_proxy\"" >>etc/environment
echo "https_proxy=\"$https_proxy\"" >>etc/environment
echo "ftp_proxy=\"$ftp_proxy\"" >>etc/environment
echo "no_proxy=\"$no_proxy\"" >>etc/environment
# Trying to look for a local mirror server
# Not sure this will work for everyone...
#
# grep -e '^deb\{1,\}.[^-]*ubuntu.[^-]*main'
# => will search for lines starting with "deb" but not "deb-" like in "deb-src"
# followed by "ubuntu" string in it but not "ubuntu-" like in "ubuntu-updates"
# followed by "main" string in it too.
#
# head -1
# => will just display the first result
#
# grep -Po '^.*?\K(?<=http\:\/\/).*?(?=\/)'
# => will extract the substring surrounded by the 2 main strings "http://" and "/"
#
mirror_server=`cat /etc/apt/sources.list | grep -e '^deb\{1,\}.[^-]*ubuntu.[^-]*main' | head -1 | grep -Po '^.*?\K(?<=http\:\/\/).*?(?=\/)'`
archive_server=""
if [ -z "$mirror_server" ]; then
is_local=`echo $mirror_server | grep 'ubuntu\.com'`
if [ $is_local -eq 0 ]; then
archive_server="/$mirror_server/distrib/"$ARMARCH"/linux/ubuntu/mirror"
fi
fi
sh -c "echo 'deb http:/$archive_server/ports.ubuntu.com/ubuntu-ports/ "$DISTRIB" main universe multiverse restricted' > etc/apt/sources.list"
sh -c "echo 'deb-src http:/$archive_server/ports.ubuntu.com/ubuntu-ports/ "$DISTRIB" main universe multiverse restricted' >> etc/apt/sources.list"
sh -c "echo 'deb http:/$archive_server/ports.ubuntu.com/ubuntu-ports/ "$DISTRIB"-security main universe multiverse restricted' >> etc/apt/sources.list"
sh -c "echo 'deb-src http:/$archive_server/ports.ubuntu.com/ubuntu-ports/ "$DISTRIB"-security main universe multiverse restricted' >> etc/apt/sources.list"
sh -c "echo 'deb http:/$archive_server/ports.ubuntu.com/ubuntu-ports/ "$DISTRIB"-updates main universe multiverse restricted' >> etc/apt/sources.list"
sh -c "echo 'deb-src http:/$archive_server/ports.ubuntu.com/ubuntu-ports/ "$DISTRIB"-updates main universe multiverse restricted' >> etc/apt/sources.list"
echo "Congratulation, you are done!"