| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
set -eu |
| 4 |
|
| 5 |
######################################################################## |
| 6 |
# Configure and run an unprivileged container as a non-root user |
| 7 |
######################################################################## |
| 8 |
|
| 9 |
uidrange=$(awk -F : "{if (\$1 == \"${USER}\") {print(\$2, \$3)}}" /etc/subuid) |
| 10 |
gidrange=$(awk -F : "{if (\$1 == \"${USER}\") {print(\$2, \$3)}}" /etc/subgid) |
| 11 |
distro=$(distro-info -f --stable | awk '{ print tolower($1) }') |
| 12 |
stable=$(distro-info --stable) |
| 13 |
|
| 14 |
set -x |
| 15 |
|
| 16 |
mkdir -p ~/.config/lxc |
| 17 |
|
| 18 |
# see https://github.com/lxc/lxc/blob/899478b7159464e0fa6b88d85790f59bb852a7f1/src/lxc/start.c#L99 |
| 19 |
chmod +x ~ |
| 20 |
|
| 21 |
# no network as this would require some setup as root |
| 22 |
tee ~/.config/lxc/default.conf <<CONFIG |
| 23 |
lxc.include = /etc/lxc/default.conf |
| 24 |
lxc.net = |
| 25 |
lxc.net.0.type = empty |
| 26 |
lxc.idmap = u 0 $uidrange |
| 27 |
lxc.idmap = g 0 $gidrange |
| 28 |
lxc.mount.auto = proc:mixed sys:ro cgroup:mixed |
| 29 |
lxc.apparmor.profile = unconfined |
| 30 |
CONFIG |
| 31 |
|
| 32 |
lxc-create -t download -n mycontainer -- -d ${distro} -r ${stable} -a amd64 |
| 33 |
|
| 34 |
systemd-run --scope --quiet --user --property=Delegate=yes \ |
| 35 |
lxc-start -n mycontainer |
| 36 |
|
| 37 |
systemd-run --scope --quiet --user --property=Delegate=yes \ |
| 38 |
lxc-attach -n mycontainer -- hostname |
| 39 |
|
| 40 |
lxc-stop -n mycontainer |
| 41 |
lxc-destroy -n mycontainer |
| 42 |
|