2
0
mirror of https://github.com/linka-cloud/d2vm.git synced 2024-11-29 19:16:24 +00:00

convert / build: add networking support through network-manager flag

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
Adphi 2022-08-08 17:58:49 +02:00
parent 7ea15f0463
commit c25a449e69
Signed by: adphi
GPG Key ID: F2159213400E50AB
8 changed files with 91 additions and 21 deletions

View File

@ -27,7 +27,7 @@ func testSysconfig(t *testing.T, ctx context.Context, img, sysconf, kernel, init
sys, err := sysconfig(r) sys, err := sysconfig(r)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, sysconf, sys) assert.Equal(t, sysconf, sys)
d, err := NewDockerfile(r, img, "root") d, err := NewDockerfile(r, img, "root", "")
require.NoError(t, err) require.NoError(t, err)
logrus.Infof("docker image based on %s", d.Release.Name) logrus.Infof("docker image based on %s", d.Release.Name)
p := filepath.Join(tmpPath, docker.FormatImgName(img)) p := filepath.Join(tmpPath, docker.FormatImgName(img))
@ -75,12 +75,6 @@ func TestSyslinuxCfg(t *testing.T) {
initrd: "/boot/initrd.img", initrd: "/boot/initrd.img",
sysconfig: syslinuxCfgUbuntu, sysconfig: syslinuxCfgUbuntu,
}, },
{
image: "debian:8",
kernel: "/vmlinuz",
initrd: "/initrd.img",
sysconfig: syslinuxCfgDebian,
},
{ {
image: "debian:9", image: "debian:9",
kernel: "/vmlinuz", kernel: "/vmlinuz",

View File

@ -30,10 +30,11 @@ import (
) )
var ( var (
file = "Dockerfile" file = "Dockerfile"
tag = uuid.New().String() tag = uuid.New().String()
buildArgs []string networkManager string
buildCmd = &cobra.Command{ buildArgs []string
buildCmd = &cobra.Command{
Use: "build [context directory]", Use: "build [context directory]",
Short: "Build a vm image from Dockerfile", Short: "Build a vm image from Dockerfile",
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
@ -54,7 +55,7 @@ var (
if err := docker.Build(cmd.Context(), tag, file, args[0], buildArgs...); err != nil { if err := docker.Build(cmd.Context(), tag, file, args[0], buildArgs...); err != nil {
return err return err
} }
return d2vm.Convert(cmd.Context(), tag, size, password, output, cmdLineExtra) return d2vm.Convert(cmd.Context(), tag, size, password, output, cmdLineExtra, d2vm.NetworkManager(networkManager))
}, },
} }
) )
@ -71,4 +72,5 @@ func init() {
buildCmd.Flags().BoolVarP(&debug, "debug", "d", false, "Enable Debug output") buildCmd.Flags().BoolVarP(&debug, "debug", "d", false, "Enable Debug output")
buildCmd.Flags().BoolVar(&force, "force", false, "Override output image") buildCmd.Flags().BoolVar(&force, "force", false, "Override output image")
buildCmd.Flags().StringVar(&cmdLineExtra, "append-to-cmdline", "", "Extra kernel cmdline arguments to append to the generated one") buildCmd.Flags().StringVar(&cmdLineExtra, "append-to-cmdline", "", "Extra kernel cmdline arguments to append to the generated one")
buildCmd.Flags().StringVar(&networkManager, "network-manager", "", "Network manager to use for the image: none, netplan, ifupdown")
} }

View File

@ -79,7 +79,7 @@ var (
return err return err
} }
} }
return d2vm.Convert(cmd.Context(), img, size, password, output, cmdLineExtra) return d2vm.Convert(cmd.Context(), img, size, password, output, cmdLineExtra, d2vm.NetworkManager(networkManager))
}, },
} }
) )
@ -100,5 +100,6 @@ func init() {
convertCmd.Flags().BoolVarP(&debug, "debug", "d", false, "Enable Debug output") convertCmd.Flags().BoolVarP(&debug, "debug", "d", false, "Enable Debug output")
convertCmd.Flags().BoolVarP(&force, "force", "f", false, "Override output qcow2 image") convertCmd.Flags().BoolVarP(&force, "force", "f", false, "Override output qcow2 image")
convertCmd.Flags().StringVar(&cmdLineExtra, "append-to-cmdline", "", "Extra kernel cmdline arguments to append to the generated one") convertCmd.Flags().StringVar(&cmdLineExtra, "append-to-cmdline", "", "Extra kernel cmdline arguments to append to the generated one")
convertCmd.Flags().StringVar(&networkManager, "network-manager", "", "Network manager to use for the image: none, netplan, ifupdown")
rootCmd.AddCommand(convertCmd) rootCmd.AddCommand(convertCmd)
} }

View File

