2022-04-19 12:01:08 +00:00
|
|
|
// Copyright 2022 Linka Cloud All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2022-04-21 16:28:50 +00:00
|
|
|
package d2vm
|
2022-04-19 12:01:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-04-19 12:17:30 +00:00
|
|
|
exec2 "os/exec"
|
2022-04-19 12:01:08 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2022-04-21 16:28:50 +00:00
|
|
|
"github.com/c2h5oh/datasize"
|
2023-02-23 14:02:12 +00:00
|
|
|
"github.com/google/uuid"
|
2022-04-19 12:01:08 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"go.uber.org/multierr"
|
|
|
|
|
|
|
|
"go.linka.cloud/d2vm/pkg/exec"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
hosts = `127.0.0.1 localhost
|
|
|
|
|
|
|
|
# The following lines are desirable for IPv6 capable hosts
|
|
|
|
::1 ip6-localhost ip6-loopback
|
|
|
|
fe00::0 ip6-localnet
|
|
|
|
ff00::0 ip6-mcastprefix
|
|
|
|
ff02::1 ip6-allnodes
|
|
|
|
ff02::2 ip6-allrouters
|
|
|
|
ff02::3 ip6-allhosts
|
|
|
|
`
|
2022-04-24 13:49:01 +00:00
|
|
|
perm os.FileMode = 0644
|
|
|
|
)
|
|
|
|
|
2023-09-11 11:44:12 +00:00
|
|
|
var formats = []string{"qcow2", "qed", "raw", "vdi", "vhd", "vmdk"}
|
2022-08-05 15:39:04 +00:00
|
|
|
|
2022-09-11 18:01:37 +00:00
|
|
|
type Builder interface {
|
|
|
|
Build(ctx context.Context) (err error)
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2022-04-19 12:01:08 +00:00
|
|
|
type builder struct {
|
2023-09-11 11:44:12 +00:00
|
|
|
osRelease OSRelease
|
|
|
|
config Config
|
|
|
|
bootloader Bootloader
|
2022-04-19 12:01:08 +00:00
|
|
|
|
2022-04-21 16:28:50 +00:00
|
|
|
src string
|
2022-04-24 13:49:01 +00:00
|
|
|
img *image
|
2022-04-21 16:28:50 +00:00
|
|
|
diskRaw string
|
|
|
|
diskOut string
|
|
|
|
format string
|
|
|
|
|
2023-02-22 16:26:32 +00:00
|
|
|
size uint64
|
2022-04-21 16:28:50 +00:00
|
|
|
mntPoint string
|
2022-04-19 12:01:08 +00:00
|
|
|
|
2023-02-22 16:26:32 +00:00
|
|
|
splitBoot bool
|
|
|
|
bootSize uint64
|
|
|
|
|
2023-02-23 14:02:12 +00:00
|
|
|
loDevice string
|
|
|
|
bootPart string
|
|
|
|
rootPart string
|
|
|
|
cryptPart string
|
|
|
|
cryptRoot string
|
|
|
|
mappedCryptRoot string
|
|
|
|
bootUUID string
|
|
|
|
rootUUID string
|
|
|
|
cryptUUID string
|
|
|
|
|
|
|
|
luksPassword string
|
2023-02-22 16:26:32 +00:00
|
|
|
|
2022-08-07 16:24:02 +00:00
|
|
|
cmdLineExtra string
|
2022-04-19 12:01:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 11:44:12 +00:00
|
|
|
func NewBuilder(ctx context.Context, workdir, imgTag, disk string, size uint64, osRelease OSRelease, format string, cmdLineExtra string, splitBoot bool, bootSize uint64, luksPassword string, bootLoader string) (Builder, error) {
|
2022-04-19 12:17:30 +00:00
|
|
|
if err := checkDependencies(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-23 14:02:12 +00:00
|
|
|
if luksPassword != "" {
|
|
|
|
if !splitBoot {
|
|
|
|
return nil, fmt.Errorf("luks encryption requires split boot")
|
|
|
|
}
|
2023-03-01 12:29:10 +00:00
|
|
|
if !osRelease.SupportsLUKS() {
|
|
|
|
return nil, fmt.Errorf("luks encryption not supported on %s %s", osRelease.ID, osRelease.VersionID)
|
|
|
|
}
|
2023-02-23 14:02:12 +00:00
|
|
|
}
|
2022-04-21 16:28:50 +00:00
|
|
|
f := strings.ToLower(format)
|
|
|
|
valid := false
|
|
|
|
for _, v := range formats {
|
|
|
|
if valid = v == f; valid {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !valid {
|
|
|
|
return nil, fmt.Errorf("invalid format: %s valid formats are: %s", f, strings.Join(formats, " "))
|
|
|
|
}
|
2022-04-21 19:15:39 +00:00
|
|
|
|
2023-02-22 16:26:32 +00:00
|
|
|
if splitBoot && bootSize < 50 {
|
|
|
|
return nil, fmt.Errorf("boot partition size must be at least 50MiB")
|
|
|
|
}
|
|
|
|
|
|
|
|
if splitBoot && bootSize >= size {
|
|
|
|
return nil, fmt.Errorf("boot partition size must be less than the disk size")
|
|
|
|
}
|
|
|
|
|
2023-09-11 11:44:12 +00:00
|
|
|
if bootLoader == "" {
|
|
|
|
bootLoader = "syslinux"
|
2022-04-21 19:15:39 +00:00
|
|
|
}
|
2023-09-11 11:44:12 +00:00
|
|
|
|
|
|
|
config, err := osRelease.Config()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-04-21 19:15:39 +00:00
|
|
|
}
|
2023-09-11 11:44:12 +00:00
|
|
|
|
|
|
|
blp, err := BootloaderByName(bootLoader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-09-11 13:42:06 +00:00
|
|
|
bl, err := blp.New(config, osRelease)
|
2023-09-11 11:44:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-19 12:01:08 +00:00
|
|
|
if size == 0 {
|
2023-02-22 16:26:32 +00:00
|
|
|
size = 10 * uint64(datasize.GB)
|
2022-04-19 12:01:08 +00:00
|
|
|
}
|
|
|
|
if disk == "" {
|
|
|
|
disk = "disk0"
|
|
|
|
}
|
2022-04-24 13:49:01 +00:00
|
|
|
img, err := NewImage(ctx, imgTag, workdir)
|
2022-04-21 16:28:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-24 13:49:01 +00:00
|
|
|
// i, err := os.Stat(imgTar)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
// if i.Size() > size {
|
|
|
|
// s := datasize.ByteSize(math.Ceil(datasize.ByteSize(i.Size()).GBytes())) * datasize.GB
|
|
|
|
// logrus.Warnf("%s is smaller than rootfs size, using %s", datasize.ByteSize(size), s)
|
|
|
|
// size = int64(s)
|
|
|
|
// }
|
2022-04-19 12:01:08 +00:00
|
|
|
b := &builder{
|
2022-08-07 16:24:02 +00:00
|
|
|
osRelease: osRelease,
|
2023-09-11 11:44:12 +00:00
|
|
|
config: config,
|
|
|
|
bootloader: bl,
|
2022-08-07 16:24:02 +00:00
|
|
|
img: img,
|
|
|
|
diskRaw: filepath.Join(workdir, disk+".d2vm.raw"),
|
|
|
|
diskOut: filepath.Join(workdir, disk+"."+format),
|
|
|
|
format: f,
|
|
|
|
size: size,
|
|
|
|
mntPoint: filepath.Join(workdir, "/mnt"),
|
|
|
|
cmdLineExtra: cmdLineExtra,
|
2023-02-22 16:26:32 +00:00
|
|
|
splitBoot: splitBoot,
|
|
|
|
bootSize: bootSize,
|
2023-02-23 14:02:12 +00:00
|
|
|
luksPassword: luksPassword,
|
2022-04-19 12:01:08 +00:00
|
|
|
}
|
|
|
|
if err := os.MkdirAll(b.mntPoint, os.ModePerm); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *builder) Build(ctx context.Context) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logrus.WithError(err).Error("Build failed")
|
|
|
|
if err := b.unmountImg(context.Background()); err != nil {
|
|
|
|
logrus.WithError(err).Error("failed to unmount")
|
|
|
|
}
|
|
|
|
if err := b.cleanUp(context.Background()); err != nil {
|
|
|
|
logrus.WithError(err).Error("failed to cleanup")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err = b.cleanUp(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = b.makeImg(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = b.mountImg(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = b.copyRootFS(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = b.setupRootFS(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-11 13:42:06 +00:00
|
|
|
if err = b.installBootloader(ctx); err != nil {
|
2022-04-19 12:01:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = b.unmountImg(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-21 16:28:50 +00:00
|
|
|
if err = b.convert2Img(ctx); err != nil {
|
2022-04-19 12:01:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = b.cleanUp(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *builder) cleanUp(ctx context.Context) error {
|
|
|
|
return os.RemoveAll(b.diskRaw)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *builder) makeImg(ctx context.Context) error {
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
logrus.Infof("creating raw image")
|
|
|
|
if err := block(b.diskRaw, b.size); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-24 14:48:21 +00:00
|
|
|
|
2023-02-22 16:26:32 +00:00
|
|
|
var args []string
|
|
|
|
if b.splitBoot {
|
|
|
|
args = []string{"-s", b.diskRaw,
|
|
|
|
"mklabel", "msdos", "mkpart", "primary", "1Mib", fmt.Sprintf("%dMib", b.bootSize),
|
|
|
|
"mkpart", "primary", fmt.Sprintf("%dMib", b.bootSize), "100%",
|
|
|
|
"set", "1", "boot", "on",
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
args = []string{"-s", b.diskRaw, "mklabel", "msdos", "mkpart", "primary", "1Mib", "100%", "set", "1", "boot", "on"}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := exec.Run(ctx, "parted", args...); err != nil {
|
2022-04-24 14:48:21 +00:00
|
|
|
return err
|
2022-04-19 12:01:08 +00:00
|
|
|
}
|
2023-02-22 16:26:32 +00:00
|
|
|
|
2022-04-19 12:01:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *builder) mountImg(ctx context.Context) error {
|
|
|
|
logrus.Infof("mounting raw image")
|
|
|
|
o, _, err := exec.RunOut(ctx, "losetup", "--show", "-f", b.diskRaw)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.loDevice = strings.TrimSuffix(o, "\n")
|
2023-02-15 09:34:32 +00:00
|
|
|
if err := exec.Run(ctx, "kpartx", "-a", b.loDevice); err != nil {
|
2022-04-19 12:01:08 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-02-22 16:26:32 +00:00
|
|
|
b.bootPart = fmt.Sprintf("/dev/mapper/%sp1", filepath.Base(b.loDevice))
|
2023-02-28 11:01:18 +00:00
|
|
|
b.rootPart = ifElse(b.splitBoot, fmt.Sprintf("/dev/mapper/%sp2", filepath.Base(b.loDevice)), b.bootPart)
|
2023-02-23 14:02:12 +00:00
|
|
|
if b.isLuksEnabled() {
|
|
|
|
logrus.Infof("encrypting root partition")
|
|
|
|
f, err := os.CreateTemp("", "key")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
defer os.Remove(f.Name())
|
|
|
|
if _, err := f.WriteString(b.luksPassword); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// cryptsetup luksFormat --batch-mode --verify-passphrase --type luks2 $ROOT_DEVICE $KEY_FILE
|
|
|
|
if err := exec.Run(ctx, "cryptsetup", "luksFormat", "--batch-mode", "--type", "luks2", b.rootPart, f.Name()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.cryptRoot = fmt.Sprintf("d2vm-%s-root", uuid.New().String())
|
|
|
|
// cryptsetup open -d $KEY_FILE $ROOT_DEVICE $ROOT_LABEL
|
|
|
|
if err := exec.Run(ctx, "cryptsetup", "open", "--key-file", f.Name(), b.rootPart, b.cryptRoot); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.cryptPart = b.rootPart
|
|
|
|
b.rootPart = "/dev/mapper/root"
|
|
|
|
b.mappedCryptRoot = filepath.Join("/dev/mapper", b.cryptRoot)
|
|
|
|
logrus.Infof("creating raw image file system")
|
|
|
|
if err := exec.Run(ctx, "mkfs.ext4", b.mappedCryptRoot); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := exec.Run(ctx, "mount", b.mappedCryptRoot, b.mntPoint); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logrus.Infof("creating raw image file system")
|
|
|
|
if err := exec.Run(ctx, "mkfs.ext4", b.rootPart); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := exec.Run(ctx, "mount", b.rootPart, b.mntPoint); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-19 12:01:08 +00:00
|
|
|
}
|
2023-02-22 16:26:32 +00:00
|
|
|
if !b.splitBoot {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(filepath.Join(b.mntPoint, "boot"), os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := exec.Run(ctx, "mkfs.ext4", b.bootPart); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := exec.Run(ctx, "mount", b.bootPart, filepath.Join(b.mntPoint, "boot")); err != nil {
|
2022-04-19 12:01:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *builder) unmountImg(ctx context.Context) error {
|
|
|
|
logrus.Infof("unmounting raw image")
|
|
|
|
var merr error
|
2023-02-22 16:26:32 +00:00
|
|
|
if b.splitBoot {
|
2023-02-23 14:02:12 +00:00
|
|
|
merr = multierr.Append(merr, exec.Run(ctx, "umount", filepath.Join(b.mntPoint, "boot")))
|
2023-02-22 16:26:32 +00:00
|
|
|
}
|
2023-02-23 14:02:12 +00:00
|
|
|
merr = multierr.Append(merr, exec.Run(ctx, "umount", b.mntPoint))
|
|
|
|
if b.isLuksEnabled() {
|
2023-02-23 19:13:37 +00:00
|
|
|
merr = multierr.Append(merr, exec.Run(ctx, "cryptsetup", "close", b.mappedCryptRoot))
|
2022-04-19 12:01:08 +00:00
|
|
|
}
|
2023-02-23 14:02:12 +00:00
|
|
|
return multierr.Combine(
|
|
|
|
merr,
|
|
|
|
exec.Run(ctx, "kpartx", "-d", b.loDevice),
|
|
|
|
exec.Run(ctx, "losetup", "-d", b.loDevice),
|
|
|
|
)
|
2022-04-19 12:01:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *builder) copyRootFS(ctx context.Context) error {
|
|
|
|
logrus.Infof("copying rootfs to raw image")
|
2022-04-24 13:49:01 +00:00
|
|
|
if err := b.img.Flatten(ctx, b.mntPoint); err != nil {
|
2022-04-19 12:01:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-22 16:26:32 +00:00
|
|
|
func diskUUID(ctx context.Context, disk string) (string, error) {
|
|
|
|
o, _, err := exec.RunOut(ctx, "blkid", "-s", "UUID", "-o", "value", disk)
|
2022-04-19 12:01:08 +00:00
|
|
|
if err != nil {
|
2023-02-22 16:26:32 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return strings.TrimSuffix(o, "\n"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *builder) setupRootFS(ctx context.Context) (err error) {
|
|
|
|
logrus.Infof("setting up rootfs")
|
2023-02-28 11:01:18 +00:00
|
|
|
b.rootUUID, err = diskUUID(ctx, ifElse(b.isLuksEnabled(), b.mappedCryptRoot, b.rootPart))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-22 16:26:32 +00:00
|
|
|
var fstab string
|
|
|
|
if b.splitBoot {
|
|
|
|
b.bootUUID, err = diskUUID(ctx, b.bootPart)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-23 14:02:12 +00:00
|
|
|
if b.isLuksEnabled() {
|
|
|
|
b.cryptUUID, err = diskUUID(ctx, b.cryptPart)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-02-22 16:26:32 +00:00
|
|
|
fstab = fmt.Sprintf("UUID=%s / ext4 errors=remount-ro 0 1\nUUID=%s /boot ext4 errors=remount-ro 0 2\n", b.rootUUID, b.bootUUID)
|
|
|
|
} else {
|
|
|
|
b.bootUUID = b.rootUUID
|
|
|
|
fstab = fmt.Sprintf("UUID=%s / ext4 errors=remount-ro 0 1\n", b.bootUUID)
|
2022-04-19 12:01:08 +00:00
|
|
|
}
|
2022-04-24 13:49:01 +00:00
|
|
|
if err := b.chWriteFile("/etc/fstab", fstab, perm); err != nil {
|
2022-04-19 12:01:08 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-04-24 13:49:01 +00:00
|
|
|
if err := b.chWriteFileIfNotExist("/etc/resolv.conf", "nameserver 8.8.8.8", 0644); err != nil {
|
2022-04-19 12:01:08 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-04-24 13:49:01 +00:00
|
|
|
if err := b.chWriteFileIfNotExist("/etc/hostname", "localhost", perm); err != nil {
|
2022-04-19 12:01:08 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-04-24 13:49:01 +00:00
|
|
|
if err := b.chWriteFileIfNotExist("/etc/hosts", hosts, perm); err != nil {
|
2022-04-19 12:01:08 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-04-24 13:49:01 +00:00
|
|
|
// TODO(adphi): is it the righ fix ?
|
2022-08-12 11:42:55 +00:00
|
|
|
if err := os.RemoveAll("/usr/sbin/policy-rc.d"); err != nil {
|
2022-04-19 12:01:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.RemoveAll(b.chPath("/.dockerenv")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-22 16:26:32 +00:00
|
|
|
// create a symlink to /boot for non-alpine images in order to have a consistent path
|
|
|
|
// even if the image is not split
|
|
|
|
if _, err := os.Stat(b.chPath("/boot/boot")); os.IsNotExist(err) {
|
|
|
|
if err := os.Symlink(".", b.chPath("/boot/boot")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-19 12:01:08 +00:00
|
|
|
}
|
2023-02-22 16:26:32 +00:00
|
|
|
switch b.osRelease.ID {
|
|
|
|
case ReleaseAlpine:
|
|
|
|
by, err := os.ReadFile(b.chPath("/etc/inittab"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
by = append(by, []byte("\n"+"ttyS0::respawn:/sbin/getty -L ttyS0 115200 vt100\n")...)
|
|
|
|
if err := b.chWriteFile("/etc/inittab", string(by), perm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := b.chWriteFileIfNotExist("/etc/network/interfaces", "", perm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case ReleaseUbuntu:
|
|
|
|
if b.osRelease.VersionID >= "20.04" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
case ReleaseDebian, ReleaseKali:
|
|
|
|
t, err := os.Readlink(b.chPath("/vmlinuz"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Symlink(t, b.chPath("/boot/vmlinuz")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t, err = os.Readlink(b.chPath("/initrd.img"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Symlink(t, b.chPath("/boot/initrd.img")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return nil
|
2022-04-19 12:01:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-11 11:44:12 +00:00
|
|
|
func (b *builder) cmdline(_ context.Context) string {
|
2023-02-23 14:02:12 +00:00
|
|
|
if b.isLuksEnabled() {
|
2023-02-28 11:01:18 +00:00
|
|
|
switch b.osRelease.ID {
|
|
|
|
case ReleaseAlpine:
|
2023-09-11 11:44:12 +00:00
|
|
|
return b.config.Cmdline(RootUUID(b.rootUUID), "root=/dev/mapper/root", "cryptdm=root", "cryptroot=UUID="+b.cryptUUID, b.cmdLineExtra)
|
2023-02-28 11:01:18 +00:00
|
|
|
case ReleaseCentOS:
|
2023-09-11 11:44:12 +00:00
|
|
|
return b.config.Cmdline(RootUUID(b.rootUUID), "rd.luks.name=UUID="+b.rootUUID+" rd.luks.uuid="+b.cryptUUID+" rd.luks.crypttab=0", b.cmdLineExtra)
|
2023-02-28 11:01:18 +00:00
|
|
|
default:
|
2023-03-01 12:29:10 +00:00
|
|
|
// for some versions of debian, the cryptopts parameter MUST contain all the following: target,source,key,opts...
|
2023-02-28 16:42:52 +00:00
|
|
|
// see https://salsa.debian.org/cryptsetup-team/cryptsetup/-/blob/debian/buster/debian/functions
|
|
|
|
// and https://cryptsetup-team.pages.debian.net/cryptsetup/README.initramfs.html
|
2023-09-11 11:44:12 +00:00
|
|
|
return b.config.Cmdline(nil, "root=/dev/mapper/root", "cryptopts=target=root,source=UUID="+b.cryptUUID+",key=none,luks", b.cmdLineExtra)
|
2023-02-23 19:13:37 +00:00
|
|
|
}
|
2023-02-23 14:02:12 +00:00
|
|
|
} else {
|
2023-09-11 11:44:12 +00:00
|
|
|
return b.config.Cmdline(RootUUID(b.rootUUID), b.cmdLineExtra)
|
2023-02-23 14:02:12 +00:00
|
|
|
}
|
2022-04-19 12:01:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 13:42:06 +00:00
|
|
|
func (b *builder) installBootloader(ctx context.Context) error {
|
|
|
|
logrus.Infof("installing bootloader")
|
|
|
|
return b.bootloader.Setup(ctx, b.loDevice, b.mntPoint, b.cmdline(ctx))
|
2022-04-19 12:01:08 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 16:28:50 +00:00
|
|
|
func (b *builder) convert2Img(ctx context.Context) error {
|
|
|
|
logrus.Infof("converting to %s", b.format)
|
|
|
|
return exec.Run(ctx, "qemu-img", "convert", b.diskRaw, "-O", b.format, b.diskOut)
|
2022-04-19 12:01:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *builder) chWriteFile(path string, content string, perm os.FileMode) error {
|
|
|
|
return os.WriteFile(b.chPath(path), []byte(content), perm)
|
|
|
|
}
|
|
|
|
|
2022-04-24 13:49:01 +00:00
|
|
|
func (b *builder) chWriteFileIfNotExist(path string, content string, perm os.FileMode) error {
|
|
|
|
if i, err := os.Stat(b.chPath(path)); err == nil && i.Size() != 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return os.WriteFile(b.chPath(path), []byte(content), perm)
|
|
|
|
}
|
|
|
|
|
2022-04-19 12:01:08 +00:00
|
|
|
func (b *builder) chPath(path string) string {
|
|
|
|
return fmt.Sprintf("%s%s", b.mntPoint, path)
|
|
|
|
}
|
|
|
|
|
2023-02-23 14:02:12 +00:00
|
|
|
func (b *builder) isLuksEnabled() bool {
|
|
|
|
return b.luksPassword != ""
|
|
|
|
}
|
|
|
|
|
2022-04-24 13:49:01 +00:00
|
|
|
func (b *builder) Close() error {
|
|
|
|
return b.img.Close()
|
|
|
|
}
|
|
|
|
|
2023-02-22 16:26:32 +00:00
|
|
|
func block(path string, size uint64) error {
|
2022-04-19 12:01:08 +00:00
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2023-02-22 16:26:32 +00:00
|
|
|
return f.Truncate(int64(size))
|
2022-04-19 12:01:08 +00:00
|
|
|
}
|
2022-04-19 12:17:30 +00:00
|
|
|
|
|
|
|
func checkDependencies() error {
|
|
|
|
var merr error
|
2023-02-23 14:02:12 +00:00
|
|
|
for _, v := range []string{"mount", "blkid", "tar", "losetup", "parted", "kpartx", "qemu-img", "extlinux", "dd", "mkfs.ext4", "cryptsetup"} {
|
2022-04-19 12:17:30 +00:00
|
|
|
if _, err := exec2.LookPath(v); err != nil {
|
|
|
|
merr = multierr.Append(merr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return merr
|
|
|
|
}
|
2022-04-21 16:28:50 +00:00
|
|
|
|
|
|
|
func OutputFormats() []string {
|
|
|
|
return formats[:]
|
|
|
|
}
|
2023-02-28 11:01:18 +00:00
|
|
|
|
|
|
|
func ifElse(v bool, t string, f string) string {
|
|
|
|
if v {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|