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

d2vm: add version command, lookup mbr.bin in well-known paths

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
Adphi 2022-04-21 21:15:39 +02:00
parent a21fb68b7b
commit 1a97b45861
Signed by: adphi
GPG Key ID: F2159213400E50AB
7 changed files with 63 additions and 6 deletions

View File

@ -9,7 +9,7 @@ RUN go mod download
COPY . . COPY . .
RUN go build -o d2vm ./cmd/d2vm RUN make build
FROM ubuntu FROM ubuntu

View File

@ -38,3 +38,6 @@ docker-run:
-v $(PWD):/build \ -v $(PWD):/build \
-w /build \ -w /build \
$(DOCKER_IMAGE) bash $(DOCKER_IMAGE) bash
build:
@go build -o d2vm -ldflags "-s -w -X '$(MODULE).Version=$(VERSION)' -X '$(MODULE).BuildDate=$(shell date)'" ./cmd/d2vm

View File

@ -66,13 +66,23 @@ ff02::3 ip6-allhosts
KERNEL /boot/vmlinuz KERNEL /boot/vmlinuz
APPEND ro root=UUID=%s initrd=/boot/initrd.img net.ifnames=0 console=tty0 console=ttyS0,115200n8 APPEND ro root=UUID=%s initrd=/boot/initrd.img net.ifnames=0 console=tty0 console=ttyS0,115200n8
` `
mbrBin = "/usr/lib/EXTLINUX/mbr.bin"
) )
var ( var (
fdiskCmds = []string{"n", "p", "1", "", "", "a", "w"} 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{
// debian path
"/usr/lib/syslinux/mbr/mbr.bin",
// ubuntu path
"/usr/lib/EXTLINUX/mbr.bin",
// alpine path
"/usr/share/syslinux/mbr.bin",
// centos path
"/usr/share/syslinux/mbr.bin",
}
) )
type builder struct { type builder struct {
@ -86,6 +96,8 @@ type builder struct {
size int64 size int64
mntPoint string mntPoint string
mbrPath string
loDevice string loDevice string
loPart string loPart string
diskUUD string diskUUD string
@ -105,6 +117,17 @@ func NewBuilder(workdir, src, disk string, size int64, osRelease OSRelease, form
if !valid { if !valid {
return nil, fmt.Errorf("invalid format: %s valid formats are: %s", f, strings.Join(formats, " ")) return nil, fmt.Errorf("invalid format: %s valid formats are: %s", f, strings.Join(formats, " "))
} }
mbrBin := ""
for _, v := range mbrPaths {
if _, err := os.Stat(v); err == nil {
mbrBin = v
break
}
}
if mbrBin == "" {
return nil, fmt.Errorf("unable to find syslinux's mbr.bin path")
}
if size == 0 { if size == 0 {
size = 10 * int64(datasize.GB) size = 10 * int64(datasize.GB)
} }
@ -127,6 +150,7 @@ func NewBuilder(workdir, src, disk string, size int64, osRelease OSRelease, form
diskOut: filepath.Join(workdir, disk+".qcow2"), diskOut: filepath.Join(workdir, disk+".qcow2"),
format: f, format: f,
size: size, size: size,
mbrPath: mbrBin,
mntPoint: filepath.Join(workdir, "/mnt"), mntPoint: filepath.Join(workdir, "/mnt"),
} }
if err := os.MkdirAll(b.mntPoint, os.ModePerm); err != nil { if err := os.MkdirAll(b.mntPoint, os.ModePerm); err != nil {
@ -322,7 +346,7 @@ func (b *builder) installKernel(ctx context.Context) error {
func (b *builder) setupMBR(ctx context.Context) error { func (b *builder) setupMBR(ctx context.Context) error {
logrus.Infof("writing MBR") logrus.Infof("writing MBR")
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 { if err := exec.Run(ctx, "dd", fmt.Sprintf("if=%s", b.mbrPath), fmt.Sprintf("of=%s", b.diskRaw), "bs=440", "count=1", "conv=notrunc"); err != nil {
return err return err
} }
return nil return nil
@ -357,9 +381,6 @@ func checkDependencies() error {
merr = multierr.Append(merr, err) merr = multierr.Append(merr, err)
} }
} }
if _, err := os.Stat(mbrBin); err != nil {
merr = multierr.Append(merr, err)
}
return merr return merr
} }

View File

@ -18,6 +18,8 @@ import (
"context" "context"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"go.linka.cloud/d2vm"
) )
var ( var (
@ -31,6 +33,7 @@ var (
rootCmd = &cobra.Command{ rootCmd = &cobra.Command{
Use: "d2vm", Use: "d2vm",
SilenceUsage: true, SilenceUsage: true,
Version: d2vm.Version,
} }
) )

23
cmd/d2vm/version.go Normal file
View File

@ -0,0 +1,23 @@
package main
import (
"fmt"
"github.com/spf13/cobra"
"go.linka.cloud/d2vm"
)
var (
cmdVersion = &cobra.Command{
Use: "version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(d2vm.Version)
fmt.Println(d2vm.BuildDate)
},
}
)
func init() {
rootCmd.AddCommand(cmdVersion)
}

View File

@ -20,6 +20,7 @@ ARG SSH_KEY=https://github.com/${USER}.keys
# Setup user environment # Setup user environment
RUN DEBIAN_FRONTEND=noninteractive apt install -y \ RUN DEBIAN_FRONTEND=noninteractive apt install -y \
bash-completion \
curl \ curl \
zsh \ zsh \
git \ git \

6
version.go Normal file
View File

@ -0,0 +1,6 @@
package d2vm
var (
Version = ""
BuildDate = ""
)