2
0
mirror of https://github.com/linka-cloud/d2vm.git synced 2024-11-21 15:26:24 +00:00

builder: check dependencies

qemu.sh: fix kvm flag

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
Adphi 2022-04-19 14:17:30 +02:00
parent 362c6e1d8c
commit c240aac13d
Signed by: adphi
GPG Key ID: 46BE4062DB2397FF
2 changed files with 20 additions and 2 deletions

View File

@ -19,6 +19,7 @@ import (
"context"
"fmt"
"os"
exec2 "os/exec"
"path/filepath"
"strings"
@ -63,6 +64,7 @@ ff02::3 ip6-allhosts
KERNEL /boot/vmlinuz
APPEND ro root=UUID=%s initrd=/boot/initrd.img net.ifnames=0 console=tty0 console=ttyS0,115200n8
`
mbrBin = "/usr/lib/EXTLINUX/mbr.bin"
)
var (
@ -84,6 +86,9 @@ type builder struct {
}
func NewBuilder(workdir, src, disk string, size int64, osRelease OSRelease) (*builder, error) {
if err := checkDependencies(); err != nil {
return nil, err
}
if size == 0 {
size = 1
}
@ -291,7 +296,7 @@ func (b *builder) installKernel(ctx context.Context) error {
func (b *builder) setupMBR(ctx context.Context) error {
logrus.Infof("writing MBR")
if err := exec.Run(ctx, "dd", "if=/usr/lib/EXTLINUX/mbr.bin", fmt.Sprintf("of=%s", b.diskRaw), "bs=440", "count=1", "conv=notrunc"); err != nil {
if err := exec.Run(ctx, "dd", fmt.Sprintf("if=%s", mbrBin), fmt.Sprintf("of=%s", b.diskRaw), "bs=440", "count=1", "conv=notrunc"); err != nil {
return err
}
return nil
@ -318,3 +323,16 @@ func block(path string, size int64) error {
defer f.Close()
return f.Truncate(size)
}
func checkDependencies() error {
var merr error
for _, v := range []string{"mount", "blkid", "tar", "kpartx", "losetup", "qemu-img", "extlinux", "dd", "mkfs", "fdisk"} {
if _, err := exec2.LookPath(v); err != nil {
merr = multierr.Append(merr, err)
}
}
if _, err := os.Stat(mbrBin); err != nil {
merr = multierr.Append(merr, err)
}
return merr
}

View File

@ -21,7 +21,7 @@ ARGS=""
if [[ $OSTYPE == 'darwin'* ]]; then
ARGS="-M accel=hvf"
else
ARGS="-use-kvm"
ARGS="-enable-kvm"
fi
qemu-system-x86_64 -drive file=$IMG,index=0,media=disk,format=qcow2 -m 4096 -cpu host -nographic ${ARGS} ${@: 2}