Skip to content

Commit 2a32137

Browse files
authored
Merge pull request #69 from AkihiroSuda/dev-refactor
cidata: more refactor + add shellcheck
2 parents 2c3c44a + 91b5323 commit 2a32137

11 files changed

+237
-186
lines changed

Diff for: .github/workflows/test.yml

+29
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,35 @@ jobs:
2121
version: v1.35
2222
args: --verbose
2323

24+
shellcheck:
25+
runs-on: ubuntu-20.04
26+
timeout-minutes: 20
27+
steps:
28+
- uses: actions/checkout@v2
29+
with:
30+
fetch-depth: 1
31+
- name: Install shellcheck
32+
run: |
33+
sudo apt-get update
34+
sudo apt-get install -y shellcheck
35+
- name: Run shellcheck
36+
run: find . -name '*.sh' | xargs shellcheck
37+
38+
shfmt:
39+
runs-on: ubuntu-20.04
40+
timeout-minutes: 20
41+
steps:
42+
- uses: actions/setup-go@v2
43+
with:
44+
go-version: 1.16.x
45+
- uses: actions/checkout@v2
46+
with:
47+
fetch-depth: 1
48+
- name: Install shfmt
49+
run: GO111MODULE=on go get mvdan.cc/sh/v3/cmd/shfmt
50+
- name: Run shfmt
51+
run: find . -name '*.sh' | xargs shfmt -s -d
52+
2453
basic:
2554
name: Basic tests
2655
runs-on: ${{ matrix.os }}

Diff for: README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ It may work on NetBSD and Windows hosts as well.
2525

2626
✅ Intel on ARM
2727

28-
✅ Various guest Linux distributions: Ubuntu, Debian, Fedora, ...
28+
✅ Various guest Linux distributions: [Ubuntu](./examples/ubuntu.yaml), [Debian](./examples/debian.yaml), [Fedora](./examples/fedora.yaml), [Alpine](./examples/alpine.yaml), ...
2929

