Skip to content

Commit c0ef451

Browse files
committed
feature: non-root user support
feature: GitHub actions test integration
1 parent b1485be commit c0ef451

File tree

11 files changed

+338
-19
lines changed

11 files changed

+338
-19
lines changed

.dockerignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
/.git
33
/.github
44
/.gitignore
5+
/data
6+
/disk.img
7+
/test
58
/CHANGELOG.md
69
/LICENSE
710
/README.md
11+
.DS_Store

.github/workflows/test.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: test
2+
3+
on:
4+
push:
5+
branches: ['master']
6+
paths-ignore: ['**.md']
7+
pull_request:
8+
branches: ['master']
9+
paths-ignore: ['**.md']
10+
11+
jobs:
12+
test:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v2
18+
19+
- name: Set up QEMU
20+
uses: docker/setup-qemu-action@v1
21+
22+
- name: Set up Docker Buildx
23+
uses: docker/setup-buildx-action@v1
24+
25+
- name: Run tests
26+
run: |
27+
chmod +x test/test-container
28+
./test/test-container

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
11
/.idea
2+
/*.iml
3+
4+
/.git
5+
/.github
6+
/.gitignore
7+
/CHANGELOG.md
8+
/LICENSE
9+
/README.md
10+
11+
/data
12+
/disk.img

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Changelog
22

3+
## 1.1.0 (2021-09-21)
4+
* Support for working with non-root user
5+
* Automated testing of container using GitHub actions
6+
37
## 1.0.0 (2021-09-06)
48
* Initial version using semver

Dockerfile

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,41 @@
11
FROM ubuntu:20.04
22

3+
ENV TZ=UTC \
4+
PUID=1000 \
5+
PGID=1000 \
6+
LIBGUESTFS_DEBUG=0 \
7+
LIBGUESTFS_TRACE=0
8+
39
RUN apt-get update \
4-
&& DEBIAN_FRONTEND=noninteractive apt-get install -qq --no-install-recommends \
5-
libguestfs-tools \
6-
qemu-utils \
7-
linux-image-generic
10+
&& DEBIAN_FRONTEND=noninteractive apt-get -y --no-install-recommends install \
11+
ca-certificates \
12+
dumb-init \
13+
curl \
14+
libguestfs-tools \
15+
linux-image-generic \
16+
qemu-utils \
17+
tzdata \
18+
&& groupadd \
19+
--gid ${PGID} \
20+
libguestfs \
21+
&& useradd \
22+
--uid ${PUID} \
23+
--gid libguestfs \
24+
--shell /bin/bash \
25+
libguestfs \
26+
&& rm -rf /tmp/* /var/lib/apt/list/*
827

9-
ENV LIBGUESTFS_DEBUG=0 \
10-
LIBGUESTFS_TRACE=0 \
11-
LIBGUESTFS_BACKEND=direct
28+
COPY --from=crazymax/yasu:latest / /
29+
COPY rootfs /
30+
RUN chmod +x \
31+
/usr/local/bin/entrypoint.sh \
32+
/usr/local/bin/entrypoint-user.sh
1233

1334
WORKDIR /
35+
VOLUME ["/data"]
36+
37+
ENTRYPOINT ["/usr/bin/dumb-init", "--", "/usr/local/bin/entrypoint.sh"]
38+
CMD ["echo", "Usage: COMMAND [ARG...]\nExample: guestfish --version"]
1439

15-
CMD ["/usr/bin/guestfish", "-h"]
40+
HEALTHCHECK --interval=5s --timeout=5s --start-period=20s \
41+
CMD pgrep qemu &>1 || exit 1

README.md

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# bkahlert/libguestfs [![Build Status](https://img.shields.io/github/workflow/status/bkahlert/libguestfs/build?label=Build&logo=github&logoColor=fff)](https://github.com/bkahlert/libguestfs/actions/workflows/build-and-publish.yml) [![Repository Size](https://img.shields.io/github/repo-size/bkahlert/libguestfs?color=01818F&label=Repo%20Size&logo=Git&logoColor=fff)](https://github.com/bkahlert/libguestfs) [![Repository Size](https://img.shields.io/github/license/bkahlert/libguestfs?color=29ABE2&label=License&logo=data%3Aimage%2Fsvg%2Bxml%3Bbase64%2CPHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA1OTAgNTkwIiAgeG1sbnM6dj0iaHR0cHM6Ly92ZWN0YS5pby9uYW5vIj48cGF0aCBkPSJNMzI4LjcgMzk1LjhjNDAuMy0xNSA2MS40LTQzLjggNjEuNC05My40UzM0OC4zIDIwOSAyOTYgMjA4LjljLTU1LjEtLjEtOTYuOCA0My42LTk2LjEgOTMuNXMyNC40IDgzIDYyLjQgOTQuOUwxOTUgNTYzQzEwNC44IDUzOS43IDEzLjIgNDMzLjMgMTMuMiAzMDIuNCAxMy4yIDE0Ny4zIDEzNy44IDIxLjUgMjk0IDIxLjVzMjgyLjggMTI1LjcgMjgyLjggMjgwLjhjMCAxMzMtOTAuOCAyMzcuOS0xODIuOSAyNjEuMWwtNjUuMi0xNjcuNnoiIGZpbGw9IiNmZmYiIHN0cm9rZT0iI2ZmZiIgc3Ryb2tlLXdpZHRoPSIxOS4yMTIiIHN0cm9rZS1saW5lam9pbj0icm91bmQiLz48L3N2Zz4%3D)](https://github.com/bkahlert/libguestfs/blob/master/LICENSE)
22

33
* Containerized libguestfs including virt-customize, guestfish, etc.
4-
* Scripts
4+
* Runs as non-root user
5+
* Multi-platform image
6+
* Helper scripts
57
* `guestfish` — Opens the raw image disk file using guestfish.
68
* `virt-customize` — Opens the raw image disk using libguestfs' virt-customize.
79
* `pi` — Opens the raw image disk using a dockerized ARM emulator that emulates a Raspberry Pi.
@@ -30,23 +32,52 @@ Following platforms for this image are available:
3032
* linux/amd64
3133
* linux/arm/v7
3234
* linux/arm64/v8
33-
* linux/riscv64
3435
* linux/ppc64le
36+
* linux/riscv64
3537
* linux/s390x
3638

39+
## Environment variables
40+
41+
* `TZ`: The timezone assigned to the container (default `UTC`)
42+
* `PUID`: The user id to use (default `1000`)
43+
* `PGID`: The group id to use (default `1000`)
44+
* `LIBGUESTFS_DEBUG`: Set this to 1 in order to enable massive amounts of debug messages. If you think there is some problem inside the libguestfs appliance, then you should use this option. (default: `0`)
45+
* `LIBGUESTFS_TRACE`: Set this to 1 and libguestfs will print out each command / API call in a format which is similar to guestfish commands. (default: `0`)
3746

3847
## Usage
3948

49+
### Interactively
50+
51+
```shell
52+
docker run -it --rm \
53+
-v "$(pwd)/data:/data" \
54+
-v "$(pwd)/disk.img:/disk.img" \
55+
bkahlert/libguestfs:edge \
56+
guestfish
57+
58+
><fs> add /disk.img
59+
><fs> launch
60+
><fs> mount /dev/sda /
61+
><fs> ls /
62+
><fs> copy-out /boot data
63+
><fs> umount-all
64+
><fs> exit
65+
```
66+
67+
### Automatically
68+
4069
```shell
41-
docker run --rm -i \
42-
-v "$(pwd)/data:/data" -v "$(pwd)/disk.img:/disk.img" \
43-
bkahlert/libguestfs guestfish \
70+
docker run --rm \
71+
-v "$(pwd)/data:/data" \
72+
-v "$(pwd)/disk.img:/disk.img" \
73+
bkahlert/libguestfs:edge \
74+
guestfish \
4475
--ro \
4576
--add /disk.img \
46-
--mount /dev/sda2:/ \
47-
--mount /dev/sda1:/boot \
77+
--mount /dev/sda:/ \
4878
<<COMMANDS
49-
-copy-out "/boot" "data"
79+
ls /
80+
-copy-out /boot data
5081
umount-all
5182
exit
5283
COMMANDS
@@ -57,6 +88,26 @@ the `guestfish` tool and executes all guestfish commands enclosed by `COMMANDS`
5788

5889
In this case the directory `/boot` and its contents is copied to data.
5990

91+
> :bulb: Did you notice the leading dash in front of the `copy-out` command? Running guestfish non-interactively the first command that gives an error causes the whole shell to exit. By prefixing a command with `-` guestfish will not exit if an error is encountered.
92+
93+
> :bulb: If you prefix a command with `!` (e.g. `!id`) the command will run on the host instead of the mounted guest. Since the libguestfs tools are containerized themselves, "host" signifies the containerized libguestfs hosting Ubuntu installation — and not you actual OS.
94+
95+
96+
## Troubleshooting
97+
98+
If you run into problems, try running your intended steps
99+
interactively with verbose logging turned on:
100+
101+
```shell
102+
docker run -it --rm \
103+
-e "LIBGUESTFS_DEBUG=1" \
104+
-e "LIBGUESTFS_TRACE=1" \
105+
-v "$(pwd)/data:/data" \
106+
-v "$(pwd)/disk.img:/disk.img" \
107+
bkahlert/libguestfs:edge \
108+
guestfish
109+
```
110+
60111

61112
## Contributing
62113

docker-bake.hcl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@ target "image-all" {
2929
"linux/arm/v7",
3030
// "linux/arm64",
3131
"linux/arm64/v8",
32-
"linux/riscv64",
3332
"linux/ppc64le",
33+
"linux/riscv64",
3434
"linux/s390x"
35-
// "linux/mips64le",
36-
// "linux/mips64",
3735
]
3836
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
# see https://libguestfs.org/guestfs-faq.1.html
6+
if [ "$(id -u)" -eq 0 ]; then
7+
export LIBGUESTFS_BACKEND=direct
8+
fi
9+
10+
exec "$@"

rootfs/usr/local/bin/entrypoint.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
# Fix access rights to stdout and stderr
6+
chown "${PUID}:${PGID}" /proc/self/fd/1 /proc/self/fd/2 || true
7+
8+
# Update UID and GID
9+
if [ -n "${PGID}" ] && [ "${PGID}" != "$(id -g libguestfs)" ]; then
10+
# echo "Switching to PGID ${PGID}..."
11+
sed -i -e "s/^libguestfs:\([^:]*\):[0-9]*/libguestfs:\1:${PGID}/" /etc/group
12+
sed -i -e "s/^libguestfs:\([^:]*\):\([0-9]*\):[0-9]*/libguestfs:\1:\2:${PGID}/" /etc/passwd
13+
fi
14+
if [ -n "${PUID}" ] && [ "${PUID}" != "$(id -u libguestfs)" ]; then
15+
# echo "Switching to PUID ${PUID}..."
16+
sed -i -e "s/^libguestfs:\([^:]*\):[0-9]*:\([0-9]*\)/libguestfs:\1:${PUID}:\2/" /etc/passwd
17+
fi
18+
19+
# Get ownership of home
20+
mkdir -p /home/libguestfs
21+
chown -R libguestfs /home/libguestfs
22+
chmod -R u+rw /home/libguestfs
23+
24+
# Get ownership of data
25+
mkdir -p /data
26+
chown -R libguestfs /data
27+
chmod -R u+rw /data
28+
29+
# Get ownershop of disk.img
30+
[ -e /disk.img ] || touch /disk.img
31+
chown -R libguestfs /disk.img
32+
chmod -R u+rw /disk.img
33+
34+
# Timezone
35+
TZ=${TZ:-UTC}
36+
ln -snf "/usr/share/zoneinfo/${TZ}" /etc/localtime
37+
echo "${TZ}" > /etc/timezone
38+
39+
# see https://libguestfs.org/guestfs-faq.1.html#where-can-i-get-the-latest-binaries-for
40+
chmod 0644 /boot/vmlinuz*
41+
usermod -a -G kvm libguestfs
42+
43+
# From here, step down to libguestfs user
44+
yasu libguestfs:libguestfs entrypoint-user.sh "$@"

0 commit comments

Comments
 (0)