2
0
mirror of https://github.com/linka-cloud/d2vm.git synced 2024-11-22 07:46:25 +00:00

d2vm: remove kpartx dependency, use parted instead of fdisk

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
Adphi 2022-04-24 16:48:21 +02:00
parent 085e57a07a
commit 13d913db38
Signed by: adphi
GPG Key ID: F2159213400E50AB
2 changed files with 7 additions and 24 deletions

View File

@ -16,13 +16,11 @@ FROM ubuntu
RUN apt-get update && \ RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get -y install --no-install-recommends \ DEBIAN_FRONTEND=noninteractive apt-get -y install --no-install-recommends \
util-linux \ util-linux \
kpartx \ parted \
e2fsprogs \ e2fsprogs \
xfsprogs \
mount \ mount \
tar \ tar \
extlinux \ extlinux \
uuid-runtime \
qemu-utils qemu-utils
COPY --from=docker:dind /usr/local/bin/docker /usr/local/bin/ COPY --from=docker:dind /usr/local/bin/docker /usr/local/bin/

View File

@ -15,7 +15,6 @@
package d2vm package d2vm
import ( import (
"bytes"
"context" "context"
"fmt" "fmt"
"os" "os"
@ -68,8 +67,6 @@ ff02::3 ip6-allhosts
) )
var ( var (
fdiskCmds = []string{"n", "p", "1", "", "", "a", "w"}
formats = []string{"qcow2", "qed", "raw", "vdi", "vhd", "vmdk"} formats = []string{"qcow2", "qed", "raw", "vdi", "vhd", "vmdk"}
mbrPaths = []string{ mbrPaths = []string{
@ -225,18 +222,9 @@ func (b *builder) makeImg(ctx context.Context) error {
if err := block(b.diskRaw, b.size); err != nil { if err := block(b.diskRaw, b.size); err != nil {
return err return err
} }
c := exec.CommandContext(ctx, "fdisk", b.diskRaw)
var i bytes.Buffer if err := exec.Run(ctx, "parted", "-s", b.diskRaw, "mklabel", "msdos", "mkpart", "primary", "1Mib", "100%", "set", "1", "boot", "on"); err != nil {
for _, v := range fdiskCmds { return err
if _, err := i.Write([]byte(v + "\n")); err != nil {
return err
}
}
var e bytes.Buffer
c.Stdin = &i
c.Stderr = &e
if err := c.Run(); err != nil {
return fmt.Errorf("%w: %s", err, e.String())
} }
return nil return nil
} }
@ -248,10 +236,10 @@ func (b *builder) mountImg(ctx context.Context) error {
return err return err
} }
b.loDevice = strings.TrimSuffix(o, "\n") b.loDevice = strings.TrimSuffix(o, "\n")
if err := exec.Run(ctx, "kpartx", "-a", b.loDevice); err != nil { if err := exec.Run(ctx, "partprobe", b.loDevice); err != nil {
return err return err
} }
b.loPart = fmt.Sprintf("/dev/mapper/%sp1", filepath.Base(b.loDevice)) b.loPart = fmt.Sprintf("%sp1", b.loDevice)
logrus.Infof("creating raw image file system") logrus.Infof("creating raw image file system")
if err := exec.Run(ctx, "mkfs.ext4", b.loPart); err != nil { if err := exec.Run(ctx, "mkfs.ext4", b.loPart); err != nil {
return err return err
@ -268,9 +256,6 @@ func (b *builder) unmountImg(ctx context.Context) error {
if err := exec.Run(ctx, "umount", b.mntPoint); err != nil { if err := exec.Run(ctx, "umount", b.mntPoint); err != nil {
merr = multierr.Append(merr, err) merr = multierr.Append(merr, err)
} }
if err := exec.Run(ctx, "kpartx", "-d", b.loDevice); err != nil {
merr = multierr.Append(merr, err)
}
if err := exec.Run(ctx, "losetup", "-d", b.loDevice); err != nil { if err := exec.Run(ctx, "losetup", "-d", b.loDevice); err != nil {
merr = multierr.Append(merr, err) merr = multierr.Append(merr, err)
} }
@ -396,7 +381,7 @@ func block(path string, size int64) error {
func checkDependencies() error { func checkDependencies() error {
var merr error var merr error
for _, v := range []string{"mount", "blkid", "tar", "kpartx", "losetup", "qemu-img", "extlinux", "dd", "mkfs", "fdisk"} { for _, v := range []string{"mount", "blkid", "tar", "losetup", "parted", "partprobe", "qemu-img", "extlinux", "dd", "mkfs"} {
if _, err := exec2.LookPath(v); err != nil { if _, err := exec2.LookPath(v); err != nil {
merr = multierr.Append(merr, err) merr = multierr.Append(merr, err)
} }