Skip to content

Commit 0696d1d

Browse files
committed
guestagent: Avoid restarting in the boot script if the executable and config have no changes.
Signed-off-by: Norio Nomura <[email protected]>
1 parent 1152d87 commit 0696d1d

File tree

2 files changed

+79
-34
lines changed

2 files changed

+79
-34
lines changed

cmd/lima-guestagent/install_systemd_linux.go

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
package main
55

66
import (
7+
"bytes"
78
_ "embed"
89
"errors"
910
"fmt"
1011
"os"
1112
"os/exec"
1213
"path/filepath"
14+
"slices"
1315
"strings"
1416

1517
"github.com/sirupsen/logrus"
@@ -24,13 +26,18 @@ func newInstallSystemdCommand() *cobra.Command {
2426
Short: "Install a systemd unit (user)",
2527
RunE: installSystemdAction,
2628
}
29+
installSystemdCommand.Flags().Bool("guestagent-updated", false, "Indicate that the guest agent has been updated")
2730
installSystemdCommand.Flags().Int("vsock-port", 0, "Use vsock server on specified port")
2831
installSystemdCommand.Flags().String("virtio-port", "", "Use virtio server instead a UNIX socket")
2932
return installSystemdCommand
3033
}
3134

3235
func installSystemdAction(cmd *cobra.Command, _ []string) error {
3336
ctx := cmd.Context()
37+
guestAgentUpdated, err := cmd.Flags().GetBool("guestagent-updated")
38+
if err != nil {
39+
return err
40+
}
3441
vsockPort, err := cmd.Flags().GetInt("vsock-port")
3542
if err != nil {
3643
return err
@@ -48,24 +55,42 @@ func installSystemdAction(cmd *cobra.Command, _ []string) error {
4855
return err
4956
}
5057
unitPath := "/etc/systemd/system/lima-guestagent.service"
58+
unitFileChanged := true
5159
if _, err := os.Stat(unitPath); !errors.Is(err, os.ErrNotExist) {
52-
logrus.Infof("File %q already exists, overwriting", unitPath)
60+
if existingUnit, err := os.ReadFile(unitPath); err == nil && bytes.Equal(unit, existingUnit) {
61+
logrus.Infof("File %q is up-to-date", unitPath)
62+
unitFileChanged = false
63+
} else {
64+
logrus.Infof("File %q needs update", unitPath)
65+
}
5366
} else {
5467
unitDir := filepath.Dir(unitPath)
5568
if err := os.MkdirAll(unitDir, 0o755); err != nil {
5669
return err
5770
}
5871
}
59-
if err := os.WriteFile(unitPath, unit, 0o644); err != nil {
60-
return err
72+
if unitFileChanged {
73+
if err := os.WriteFile(unitPath, unit, 0o644); err != nil {
74+
return err
75+
}
76+
logrus.Infof("Written file %q", unitPath)
77+
} else if !guestAgentUpdated {
78+
logrus.Info("lima-guestagent.service already up-to-date")
79+
return nil
6180
}
62-
logrus.Infof("Written file %q", unitPath)
63-
args := [][]string{
64-
{"daemon-reload"},
65-
{"enable", "lima-guestagent.service"},
66-
{"start", "lima-guestagent.service"},
67-
{"try-restart", "lima-guestagent.service"},
81+
// unitFileChanged || guestAgentUpdated
82+
args := make([][]string, 0, 4)
83+
if unitFileChanged {
84+
args = append(args, []string{"daemon-reload"})
6885
}
86+
args = slices.Concat(
87+
args,
88+
[][]string{
89+
{"enable", "lima-guestagent.service"},
90+
{"try-restart", "lima-guestagent.service"}, // try-restart: restart if running, otherwise do nothing
91+
{"start", "lima-guestagent.service"}, // start: start if not running, otherwise do nothing
92+
},
93+
)
6994
for _, args := range args {
7095
cmd := exec.CommandContext(ctx, "systemctl", append([]string{"--system"}, args...)...)
7196
cmd.Stdout = os.Stdout
Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/bin/bash
22

33
# SPDX-FileCopyrightText: Copyright The Lima Authors
44
# SPDX-License-Identifier: Apache-2.0
@@ -19,46 +19,66 @@ fi
1919

2020
# Install or update the guestagent binary
2121
mkdir -p "${LIMA_CIDATA_GUEST_INSTALL_PREFIX}"/bin
22-
install -m 755 "${LIMA_CIDATA_MNT}"/lima-guestagent "${LIMA_CIDATA_GUEST_INSTALL_PREFIX}"/bin/lima-guestagent
22+
guestagent_updated=false
23+
if diff -q "${LIMA_CIDATA_MNT}"/lima-guestagent "${LIMA_CIDATA_GUEST_INSTALL_PREFIX}"/bin/lima-guestagent 2>/dev/null; then
24+
echo "${LIMA_CIDATA_GUEST_INSTALL_PREFIX}/bin/lima-guestagent is up-to-date"
25+
else
26+
install -m 755 "${LIMA_CIDATA_MNT}"/lima-guestagent "${LIMA_CIDATA_GUEST_INSTALL_PREFIX}"/bin/lima-guestagent
27+
guestagent_updated=true
28+
fi
2329

2430
# Launch the guestagent service
2531
if [ -f /sbin/openrc-run ]; then
26-
# Convert .env to conf.d by wrapping values in double quotes.
27-
# Split the variable and value at the first "=" to handle cases where the value contains additional "=" characters.
28-
sed -E 's/^([^=]+)=(.*)/\1="\2"/' "${LIMA_CIDATA_MNT}/lima.env" >"/etc/conf.d/lima-guestagent"
29-
# Install the openrc lima-guestagent service script
30-
cat >/etc/init.d/lima-guestagent <<'EOF'
31-
#!/sbin/openrc-run
32-
supervisor=supervise-daemon
32+
print_config() {
33+
# Convert .env to conf.d by wrapping values in double quotes.
34+
# Split the variable and value at the first "=" to handle cases where the value contains additional "=" characters.
35+
sed -E 's/^([^=]+)=(.*)/\1="\2"/' "${LIMA_CIDATA_MNT}/lima.env"
36+
}
37+
print_script() {
38+
# the openrc lima-guestagent service script
39+
cat <<-'EOF'
40+
#!/sbin/openrc-run
41+
supervisor=supervise-daemon
3342
34-
log_file="${log_file:-/var/log/${RC_SVCNAME}.log}"
35-
err_file="${err_file:-${log_file}}"
36-
log_mode="${log_mode:-0644}"
37-
log_owner="${log_owner:-root:root}"
43+
log_file="${log_file:-/var/log/${RC_SVCNAME}.log}"
44+
err_file="${err_file:-${log_file}}"
45+
log_mode="${log_mode:-0644}"
46+
log_owner="${log_owner:-root:root}"
3847
39-
supervise_daemon_args="${supervise_daemon_opts:---stderr \"${err_file}\" --stdout \"${log_file}\"}"
48+
supervise_daemon_args="${supervise_daemon_opts:---stderr \"${err_file}\" --stdout \"${log_file}\"}"
4049
41-
name="lima-guestagent"
42-
description="Forward ports to the lima-hostagent"
50+
name="lima-guestagent"
51+
description="Forward ports to the lima-hostagent"
52+
53+
command=${LIMA_CIDATA_GUEST_INSTALL_PREFIX}/bin/lima-guestagent
54+
command_args="daemon --debug=${LIMA_CIDATA_DEBUG} --vsock-port \"${LIMA_CIDATA_VSOCK_PORT}\" --virtio-port \"${LIMA_CIDATA_VIRTIO_PORT}\""
55+
command_background=true
56+
pidfile="/run/lima-guestagent.pid"
57+
EOF
58+
}
59+
if [ "${guestagent_updated}" = "false" ] &&
60+
diff -q <(print_config) /etc/conf.d/lima-guestagent 2>/dev/null &&
61+
diff -q <(print_script) /etc/init.d/lima-guestagent 2>/dev/null; then
62+
echo "lima-guestagent service already up-to-date"
63+
exit 0
64+
fi
4365

44-
command=${LIMA_CIDATA_GUEST_INSTALL_PREFIX}/bin/lima-guestagent
45-
command_args="daemon --debug=${LIMA_CIDATA_DEBUG} --vsock-port \"${LIMA_CIDATA_VSOCK_PORT}\" --virtio-port \"${LIMA_CIDATA_VIRTIO_PORT}\""
46-
command_background=true
47-
pidfile="/run/lima-guestagent.pid"
48-
EOF
66+
print_config >/etc/conf.d/lima-guestagent
67+
print_script >/etc/init.d/lima-guestagent
4968
chmod 755 /etc/init.d/lima-guestagent
5069

5170
rc-update add lima-guestagent default
52-
rc-service lima-guestagent start
71+
rc-service --ifstarted lima-guestagent restart # restart if running, otherwise do nothing
72+
rc-service --ifstopped lima-guestagent start # start if not running, otherwise do nothing
5373
else
5474
# Remove legacy systemd service
5575
rm -f "${LIMA_CIDATA_HOME}/.config/systemd/user/lima-guestagent.service"
5676

5777
if [ "${LIMA_CIDATA_VSOCK_PORT}" != "0" ]; then
58-
sudo "${LIMA_CIDATA_GUEST_INSTALL_PREFIX}"/bin/lima-guestagent install-systemd --debug="${LIMA_CIDATA_DEBUG}" --vsock-port "${LIMA_CIDATA_VSOCK_PORT}"
78+
sudo "${LIMA_CIDATA_GUEST_INSTALL_PREFIX}"/bin/lima-guestagent install-systemd --debug="${LIMA_CIDATA_DEBUG}" --guestagent-updated="${guestagent_updated}" --vsock-port "${LIMA_CIDATA_VSOCK_PORT}"
5979
elif [ "${LIMA_CIDATA_VIRTIO_PORT}" != "" ]; then
60-
sudo "${LIMA_CIDATA_GUEST_INSTALL_PREFIX}"/bin/lima-guestagent install-systemd --debug="${LIMA_CIDATA_DEBUG}" --virtio-port "${LIMA_CIDATA_VIRTIO_PORT}"
80+
sudo "${LIMA_CIDATA_GUEST_INSTALL_PREFIX}"/bin/lima-guestagent install-systemd --debug="${LIMA_CIDATA_DEBUG}" --guestagent-updated="${guestagent_updated}" --virtio-port "${LIMA_CIDATA_VIRTIO_PORT}"
6181
else
62-
sudo "${LIMA_CIDATA_GUEST_INSTALL_PREFIX}"/bin/lima-guestagent install-systemd --debug="${LIMA_CIDATA_DEBUG}"
82+
sudo "${LIMA_CIDATA_GUEST_INSTALL_PREFIX}"/bin/lima-guestagent install-systemd --debug="${LIMA_CIDATA_DEBUG}" --guestagent-updated="${guestagent_updated}"
6383
fi
6484
fi

0 commit comments

Comments
 (0)