From f83aa9bf74c79ba5d45b47fab51b8416634ffd13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Mon, 18 Aug 2025 22:28:20 +0200 Subject: [PATCH 1/2] Remove VMType check from the lima-init boot script MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Do this when setting up the cidata instead, next to the decision on whether to use "cidata" dir or "cidata.iso". This means that this particular boot script is not depending on the driver, but can be used from all container drivers. Signed-off-by: Anders F Björklund --- pkg/cidata/cidata.TEMPLATE.d/lima.env | 5 +++++ pkg/cidata/cidata.go | 20 +++++++++++++------- pkg/cidata/template.go | 1 + pkg/driver/wsl2/boot/02-wsl2-setup.sh | 4 ++-- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/pkg/cidata/cidata.TEMPLATE.d/lima.env b/pkg/cidata/cidata.TEMPLATE.d/lima.env index e08129b5a22..8cd9453ccdc 100644 --- a/pkg/cidata/cidata.TEMPLATE.d/lima.env +++ b/pkg/cidata/cidata.TEMPLATE.d/lima.env @@ -65,3 +65,8 @@ LIMA_CIDATA_PLAIN=1 {{- else}} LIMA_CIDATA_PLAIN= {{- end}} +{{- if .NoCloudInit}} +LIMA_CIDATA_NO_CLOUD_INIT=1 +{{- else}} +LIMA_CIDATA_NO_CLOUD_INIT= +{{- end}} diff --git a/pkg/cidata/cidata.go b/pkg/cidata/cidata.go index 4a38c0c2fbf..b6f39223a9e 100644 --- a/pkg/cidata/cidata.go +++ b/pkg/cidata/cidata.go @@ -118,6 +118,11 @@ func setupEnv(instConfigEnv map[string]string, propagateProxyEnv bool, slirpGate return env, nil } +func useCloudInit(instConfig *limayaml.LimaYAML) bool { + // all drivers but WSL2 use cloud-init + return *instConfig.VMType != limayaml.WSL2 +} + func templateArgs(ctx context.Context, bootScripts bool, instDir, name string, instConfig *limatype.LimaYAML, udpDNSLocalPort, tcpDNSLocalPort, vsockPort int, virtioPort string) (*TemplateArgs, error) { if err := limayaml.Validate(instConfig, false); err != nil { return nil, err @@ -138,12 +143,13 @@ func templateArgs(ctx context.Context, bootScripts bool, instDir, name string, i Containerd: Containerd{System: *instConfig.Containerd.System, User: *instConfig.Containerd.User, Archive: archive}, SlirpNICName: networks.SlirpNICName, - VMType: *instConfig.VMType, - VSockPort: vsockPort, - VirtioPort: virtioPort, - Plain: *instConfig.Plain, - TimeZone: *instConfig.TimeZone, - Param: instConfig.Param, + VMType: *instConfig.VMType, + VSockPort: vsockPort, + VirtioPort: virtioPort, + Plain: *instConfig.Plain, + TimeZone: *instConfig.TimeZone, + NoCloudInit: !useCloudInit(instConfig), + Param: instConfig.Param, } if instConfig.VMOpts.VZ.Rosetta.Enabled != nil { @@ -466,7 +472,7 @@ func GenerateISO9660(ctx context.Context, drv driver.Driver, instDir, name strin }) } - if args.VMType == limatype.WSL2 { + if !useCloudInit(instConfig) { layout = append(layout, iso9660util.Entry{ Path: "ssh_authorized_keys", Reader: strings.NewReader(strings.Join(args.SSHPubKeys, "\n")), diff --git a/pkg/cidata/template.go b/pkg/cidata/template.go index 3d886e7a5e6..84dfafce86d 100644 --- a/pkg/cidata/template.go +++ b/pkg/cidata/template.go @@ -114,6 +114,7 @@ type TemplateArgs struct { VirtioPort string Plain bool TimeZone string + NoCloudInit bool } func ValidateTemplateArgs(args *TemplateArgs) error { diff --git a/pkg/driver/wsl2/boot/02-wsl2-setup.sh b/pkg/driver/wsl2/boot/02-wsl2-setup.sh index cb5639657d3..0a4d86f42d1 100755 --- a/pkg/driver/wsl2/boot/02-wsl2-setup.sh +++ b/pkg/driver/wsl2/boot/02-wsl2-setup.sh @@ -4,8 +4,8 @@ # SPDX-License-Identifier: Apache-2.0 # This script replaces the cloud-init functionality of creating a user and setting its SSH keys -# when using a WSL2 VM. -[ "$LIMA_CIDATA_VMTYPE" = "wsl2" ] || exit 0 +# when cloud-init is not available +[ "$LIMA_CIDATA_NO_CLOUD_INIT" = "1" ] || exit 0 # create user # shellcheck disable=SC2153 From 78f6afcbc3be27c83a4fd0253a90ed39f529e2ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Sat, 13 Sep 2025 14:03:12 +0200 Subject: [PATCH 2/2] Add feature for CloudInit instead of VMType name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of hardcoding which drivers do not use cloud-init, make it into a driver feature. Currently, that is only WSL2. The container drivers do mount a cidata.iso, instead they mount a cidata dir. Then they include a boot script for it. Signed-off-by: Anders F Björklund --- pkg/cidata/cidata.go | 17 ++++++----------- pkg/driver/driver.go | 1 + ...-wsl2-setup.sh => 02-no-cloud-init-setup.sh} | 2 +- pkg/driver/wsl2/wsl_driver_windows.go | 1 + pkg/hostagent/hostagent.go | 3 ++- 5 files changed, 11 insertions(+), 13 deletions(-) rename pkg/driver/wsl2/boot/{02-wsl2-setup.sh => 02-no-cloud-init-setup.sh} (92%) diff --git a/pkg/cidata/cidata.go b/pkg/cidata/cidata.go index b6f39223a9e..ad024acba14 100644 --- a/pkg/cidata/cidata.go +++ b/pkg/cidata/cidata.go @@ -118,12 +118,7 @@ func setupEnv(instConfigEnv map[string]string, propagateProxyEnv bool, slirpGate return env, nil } -func useCloudInit(instConfig *limayaml.LimaYAML) bool { - // all drivers but WSL2 use cloud-init - return *instConfig.VMType != limayaml.WSL2 -} - -func templateArgs(ctx context.Context, bootScripts bool, instDir, name string, instConfig *limatype.LimaYAML, udpDNSLocalPort, tcpDNSLocalPort, vsockPort int, virtioPort string) (*TemplateArgs, error) { +func templateArgs(ctx context.Context, bootScripts bool, instDir, name string, instConfig *limatype.LimaYAML, udpDNSLocalPort, tcpDNSLocalPort, vsockPort int, virtioPort string, noCloudInit bool) (*TemplateArgs, error) { if err := limayaml.Validate(instConfig, false); err != nil { return nil, err } @@ -148,7 +143,7 @@ func templateArgs(ctx context.Context, bootScripts bool, instDir, name string, i VirtioPort: virtioPort, Plain: *instConfig.Plain, TimeZone: *instConfig.TimeZone, - NoCloudInit: !useCloudInit(instConfig), + NoCloudInit: noCloudInit, Param: instConfig.Param, } @@ -356,7 +351,7 @@ func templateArgs(ctx context.Context, bootScripts bool, instDir, name string, i } func GenerateCloudConfig(ctx context.Context, instDir, name string, instConfig *limatype.LimaYAML) error { - args, err := templateArgs(ctx, false, instDir, name, instConfig, 0, 0, 0, "") + args, err := templateArgs(ctx, false, instDir, name, instConfig, 0, 0, 0, "", false) if err != nil { return err } @@ -378,8 +373,8 @@ func GenerateCloudConfig(ctx context.Context, instDir, name string, instConfig * return os.WriteFile(filepath.Join(instDir, filenames.CloudConfig), config, 0o444) } -func GenerateISO9660(ctx context.Context, drv driver.Driver, instDir, name string, instConfig *limatype.LimaYAML, udpDNSLocalPort, tcpDNSLocalPort int, guestAgentBinary, nerdctlArchive string, vsockPort int, virtioPort string) error { - args, err := templateArgs(ctx, true, instDir, name, instConfig, udpDNSLocalPort, tcpDNSLocalPort, vsockPort, virtioPort) +func GenerateISO9660(ctx context.Context, drv driver.Driver, instDir, name string, instConfig *limatype.LimaYAML, udpDNSLocalPort, tcpDNSLocalPort int, guestAgentBinary, nerdctlArchive string, vsockPort int, virtioPort string, noCloudInit bool) error { + args, err := templateArgs(ctx, true, instDir, name, instConfig, udpDNSLocalPort, tcpDNSLocalPort, vsockPort, virtioPort, noCloudInit) if err != nil { return err } @@ -472,7 +467,7 @@ func GenerateISO9660(ctx context.Context, drv driver.Driver, instDir, name strin }) } - if !useCloudInit(instConfig) { + if noCloudInit { layout = append(layout, iso9660util.Entry{ Path: "ssh_authorized_keys", Reader: strings.NewReader(strings.Join(args.SSHPubKeys, "\n")), diff --git a/pkg/driver/driver.go b/pkg/driver/driver.go index 94c192f0cd4..fc3a1afd653 100644 --- a/pkg/driver/driver.go +++ b/pkg/driver/driver.go @@ -106,4 +106,5 @@ type DriverFeatures struct { CanRunGUI bool `json:"canRunGui,omitempty"` DynamicSSHAddress bool `json:"dynamicSSHAddress"` SkipSocketForwarding bool `json:"skipSocketForwarding"` + NoCloudInit bool `json:"noCloudInit"` } diff --git a/pkg/driver/wsl2/boot/02-wsl2-setup.sh b/pkg/driver/wsl2/boot/02-no-cloud-init-setup.sh similarity index 92% rename from pkg/driver/wsl2/boot/02-wsl2-setup.sh rename to pkg/driver/wsl2/boot/02-no-cloud-init-setup.sh index 0a4d86f42d1..ebfe351cd3e 100755 --- a/pkg/driver/wsl2/boot/02-wsl2-setup.sh +++ b/pkg/driver/wsl2/boot/02-no-cloud-init-setup.sh @@ -22,4 +22,4 @@ chmod 600 "${LIMA_CIDATA_HOME}"/.ssh/authorized_keys echo "${LIMA_CIDATA_USER} ALL=(ALL) NOPASSWD:ALL" | tee -a /etc/sudoers.d/99_lima_sudoers # symlink CIDATA to the hardcoded path for requirement checks (TODO: make this not hardcoded) -ln -sfFn "${LIMA_CIDATA_MNT}" /mnt/lima-cidata +[ "$LIMA_CIDATA_MNT" = "/mnt/lima-cidata" ] || ln -sfFn "${LIMA_CIDATA_MNT}" /mnt/lima-cidata diff --git a/pkg/driver/wsl2/wsl_driver_windows.go b/pkg/driver/wsl2/wsl_driver_windows.go index 17886de8863..ae8252b1639 100644 --- a/pkg/driver/wsl2/wsl_driver_windows.go +++ b/pkg/driver/wsl2/wsl_driver_windows.go @@ -310,6 +310,7 @@ func (l *LimaWslDriver) Info() driver.Info { info.Features = driver.DriverFeatures{ DynamicSSHAddress: true, SkipSocketForwarding: true, + NoCloudInit: true, CanRunGUI: l.canRunGUI(), } return info diff --git a/pkg/hostagent/hostagent.go b/pkg/hostagent/hostagent.go index 3affdf6b0c1..41738e2d28e 100644 --- a/pkg/hostagent/hostagent.go +++ b/pkg/hostagent/hostagent.go @@ -162,11 +162,12 @@ func New(ctx context.Context, instName string, stdout io.Writer, signalCh chan o vSockPort := limaDriver.Info().VsockPort virtioPort := limaDriver.Info().VirtioPort + noCloudInit := limaDriver.Info().Features.NoCloudInit if err := cidata.GenerateCloudConfig(ctx, inst.Dir, instName, inst.Config); err != nil { return nil, err } - if err := cidata.GenerateISO9660(ctx, limaDriver, inst.Dir, instName, inst.Config, udpDNSLocalPort, tcpDNSLocalPort, o.guestAgentBinary, o.nerdctlArchive, vSockPort, virtioPort); err != nil { + if err := cidata.GenerateISO9660(ctx, limaDriver, inst.Dir, instName, inst.Config, udpDNSLocalPort, tcpDNSLocalPort, o.guestAgentBinary, o.nerdctlArchive, vSockPort, virtioPort, noCloudInit); err != nil { return nil, err }