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

feat: add --dns and --dns-search flags to customize the vm dns configuration

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
2026-01-21 17:23:57 +01:00
parent f799dc4891
commit 0ecdf57a6a
9 changed files with 58 additions and 6 deletions

View File

@@ -169,6 +169,8 @@ Flags:
--boot-fs string Filesystem to use for the boot partition, ext4 or fat32 --boot-fs string Filesystem to use for the boot partition, ext4 or fat32
--boot-size uint Size of the boot partition in MB (default 100) --boot-size uint Size of the boot partition in MB (default 100)
--bootloader string Bootloader to use: syslinux, grub, grub-bios, grub-efi, defaults to syslinux on amd64 and grub-efi on arm64 --bootloader string Bootloader to use: syslinux, grub, grub-bios, grub-efi, defaults to syslinux on amd64 and grub-efi on arm64
--dns strings DNS servers to set in the generated image
--dns-search strings DNS search domains to set in the generated image
--force Override output qcow2 image --force Override output qcow2 image
-h, --help help for convert -h, --help help for convert
--hostname string Hostname to set in the generated image (default "localhost") --hostname string Hostname to set in the generated image (default "localhost")
@@ -328,6 +330,8 @@ Flags:
--boot-fs string Filesystem to use for the boot partition, ext4 or fat32 --boot-fs string Filesystem to use for the boot partition, ext4 or fat32
--boot-size uint Size of the boot partition in MB (default 100) --boot-size uint Size of the boot partition in MB (default 100)
--bootloader string Bootloader to use: syslinux, grub, grub-bios, grub-efi, defaults to syslinux on amd64 and grub-efi on arm64 --bootloader string Bootloader to use: syslinux, grub, grub-bios, grub-efi, defaults to syslinux on amd64 and grub-efi on arm64
--dns strings DNS servers to set in the generated image
--dns-search strings DNS search domains to set in the generated image
--build-arg stringArray Set build-time variables --build-arg stringArray Set build-time variables
-f, --file string Name of the Dockerfile -f, --file string Name of the Dockerfile
--force Override output qcow2 image --force Override output qcow2 image

View File

