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

feat: add --add-host flag to customize host-to-IP mapping in /etc/hosts

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
2026-01-21 17:42:48 +01:00
parent 0ecdf57a6a
commit 8a21a9cee8
9 changed files with 44 additions and 4 deletions

View File

@@ -166,6 +166,7 @@ Usage:
Flags: Flags:
--append-to-cmdline string Extra kernel cmdline arguments to append to the generated one --append-to-cmdline string Extra kernel cmdline arguments to append to the generated one
--add-host strings Add a custom host-to-IP mapping (host:ip) to the /etc/hosts file in the generated image
--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
@@ -327,6 +328,7 @@ Usage:
Flags: Flags:
--append-to-cmdline string Extra kernel cmdline arguments to append to the generated one --append-to-cmdline string Extra kernel cmdline arguments to append to the generated one
--add-host strings Add a custom host-to-IP mapping (host:ip) to the /etc/hosts file in the generated image
--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

View File

@@ -87,9 +87,10 @@ type builder struct {
hostname string hostname string
dns []string dns []string
dnsSearch []string dnsSearch []string
hosts 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, dns, dnsSearch []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, extraHosts map[string]string) (Builder, error) {
var arch string var arch string
switch platform { switch platform {
case "linux/amd64": case "linux/amd64":
@@ -189,6 +190,10 @@ func NewBuilder(ctx context.Context, workdir, imgTag, disk string, size uint64,
if len(dns) == 0 { if len(dns) == 0 {
dns = []string{"8.8.8.8"} dns = []string{"8.8.8.8"}
} }
hosts := hosts
for k, v := range extraHosts {
hosts += fmt.Sprintf("%s %s\n", v, k)
}
b := &builder{ b := &builder{
osRelease: osRelease, osRelease: osRelease,
config: config, config: config,
@@ -208,6 +213,7 @@ func NewBuilder(ctx context.Context, workdir, imgTag, disk string, size uint64,
hostname: hostname, hostname: hostname,
dns: dns, dns: dns,
dnsSearch: dnsSearch, dnsSearch: dnsSearch,
hosts: hosts,
} }
if err := b.checkDependencies(); err != nil { if err := b.checkDependencies(); err != nil {
return nil, err return nil, err
@@ -428,7 +434,7 @@ func (b *builder) setupRootFS(ctx context.Context) (err error) {
if err := b.chWriteFile("/etc/hostname", b.hostname+"\n", perm); err != nil { if err := b.chWriteFile("/etc/hostname", b.hostname+"\n", perm); err != nil {
return err return err
} }
if err := b.chWriteFileIfNotExist("/etc/hosts", hosts, perm); err != nil { if err := b.chWriteFile("/etc/hosts", b.hosts, perm); err != nil {
return err return err
} }
// TODO(adphi): is it the righ fix ? // TODO(adphi): is it the righ fix ?

View File

@@ -113,6 +113,7 @@ var (
d2vm.WithHostname(hostname), d2vm.WithHostname(hostname),
d2vm.WithDNS(dns), d2vm.WithDNS(dns),
d2vm.WithDNSSearch(dnsSearch), d2vm.WithDNSSearch(dnsSearch),
d2vm.WithExtraHosts(extraHosts),
); err != nil { ); err != nil {
return err return err
} }

View File

@@ -94,6 +94,7 @@ var (
d2vm.WithHostname(hostname), d2vm.WithHostname(hostname),
d2vm.WithDNS(dns), d2vm.WithDNS(dns),
d2vm.WithDNSSearch(dnsSearch), d2vm.WithDNSSearch(dnsSearch),
d2vm.WithExtraHosts(extraHosts),
); err != nil { ); err != nil {
return err return err
} }

View File

@@ -48,9 +48,12 @@ var (
hostname string hostname string
dns []string dns []string
dnsSearch []string dnsSearch []string
hosts []string
extraHosts map[string]string
) )
func validateFlags() error { func validateFlags() (err error) {
switch platform { switch platform {
case "linux/amd64": case "linux/amd64":
if bootloader == "" { if bootloader == "" {
@@ -98,6 +101,10 @@ func validateFlags() error {
return fmt.Errorf("%s already exists", output) return fmt.Errorf("%s already exists", output)
} }
} }
extraHosts, err = validateHosts(hosts...)
if err != nil {
return fmt.Errorf("invalid --add-host value: %w", err)
}
return nil return nil
} }
@@ -123,5 +130,19 @@ func buildFlags() *pflag.FlagSet {
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(&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") flags.StringSliceVar(&dnsSearch, "dns-search", []string{}, "DNS search domains to set in the generated image")
flags.StringSliceVar(&hosts, "add-host", []string{}, "Add a custom host-to-IP mapping (host:ip) to the /etc/hosts file in the generated image")
return flags return flags
} }
func validateHosts(vals ...string) (map[string]string, error) {
out := make(map[string]string)
for _, val := range vals {
// allow for IPv6 addresses in extra hosts by only splitting on first ":"
k, v, ok := strings.Cut(val, ":")
if !ok || k == "" {
return nil, fmt.Errorf("bad format for add-host: %q", val)
}
out[k] = v
}
return out, nil
}

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, o.dns, o.dnsSearch) 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, o.hosts)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -38,6 +38,7 @@ type convertOptions struct {
hostname string hostname string
dns []string dns []string
dnsSearch []string dnsSearch []string
hosts map[string]string
} }
func (o *convertOptions) hasGrubBIOS() bool { func (o *convertOptions) hasGrubBIOS() bool {
@@ -149,3 +150,9 @@ func WithDNSSearch(dnsSearch []string) ConvertOption {
o.dnsSearch = dnsSearch o.dnsSearch = dnsSearch
} }
} }
func WithExtraHosts(hosts map[string]string) ConvertOption {
return func(o *convertOptions) {
o.hosts = hosts
}
}

View File

@@ -9,6 +9,7 @@ d2vm build [context directory] [flags]
### Options ### Options
``` ```
--add-host strings Add a custom host-to-IP mapping (host:ip) to the /etc/hosts file in the generated image
--append-to-cmdline string Extra kernel cmdline arguments to append to the generated one --append-to-cmdline string Extra kernel cmdline arguments to append to the generated one
--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)

View File

@@ -9,6 +9,7 @@ d2vm convert [docker image] [flags]
### Options ### Options
``` ```
--add-host strings Add a custom host-to-IP mapping (host:ip) to the /etc/hosts file in the generated image
--append-to-cmdline string Extra kernel cmdline arguments to append to the generated one --append-to-cmdline string Extra kernel cmdline arguments to append to the generated one
--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)