blob: 19dd0af6179556be00d03f6c4f4aae6b109faf8c (
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
|
#!/bin/sh
#
# Copyright (C) 2017 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
set -e
if [ $(id -u) -ne 0 ] ; then
echo "ERROR: needs to be executed as root"
exit 1
fi
channel=candidate
snap=
unsigned_core=
unsigned_gadget=
unsigned_kernel=
show_help() {
echo "Usage: create-image.sh [OPTIONS]"
echo
echo "Generate an Ubuntu Core image"
echo
echo "optional arguments:"
echo " --channel=<channel> Select another channel to build the image from (default: $channel)"
echo " --snap=<name> Extra snap to install"
echo " --unsigned-core-snap Use an unsigned core snap (so that it might be replaced during a test"
echo " --unsigned-gadget-snap Use an unsigned gadget snap"
echo " --unsigned-kernel-snap Use an unsigned kernel snap"
}
while [ -n "$1" ]; do
case "$1" in
--help)
show_help
exit
;;
--channel=*)
channel=${1#*=}
shift
;;
--snap=*)
snap=${1#*=}
shift
;;
--unsigned-core-snap)
unsigned_core=1
shift
;;
--unsigned-gadget-snap)
unsigned_gadget=1
shift
;;
--unsigned-kernel-snap)
unsigned_kernel=1
shift
;;
*)
echo "Unknown option: $1"
show_help
exit 1
;;
esac
done
model=pc
arch=amd64
image_name=ubuntu-core-16.img
ubuntu_image_extra_args=
if [ -n "$snap" ] ; then
ubuntu_image_extra_args="--extra-snaps $snap"
fi
if [ -n "$unsigned_core" ] ; then
snap download --$channel core
ubuntu_image_extra_args="$ubuntu_image_extra_args --extra-snaps core_*.snap"
fi
if [ -n "$unsigned_gadget" ] ; then
snap download --$channel pc
ubuntu_image_extra_args="$ubuntu_image_extra_args --extra-snaps pc_*.snap"
fi
if [ -n "$unsigned_kernel" ] ; then
snap download --$channel pc-kernel
ubuntu_image_extra_args="$ubuntu_image_extra_args --extra-snaps pc-kernel_*.snap"
fi
ubuntu-image \
--channel $channel \
-O $(pwd) \
--image-size 4G \
$ubuntu_image_extra_args \
$model.model
mv $model.img $image_name
# kpartx -s: don't return until the partitions are created
partitions=$(sudo kpartx -asv ubuntu-core-16.img | grep "add map" | cut -d' ' -f3)
for part in $partitions; do
loop_path=/dev/mapper/$part
if blkid -s LABEL "$loop_path" | grep 'LABEL="writable"'; then
break
fi
done
if [ -z "$loop_path" ]; then
echo "Writable partition not found in image"
return 1
fi
tmp_mount=$(mktemp -d)
mount "$loop_path" "$tmp_mount"
# Migrate all systemd units from core snap into the writable area. This
# would be normally done on firstboot by the initramfs but we can't rely
# on that because we are adding another file in there and that will
# prevent the initramfs from transitioning any files.
core_snap=$(find $tmp_mount/system-data/var/lib/snapd/snaps -name "core_*.snap")
tmp_core=$(mktemp -d)
mount $core_snap $tmp_core
mkdir -p $tmp_mount/system-data/etc/systemd
cp -rav $tmp_core/etc/systemd/* \
$tmp_mount/system-data/etc/systemd/
umount $tmp_core
rm -rf $tmp_core
# system-user assertion which gives us our test:test user we use to
# log into the system
mkdir -p $tmp_mount/system-data/var/lib/snapd/seed/assertions
cp test-user.assertion $tmp_mount/system-data/var/lib/snapd/seed/assertions
# Disable console-conf for the first boot
mkdir -p $tmp_mount/system-data/var/lib/console-conf/
touch $tmp_mount/system-data/var/lib/console-conf/complete
# Create systemd service which is running on firstboot and sets up
# various things for us.
mkdir -p $tmp_mount/system-data/etc/systemd/system
cat << 'EOF' > $tmp_mount/system-data/etc/systemd/system/devmode-firstboot.service
[Unit]
Description=Run devmode firstboot setup
After=snapd.service snapd.socket
[Service]
Type=oneshot
ExecStart=/writable/system-data/var/lib/devmode-firstboot/run.sh
RemainAfterExit=yes
TimeoutSec=3min
EOF
mkdir -p $tmp_mount/system-data/etc/systemd/system/multi-user.target.wants
ln -sf /etc/systemd/system/devmode-firstboot.service \
$tmp_mount/system-data/etc/systemd/system/multi-user.target.wants/devmode-firstboot.service
mkdir $tmp_mount/system-data/var/lib/devmode-firstboot
cat << EOF > $tmp_mount/system-data/var/lib/devmode-firstboot/00-snapd-config.yaml
network:
version: 2
ethernets:
eth0:
dhcp4: true
EOF
cat << 'EOF' > $tmp_mount/system-data/var/lib/devmode-firstboot/run.sh
#!/bin/sh
set -e
# Don't start again if we're already done
if [ -e /writable/system-data/var/lib/devmode-firstboot/complete ] ; then
exit 0
fi
echo "Start devmode-firstboot $(date -Iseconds --utc)"
if [ "$(snap managed)" = "true" ]; then
echo "System already managed, exiting"
exit 0
fi
# no changes at all
until snap changes ; do
echo "No changes yet, waiting"
sleep 1
done
while snap changes | grep -qE '(Do|Doing) .*Initialize system state' ; do
echo "Initialize system state is in progress, waiting"
sleep 1
done
if [ -n "$(snap known system-user)" ]; then
echo "Trying to create known user"
snap create-user --known --sudoer
fi
cp /writable/system-data/var/lib/devmode-firstboot/00-snapd-config.yaml /writable/system-data/etc/netplan
# Enable console-conf again
rm /writable/system-data/var/lib/console-conf/complete
# Mark us done
touch /writable/system-data/var/lib/devmode-firstboot/complete
# Reboot the system as it's now prepared for the user
reboot
EOF
chmod +x $tmp_mount/system-data/var/lib/devmode-firstboot/run.sh
umount $tmp_mount
kpartx -d $image_name
rm -rf $tmp_mount
|