@@ -84,10 +84,12 @@ type builder struct {
cmdLineExtra string cmdLineExtra string
arch string arch string
hostname string hostname string
dns []string
dnsSearch []string
} }
func NewBuilder(ctx context.Context, workdir, imgTag, disk string, size uint64, osRelease OSRelease, format string, cmdLineExtra string, splitBoot bool, bootFS BootFS, bootSize uint64, luksPassword string, bootLoader string, platform, hostname string) (Builder, error) { func NewBuilder(ctx context.Context, workdir, imgTag, disk string, size uint64, osRelease OSRelease, format string, cmdLineExtra string, splitBoot bool, bootFS BootFS, bootSize uint64, luksPassword string, bootLoader string, platform, hostname string, dns, dnsSearch []string) (Builder, error) {
var arch string var arch string
switch platform { switch platform {
case "linux/amd64": case "linux/amd64":
@@ -184,6 +186,9 @@ func NewBuilder(ctx context.Context, workdir, imgTag, disk string, size uint64,
if hostname == "" { if hostname == "" {
hostname = "localhost" hostname = "localhost"
} }
if len(dns) == 0 {
dns = []string{"8.8.8.8"}
}
b := &builder{ b := &builder{
osRelease: osRelease, osRelease: osRelease,
config: config, config: config,
@@ -201,6 +206,8 @@ func NewBuilder(ctx context.Context, workdir, imgTag, disk string, size uint64,
luksPassword: luksPassword, luksPassword: luksPassword,
arch: arch, arch: arch,
hostname: hostname, hostname: hostname,
dns: dns,
dnsSearch: dnsSearch,
} }
if err := b.checkDependencies(); err != nil { if err := b.checkDependencies(); err != nil {
return nil, err return nil, err
@@ -415,7 +422,7 @@ func (b *builder) setupRootFS(ctx context.Context) (err error) {
if err := b.chWriteFile("/etc/fstab", fstab, perm); err != nil { if err := b.chWriteFile("/etc/fstab", fstab, perm); err != nil {
return err return err
} }
if err := b.chWriteFileIfNotExist("/etc/resolv.conf", "nameserver 8.8.8.8", 0644); err != nil { if err := b.chWriteFile("/etc/resolv.conf", b.resolvConf(), 0644); err != nil {
return err return err
} }
if err := b.chWriteFile("/etc/hostname", b.hostname+"\n", perm); err != nil { if err := b.chWriteFile("/etc/hostname", b.hostname+"\n", perm); err != nil {
@@ -500,6 +507,21 @@ func (b *builder) isLuksEnabled() bool {
return b.luksPassword != "" return b.luksPassword != ""
} }
func (b *builder) resolvConf() string {
var sb strings.Builder
for _, v := range b.dns {
sb.WriteString("nameserver ")
sb.WriteString(v)
sb.WriteString("\n")
}
if len(b.dnsSearch) > 0 {
sb.WriteString("search ")
sb.WriteString(strings.Join(b.dnsSearch, " "))
sb.WriteString("\n")
}
return sb.String()
}
func (b *builder) Close() error { func (b *builder) Close() error {
return b.img.Close() return b.img.Close()
} }

View File

@@ -111,6 +111,8 @@ var (
d2vm.WithPlatform(platform), d2vm.WithPlatform(platform),
d2vm.WithPull(false), d2vm.WithPull(false),
d2vm.WithHostname(hostname), d2vm.WithHostname(hostname),
d2vm.WithDNS(dns),
d2vm.WithDNSSearch(dnsSearch),
); err != nil { ); err != nil {
return err return err
} }

View File

@@ -92,6 +92,8 @@ var (
d2vm.WithPlatform(platform), d2vm.WithPlatform(platform),
d2vm.WithPull(pull), d2vm.WithPull(pull),
d2vm.WithHostname(hostname), d2vm.WithHostname(hostname),
d2vm.WithDNS(dns),
d2vm.WithDNSSearch(dnsSearch),
); err != nil { ); err != nil {
return err return err
} }

View File

@@ -45,7 +45,9 @@ var (
keepCache bool keepCache bool
platform string platform string
hostname string hostname string
dns []string
dnsSearch []string
) )
func validateFlags() error { func validateFlags() error {
@@ -119,5 +121,7 @@ func buildFlags() *pflag.FlagSet {
flags.StringVar(&platform, "platform", d2vm.Arch, "Platform to use for the container disk image, linux/arm64 and linux/arm64 are supported") flags.StringVar(&platform, "platform", d2vm.Arch, "Platform to use for the container disk image, linux/arm64 and linux/arm64 are supported")
flags.BoolVar(&pull, "pull", false, "Always pull docker image") flags.BoolVar(&pull, "pull", false, "Always pull docker image")
flags.StringVar(&hostname, "hostname", "localhost", "Hostname to set in the generated image") flags.StringVar(&hostname, "hostname", "localhost", "Hostname to set in the generated image")
flags.StringSliceVar(&dns, "dns", []string{}, "DNS servers to set in the generated image")
flags.StringSliceVar(&dnsSearch, "dns-search", []string{}, "DNS search domains to set in the generated image")
return flags return flags
} }

View File

@@ -88,7 +88,7 @@ func Convert(ctx context.Context, img string, opts ...ConvertOption) error {
if format == "" { if format == "" {
format = "raw" format = "raw"
} }
b, err := NewBuilder(ctx, tmpPath, imgUUID, "", o.size, r, format, o.cmdLineExtra, o.splitBoot, o.bootFS, o.bootSize, o.luksPassword, o.bootLoader, o.platform, o.hostname) b, err := NewBuilder(ctx, tmpPath, imgUUID, "", o.size, r, format, o.cmdLineExtra, o.splitBoot, o.bootFS, o.bootSize, o.luksPassword, o.bootLoader, o.platform, o.hostname, o.dns, o.dnsSearch)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -35,7 +35,9 @@ type convertOptions struct {
platform string platform string
pull bool pull bool
hostname string hostname string
dns []string
dnsSearch []string
} }
func (o *convertOptions) hasGrubBIOS() bool { func (o *convertOptions) hasGrubBIOS() bool {
@@ -135,3 +137,15 @@ func WithHostname(hostname string) ConvertOption {
o.hostname = hostname o.hostname = hostname
} }
} }
func WithDNS(dns []string) ConvertOption {
return func(o *convertOptions) {
o.dns = dns
}
}
func WithDNSSearch(dnsSearch []string) ConvertOption {
return func(o *convertOptions) {
o.dnsSearch = dnsSearch
}
}

View File

@@ -14,6 +14,8 @@ d2vm build [context directory] [flags]
--boot-size uint Size of the boot partition in MB (default 100) --boot-size uint Size of the boot partition in MB (default 100)
--bootloader string Bootloader to use: syslinux, grub, grub-bios, grub-efi, defaults to syslinux on amd64 and grub-efi on arm64 --bootloader string Bootloader to use: syslinux, grub, grub-bios, grub-efi, defaults to syslinux on amd64 and grub-efi on arm64
--build-arg stringArray Set build-time variables --build-arg stringArray Set build-time variables
--dns strings DNS servers to set in the generated image
--dns-search strings DNS search domains to set in the generated image
-f, --file string Name of the Dockerfile -f, --file string Name of the Dockerfile
--force Override output qcow2 image --force Override output qcow2 image
-h, --help help for build -h, --help help for build

View File

@@ -13,6 +13,8 @@ d2vm convert [docker image] [flags]
--boot-fs string Filesystem to use for the boot partition, ext4 or fat32 --boot-fs string Filesystem to use for the boot partition, ext4 or fat32
--boot-size uint Size of the boot partition in MB (default 100) --boot-size uint Size of the boot partition in MB (default 100)
--bootloader string Bootloader to use: syslinux, grub, grub-bios, grub-efi, defaults to syslinux on amd64 and grub-efi on arm64 --bootloader string Bootloader to use: syslinux, grub, grub-bios, grub-efi, defaults to syslinux on amd64 and grub-efi on arm64
--dns strings DNS servers to set in the generated image
--dns-search strings DNS search domains to set in the generated image
--force Override output qcow2 image --force Override output qcow2 image
-h, --help help for convert -h, --help help for convert
--hostname string Hostname to set in the generated image (default "localhost") --hostname string Hostname to set in the generated image (default "localhost")