2
0
mirror of https://github.com/linka-cloud/d2vm.git synced 2026-01-25 19:15:04 +00:00

4 Commits

Author SHA1 Message Date
fa3a4f6039 d2vm: remove non working rhel support
Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
2022-04-21 21:31:57 +02:00
1a97b45861 d2vm: add version command, lookup mbr.bin in well-known paths
Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
2022-04-21 21:15:39 +02:00
a21fb68b7b fix typo (again)
Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
2022-04-21 19:13:46 +02:00
04bf1810e2 fix typo
Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
2022-04-21 18:37:33 +02:00
11 changed files with 76 additions and 13 deletions

View File

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

View File

@@ -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

View File

@@ -28,9 +28,13 @@ Working and tested:
Need fix:
- [ ] CentOS / RHEL
- [ ] CentOS
The program use the `/etc/os-release` file to discovery the Linux Distribution and install the Kernel,
Unsupported:
- [ ] RHEL
The program use the `/etc/os-release` file to discover the Linux distribution and install the Kernel,
if the file is missing, the build cannot succeed.
Obviously, **Distroless** images are not supported.

View File

@@ -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 {
@@ -309,7 +333,7 @@ func (b *builder) installKernel(ctx context.Context) error {
sysconfig = syslinuxCfgDebian
case ReleaseAlpine:
sysconfig = syslinuxCfgAlpine
case ReleaseCentOS, ReleaseRHEL:
case ReleaseCentOS:
sysconfig = syslinuxCfgCentOS
default:
return fmt.Errorf("%s: distribution not supported", b.osRelease.ID)
@@ -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
}

View File

@@ -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
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

@@ -63,7 +63,7 @@ func NewDockerfile(release OSRelease, img, password string) (Dockerfile, error)
d.tmpl = ubuntuDockerfileTemplate
case ReleaseAlpine:
d.tmpl = alpineDockerfileTemplate
case ReleaseCentOS, ReleaseRHEL:
case ReleaseCentOS:
d.tmpl = centOSDockerfileTemplate
default:
return Dockerfile{}, fmt.Errorf("unsupported distribution: %s", release.ID)

View File

@@ -2,7 +2,7 @@ FROM ubuntu
# Install netplan sudo ssh-server and dns utils
RUN apt update && DEBIAN_FRONTEND=noninteractive apt install -y \
ameu-guest-agent \
qemu-guest-agent \
netplan.io \
dnsutils \
sudo \
@@ -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 \

View File

@@ -8,7 +8,7 @@ FROM ubuntu
# Install netplan sudo ssh-server and dns utils
RUN apt update && DEBIAN_FRONTEND=noninteractive apt install -y \
ameu-guest-agent \
qemu-guest-agent \
netplan.io \
dnsutils \
sudo \

View File

@@ -46,8 +46,10 @@ func (r Release) Supported() bool {
return true
case ReleaseAlpine:
return true
case ReleaseCentOS, ReleaseRHEL:
case ReleaseCentOS:
return true
case ReleaseRHEL:
return false
default:
return false
}

6
version.go Normal file
View File

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