|
| 1 | +# Creating and using chroots and containers |
| 2 | + |
| 3 | +chroots and containers can be set up and used for many purposes, including: |
| 4 | + |
| 5 | +- running glibc software on musl (and vice versa) |
| 6 | +- running software in a more controlled or sandboxed environment |
| 7 | +- creating a rootfs for bootstrapping a system |
| 8 | + |
| 9 | +## Chroot Creation |
| 10 | + |
| 11 | +### xvoidstrap |
| 12 | + |
| 13 | +[`xvoidstrap(1)`](https://man.voidlinux.org/xvoidstrap.1) (from `xtools`) can be |
| 14 | +used to create the chroot: |
| 15 | + |
| 16 | +``` |
| 17 | +# mkdir <chroot_dir> |
| 18 | +# XBPS_ARCH=<chroot_arch> xvoidstrap <chroot_dir> base-voidstrap <other_pkgs> |
| 19 | +``` |
| 20 | + |
| 21 | +`<other_pkgs>` is only needed if you want to pre-install other packages in the |
| 22 | +chroot. |
| 23 | + |
| 24 | +### Manual Method |
| 25 | + |
| 26 | +Alternatively, this process can be done manually. |
| 27 | + |
| 28 | +Create a directory that will contain the chroot, then install a base system in |
| 29 | +it via the `base-voidstrap` package: |
| 30 | + |
| 31 | +``` |
| 32 | +# mkdir -p "<chroot_dir>/var/db/xbps/keys" |
| 33 | +# cp -a /var/db/xbps/keys/* "<chroot_dir>/var/db/xbps/keys" |
| 34 | +# XBPS_ARCH=<chroot_arch> xbps-install -S -r <chroot_dir> -R <repository> base-voidstrap <other_pkgs> |
| 35 | +``` |
| 36 | + |
| 37 | +The `<repository>` may [vary depending on |
| 38 | +architecture](../../xbps/repositories/index.md#the-main-repository). |
| 39 | + |
| 40 | +`<other_pkgs>` is only needed if you want to pre-install other packages in the |
| 41 | +chroot. |
| 42 | + |
| 43 | +## Chroot Usage |
| 44 | + |
| 45 | +### xchroot |
| 46 | + |
| 47 | +[`xchroot(1)`](https://man.voidlinux.org/xchroot.1) (from `xtools`) can be used |
| 48 | +to automatically set up and enter the chroot. |
| 49 | + |
| 50 | +### Manual Method |
| 51 | + |
| 52 | +Alternatively, this process can be done manually. |
| 53 | + |
| 54 | +If network access is required, copy `/etc/resolv.conf` into the chroot; |
| 55 | +`/etc/hosts` may need to be copied as well. |
| 56 | + |
| 57 | +Several directories then need to be mounted as follows: |
| 58 | + |
| 59 | +``` |
| 60 | +# mount -t proc none <chroot_dir>/proc |
| 61 | +# mount -t sysfs none <chroot_dir>/sys |
| 62 | +# mount --rbind /dev <chroot_dir>/dev |
| 63 | +# mount --rbind /run <chroot_dir>/run |
| 64 | +``` |
| 65 | + |
| 66 | +Use [chroot(1)](https://man.voidlinux.org/chroot.1) to change to the new root, |
| 67 | +then run programs and do tasks as usual. Once finished with the chroot, unmount |
| 68 | +the chroot using [umount(8)](https://man.voidlinux.org/umount.8). If any |
| 69 | +destructive actions are taken on the chroot directory without unmounting first, |
| 70 | +you may need to reboot to repopulate the affected directories. |
| 71 | + |
| 72 | +### Alternatives |
| 73 | + |
| 74 | +#### Bubblewrap |
| 75 | + |
| 76 | +[bwrap(1)](https://man.voidlinux.org/bwrap.1) (from the `bubblewrap` package) |
| 77 | +has additional features like the ability for sandboxing and does not require |
| 78 | +root access. |
| 79 | + |
| 80 | +`bwrap` is very flexible and can be used in many ways, for example: |
| 81 | + |
| 82 | +``` |
| 83 | +$ bwrap --bind <chroot_dir> / \ |
| 84 | + --dev /dev \ |
| 85 | + --proc /proc \ |
| 86 | + --bind /sys /sys \ |
| 87 | + --bind /run /run \ |
| 88 | + --ro-bind /etc/resolv.conf /etc/resolv.conf \ |
| 89 | + --ro-bind /etc/passwd /etc/passwd \ |
| 90 | + --ro-bind /etc/group /etc/group \ |
| 91 | + <command> |
| 92 | +``` |
| 93 | + |
| 94 | +In this example, you will not be able to add or edit users or groups. When |
| 95 | +running graphical applications with Xorg, you may need to also bind-mount |
| 96 | +`~/.Xauthority` or other files or directories. |
| 97 | + |
| 98 | +The [bwrap(1) manpage](https://man.voidlinux.org/bwrap.1) and the [Arch Wiki |
| 99 | +article](https://wiki.archlinux.org/title/Bubblewrap#Usage_examples) contain |
| 100 | +more examples of `bwrap` usage. |
| 101 | + |
| 102 | +#### Flatpak |
| 103 | + |
| 104 | +[Flatpak](../external-applications.md#flatpak) is a convenient option for |
| 105 | +running many applications, including graphical or proprietary ones, on both |
| 106 | +glibc and musl systems. |
| 107 | + |
| 108 | +#### Application Containers |
| 109 | + |
| 110 | +If a more integrated and polished solution is desired, Void also [provides OCI |
| 111 | +containers](https://github.com/void-linux/void-docker/pkgs/container/void-linux) |
| 112 | +that work with tools like [docker](https://www.docker.com) and |
| 113 | +[podman](https://man.voidlinux.org/podman.1). These containers do not require |
| 114 | +the creation of a chroot directory before usage. |
0 commit comments