-
Notifications
You must be signed in to change notification settings - Fork 11
/
step_create_image.go
247 lines (215 loc) · 6.53 KB
/
step_create_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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package images
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"github.com/cilium/little-vm-helper/pkg/arch"
"github.com/cilium/little-vm-helper/pkg/logcmd"
"github.com/cilium/little-vm-helper/pkg/step"
"github.com/sirupsen/logrus"
)
var (
// DelImageIfExists: if set to true, image will be deleted at Cleanup() by the CreateImage step
DelImageIfExists = "DelImageIfExist"
rootDev = "/dev/vda"
rootFsType = "ext4"
resizeFS = "resize2fs"
)
// Approach for creating images:
// - Base (root) images are build using mmdebstrap and copying files using guestfish.
// - Non-root images are build using virt-customize, by copying the parent.
// - All images use the raw format (not qcow2)
// - Images are read-only. Users can use them to create other images (by copying or via qcow2)
//
// Alternative options I considred and may be useful for future reference:
// - using qemu-nbd+chroot, would probably be a faster way to do this, but it requires root.
// - using either debootstrap, or multistrap (with fakeroot and fakechroot) instead of mmdebstrap.
// The latter seems faster, so I thought I'd use it. If something breaks, we can always go another
// route.
// - using the go bindings for libguestfs (https://libguestfs.org/guestfs-golang.3.html). Using the
// CLI seemed simpler.
// - having bootable images. I don't think we need this since we can specify --kernel and friends
// in qemu.
// - having the images in qcow2 so that we save some space. I think the sparsity of the files is
// enough, so decided to keep things simple. Note that we can use virt-sparsify if we want to (e.g.,
// when downloading images).
// CreateImage is a step for creating an image. Its cleanup will delete the image if DelImageIfExists is set.
type CreateImage struct {
*StepConf
}
func NewCreateImage(cnf *StepConf) *CreateImage {
return &CreateImage{
StepConf: cnf,
}
}
var extLinuxConf = fmt.Sprintf(`
default linux
timeout 0
label linux
kernel /vmlinuz
append initrd=initrd.img root=%s rw console=ttyS0
`, rootDev)
// makeRootImage creates a root (with respect to the image forest hierarch) image
func (s *CreateImage) makeRootImage(ctx context.Context) error {
if s == nil || s.imgCnf == nil {
return errors.New("step configuration or image configuration is nil")
}
imgFname := filepath.Join(s.imagesDir, s.imgCnf.Name)
tarFname := path.Join(s.imagesDir, fmt.Sprintf("%s.tar", s.imgCnf.Name))
bootable := arch.Bootable(s.imgCnf.Bootable)
// build package list: add a kernel if building a bootable image
packages := make([]string, 0, len(s.imgCnf.Packages)+1)
if bootable {
packages = append(packages, "linux-image-amd64")
}
packages = append(packages, s.imgCnf.Packages...)
cmd := exec.CommandContext(ctx, Mmdebstrap,
"sid",
"--include", strings.Join(packages, ","),
tarFname,
)
err := logcmd.RunAndLogCommand(cmd, s.log)
if err != nil {
return err
}
defer func() {
err := os.Remove(tarFname)
if err != nil {
s.log.WithError(err).Info("failed to remove tarfile")
}
}()
imgSize := DefaultImageSize
if size := s.imgCnf.ImageSize; size != "" {
imgSize = size
}
// example: guestfish -N foo.img=disk:8G -- mkfs ext4 /dev/vda : mount /dev/vda / : tar-in /tmp/foo.tar /
if bootable {
dirname, err := os.MkdirTemp("", "extlinux-")
if err != nil {
return err
}
defer func() {
os.RemoveAll(dirname)
}()
fname := filepath.Join(dirname, "extlinux.conf")
if err := os.WriteFile(fname, []byte(extLinuxConf), 0722); err != nil {
return err
}
cmd = exec.CommandContext(ctx, GuestFish,
"-N", fmt.Sprintf("%s=disk:%s", imgFname, imgSize),
"--",
"part-disk", rootDev, "mbr",
":",
"part-set-bootable", rootDev, "1", "true",
":",
"mkfs", rootFsType, rootDev,
":",
"mount", rootDev, "/",
":",
"tar-in", tarFname, "/",
":",
"extlinux", "/",
":",
"copy-in", fname, "/",
)
} else {
cmd = exec.CommandContext(ctx, GuestFish,
"-N", fmt.Sprintf("%s=disk:%s", imgFname, imgSize),
"--",
"mkfs", rootFsType, rootDev,
":",
"mount", rootDev, "/",
":",
"tar-in", tarFname, "/",
)
}
if err := logcmd.RunAndLogCommand(cmd, s.log); err != nil {
return err
}
if imageFormatFromFname(imgFname) == "qcow2" {
tmpImage := fmt.Sprintf("%s.img", imgFname)
if err := os.Rename(imgFname, tmpImage); err != nil {
return err
}
defer os.Remove(tmpImage)
cmd := exec.CommandContext(ctx, QemuImg, "convert", "-f", "raw", "-O", "qcow2", tmpImage, imgFname)
return logcmd.RunAndLogCommand(cmd, s.log)
}
return nil
}
func resizeImage(ctx context.Context,
log logrus.FieldLogger,
imgFname string, size string,
) error {
// resize image
cmd := exec.CommandContext(ctx, QemuImg, "resize", imgFname, size)
err := logcmd.RunAndLogCommand(cmd, log)
if err != nil {
return err
}
// resize filesystem
cmd = exec.CommandContext(ctx, GuestFish,
"-a", imgFname,
"--",
"run",
":",
resizeFS,
rootDev,
)
err = logcmd.RunAndLogCommand(cmd, log)
if err != nil {
return err
}
return nil
}
// makeDerivedImage creates a non-root image
func (s *CreateImage) makeDerivedImage(ctx context.Context) error {
parFname := filepath.Join(s.imagesDir, s.imgCnf.Parent)
imgFname := filepath.Join(s.imagesDir, s.imgCnf.Name)
parFmt := imageFormatFromFname(parFname)
imgFmt := imageFormatFromFname(imgFname)
cmd := exec.CommandContext(ctx, QemuImg, "convert", "-f", parFmt, "-O", imgFmt, parFname, imgFname)
err := logcmd.RunAndLogCommand(cmd, s.log)
if err != nil {
return err
}
// We could check whether the image is the same, but this is tricky.
// The configuration of the parent does not always exist, so we would
// have to check the current size of the image. To make life easier, we
// always resize if the user defines a size.
if size := s.imgCnf.ImageSize; size != "" {
if err := resizeImage(ctx, s.log, imgFname, size); err != nil {
return err
}
}
if len(s.imgCnf.Packages) > 0 {
cmd = exec.CommandContext(ctx, VirtCustomize,
"-a", imgFname,
"--install", strings.Join(s.imgCnf.Packages, ","),
)
return logcmd.RunAndLogCommand(cmd, s.log)
}
return nil
}
func (s *CreateImage) Do(ctx context.Context) (step.Result, error) {
var err error
if s.imgCnf.Parent == "" {
err = s.makeRootImage(ctx)
} else {
err = s.makeDerivedImage(ctx)
}
if err != nil {
s.log.WithField("image", s.imgCnf.Name).WithError(err).Error("error buiding image")
return step.Stop, err
}
return step.Continue, nil
}
func (s *CreateImage) Cleanup(ctx context.Context) {
}