forked from zyga/devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ubuntu-image
executable file
·251 lines (223 loc) · 6.67 KB
/
ubuntu-image
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
#!/bin/sh
version="0.11"
show_device_names() {
echo "Devices supported by Canonical (official)"
echo " pc: Modern Intel/AMD Computer (64 bit)"
echo " i386: Legacy Intel/AMD Computer (32 bit)"
echo " pi2: Raspberry Pi 2"
echo " dragon: 410c DragonBoard"
echo
echo "Community devices"
echo " bbb: BeagleBone Black"
}
# need sudo to check if we're in an LXC container
if [ $(id -u) -ne "0" ]; then
echo "Please run me with sudo"
exit
fi
if grep -qa container=lxc /proc/1/environ; then
echo "You're in a container, refusing to run."
exit
fi
# Handle command line arguments
while [ -n "$1" ]; do
case "$1" in
--help|-h)
echo "Usage: ubuntu-image [--[no-]developer-mode] [device-name|?]"
exit 0
;;
--version)
echo "ubuntu-image, version $version"
exit 0
;;
--developer-mode)
image_kind=devel
shift
;;
--no-developer-mode)
image_kind=vanilla
shift
;;
'?')
echo "Recognized device names"
echo
show_device_names
exit
;;
*)
# Perhaps it is a device name?
case $1 in
pc|i386|pi2|dragon|bbb)
device="$1"
shift
;;
*)
echo "Unrecognized argument: $1" 1>&2
exit 1
;;
esac
;;
esac
done
# Add a notice about 16 series being under development
echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
echo "X NOTE: Ubuntu 16 series is still under development! X"
echo "X The exact layout of the kernel and gadget snaps will likely change. X"
echo "X You may have to re-flash your device to receive further upgrades. X"
echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
sleep 5s
# Interactively ask about the device if necessary.
while [ -z "$device" ]; do
echo "Which device do you have?"
echo
show_device_names
echo
echo -n "device> "
read device
case "$device" in
pc|i386|pi2|dragon|bbb)
;;
*)
echo "Unrecognized device name: $device"
device=""
;;
esac
done
# Interactively ask if developer mode should be enabled.
while [ -z "$image_kind" ]; do
echo "What kind of image do you want to build?"
echo
echo "devel: Developer image that contains your public SSH key"
echo "vanilla: Pristine image intended for sharing or installation"
echo
echo -n "image kind> "
read image_kind
case "$image_kind" in
devel)
image_kind=devel
;;
vanilla)
image_kind=vanilla
;;
*)
echo "Unrecognized image kind: $image_kind"
image_kind=
;;
esac
done
# Re-affirm the user as to which device is selected
echo "Selected device: $device"
# Pick the right values for ubuntu-device-flash
channel=edge
os=ubuntu-core
gadget_hash=
gadget_url=
kernel_hash=
kernel_url=
os_hash=
os_url=
udf_hash=a8acd7b871cb05d14a2e6e9ad780e6699225884e1b9536d2fa290164794d31b6050d1b4fc468a3d3acef5066e1e3f0eebc3359cc7e6c207d758edd1789c54638
udf_url=https://people.canonical.com/~mvo/all-snaps/ubuntu-device-flash
case $device in
pc)
kernel=canonical-pc-linux
gadget=canonical-pc
;;
i386)
kernel=canonical-pc-linux
gadget=canonical-i386
;;
pi2)
kernel=canonical-pi2-linux
gadget=canonical-pi2
;;
bbb)
kernel=linux-armhf
gadget=beagleblack
;;
dragon)
kernel=canonical-snapdragon-linux
gadget=canonical-dragon
;;
*)
echo "BUG: no information how to build an image for device: $device"
exit 2
;;
esac
cache_dir="${XDG_CACHE_HOME:-$HOME/.cache}/ubuntu-image"
download() {
blob_url="$1"
blob_hash="$2"
msg="$3"
if [ -f "$cache_dir/blob.$blob_hash" ]; then
cached_blob_hash=$(sha512sum < "$cache_dir/blob.$blob_hash" | cut -f 1 -d ' ')
if [ "$cached_blob_hash" != "$blob_hash" ]; then
echo "Removing corrupted copy of $(basename "$blob_url")"
rm -f "$cache_dir/blob.$blob_hash"
else
return
fi
fi
if [ -z "$msg" ]; then
echo "Downloading $blob_url..."
else
echo "$msg"
fi
blob_download_fname=$(tempfile --prefix=blob-)
trap "rm -f $blob_download_fname" EXIT
if ! wget --quiet --user-agent="ubuntu-image/$version" --output-document="$blob_download_fname" "$blob_url"; then
echo "Failed to download $blob_url"
exit 1
fi
blob_download_hash=$(sha512sum < "$blob_download_fname" | cut -f 1 -d ' ')
if [ "$blob_download_hash" != "$blob_hash" ]; then
echo "Failed to verify integrity of $blob_url"
exit 1
fi
mkdir -p "$cache_dir"
mv --no-clobber "$blob_download_fname" "$cache_dir/blob.$blob_hash"
}
# Download and verify integrity of ubuntu-device-flash
if [ -n "$udf_url" ]; then
download "$udf_url" "$udf_hash" "Downloading ubuntu-device-flash..."
udf="$cache_dir/blob.$udf_hash"
chmod +x "$udf"
fi
# Download snaps that are not published in the store
if [ -n "$os_url" ]; then
download "$os_url" "$os_hash" "Downloading OS snap..."
os="$cache_dir/blob.$os_hash"
fi
if [ -n "$kernel_url" ]; then
download "$kernel_url" "$kernel_hash" "Downloading kernel snap..."
kernel="$cache_dir/blob.$kernel_hash"
fi
if [ -n "$gadget_url" ]; then
download "$gadget_url" "$gadget_hash" "Downloading gadget snap..."
gadget="$cache_dir/blob.$gadget_hash"
fi
test -z "$os" && echo "BUG: no OS snap for device: $device" && exit 2
test -z "$kernel" && echo "BUG: no kernel snap for device: $device" && exit 2
test -z "$gadget" && echo "BUG: no gadget snap for device: $device" && exit 2
# Check if required tools are present
test -z $(which kpartx) && (
echo "Installing required dependency: kpartx"
sudo apt-get install kpartx --yes
)
# Run ubuntu device flash
echo "Building image..."
case "$image_kind" in
vanilla)
exec sudo "$udf" core 16 \
--channel "$channel" --kernel "$kernel" --os "$os" --gadget "$gadget" \
-o "$device.img"
;;
devel)
exec sudo "$udf" core 16 \
--channel "$channel" --kernel "$kernel" --os "$os" --gadget "$gadget" \
--developer-mode -o "${device}-devel.img"
;;
*)
echo "BUG: no information how to build image kind: $image_kind" && exit 2
;;
esac