Repository: lima-vm/lima
Pull request #1736 (2023-08-12 11:39:14)

Add variable for the guest home directory
Just a refactoring, to avoid hardcoding `/home` and `linux` everywhere in cidata.


Patch:
diff --git a/docs/internal.md b/docs/internal.md
index c785c08e..676080d0 100644
--- a/docs/internal.md
+++ b/docs/internal.md
@@ -151,6 +151,7 @@ The volume label is "cidata", as defined by [cloud-init NoCloud](https://cloudin
 - `LIMA_CIDATA_MNT`: the mount point of the disk. `/mnt/lima-cidata`.
 - `LIMA_CIDATA_USER`: the user name string
 - `LIMA_CIDATA_UID`: the numeric UID
+- `LIMA_CIDATA_HOME`: the guest home directory
 - `LIMA_CIDATA_HOSTHOME_MOUNTPOINT`: the mount point of the host home directory, or empty if not mounted
 - `LIMA_CIDATA_MOUNTS`: the number of the Lima mounts
 - `LIMA_CIDATA_MOUNTS_%d_MOUNTPOINT`: the N-th mount point of Lima mounts (N=0, 1, ...)
diff --git a/pkg/cidata/cidata.TEMPLATE.d/boot.sh b/pkg/cidata/cidata.TEMPLATE.d/boot.sh
index b2fab88e..7e26757b 100644
--- a/pkg/cidata/cidata.TEMPLATE.d/boot.sh
+++ b/pkg/cidata/cidata.TEMPLATE.d/boot.sh
@@ -52,7 +52,7 @@ if [ -d "${LIMA_CIDATA_MNT}"/provision.system ]; then
 	done
 fi
 
-USER_SCRIPT="/home/${LIMA_CIDATA_USER}.linux/.lima-user-script"
+USER_SCRIPT="${LIMA_CIDATA_HOME}/.lima-user-script"
 if [ -d "${LIMA_CIDATA_MNT}"/provision.user ]; then
 	if [ ! -f /sbin/openrc-run ]; then
 		until [ -e "/run/user/${LIMA_CIDATA_UID}/systemd/private" ]; do sleep 3; done
diff --git a/pkg/cidata/cidata.TEMPLATE.d/boot/08-shell-prompt.sh b/pkg/cidata/cidata.TEMPLATE.d/boot/08-shell-prompt.sh
index 159934d6..27321a39 100644
--- a/pkg/cidata/cidata.TEMPLATE.d/boot/08-shell-prompt.sh
+++ b/pkg/cidata/cidata.TEMPLATE.d/boot/08-shell-prompt.sh
@@ -3,13 +3,13 @@ set -eux
 
 # This script is only intended for the default.yaml image, which is based on Ubuntu LTS
 
-if [ "${LIMA_CIDATA_NAME}" = "default" ] && command -v patch >/dev/null 2>&1 && grep -q color_prompt "/home/${LIMA_CIDATA_USER}.linux/.bashrc"; then
+if [ "${LIMA_CIDATA_NAME}" = "default" ] && command -v patch >/dev/null 2>&1 && grep -q color_prompt "${LIMA_CIDATA_HOME}/.bashrc"; then
 
-	! grep -q "^# Lima PS1" "/home/${LIMA_CIDATA_USER}.linux/.bashrc" || exit 0
+	! grep -q "^# Lima PS1" "${LIMA_CIDATA_HOME}/.bashrc" || exit 0
 
 	# Change the default shell prompt from "green" to "lime" (#BFFF00)
 
-	patch --forward -r - "/home/${LIMA_CIDATA_USER}.linux/.bashrc" <<'EOF'
+	patch --forward -r - "${LIMA_CIDATA_HOME}/.bashrc" <<'EOF'
 @@ -37,7 +37,11 @@
  
  # set a fancy prompt (non-color, unless we know we "want" color)
diff --git a/pkg/cidata/cidata.TEMPLATE.d/boot/20-rootless-base.sh b/pkg/cidata/cidata.TEMPLATE.d/boot/20-rootless-base.sh
index 2edeea5b..d0efb254 100644
--- a/pkg/cidata/cidata.TEMPLATE.d/boot/20-rootless-base.sh
+++ b/pkg/cidata/cidata.TEMPLATE.d/boot/20-rootless-base.sh
@@ -6,15 +6,15 @@ command -v systemctl >/dev/null 2>&1 || exit 0
 
 # Set up env
 for f in .profile .bashrc .zshrc; do
-	if ! grep -q "# Lima BEGIN" "/home/${LIMA_CIDATA_USER}.linux/$f"; then
-		cat >>"/home/${LIMA_CIDATA_USER}.linux/$f" <<EOF
+	if ! grep -q "# Lima BEGIN" "${LIMA_CIDATA_HOME}/$f"; then
+		cat >>"${LIMA_CIDATA_HOME}/$f" <<EOF
 # Lima BEGIN
 # Make sure iptables and mount.fuse3 are available
 PATH="\$PATH:/usr/sbin:/sbin"
 export PATH
 EOF
 		if compare_version.sh "$(uname -r)" -lt "5.13"; then
-			cat >>"/home/${LIMA_CIDATA_USER}.linux/$f" <<EOF
+			cat >>"${LIMA_CIDATA_HOME}/$f" <<EOF
 # fuse-overlayfs is the most stable snapshotter for rootless, on kernel < 5.13
 # https://github.com/lima-vm/lima/issues/383
 # https://rootlesscontaine.rs/how-it-works/overlayfs/
@@ -22,10 +22,10 @@ CONTAINERD_SNAPSHOTTER="fuse-overlayfs"
 export CONTAINERD_SNAPSHOTTER
 EOF
 		fi
-		cat >>"/home/${LIMA_CIDATA_USER}.linux/$f" <<EOF
+		cat >>"${LIMA_CIDATA_HOME}/$f" <<EOF
 # Lima END
 EOF
-		chown "${LIMA_CIDATA_USER}" "/home/${LIMA_CIDATA_USER}.linux/$f"
+		chown "${LIMA_CIDATA_USER}" "${LIMA_CIDATA_HOME}/$f"
 	fi
 done
 # Enable cgroup delegation (only meaningful on cgroup v2)
diff --git a/pkg/cidata/cidata.TEMPLATE.d/boot/25-guestagent-base.sh b/pkg/cidata/cidata.TEMPLATE.d/boot/25-guestagent-base.sh
index b585a526..3276ec2a 100644
--- a/pkg/cidata/cidata.TEMPLATE.d/boot/25-guestagent-base.sh
+++ b/pkg/cidata/cidata.TEMPLATE.d/boot/25-guestagent-base.sh
@@ -38,7 +38,7 @@ EOF
 	rc-service lima-guestagent start
 else
 	# Remove legacy systemd service
-	rm -f "/home/${LIMA_CIDATA_USER}.linux/.config/systemd/user/lima-guestagent.service"
+	rm -f "${LIMA_CIDATA_HOME}/.config/systemd/user/lima-guestagent.service"
 
 	sudo "${LIMA_CIDATA_GUEST_INSTALL_PREFIX}"/bin/lima-guestagent install-systemd
 fi
diff --git a/pkg/cidata/cidata.TEMPLATE.d/boot/40-install-containerd.sh b/pkg/cidata/cidata.TEMPLATE.d/boot/40-install-containerd.sh
index 5e0ba28a..aee4fc3e 100644
--- a/pkg/cidata/cidata.TEMPLATE.d/boot/40-install-containerd.sh
+++ b/pkg/cidata/cidata.TEMPLATE.d/boot/40-install-containerd.sh
@@ -59,9 +59,9 @@ EOF
 fi
 
 if [ "${LIMA_CIDATA_CONTAINERD_USER}" = 1 ]; then
-	if [ ! -e "/home/${LIMA_CIDATA_USER}.linux/.config/containerd/config.toml" ]; then
-		mkdir -p "/home/${LIMA_CIDATA_USER}.linux/.config/containerd"
-		cat >"/home/${LIMA_CIDATA_USER}.linux/.config/containerd/config.toml" <<EOF
+	if [ ! -e "${LIMA_CIDATA_HOME}/.config/containerd/config.toml" ]; then
+		mkdir -p "${LIMA_CIDATA_HOME}/.config/containerd"
+		cat >"${LIMA_CIDATA_HOME}/.config/containerd/config.toml" <<EOF
   version = 2
   [proxy_plugins]
     [proxy_plugins."fuse-overlayfs"]
@@ -71,13 +71,13 @@ if [ "${LIMA_CIDATA_CONTAINERD_USER}" = 1 ]; then
       type = "snapshot"
       address = "/run/user/${LIMA_CIDATA_UID}/containerd-stargz-grpc/containerd-stargz-grpc.sock"
 EOF
-		chown -R "${LIMA_CIDATA_USER}" "/home/${LIMA_CIDATA_USER}.linux/.config"
+		chown -R "${LIMA_CIDATA_USER}" "${LIMA_CIDATA_HOME}/.config"
 	fi
 	selinux=
 	if command -v selinuxenabled >/dev/null 2>&1 && selinuxenabled; then
 		selinux=1
 	fi
-	if [ ! -e "/home/${LIMA_CIDATA_USER}.linux/.config/systemd/user/containerd.service" ]; then
+	if [ ! -e "${LIMA_CIDATA_HOME}/.config/systemd/user/containerd.service" ]; then
 		until [ -e "/run/user/${LIMA_CIDATA_UID}/systemd/private" ]; do sleep 3; done
 		if [ -n "$selinux" ]; then
 			echo "Temporarily disabling SELinux, during installing containerd units"
diff --git a/pkg/cidata/cidata.TEMPLATE.d/lima.env b/pkg/cidata/cidata.TEMPLATE.d/lima.env
index e584a56e..3838d28f 100644
--- a/pkg/cidata/cidata.TEMPLATE.d/lima.env
+++ b/pkg/cidata/cidata.TEMPLATE.d/lima.env
@@ -1,6 +1,7 @@
 LIMA_CIDATA_NAME={{ .Name }}
 LIMA_CIDATA_USER={{ .User }}
 LIMA_CIDATA_UID={{ .UID }}
+LIMA_CIDATA_HOME={{ .Home}}
 LIMA_CIDATA_HOSTHOME_MOUNTPOINT={{ .HostHomeMountPoint }}
 LIMA_CIDATA_MOUNTS={{ len .Mounts }}
 {{- range $i, $val := .Mounts}}
diff --git a/pkg/cidata/cidata.TEMPLATE.d/user-data b/pkg/cidata/cidata.TEMPLATE.d/user-data
index fc54e113..cb1acba8 100644
--- a/pkg/cidata/cidata.TEMPLATE.d/user-data
+++ b/pkg/cidata/cidata.TEMPLATE.d/user-data
@@ -17,7 +17,7 @@ mounts:
 users:
   - name: "{{.User}}"
     uid: "{{.UID}}"
-    homedir: "/home/{{.User}}.linux"
+    homedir: "{{.Home}}"
     shell: /bin/bash
     sudo: ALL=(ALL) NOPASSWD:ALL
     lock_passwd: true
diff --git a/pkg/cidata/cidata.go b/pkg/cidata/cidata.go
index 82dc2578..1606a803 100644
--- a/pkg/cidata/cidata.go
+++ b/pkg/cidata/cidata.go
@@ -121,6 +121,7 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML, udpDNSLocalPort
 		Name:               name,
 		User:               u.Username,
 		UID:                uid,
+		Home:               fmt.Sprintf("/home/%s.linux", u.Username),
 		GuestInstallPrefix: *y.GuestInstallPrefix,
 		Containerd:         Containerd{System: *y.Containerd.System, User: *y.Containerd.User},
 		SlirpNICName:       networks.SlirpNICName,
diff --git a/pkg/cidata/template.go b/pkg/cidata/template.go
index 044c232e..76410f8a 100644
--- a/pkg/cidata/template.go
+++ b/pkg/cidata/template.go
@@ -56,6 +56,7 @@ type TemplateArgs struct {
 	Name                            string // instance name
 	IID                             string // instance id
 	User                            string // user name
+	Home                            string // home directory
 	UID                             int
 	SSHPubKeys                      []string
 	Mounts                          []Mount
@@ -93,6 +94,9 @@ func ValidateTemplateArgs(args TemplateArgs) error {
 	if args.UID == 0 {
 		return errors.New("field UID must not be 0")
 	}
+	if args.Home == "" {
+		return errors.New("field Home must be set")
+	}
 	if len(args.SSHPubKeys) == 0 {
 		return errors.New("field SSHPubKeys must be set")
 	}


Test patch:
diff --git a/pkg/cidata/template_test.go b/pkg/cidata/template_test.go
index ac73dedd..f09f0225 100644
--- a/pkg/cidata/template_test.go
+++ b/pkg/cidata/template_test.go
@@ -13,6 +13,7 @@ func TestTemplate(t *testing.T) {
 		Name: "default",
 		User: "foo",
 		UID:  501,
+		Home: "/home/foo.linux",
 		SSHPubKeys: []string{
 			"ssh-rsa dummy foo@example.com",
 		},
@@ -41,6 +42,7 @@ func TestTemplate9p(t *testing.T) {
 		Name: "default",
 		User: "foo",
 		UID:  501,
+		Home: "/home/foo.linux",
 		SSHPubKeys: []string{
 			"ssh-rsa dummy foo@example.com",
 		},