Path: blob/master/pkg/cidata/cidata.TEMPLATE.d/boot/20-rootless-base.sh
2648 views
#!/bin/sh12# SPDX-FileCopyrightText: Copyright The Lima Authors3# SPDX-License-Identifier: Apache-2.045set -eux67# This script does not work unless systemd is available8command -v systemctl >/dev/null 2>&1 || exit 0910if [ -O "${LIMA_CIDATA_HOME}" ]; then11# Fix ownership of the user home directory when created by root.12# In cases where mount points exist in the user's home directory, the home directory and13# the mount points are created by root before the user is created. This leads to the home14# directory being owned by root.15# Following commands fix the ownership of the home directory and its contents (on the same filesystem)16# is updated to the correct user.17# shellcheck disable=SC2046 # it fails if find results are quoted.18chown "${LIMA_CIDATA_USER}" $(find "${LIMA_CIDATA_HOME}" -xdev) ||19true # Ignore errors because changing owner of the mount points may fail but it is not critical.20fi2122# Set up env23for f in .profile .bashrc .zshrc; do24if ! grep -q "# Lima BEGIN" "${LIMA_CIDATA_HOME}/$f"; then25cat >>"${LIMA_CIDATA_HOME}/$f" <<EOF26# Lima BEGIN27# Make sure iptables and mount.fuse3 are available28PATH="\$PATH:/usr/sbin:/sbin"29export PATH30EOF31if compare_version.sh "$(uname -r)" -lt "5.13"; then32cat >>"${LIMA_CIDATA_HOME}/$f" <<EOF33# fuse-overlayfs is the most stable snapshotter for rootless, on kernel < 5.1334# https://github.com/lima-vm/lima/issues/38335# https://rootlesscontaine.rs/how-it-works/overlayfs/36CONTAINERD_SNAPSHOTTER="fuse-overlayfs"37export CONTAINERD_SNAPSHOTTER38EOF39fi40cat >>"${LIMA_CIDATA_HOME}/$f" <<EOF41# Lima END42EOF43chown "${LIMA_CIDATA_USER}" "${LIMA_CIDATA_HOME}/$f"44fi45done46# Enable cgroup delegation (only meaningful on cgroup v2)47if [ ! -e "/etc/systemd/system/[email protected]/lima.conf" ]; then48mkdir -p "/etc/systemd/system/[email protected]"49cat >"/etc/systemd/system/[email protected]/lima.conf" <<EOF50[Service]51Delegate=yes52EOF53fi54systemctl daemon-reload5556# Set up sysctl57sysctl_conf="/etc/sysctl.d/99-lima.conf"58if [ ! -e "${sysctl_conf}" ]; then59if [ -e "/proc/sys/kernel/unprivileged_userns_clone" ]; then60echo "kernel.unprivileged_userns_clone=1" >>"${sysctl_conf}"61fi62echo "net.ipv4.ping_group_range = 0 2147483647" >>"${sysctl_conf}"63echo "net.ipv4.ip_unprivileged_port_start=0" >>"${sysctl_conf}"64sysctl --system65fi6667# Set up subuid68for f in /etc/subuid /etc/subgid; do69# systemd-homed expects the subuid range to be within 524288-1878982656 (0x80000-0x6fff0000).70# See userdbctl.71# 1073741824 (1G) is just an arbitrary number.72# 1073741825-1878982656 is left blank for additional accounts.73subuid_begin=52428874# https://github.com/moby/moby/issues/49810#issuecomment-280810819175[ "${LIMA_CIDATA_UID}" -ge "${subuid_begin}" ] && subuid_begin="$((LIMA_CIDATA_UID + 1))"76grep -qw "${LIMA_CIDATA_USER}" $f || echo "${LIMA_CIDATA_USER}:${subuid_begin}:1073741824" >>$f77done7879# Start systemd session80systemctl start systemd-logind.service81loginctl enable-linger "${LIMA_CIDATA_USER}"828384