Create smaller VM qcow2 disk images#7
Conversation
This lets us potentially drop python dependencies once we get rid of cloud-init. Signed-off-by: Mark Yen <mark.yen@suse.com>
Switch to kernel-default-base to avoid pulling in kernel modules that will never be used in a VM. Signed-off-by: Mark Yen <mark.yen@suse.com>
This drops cloud-init and switches to a minimal golang implementation that is just enough to read what lima outputs. This lets us drop all of python in the image. We also switch to systemd-networkd instead of NetworkManager because it is slightly easier to configure in this scenario. Signed-off-by: Mark Yen <mark.yen@suse.com>
src/rd-init/main.go
Outdated
| if err != nil { | ||
| return fmt.Errorf("failed to start unit %s: %w", unit, err) | ||
| } | ||
| slog.InfoContext(ctx, "restarted systemd unit", "unit", unit, "result", <-ch) |
There was a problem hiding this comment.
You might want to consider adding context cancellation statement
select {
case result := <-ch:
slog.InfoContext(ctx, "restarted systemd unit", "unit", unit, "result", result)
case <-ctx.Done():
return fmt.Errorf(...)
}
src/rd-init/main.go
Outdated
| // Notify ready before we reload the other units; otherwise we end up | ||
| // blocking startup due to a loop with systemd-networkd. | ||
| if _, err := daemon.SdNotify(true, daemon.SdNotifyReady); err != nil { | ||
| return err |
There was a problem hiding this comment.
Should this one return fmt.Errorf("failed to notify systemd: %w", err)
There was a problem hiding this comment.
Yes, that makes sense; changed.
| ) | ||
|
|
||
| // Load /mnt/lima-cidata/meta-data | ||
| func LoadMetadata(ctx context.Context) ([]string, error) { |
There was a problem hiding this comment.
I can only assume that we are forced to adhere to function signature that returns ([]string, error) because []string is not used at all.
There was a problem hiding this comment.
Yeah, it's to make it look like the other functions:
| return nil, fmt.Errorf("failed to unmarshal meta-data file: %w", err) | ||
| } | ||
| slog.InfoContext(ctx, "setting host name", "hostname", metaData.LocalHostName) | ||
| if err := unix.Sethostname([]byte(metaData.LocalHostName)); err != nil { |
There was a problem hiding this comment.
Is the hostname already validated?
There was a problem hiding this comment.
It doesn't make much sense for us to validate it (more than would otherwise be done); we don't have any specific requirements other than whatever the kernel supports. This is generally generated by Lima anyway, so we don't actually care.
Note that since this is golang, we don't use the native libc and just use the syscall directly; this means we end up calling the implementation, which doesn't appear to do much checking.
src/rd-init/metadata.go
Outdated
| LocalHostName string `yaml:"local-hostname"` | ||
| } | ||
|
|
||
| file, err := os.Open("/mnt/lima-cidata/meta-data") |
There was a problem hiding this comment.
This /mnt/lima-cidata/meta-data might be clearer as a constant.
src/rd-init/network.go
Outdated
|
|
||
| func LoadNetworkConfig(ctx context.Context) ([]string, error) { | ||
| hasChanges := false | ||
| file, err := os.Open("/mnt/lima-cidata/network-config") |
src/rd-init/userdata.go
Outdated
| err := os.WriteFile( | ||
| fmt.Sprintf("/etc/sudoers.d/90-lima-user-%s", userEntry.Name), | ||
| []byte(userEntry.Name + " " + userEntry.Sudo), | ||
| 0o644) |
There was a problem hiding this comment.
Should this be 0o440 or 0o400 read only by root/group?
There was a problem hiding this comment.
The monitoring-plugins-zypper package contains a /etc/sudoers.d/check_zypper that's 0400, I'll follow that.
Signed-off-by: Mark Yen <mark.yen@suse.com>
src/rd-init/main.go
Outdated
| if err != nil { | ||
| return fmt.Errorf("failed to start unit %s: %w", unit, err) | ||
| } | ||
| slog.InfoContext(ctx, "restarted systemd unit", "unit", unit, "result", <-ch) |
src/rd-init/main.go
Outdated
| // Notify ready before we reload the other units; otherwise we end up | ||
| // blocking startup due to a loop with systemd-networkd. | ||
| if _, err := daemon.SdNotify(true, daemon.SdNotifyReady); err != nil { | ||
| return err |
There was a problem hiding this comment.
Yes, that makes sense; changed.
| ) | ||
|
|
||
| // Load /mnt/lima-cidata/meta-data | ||
| func LoadMetadata(ctx context.Context) ([]string, error) { |
There was a problem hiding this comment.
Yeah, it's to make it look like the other functions:
src/rd-init/metadata.go
Outdated
| LocalHostName string `yaml:"local-hostname"` | ||
| } | ||
|
|
||
| file, err := os.Open("/mnt/lima-cidata/meta-data") |
| return nil, fmt.Errorf("failed to unmarshal meta-data file: %w", err) | ||
| } | ||
| slog.InfoContext(ctx, "setting host name", "hostname", metaData.LocalHostName) | ||
| if err := unix.Sethostname([]byte(metaData.LocalHostName)); err != nil { |
There was a problem hiding this comment.
It doesn't make much sense for us to validate it (more than would otherwise be done); we don't have any specific requirements other than whatever the kernel supports. This is generally generated by Lima anyway, so we don't actually care.
Note that since this is golang, we don't use the native libc and just use the syscall directly; this means we end up calling the implementation, which doesn't appear to do much checking.
src/rd-init/network.go
Outdated
|
|
||
| func LoadNetworkConfig(ctx context.Context) ([]string, error) { | ||
| hasChanges := false | ||
| file, err := os.Open("/mnt/lima-cidata/network-config") |
src/rd-init/userdata.go
Outdated
| err := os.WriteFile( | ||
| fmt.Sprintf("/etc/sudoers.d/90-lima-user-%s", userEntry.Name), | ||
| []byte(userEntry.Name + " " + userEntry.Sudo), | ||
| 0o644) |
There was a problem hiding this comment.
The monitoring-plugins-zypper package contains a /etc/sudoers.d/check_zypper that's 0400, I'll follow that.
This reduces the disk image size by replacing some things with in-house equivalents that only does what we need:
The combination of the first and third allows us to drop python from the image. But I'm not sure doing a full replacement of cloud-init is a good thing to do here.
FWIW, looking at the
taroutput (because that's easier),/usr/local/binhas:So that amount isn't going away, though we may need to figure out how to slim down
rancher-desktop-guest-agent. We probably have too many copies of the Kubernetes client around.