Skip to content

Commit 2d8d06a

Browse files
committed
feat: add --add-host flag to customize host-to-IP mapping in /etc/hosts
Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
1 parent 3279373 commit 2d8d06a

9 files changed

Lines changed: 44 additions & 4 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ Usage:
166166
167167
Flags:
168168
--append-to-cmdline string Extra kernel cmdline arguments to append to the generated one
169+
--add-host strings Add a custom host-to-IP mapping (host:ip) to the /etc/hosts file in the generated image
169170
--boot-fs string Filesystem to use for the boot partition, ext4 or fat32
170171
--boot-size uint Size of the boot partition in MB (default 100)
171172
--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:
327328
328329
Flags:
329330
--append-to-cmdline string Extra kernel cmdline arguments to append to the generated one
331+
--add-host strings Add a custom host-to-IP mapping (host:ip) to the /etc/hosts file in the generated image
330332
--boot-fs string Filesystem to use for the boot partition, ext4 or fat32
331333
--boot-size uint Size of the boot partition in MB (default 100)
332334
--bootloader string Bootloader to use: syslinux, grub, grub-bios, grub-efi, defaults to syslinux on amd64 and grub-efi on arm64

builder.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ type builder struct {
8787
hostname string
8888
dns []string
8989
dnsSearch []string
90+
hosts string
9091
}
9192

92-
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) {
93+
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) {
9394
var arch string
9495
switch platform {
9596
case "linux/amd64":
@@ -189,6 +190,10 @@ func NewBuilder(ctx context.Context, workdir, imgTag, disk string, size uint64,
189190
if len(dns) == 0 {
190191
dns = []string{"8.8.8.8"}
191192
}
193+
hosts := hosts
194+
for k, v := range extraHosts {
195+
hosts += fmt.Sprintf("%s %s\n", v, k)
196+
}
192197
b := &builder{
193198
osRelease: osRelease,
194199
config: config,
@@ -208,6 +213,7 @@ func NewBuilder(ctx context.Context, workdir, imgTag, disk string, size uint64,
208213
hostname: hostname,
209214
dns: dns,
210215
dnsSearch: dnsSearch,
216+
hosts: hosts,
211217
}
212218
if err := b.checkDependencies(); err != nil {
213219
return nil, err
@@ -428,7 +434,7 @@ func (b *builder) setupRootFS(ctx context.Context) (err error) {
428434
if err := b.chWriteFile("/etc/hostname", b.hostname+"\n", perm); err != nil {
429435
return err
430436
}
431-
if err := b.chWriteFileIfNotExist("/etc/hosts", hosts, perm); err != nil {
437+
if err := b.chWriteFile("/etc/hosts", b.hosts, perm); err != nil {
432438
return err
433439
}
434440
// TODO(adphi): is it the righ fix ?

cmd/d2vm/build.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ var (
113113
d2vm.WithHostname(hostname),
114114
d2vm.WithDNS(dns),
115115
d2vm.WithDNSSearch(dnsSearch),
116+
d2vm.WithExtraHosts(extraHosts),
116117
); err != nil {
117118
return err
118119
}

cmd/d2vm/convert.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ var (
9494
d2vm.WithHostname(hostname),
9595
d2vm.WithDNS(dns),
9696
d2vm.WithDNSSearch(dnsSearch),
97+
d2vm.WithExtraHosts(extraHosts),
9798
); err != nil {
9899
return err
99100
}

cmd/d2vm/flags.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ var (
4848
hostname string
4949
dns []string
5050
dnsSearch []string
51+
hosts []string
52+
53+
extraHosts map[string]string
5154
)
5255

53-
func validateFlags() error {
56+
func validateFlags() (err error) {
5457
switch platform {
5558
case "linux/amd64":
5659
if bootloader == "" {
@@ -98,6 +101,10 @@ func validateFlags() error {
98101
return fmt.Errorf("%s already exists", output)
99102
}
100103
}
104+
extraHosts, err = validateHosts(hosts...)
105+
if err != nil {
106+
return fmt.Errorf("invalid --add-host value: %w", err)
107+
}
101108
return nil
102109
}
103110

@@ -123,5 +130,19 @@ func buildFlags() *pflag.FlagSet {
123130
flags.StringVar(&hostname, "hostname", "localhost", "Hostname to set in the generated image")
124131
flags.StringSliceVar(&dns, "dns", []string{}, "DNS servers to set in the generated image")
125132
flags.StringSliceVar(&dnsSearch, "dns-search", []string{}, "DNS search domains to set in the generated image")
133+
flags.StringSliceVar(&hosts, "add-host", []string{}, "Add a custom host-to-IP mapping (host:ip) to the /etc/hosts file in the generated image")
126134
return flags
127135
}
136+
137+
func validateHosts(vals ...string) (map[string]string, error) {
138+
out := make(map[string]string)
139+
for _, val := range vals {
140+
// allow for IPv6 addresses in extra hosts by only splitting on first ":"
141+
k, v, ok := strings.Cut(val, ":")
142+
if !ok || k == "" {
143+
return nil, fmt.Errorf("bad format for add-host: %q", val)
144+
}
145+
out[k] = v
146+
}
147+
return out, nil
148+
}

convert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func Convert(ctx context.Context, img string, opts ...ConvertOption) error {
8888
if format == "" {
8989
format = "raw"
9090
}
91-
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)
91+
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)
9292
if err != nil {
9393
return err
9494
}

convert_options.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type convertOptions struct {
3838
hostname string
3939
dns []string
4040
dnsSearch []string
41+
hosts map[string]string
4142
}
4243

4344
func (o *convertOptions) hasGrubBIOS() bool {
@@ -149,3 +150,9 @@ func WithDNSSearch(dnsSearch []string) ConvertOption {
149150
o.dnsSearch = dnsSearch
150151
}
151152
}
153+
154+
func WithExtraHosts(hosts map[string]string) ConvertOption {
155+
return func(o *convertOptions) {
156+
o.hosts = hosts
157+
}
158+
}

docs/content/reference/d2vm_build.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ d2vm build [context directory] [flags]
99
### Options
1010

1111
```
12+
--add-host strings Add a custom host-to-IP mapping (host:ip) to the /etc/hosts file in the generated image
1213
--append-to-cmdline string Extra kernel cmdline arguments to append to the generated one
1314
--boot-fs string Filesystem to use for the boot partition, ext4 or fat32
1415
--boot-size uint Size of the boot partition in MB (default 100)

docs/content/reference/d2vm_convert.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ d2vm convert [docker image] [flags]
99
### Options
1010

1111
```
12+
--add-host strings Add a custom host-to-IP mapping (host:ip) to the /etc/hosts file in the generated image
1213
--append-to-cmdline string Extra kernel cmdline arguments to append to the generated one
1314
--boot-fs string Filesystem to use for the boot partition, ext4 or fat32
1415
--boot-size uint Size of the boot partition in MB (default 100)

0 commit comments

Comments
 (0)