-
Notifications
You must be signed in to change notification settings - Fork 3
/
build-template
302 lines (243 loc) · 6.57 KB
/
build-template
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# Do not run directly!
fail() {
echo $2
exit $1
}
# Shortcuts
CWD=`pwd`
BUILD=${ZFS_ROOT}/${ZFS_BUILD}
ROOTRW=${BUILD}/rootfs.rw
VARRW=${BUILD}/var.rw
OPTRW=${BUILD}/opt.rw
HOMERW=${BUILD}/home.rw
ROOTRO=${BUILD}/rootfs
VARRO=${BUILD}/var
OPTRO=${BUILD}/opt
HOMERO=${BUILD}/home
DELTAS=${BUILD}/deltas
# Do a bit of sanity test
[ ! -d /${ZFS_ROOT} ] && fail 1 "The directory ${ZFS_ROOT} does not exists!"
[ -d ${BUILD} ] && fail 2 "The directory ${BUILD} already exists!"
sudo -S -v || fail 3 "sudo -S does not appear to be working"
# Install build tools
sudo -S apt-get install eatmydata debootstrap
# Create necessary subvolumes
cat <<-BANNER
*******************
Creating subvolumes
*******************
BANNER
sudo -S modprobe zfs || fail 3 "Unable to load ZFS kernel module"
sudo -S zfs create ${BUILD} || fail 3 "Unable to create build directory ${BUILD}"
sudo -S zfs create ${ROOTRW} || fail 3 "Unable to create rootfs directory ${ROOTRW}"
# Bootstrap Debian/Ubuntu
cat <<-BANNER
*******************
Running debootstrap
*******************
BANNER
sudo -S cdebootstrap --allow-unauthenticated --include=${EXTRA} ${BASE} /${ROOTRW} ${REP}
# Mount proc and sys
cat <<-BANNER
*****************
Mounting proc/sys
*****************
BANNER
sudo -S mount sysfs /${ROOTRW}/sys -t sysfs
sudo -S mount proc /${ROOTRW}/proc -t proc
# Update hostname
cat <<-BANNER
****************
Setting Hostname
****************
BANNER
cat <<-EOF | sudo -S chroot /${ROOTRW} /bin/bash -c "cat >/etc/hostname"
${NAME}
EOF
# Create subutai_template_version
cat <<-BANNER
********************************
Setting Subutai Template Version
********************************
BANNER
echo "Version is ${NAME}@${VERSION}"
cat <<-EOF | sudo -S chroot /${ROOTRW} /bin/bash -c "cat >/etc/subutai_template_version"
${NAME}@${VERSION}
EOF
# Create /etc/networking/interfaces
cat <<-BANNER
**************************
Setting Network Interfaces
**************************
BANNER
cat <<-EOF | sudo -S chroot /${ROOTRW} /bin/bash -c "cat >/etc/network/interfaces"
# Subutai Networking Default Configuration
# Local
auto lo
iface lo inet loopback
# Ethernet
auto eth0
iface eth0 inet dhcp
EOF
# Set apt-get repos
cat <<-BANNER
*************
Setting Repos
*************
BANNER
if [ -n "${REPOS}" ]; then
echo "${REPOS}" | sudo -S chroot /${ROOTRW} /bin/bash -c "cat >/etc/apt/sources.list"
fi
# Add Keys
cat <<-BANNER
***********
Adding keys
***********
BANNER
for KEY in ${KEYS}; do
sudo -S chroot /${ROOTRW} /bin/bash -c "apt-key adv --recv-keys --keyserver keyserver.ubuntu.com ${KEY}"
done
# Run update
cat <<-BANNER
********
Updating
********
BANNER
sudo -S chroot /${ROOTRW} /bin/bash -c "DEBIAN_FRONTEND=noninteractive apt-get -y update"
sudo -S chroot /${ROOTRW} /bin/bash -c "DEBIAN_FRONTEND=noninteractive apt-get -y dist-upgrade"
# Optional extras
cat <<-BANNER
*****************
Installing extras
*****************
BANNER
sudo -S chroot /${ROOTRW} /bin/bash -c "DEBIAN_FRONTEND=noninteractive apt-get -y --allow-unauthenticated install ${PKGS}"
# Run extra commands
cat <<-BANNER
**************
Extra Commands
**************
BANNER
sudo -S chroot /${ROOTRW} /bin/bash -c "${CMDS}"
# Create package list
cat <<-BANNER
*********************
Creating package list
*********************
BANNER
sudo -S chroot /${ROOTRW} /bin/bash -c "dpkg --get-selections > /packages"
sudo -S mv /${ROOTRW}/packages /$BUILD
# Clean up
cat <<-BANNER
***********
Cleaning up
***********
BANNER
sudo -S chroot /${ROOTRW} /bin/bash -c "apt-get -y clean"
sudo -S chroot /${ROOTRW} /bin/bash -c "rm -f /var/cache/apt/*.bin"
sudo -S chroot /${ROOTRW} /bin/bash -c "rm -rf /var/lib/apt/lists/*"
# Unmount
cat <<-BANNER
********************
Unmounting temporary
********************
BANNER
sudo -S umount /${ROOTRW}/sys
sudo -S umount /${ROOTRW}/proc
# Deal with mounts
cat <<-BANNER
********************
Moving files
********************
BANNER
sudo -S zfs create ${VARRW}
if ! `ls -A /${ROOTRW}/var`; then
sudo -S mv /${ROOTRW}/var/* /${VARRW}
fi
sudo -S zfs create ${OPTRW}
if ! `ls -A /${ROOTRW}/opt`; then
sudo -S mv /${ROOTRW}/opt/* /${OPTRW}
fi
sudo -S zfs create ${HOMERW}
if ! `ls -A /${ROOTRW}/home`; then
sudo -S mv /$ROOTRW/home/* /${HOMERW}
fi
# Finally snapshot all
cat <<-BANNER
********************
Readonly snapshots
********************
BANNER
sudo -S zfs snapshot ${ROOTRW}@now
sudo -S zfs snapshot ${VARRW}@now
sudo -S zfs snapshot ${OPTRW}@now
sudo -S zfs snapshot ${HOMERW}@now
# Save deltas
cat <<-BANNER
********************
Creating deltas
********************
BANNER
sudo -S mkdir /${DELTAS}
sudo -S zfs send ${ROOTRW}@now | sudo -S tee /${DELTAS}/rootfs.delta >/dev/null
sudo -S zfs send ${VARRW}@now | sudo -S tee /${DELTAS}/var.delta >/dev/null
sudo -S zfs send ${OPTRW}@now | sudo -S tee /${DELTAS}/opt.delta >/dev/null
sudo -S zfs send ${HOMERW}@now | sudo -S tee /${DELTAS}/home.delta >/dev/null
ls -l /${DELTAS}/*.delta
# Make sure deltas can be read by a normal user
sudo -S chmod 644 /${DELTAS}/*
# Delete subvolumes
cat <<-BANNER
********************
Deleting subvolumes
********************
BANNER
sudo -S zfs destroy -r ${ROOTRW}
sudo -S zfs destroy -r ${VARRW}
sudo -S zfs destroy -r ${OPTRW}
sudo -S zfs destroy -r ${HOMERW}
# Create config
cat <<-BANNER
************************
Creating template config
************************
BANNER
cat <<-EOF | sudo -S tee /${BUILD}/config >/dev/null
lxc.arch = amd64
lxc.utsname = ${NAME}
lxc.rootfs.backend=zfs
subutai.template = ${NAME}
subutai.template.owner = ${OWNER}
subutai.template.version = ${VERSION}
subutai.parent = ${NAME}
subutai.parent.owner = ${OWNER}
subutai.parent.version = ${VERSION}
lxc.mount.entry=/var/lib/lxc/${NAME}:${OWNER}:${VERSION}/var var none bind,rw 0 0
lxc.mount.entry=/var/lib/lxc/${NAME}:${OWNER}:${VERSION}/opt opt none bind,rw 0 0
lxc.mount.entry=/var/lib/lxc/${NAME}:${OWNER}:${VERSION}/home home none bind,rw 0 0
lxc.rootfs=/var/lib/lxc/${NAME}:${OWNER}:${VERSION}/rootfs
lxc.network.type = veth
lxc.network.script.up = /usr/sbin/subutai-create-interface
lxc.include = /usr/share/subutai/config/subutai.conf
lxc.tty = 0
EOF
cat <<-EOF | sudo -S tee /${BUILD}/fstab
EOF
# Bundle the darn thing in a tar.gz archive
cat <<-BANNER
************************
Creating Bundle
************************
BANNER
[ -d ${CWD}/build ] || mkdir ${CWD}/build
(cd /${BUILD} ; tar cz * >${CWD}/build/${NAME}-${OWNER}-template_${VERSION}_amd64.tar.gz)
ls -l ${CWD}/build/${NAME}-${OWNER}-template_${VERSION}_amd64.tar.gz
# Finally nuke build
cat <<-BANNER
************************
Cleaning up
************************
BANNER
sudo -S zfs destroy -r ${BUILD}
exit 0
# vim: ts=4 et nowrap autoindent