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

feat: add --hostname flag to customize the vm hostname

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
2026-01-21 17:11:38 +01:00
parent 75a9f8d9a9
commit f799dc4891
9 changed files with 26 additions and 3 deletions

View File

@@ -171,6 +171,7 @@ Flags:
--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
--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")
--keep-cache Keep the images after the build --keep-cache Keep the images after the build
--luks-password string Password to use for the LUKS encrypted root partition. If not set, the root partition will not be encrypted --luks-password string Password to use for the LUKS encrypted root partition. If not set, the root partition will not be encrypted
--network-manager string Network manager to use for the image: none, netplan, ifupdown --network-manager string Network manager to use for the image: none, netplan, ifupdown
@@ -331,6 +332,7 @@ Flags:
-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
--hostname string Hostname to set in the generated image (default "localhost")
--keep-cache Keep the images after the build --keep-cache Keep the images after the build
--luks-password string Password to use for the LUKS encrypted root partition. If not set, the root partition will not be encrypted --luks-password string Password to use for the LUKS encrypted root partition. If not set, the root partition will not be encrypted
--network-manager string Network manager to use for the image: none, netplan, ifupdown --network-manager string Network manager to use for the image: none, netplan, ifupdown

View File

@@ -83,9 +83,11 @@ type builder struct {
cmdLineExtra string cmdLineExtra string
arch string arch string
hostname 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 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) (Builder, error) {
var arch string var arch string
switch platform { switch platform {
case "linux/amd64": case "linux/amd64":
@@ -179,6 +181,9 @@ func NewBuilder(ctx context.Context, workdir, imgTag, disk string, size uint64,
// logrus.Warnf("%s is smaller than rootfs size, using %s", datasize.ByteSize(size), s) // logrus.Warnf("%s is smaller than rootfs size, using %s", datasize.ByteSize(size), s)
// size = int64(s) // size = int64(s)
// } // }
if hostname == "" {
hostname = "localhost"
}
b := &builder{ b := &builder{
osRelease: osRelease, osRelease: osRelease,
config: config, config: config,
@@ -195,6 +200,7 @@ func NewBuilder(ctx context.Context, workdir, imgTag, disk string, size uint64,
bootFS: bootFS, bootFS: bootFS,
luksPassword: luksPassword, luksPassword: luksPassword,
arch: arch, arch: arch,
hostname: hostname,
} }
if err := b.checkDependencies(); err != nil { if err := b.checkDependencies(); err != nil {
return nil, err return nil, err
@@ -412,7 +418,7 @@ func (b *builder) setupRootFS(ctx context.Context) (err error) {
if err := b.chWriteFileIfNotExist("/etc/resolv.conf", "nameserver 8.8.8.8", 0644); err != nil { if err := b.chWriteFileIfNotExist("/etc/resolv.conf", "nameserver 8.8.8.8", 0644); err != nil {
return err return err
} }
if err := b.chWriteFileIfNotExist("/etc/hostname", "localhost", 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.chWriteFileIfNotExist("/etc/hosts", hosts, perm); err != nil {

View File

@@ -110,6 +110,7 @@ var (
d2vm.WithKeepCache(keepCache), d2vm.WithKeepCache(keepCache),
d2vm.WithPlatform(platform), d2vm.WithPlatform(platform),
d2vm.WithPull(false), d2vm.WithPull(false),
d2vm.WithHostname(hostname),
); err != nil { ); err != nil {
return err return err
} }

View File

@@ -91,6 +91,7 @@ var (
d2vm.WithKeepCache(keepCache), d2vm.WithKeepCache(keepCache),
d2vm.WithPlatform(platform), d2vm.WithPlatform(platform),
d2vm.WithPull(pull), d2vm.WithPull(pull),
d2vm.WithHostname(hostname),
); err != nil { ); err != nil {
return err return err
} }

View File

@@ -44,6 +44,8 @@ var (
keepCache bool keepCache bool
platform string platform string
hostname string
) )
func validateFlags() error { func validateFlags() error {
@@ -116,5 +118,6 @@ func buildFlags() *pflag.FlagSet {
flags.BoolVar(&keepCache, "keep-cache", false, "Keep the images after the build") flags.BoolVar(&keepCache, "keep-cache", false, "Keep the images after the build")
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")
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) 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)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -34,6 +34,8 @@ type convertOptions struct {
keepCache bool keepCache bool
platform string platform string
pull bool pull bool
hostname string
} }
func (o *convertOptions) hasGrubBIOS() bool { func (o *convertOptions) hasGrubBIOS() bool {
@@ -127,3 +129,9 @@ func WithPull(b bool) ConvertOption {
o.pull = b o.pull = b
} }
} }
func WithHostname(hostname string) ConvertOption {
return func(o *convertOptions) {
o.hostname = hostname
}
}

View File

@@ -17,6 +17,7 @@ d2vm build [context directory] [flags]
-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
--hostname string Hostname to set in the generated image (default "localhost")
--keep-cache Keep the images after the build --keep-cache Keep the images after the build
--luks-password string Password to use for the LUKS encrypted root partition. If not set, the root partition will not be encrypted --luks-password string Password to use for the LUKS encrypted root partition. If not set, the root partition will not be encrypted
--network-manager string Network manager to use for the image: none, netplan, ifupdown --network-manager string Network manager to use for the image: none, netplan, ifupdown

View File

@@ -15,6 +15,7 @@ d2vm convert [docker image] [flags]
--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
--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")
--keep-cache Keep the images after the build --keep-cache Keep the images after the build
--luks-password string Password to use for the LUKS encrypted root partition. If not set, the root partition will not be encrypted --luks-password string Password to use for the LUKS encrypted root partition. If not set, the root partition will not be encrypted
--network-manager string Network manager to use for the image: none, netplan, ifupdown --network-manager string Network manager to use for the image: none, netplan, ifupdown