@ -28,7 +28,7 @@ import (
"go.linka.cloud/d2vm/pkg/docker" "go.linka.cloud/d2vm/pkg/docker"
) )
func Convert(ctx context.Context, img string, size int64, password string, output string, cmdLineExtra string) error { func Convert(ctx context.Context, img string, size int64, password string, output string, cmdLineExtra string, networkManager NetworkManager) error {
imgUUID := uuid.New().String() imgUUID := uuid.New().String()
tmpPath := filepath.Join(os.TempDir(), "d2vm", imgUUID) tmpPath := filepath.Join(os.TempDir(), "d2vm", imgUUID)
if err := os.MkdirAll(tmpPath, os.ModePerm); err != nil { if err := os.MkdirAll(tmpPath, os.ModePerm); err != nil {
@ -41,7 +41,7 @@ func Convert(ctx context.Context, img string, size int64, password string, outpu
if err != nil { if err != nil {
return err return err
} }
d, err := NewDockerfile(r, img, password) d, err := NewDockerfile(r, img, password, networkManager)
if err != nil { if err != nil {
return err return err
} }

View File

@ -19,6 +19,8 @@ import (
"fmt" "fmt"
"io" "io"
"text/template" "text/template"
"github.com/sirupsen/logrus"
) )
//go:embed templates/ubuntu.Dockerfile //go:embed templates/ubuntu.Dockerfile
@ -40,33 +42,50 @@ var (
centOSDockerfileTemplate = template.Must(template.New("centos.Dockerfile").Parse(centOSDockerfile)) centOSDockerfileTemplate = template.Must(template.New("centos.Dockerfile").Parse(centOSDockerfile))
) )
type NetworkManager string
const (
NetworkManagerNone NetworkManager = "none"
NetworkManagerIfupdown2 NetworkManager = "ifupdown"
NetworkManagerNetplan NetworkManager = "netplan"
)
type Dockerfile struct { type Dockerfile struct {
Image string Image string
Password string Password string
Release OSRelease Release OSRelease
tmpl *template.Template NetworkManager NetworkManager
tmpl *template.Template
} }
func (d Dockerfile) Render(w io.Writer) error { func (d Dockerfile) Render(w io.Writer) error {
return d.tmpl.Execute(w, d) return d.tmpl.Execute(w, d)
} }
func NewDockerfile(release OSRelease, img, password string) (Dockerfile, error) { func NewDockerfile(release OSRelease, img, password string, networkManager NetworkManager) (Dockerfile, error) {
if password == "" { if password == "" {
password = "root" password = "root"
} }
d := Dockerfile{Release: release, Image: img, Password: password} d := Dockerfile{Release: release, Image: img, Password: password, NetworkManager: networkManager}
var net NetworkManager
switch release.ID { switch release.ID {
case ReleaseDebian: case ReleaseDebian:
d.tmpl = debianDockerfileTemplate d.tmpl = debianDockerfileTemplate
net = NetworkManagerIfupdown2
case ReleaseUbuntu: case ReleaseUbuntu:
d.tmpl = ubuntuDockerfileTemplate d.tmpl = ubuntuDockerfileTemplate
net = NetworkManagerNetplan
case ReleaseAlpine: case ReleaseAlpine:
d.tmpl = alpineDockerfileTemplate d.tmpl = alpineDockerfileTemplate
net = NetworkManagerIfupdown2
case ReleaseCentOS: case ReleaseCentOS:
d.tmpl = centOSDockerfileTemplate d.tmpl = centOSDockerfileTemplate
default: default:
return Dockerfile{}, fmt.Errorf("unsupported distribution: %s", release.ID) return Dockerfile{}, fmt.Errorf("unsupported distribution: %s", release.ID)
} }
if d.NetworkManager == "" {
logrus.Warnf("no network manager specified, using distribution defaults: %s", net)
d.NetworkManager = net
}
return d, nil return d, nil
} }

View File

@ -21,3 +21,11 @@ RUN for s in devfs dmesg hwdrivers mdev; do rc-update add $s sysinit; done
RUN echo "root:{{- if .Password}}{{ .Password}}{{- else}}root{{- end}}" | chpasswd RUN echo "root:{{- if .Password}}{{ .Password}}{{- else}}root{{- end}}" | chpasswd
{{ if eq .NetworkManager "ifupdown"}}
RUN apk add --no-cache ifupdown-ng
RUN mkdir -p /etc/network && printf '\
auto eth0\n\
allow-hotplug eth0\n\
iface eth0 inet dhcp\n\
' > /etc/network/interfaces
{{ end }}

View File

@ -17,3 +17,26 @@ RUN DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
RUN systemctl preset-all RUN systemctl preset-all
RUN echo "root:{{- if .Password}}{{ .Password}}{{- else}}root{{- end}}" | chpasswd RUN echo "root:{{- if .Password}}{{ .Password}}{{- else}}root{{- end}}" | chpasswd
{{ if eq .NetworkManager "netplan" }}
RUN apt install -y netplan.io
RUN mkdir -p /etc/netplan && printf '\
network:\n\
version: 2\n\
renderer: networkd\n\
ethernets:\n\
eth0:\n\
dhcp4: true\n\
nameservers:\n\
addresses:\n\
- 8.8.8.8\n\
- 8.8.4.4\n\
' > /etc/netplan/00-netcfg.yaml \
{{ else if eq .NetworkManager "ifupdown"}}
RUN apt install -y ifupdown2
RUN mkdir -p /etc/network && printf '\
auto eth0\n\
allow-hotplug eth0\n\
iface eth0 inet dhcp\n\
' > /etc/network/interfaces
{{ end }}

View File

@ -16,3 +16,26 @@ RUN apt-get update -y && \
RUN systemctl preset-all RUN systemctl preset-all
RUN echo "root:{{- if .Password}}{{ .Password}}{{- else}}root{{- end}}" | chpasswd RUN echo "root:{{- if .Password}}{{ .Password}}{{- else}}root{{- end}}" | chpasswd
{{ if eq .NetworkManager "netplan" }}
RUN apt install -y netplan.io
RUN mkdir -p /etc/netplan && printf '\
network:\n\
version: 2\n\
renderer: networkd\n\
ethernets:\n\
eth0:\n\
dhcp4: true\n\
nameservers:\n\
addresses:\n\
- 8.8.8.8\n\
- 8.8.4.4\n\
' > /etc/netplan/00-netcfg.yaml \
{{ else if eq .NetworkManager "ifupdown"}}
RUN apt install -y ifupdown-ng
RUN mkdir -p /etc/network && printf '\
auto eth0\n\
allow-hotplug eth0\n\
iface eth0 inet dhcp\n\
' > /etc/network/interfaces
{{ end }}