3030
Related project: [sshocker (ssh with file sharing and port forwarding)](https://github.com/AkihiroSuda/sshocker)
3131

@@ -209,18 +209,18 @@ Alternatively, you may also directly ssh into the guest: `ssh -p 60022 -o NoHost
209209
Yes, it should work, but not regularly tested on ARM.
210210

211211
#### "Can I run non-Ubuntu guests?"
212-
Fedora is also known to work, see [`./examples/fedora.yaml`](./examples/fedora.yaml).
213-
This file can be loaded with `limactl start ./examples/fedora.yaml`.
212+
Debian, Fedora, and Alpine are also known to work.
213+
See [`./examples/`](./examples/).
214214

215215
An image has to satisfy the following requirements:
216-
- systemd
216+
- systemd or OpenRC
217217
- cloud-init
218218
- The following binaries to be preinstalled:
219219
- `sudo`
220220
- The following binaries to be preinstalled, or installable via the package manager:
221221
- `sshfs`
222222
- `newuidmap` and `newgidmap`
223-
- `apt-get` or `dnf` (if you want to contribute support for another package manager, run `git grep apt-get` to find out where to modify)
223+
- `apt-get`, `dnf`, or `apk` (if you want to contribute support for another package manager, run `git grep apt-get` to find out where to modify)
224224

225225
#### "Can I run other container engines such as Podman?"
226226
Yes, if you install it.

Diff for: docs/internal.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Metadata:
1717
- `lima.yaml`: the YAML
1818

1919
cloud-init:
20-
- `cidata.iso`: cloud-init ISO9660 image. See [`cidata.iso`](#cidata-iso).
20+
- `cidata.iso`: cloud-init ISO9660 image. See [`cidata.iso`](#cidataiso).
2121

2222
disk:
2323
- `basedisk`: the base image

Diff for: hack/test-example.sh

+10-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function ERROR() {
1313
echo >&2 "TEST| [ERROR] $*"
1414
}
1515

16-
if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then
16+
if [[ ${BASH_VERSINFO:-0} -lt 4 ]]; then
1717
ERROR "Bash version is too old: ${BASH_VERSION}"
1818
exit 1
1919
fi
@@ -74,21 +74,21 @@ limactl shell "$NAME" uname -a
7474
limactl shell "$NAME" cat /etc/os-release
7575
set +x
7676

77-
if [[ -n "${CHECKS["systemd"]}" ]]; then
77+
if [[ -n ${CHECKS["systemd"]} ]]; then
7878
set -x
7979
if ! limactl shell "$NAME" systemctl is-system-running --wait; then
80-
ERROR "\"systemctl is-system-running\" failed"
80+
ERROR '"systemctl is-system-running" failed'
8181
limactl shell "$NAME" systemctl
82-
if [[ -z "${CHECKS["systemd-strict"]}" ]]; then
83-
INFO "Ignoring \"systemctl is-system-running\" failure"
82+
if [[ -z ${CHECKS["systemd-strict"]} ]]; then
83+
INFO 'Ignoring "systemctl is-system-running" failure'
8484
else
8585
exit 1
8686
fi
8787
fi
8888
set +x
8989
fi
9090

91-
if [[ -n "${CHECKS["mount-home"]}" ]]; then
91+
if [[ -n ${CHECKS["mount-home"]} ]]; then
9292
hometmp="$HOME/lima-test-tmp"
9393
INFO "Testing home access (\"$hometmp\")"
9494
rm -rf "$hometmp"
@@ -104,7 +104,7 @@ if [[ -n "${CHECKS["mount-home"]}" ]]; then
104104
fi
105105
fi
106106

107-
if [[ -n "${CHECKS["containerd-user"]}" ]]; then
107+
if [[ -n ${CHECKS["containerd-user"]} ]]; then
108108
INFO "Run a nginx container with port forwarding 127.0.0.1:8080"
109109
set -x
110110
limactl shell "$NAME" nerdctl info
@@ -119,8 +119,9 @@ if [[ -n "${CHECKS["containerd-user"]}" ]]; then
119119
set +x
120120
fi
121121

122-
if [[ -n "${CHECKS["restart"]}" ]]; then
122+
if [[ -n ${CHECKS["restart"]} ]]; then
123123
INFO "Create file in the guest home directory and verify that it still exists after a restart"
124+
# shellcheck disable=SC2016
124125
limactl shell "$NAME" sh -c 'touch $HOME/sweet-home'
125126

126127
INFO "Stopping \"$NAME\""
@@ -129,6 +130,7 @@ if [[ -n "${CHECKS["restart"]}" ]]; then
129130
INFO "Restarting \"$NAME\""
130131
limactl start "$NAME"
131132

133+
# shellcheck disable=SC2016
132134
if ! limactl shell "$NAME" sh -c 'test -f $HOME/sweet-home'; then
133135
ERROR "Guest home directory does not persist across restarts"
134136
exit 1

Diff for: pkg/cidata/cidata.TEMPLATE.d/boot.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/bin/sh
22
set -eu
33

4-
INFO(){
5-
echo "LIMA| $*"
4+
INFO() {
5+
echo "LIMA| $*"
66
}
77

8-
WARNING(){
9-
echo "LIMA| WARNING: $*"
8+
WARNING() {
9+
echo "LIMA| WARNING: $*"
1010
}
1111

1212
# shellcheck disable=SC2163
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/bin/bash
2+
# bash is used for enabling `set -o pipefail`.
3+
# NOTE: On Alpine, /bin/bash is ash with ASH_BASH_COMPAT, not GNU bash
24
set -eux -o pipefail
35

46
# Restrict the rest of this script to Alpine until it has been tested with other distros
@@ -10,39 +12,40 @@ DATADIRS="/home /usr/local /etc/containerd /var/lib/containerd"
1012
# When running from RAM try to move persistent data to data-volume
1113
# FIXME: the test for tmpfs mounts is probably Alpine-specific
1214
if [ "$(awk '$2 == "/" {print $3}' /proc/mounts)" == "tmpfs" ]; then
13-
mkdir -p /mnt/data
14-
if [ -e /dev/disk/by-label/data-volume ]; then
15-
mount -t ext4 /dev/disk/by-label/data-volume /mnt/data
16-
else
17-
# Find an unpartitioned disk and create data-volume
18-
DISKS=$(lsblk --list --noheadings --output name,type | awk '$2 == "disk" {print $1}')
19-
for DISK in ${DISKS}; do
20-
IN_USE=false
21-
# Looking for a disk that is not mounted or partitioned
22-
for PART in $(awk '/^\/dev\// {gsub("/dev/", ""); print $1}' /proc/mounts); do
23-
if [ "${DISK}" == "${PART}" -o -e /sys/block/${DISK}/${PART} ]; then
24-
IN_USE=true
25-
break
26-
fi
27-
done
28-
if [ "${IN_USE}" == "false" ]; then
29-
echo 'type=83' | sfdisk --label dos /dev/${DISK}
30-
PART=$(lsblk --list /dev/${DISK} --noheadings --output name,type | awk '$2 == "part" {print $1}')
31-
mkfs.ext4 -L data-volume /dev/${PART}
32-
mount -t ext4 /dev/disk/by-label/data-volume /mnt/data
33-
for DIR in ${DATADIRS}; do
34-
DEST="/mnt/data$(dirname ${DIR})"
35-
mkdir -p ${DIR} ${DEST}
36-
mv ${DIR} ${DEST}
37-
done
38-
break
39-
fi
40-
done
41-
fi
42-
for DIR in ${DATADIRS}; do
43-
if [ -d /mnt/data${DIR} ]; then
44-
[ -e ${DIR} ] && rm -rf ${DIR}
45-
ln -s /mnt/data${DIR} ${DIR}
46-
fi
47-
done
15+
mkdir -p /mnt/data
16+
if [ -e /dev/disk/by-label/data-volume ]; then
17+
mount -t ext4 /dev/disk/by-label/data-volume /mnt/data
18+
else
19+
# Find an unpartitioned disk and create data-volume
20+
DISKS=$(lsblk --list --noheadings --output name,type | awk '$2 == "disk" {print $1}')
21+
for DISK in ${DISKS}; do
22+
IN_USE=false
23+
# Looking for a disk that is not mounted or partitioned
24+
# shellcheck disable=SC2013
25+
for PART in $(awk '/^\/dev\// {gsub("/dev/", ""); print $1}' /proc/mounts); do
26+
if [ "${DISK}" == "${PART}" ] || [ -e /sys/block/"${DISK}"/"${PART}" ]; then
27+
IN_USE=true
28+
break
29+
fi
30+
done
31+
if [ "${IN_USE}" == "false" ]; then
32+
echo 'type=83' | sfdisk --label dos /dev/"${DISK}"
33+
PART=$(lsblk --list /dev/"${DISK}" --noheadings --output name,type | awk '$2 == "part" {print $1}')
34+
mkfs.ext4 -L data-volume /dev/"${PART}"
35+
mount -t ext4 /dev/disk/by-label/data-volume /mnt/data
36+
for DIR in ${DATADIRS}; do
37+
DEST="/mnt/data$(dirname "${DIR}")"
38+
mkdir -p "${DIR}" "${DEST}"
39+
mv "${DIR}" "${DEST}"
40+
done
41+
break
42+
fi
43+
done
44+
fi
45+
for DIR in ${DATADIRS}; do
46+
if [ -d /mnt/data"${DIR}" ]; then
47+
[ -e "${DIR}" ] && rm -rf "${DIR}"
48+
ln -s /mnt/data"${DIR}" "${DIR}"
49+
fi
50+
done
4851
fi

Diff for: pkg/cidata/cidata.TEMPLATE.d/boot/10-alpine-prep.sh

+8-30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#!/bin/bash
2-
set -eux -o pipefail
1+
#!/bin/sh
2+
set -eux
33

44
# This script prepares Alpine for lima; there is nothing in here for other distros
55
test -f /etc/alpine-release || exit 0
@@ -8,15 +8,15 @@ test -f /etc/alpine-release || exit 0
88
BRANCH=edge
99
VERSION_ID=$(awk -F= '$1=="VERSION_ID" {print $2}' /etc/os-release)
1010
case ${VERSION_ID} in
11-
*_alpha*|*_beta*) BRANCH=edge;;
12-
*.*.*) BRANCH=v${VERSION_ID%.*};;
11+
*_alpha* | *_beta*) BRANCH=edge ;;
12+
*.*.*) BRANCH=v${VERSION_ID%.*} ;;
1313
esac
1414

1515
for REPO in main community; do
16-
URL="https://dl-cdn.alpinelinux.org/alpine/${BRANCH}/${REPO}"
17-
if ! grep -q "^${URL}$" /etc/apk/repositories; then
18-
echo "${URL}" >> /etc/apk/repositories
19-
fi
16+
URL="https://dl-cdn.alpinelinux.org/alpine/${BRANCH}/${REPO}"
17+
if ! grep -q "^${URL}$" /etc/apk/repositories; then
18+
echo "${URL}" >>/etc/apk/repositories
19+
fi
2020
done
2121

2222
# Alpine doesn't use PAM so we need to explicitly allow public key auth
@@ -26,28 +26,6 @@ usermod -p '*' "${LIMA_CIDATA_USER}"
2626
sed -i 's/AllowTcpForwarding no/AllowTcpForwarding yes/g' /etc/ssh/sshd_config
2727
rc-service sshd reload
2828

29-
# Create directory for the lima-guestagent socket (normally done by systemd)
30-
mkdir -p /run/user/${LIMA_CIDATA_UID}
31-
chown "${LIMA_CIDATA_USER}" /run/user/${LIMA_CIDATA_UID}
32-
chmod 700 /run/user/${LIMA_CIDATA_UID}
33-
34-
# Install the openrc lima-guestagent service script
35-
cat >/etc/init.d/lima-guestagent <<'EOF'
36-
#!/sbin/openrc-run
37-
supervisor=supervise-daemon
38-
39-
name="lima-guestagent"
40-
description="Forward ports to the lima-hostagent"
41-
42-
export XDG_RUNTIME_DIR="/run/user/${LIMA_CIDATA_UID}"
43-
command=/usr/local/bin/lima-guestagent
44-
command_args="daemon"
45-
command_background=true
46-
command_user="${LIMA_CIDATA_USER}:${LIMA_CIDATA_USER}"
47-
pidfile="${XDG_RUNTIME_DIR}/lima-guestagent.pid"
48-
EOF
49-
chmod 755 /etc/init.d/lima-guestagent
50-
5129
# mount /sys/fs/cgroup
5230
rc-service cgroups start
5331

Diff for: pkg/cidata/cidata.TEMPLATE.d/boot/20-rootless-base.sh

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
#!/bin/bash
2-
set -eux -o pipefail
1+
#!/bin/sh
2+
set -eux
33

44
# This script does not work unless systemd is available
5-
command -v systemctl 2>&1 >/dev/null || exit 0
5+
command -v systemctl >/dev/null 2>&1 || exit 0
66

77
# Set up env
88
for f in .profile .bashrc; do
9-
if ! grep -q "# Lima BEGIN" "/home/${LIMA_CIDATA_USER}.linux/$f"; then
10-
cat >>"/home/${LIMA_CIDATA_USER}.linux/$f" <<EOF
9+
if ! grep -q "# Lima BEGIN" "/home/${LIMA_CIDATA_USER}.linux/$f"; then
10+
cat >>"/home/${LIMA_CIDATA_USER}.linux/$f" <<EOF
1111
# Lima BEGIN
1212
# Make sure iptables and mount.fuse3 are available
1313
PATH="$PATH:/usr/sbin:/sbin"
@@ -16,13 +16,13 @@ CONTAINERD_SNAPSHOTTER="fuse-overlayfs"
1616
export PATH CONTAINERD_SNAPSHOTTER
1717
# Lima END
1818
EOF
19-
chown "${LIMA_CIDATA_USER}" "/home/${LIMA_CIDATA_USER}.linux/$f"
20-
fi
19+
chown "${LIMA_CIDATA_USER}" "/home/${LIMA_CIDATA_USER}.linux/$f"
20+
fi
2121
done
2222
# Enable cgroup delegation (only meaningful on cgroup v2)
2323
if [ ! -e "/etc/systemd/system/[email protected]/lima.conf" ]; then
24-
mkdir -p "/etc/systemd/system/[email protected]"
25-
cat >"/etc/systemd/system/[email protected]/lima.conf" <<EOF
24+
mkdir -p "/etc/systemd/system/[email protected]"
25+
cat >"/etc/systemd/system/[email protected]/lima.conf" <<EOF
2626
[Service]
2727
Delegate=yes
2828
EOF
@@ -32,17 +32,17 @@ systemctl daemon-reload
3232
# Set up sysctl
3333
sysctl_conf="/etc/sysctl.d/99-lima.conf"
3434
if [ ! -e "${sysctl_conf}" ]; then
35-
if [ -e "/proc/sys/kernel/unprivileged_userns_clone" ]; then
36-
echo "kernel.unprivileged_userns_clone=1" >> "${sysctl_conf}"
37-
fi
38-
echo "net.ipv4.ping_group_range = 0 2147483647" >> "${sysctl_conf}"
39-
echo "net.ipv4.ip_unprivileged_port_start=0" >> "${sysctl_conf}"
40-
sysctl --system
35+
if [ -e "/proc/sys/kernel/unprivileged_userns_clone" ]; then
36+
echo "kernel.unprivileged_userns_clone=1" >>"${sysctl_conf}"
37+
fi
38+
echo "net.ipv4.ping_group_range = 0 2147483647" >>"${sysctl_conf}"
39+
echo "net.ipv4.ip_unprivileged_port_start=0" >>"${sysctl_conf}"
40+
sysctl --system
4141
fi
4242

4343
# Set up subuid
4444
for f in /etc/subuid /etc/subgid; do
45-
grep -qw "${LIMA_CIDATA_USER}" $f || echo "${LIMA_CIDATA_USER}:100000:65536" >> $f
45+
grep -qw "${LIMA_CIDATA_USER}" $f || echo "${LIMA_CIDATA_USER}:100000:65536" >>$f
4646
done
4747

4848
# Start systemd session

0 commit comments

Comments
 (0)