This repository has been archived by the owner on Oct 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
image.go
349 lines (301 loc) · 8.31 KB
/
image.go
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package virtuakube
import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"go.universe.tf/virtuakube/internal/assets"
)
var buildTools = []string{
"qemu-system-x86_64",
"qemu-img",
"docker",
"virt-make-fs",
}
const (
dockerfile = `
FROM debian:stretch
RUN apt-get -y update
RUN DEBIAN_FRONTEND=noninteractive apt-get -y install --no-install-recommends \
ca-certificates \
dbus \
grub2 \
ifupdown \
isc-dhcp-client \
isc-dhcp-common \
linux-image-amd64 \
openssh-server \
systemd-sysv
RUN DEBIAN_FRONTEND=noninteractive apt-get -y upgrade --no-install-recommends
RUN echo "root:root" | chpasswd
RUN echo "PermitRootLogin yes" >>/etc/ssh/sshd_config
RUN echo "auto enp0s2\niface enp0s2 inet dhcp" >/etc/network/interfaces
RUN echo "supersede domain-name-servers 8.8.8.8;" >>/etc/dhcp/dhclient.conf
`
hosts = `
127.0.0.1 localhost
::1 localhost
`
fstab = `
/dev/vda1 / ext4 rw,relatime 0 1
bpffs /sys/fs/bpf bpf rw,relatime 0 0
`
)
// ImageCustomizeFunc is a function that applies customizations to a
// VM that's being built by NewImage.
type ImageCustomizeFunc func(*VM) error
// ImageConfig is the build configuration for an Image.
type ImageConfig struct {
Name string
CustomizeFuncs []ImageCustomizeFunc
NoKVM bool
}
// Image is a VM disk base image.
type Image struct {
path string
}
func (u *Universe) ImportImage(name, path string) error {
disk := randomDiskName()
src, err := os.Open(path)
if err != nil {
return fmt.Errorf("opening %q: %v", path, err)
}
dst, err := os.Create(filepath.Join(u.dir, disk))
if err != nil {
return fmt.Errorf("creating %q: %v", disk, err)
}
if _, err := io.Copy(dst, src); err != nil {
return fmt.Errorf("writing disk: %v", err)
}
u.mu.Lock()
defer u.mu.Unlock()
if u.images[name] != "" {
panic("what")
}
u.images[name] = disk
return nil
}
// NewImage builds a VM disk image using the given config.
func (u *Universe) NewImage(cfg *ImageConfig) error {
if err := checkTools(buildTools); err != nil {
return err
}
tmp, err := ioutil.TempDir(u.tmpdir, "b")
if err != nil {
return fmt.Errorf("creating tempdir in %q: %v", u.dir, err)
}
defer os.RemoveAll(tmp)
if err := ioutil.WriteFile(filepath.Join(tmp, "Dockerfile"), []byte(dockerfile), 0644); err != nil {
return fmt.Errorf("writing dockerfile: %v", err)
}
iidPath := filepath.Join(tmp, "iid")
cmd := exec.Command("docker", "build", "--iidfile", iidPath, tmp)
cmd.Stdout = u.runtimecfg.CommandLog
cmd.Stderr = u.runtimecfg.CommandLog
if err := cmd.Run(); err != nil {
return fmt.Errorf("running docker build: %v", err)
}
iid, err := ioutil.ReadFile(iidPath)
if err != nil {
return fmt.Errorf("reading image ID: %v", err)
}
cidPath := filepath.Join(tmp, "cid")
cmd = exec.Command(
"docker", "run",
"--cidfile", cidPath,
fmt.Sprintf("--mount=type=bind,source=%s,destination=/tmp/ctx", tmp),
string(iid),
"cp", "/vmlinuz", "/initrd.img", "/tmp/ctx",
)
cmd.Stdout = u.runtimecfg.CommandLog
cmd.Stderr = u.runtimecfg.CommandLog
if err := cmd.Run(); err != nil {
return fmt.Errorf("extracting kernel from container: %v", err)
}
cid, err := ioutil.ReadFile(cidPath)
if err != nil {
return fmt.Errorf("reading image ID: %v", err)
}
tarPath := filepath.Join(tmp, "fs.tar")
cmd = exec.Command(
"docker", "export",
"-o", tarPath,
string(cid),
)
cmd.Stdout = u.runtimecfg.CommandLog
cmd.Stderr = u.runtimecfg.CommandLog
if err := cmd.Run(); err != nil {
return fmt.Errorf("exporting image tarball: %v", err)
}
imgPath := filepath.Join(tmp, "fs.img")
cmd = exec.Command(
"virt-make-fs",
"--partition", "--format=qcow2",
"--type=ext4", "--size=10G",
tarPath, imgPath,
)
cmd.Stdout = u.runtimecfg.CommandLog
cmd.Stderr = u.runtimecfg.CommandLog
if err := cmd.Run(); err != nil {
return fmt.Errorf("creating image file: %v", err)
}
if err := os.Remove(tarPath); err != nil {
return fmt.Errorf("removing image tarball: %v", err)
}
tmpu, err := Create(filepath.Join(tmp, "u"), u.runtimecfg)
if err != nil {
return fmt.Errorf("creating virtuakube instance: %v", err)
}
defer tmpu.Destroy()
if err := tmpu.ImportImage("build", imgPath); err != nil {
return fmt.Errorf("importing half-built image: %v", err)
}
v, err := tmpu.NewVM(&VMConfig{
Image: "build",
MemoryMiB: 2048,
kernelConfig: &kernelConfig{
kernelPath: filepath.Join(tmp, "vmlinuz"),
initrdPath: filepath.Join(tmp, "initrd.img"),
cmdline: "root=/dev/vda1 rw",
},
})
if err != nil {
return fmt.Errorf("creating image VM: %v", err)
}
if err := v.Start(); err != nil {
return fmt.Errorf("starting image VM: %v", err)
}
if err := v.WriteFile("/etc/hosts", []byte(hosts)); err != nil {
return fmt.Errorf("install /etc/hosts: %v", err)
}
if err := v.WriteFile("/etc/fstab", []byte(fstab)); err != nil {
return fmt.Errorf("install /etc/fstab: %v", err)
}
err = v.RunMultiple(
"update-initramfs -u",
"grub-install /dev/vda",
"perl -pi -e 's/GRUB_TIMEOUT=.*/GRUB_TIMEOUT=0/' /etc/default/grub",
"update-grub2",
"rm /etc/machine-id /var/lib/dbus/machine-id",
"touch /etc/machine-id",
"chattr +i /etc/machine-id",
)
if err != nil {
return fmt.Errorf("finalize base image configuration: %v", err)
}
for _, f := range cfg.CustomizeFuncs {
if err = f(v); err != nil {
return fmt.Errorf("applying customize func: %v", err)
}
}
if _, err := v.Run("sync"); err != nil {
return fmt.Errorf("syncing image disk: %v", err)
}
v.Run("poweroff")
if err := v.Wait(context.Background()); err != nil {
return fmt.Errorf("waiting for VM shutdown: %v", err)
}
ret := randomDiskName()
cmd = exec.Command(
"qemu-img", "convert",
"-O", "qcow2",
filepath.Join(tmp, "u", tmpu.image("build")),
filepath.Join(u.dir, ret),
)
cmd.Stdout = u.runtimecfg.CommandLog
cmd.Stderr = u.runtimecfg.CommandLog
if err := cmd.Run(); err != nil {
os.Remove(ret)
return fmt.Errorf("running qemu-img convert: %v", err)
}
u.mu.Lock()
defer u.mu.Unlock()
if u.images[cfg.Name] != "" {
panic("I don't know what to do about this yet")
}
u.images[cfg.Name] = ret
return nil
}
// CustomizeInstallK8s is a build customization function that installs
// Docker and Kubernetes prerequisites, as required for NewCluster to
// function.
func CustomizeInstallK8s(v *VM) error {
repos := []byte(`
deb [arch=amd64] https://download.docker.com/linux/debian stretch stable
deb http://apt.kubernetes.io/ kubernetes-xenial main
`)
if err := v.WriteFile("/etc/apt/sources.list.d/k8s.list", repos); err != nil {
return err
}
pkgs := []string{
"apt-transport-https",
"curl",
"ebtables",
"ethtool",
"gpg",
"gpg-agent",
}
k8sPkgs := []string{
"docker-ce=18.06.*",
"kubelet",
"kubeadm",
"kubectl",
}
err := v.RunMultiple(
"DEBIAN_FRONTEND=noninteractive apt-get -y install --no-install-recommends "+strings.Join(pkgs, " "),
"curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -",
"curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -",
"apt-get -y update",
"DEBIAN_FRONTEND=noninteractive apt-get -y install --no-install-recommends "+strings.Join(k8sPkgs, " "),
"echo br_netfilter >>/etc/modules",
"echo export KUBECONFIG=/etc/kubernetes/admin.conf >>/etc/profile.d/k8s.sh",
)
if err != nil {
return err
}
return nil
}
// CustomizePreloadK8sImages is a build customization function that
// pre-pulls all the Docker images needed to fully initialize a
// Kubernetes cluster.
func CustomizePreloadK8sImages(v *VM) error {
err := v.RunMultiple(
"systemctl start docker",
"kubeadm config images pull",
)
if err != nil {
return err
}
imgs := strings.Split(string(assets.MustAsset("addon-images")), " ")
for _, img := range imgs {
if img == "" {
continue
}
if _, err := v.Run("docker pull " + img); err != nil {
return err
}
}
return nil
}
// CustomizeScript is a build customization function that executes the
// script at path on a VM running the disk image being built.
func CustomizeScript(path string) func(*VM) error {
return func(v *VM) error {
bs, err := ioutil.ReadFile(path)
if err != nil {
return fmt.Errorf("reading script %q: %v", path, err)
}
if err := v.WriteFile("/tmp/script", bs); err != nil {
return fmt.Errorf("writing script to VM: %v", err)
}
return v.RunMultiple(
"chmod +x /tmp/script",
"/tmp/script",
"rm /tmp/script",
)
}
}