mirror of
https://github.com/linka-cloud/d2vm.git
synced 2024-11-22 07:46: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:
parent
a21fb68b7b
commit
1a97b45861
@ -9,7 +9,7 @@ RUN go mod download
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN go build -o d2vm ./cmd/d2vm
|
||||
RUN make build
|
||||
|
||||
FROM ubuntu
|
||||
|
||||
|
3
Makefile
3
Makefile
@ -38,3 +38,6 @@ docker-run:
|
||||
-v $(PWD):/build \
|
||||
-w /build \
|
||||
$(DOCKER_IMAGE) bash
|
||||
|
||||
build:
|
||||
@go build -o d2vm -ldflags "-s -w -X '$(MODULE).Version=$(VERSION)' -X '$(MODULE).BuildDate=$(shell date)'" ./cmd/d2vm
|
||||
|
31
builder.go
31
builder.go
@ -66,13 +66,23 @@ 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 (
|
||||
fdiskCmds = []string{"n", "p", "1", "", "", "a", "w"}
|
||||
|
||||
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 {
|
||||
@ -86,6 +96,8 @@ type builder struct {
|
||||
size int64
|
||||
mntPoint string
|
||||
|
||||
mbrPath string
|
||||
|
||||
loDevice string
|
||||
loPart string
|
||||
diskUUD string
|
||||
@ -105,6 +117,17 @@ func NewBuilder(workdir, src, disk string, size int64, osRelease OSRelease, form
|
||||
if !valid {
|
||||
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 {
|
||||
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"),
|
||||
format: f,
|
||||
size: size,
|
||||
mbrPath: mbrBin,
|
||||
mntPoint: filepath.Join(workdir, "/mnt"),
|
||||
}
|
||||
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 {
|
||||
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 nil
|
||||
@ -357,9 +381,6 @@ func checkDependencies() error {
|
||||
merr = multierr.Append(merr, err)
|
||||
}
|
||||
}
|
||||
if _, err := os.Stat(mbrBin); err != nil {
|
||||
merr = multierr.Append(merr, err)
|
||||
}
|
||||
return merr
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,8 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"go.linka.cloud/d2vm"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -31,6 +33,7 @@ var (
|
||||
rootCmd = &cobra.Command{
|
||||
Use: "d2vm",
|
||||
SilenceUsage: true,
|
||||
Version: d2vm.Version,
|
||||
}
|
||||
)
|
||||
|
||||
|
23
cmd/d2vm/version.go
Normal file
23
cmd/d2vm/version.go
Normal 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)
|
||||
}
|
@ -20,6 +20,7 @@ ARG SSH_KEY=https://github.com/${USER}.keys
|
||||
|
||||
# Setup user environment
|
||||
RUN DEBIAN_FRONTEND=noninteractive apt install -y \
|
||||
bash-completion \
|
||||
curl \
|
||||
zsh \
|
||||
git \
|
||||
|
6
version.go
Normal file
6
version.go
Normal file
@ -0,0 +1,6 @@
|
||||
package d2vm
|
||||
|
||||
var (
|
||||
Version = ""
|
||||
BuildDate = ""
|
||||
)
|
Loading…
Reference in New Issue
Block a user