2
0
mirror of https://github.com/linka-cloud/d2vm.git synced 2024-11-29 02:56: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 adbd4c7233
commit b09f0e07ad
Signed by: adphi
GPG Key ID: 46BE4062DB2397FF
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)
require.NoError(t, err)
assert.Equal(t, sysconf, sys)
d, err := NewDockerfile(r, img, "root")
d, err := NewDockerfile(r, img, "root", "")
require.NoError(t, err)
logrus.Infof("docker image based on %s", d.Release.Name)
p := filepath.Join(tmpPath, docker.FormatImgName(img))
@ -75,12 +75,6 @@ func TestSyslinuxCfg(t *testing.T) {
initrd: "/boot/initrd.img",
sysconfig: syslinuxCfgUbuntu,
},
{
image: "debian:8",
kernel: "/vmlinuz",
initrd: "/initrd.img",
sysconfig: syslinuxCfgDebian,
},
{
image: "debian:9",
kernel: "/vmlinuz",

View File

@ -32,6 +32,7 @@ import (
var (
file = "Dockerfile"
tag = uuid.New().String()
networkManager string
buildArgs []string
buildCmd = &cobra.Command{
Use: "build [context directory]",
@ -54,7 +55,7 @@ var (
if err := docker.Build(cmd.Context(), tag, file, args[0], buildArgs...); err != nil {
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().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(&networkManager, "network-manager", "", "Network manager to use for the image: none, netplan, ifupdown")
}

View File

@ -79,7 +79,7 @@ var (
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(&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(&networkManager, "network-manager", "", "Network manager to use for the image: none, netplan, ifupdown")
rootCmd.AddCommand(convertCmd)
}

View File

@ -28,7 +28,7 @@ import (
"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()
tmpPath := filepath.Join(os.TempDir(), "d2vm", imgUUID)
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 {
return err
}
d, err := NewDockerfile(r, img, password)
d, err := NewDockerfile(r, img, password, networkManager)
if err != nil {
return err
}

View File

@ -19,6 +19,8 @@ import (
"fmt"
"io"
"text/template"
"github.com/sirupsen/logrus"
)
//go:embed templates/ubuntu.Dockerfile
@ -40,10 +42,19 @@ var (
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 {
Image string
Password string
Release OSRelease
NetworkManager NetworkManager
tmpl *template.Template
}
@ -51,22 +62,30 @@ func (d Dockerfile) Render(w io.Writer) error {
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 == "" {
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 {
case ReleaseDebian:
d.tmpl = debianDockerfileTemplate
net = NetworkManagerIfupdown2
case ReleaseUbuntu:
d.tmpl = ubuntuDockerfileTemplate
net = NetworkManagerNetplan
case ReleaseAlpine:
d.tmpl = alpineDockerfileTemplate
net = NetworkManagerIfupdown2
case ReleaseCentOS:
d.tmpl = centOSDockerfileTemplate
default:
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
}

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
{{ 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 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 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 }}