在 Alpine chroot 环境中安装 ArchLinux

来自 Alpine Linux

有时需要在 Alpine 系统上的 chroot 环境中安装另一个 Linux 系统,这会很有用。本页面解释了如何在现有的 Alpine 系统(假定具有相同的架构)中安装最新版本的 ArchLinux(x86 或 x86_64)。

一些有用的链接


  1. 在您的 Alpine 系统中

    sudo apk add bash zstd curl wget # BusyBox wget 不够用 wget --no-check-certificate https://raw.github.com/tokland/arch-bootstrap/master/arch-bootstrap.sh mkdir chroot64 sudo bash arch-bootstrap.sh -a x86_64 chroot64

  2. 然后 chroot 进入新创建的 Arch 系统。我使用以下脚本来执行此操作

    /usr/local/bin/start-chroot 的内容

    #!/bin/sh -e user=`whoami` if [ "$user" != "root" ]; then echo "This script needs root access" >&2 exit 1 fi if ! [ -d "$1" ]; then echo "Usage: $0 <chroot directory>" >&2 exit 1 fi if [ x1 = x`sysctl -ne kernel.grsecurity.chroot_deny_chmod` ]; then echo "Warning: can't suid/sgid inside chroot" >&2 fi if [ x1 = x`sysctl -ne kernel.grsecurity.chroot_deny_chroot` ]; then echo "Warning: can't chroot inside chroot" >&2 fi if [ x1 = x`sysctl -ne kernel.grsecurity.chroot_deny_mknod` ]; then echo "Warning: can't mknod inside chroot" >&2 fi if [ x1 = x`sysctl -ne kernel.grsecurity.chroot_deny_mount` ]; then echo "Warning: can't mount inside chroot" >&2 fi cd "$1" shift cp -L /etc/resolv.conf ./etc/ || true mount -t proc proc ./proc mount -t sysfs sys ./sys mount -o bind /dev ./dev # 据说下一行对于 pacman 的签名检查很重要 mount -o bind /dev/pts ./dev/pts case $1 in -l) shift;; -l*) one=${1#-l}; shift; set -- -$one "$@";; esac chroot . /bin/sh -l "$@" umount ./dev/pts umount ./dev ./sys ./proc

    至少在设置 Arch 系统时,您需要禁用 Alpine 的 grsecurity 对 suid/sgid 的禁止

    sysctl -w kernel.grsecurity.chroot_deny_chmod=0

    更多信息,请参阅

  3. 在 Arch chroot 环境中,执行以下操作。(第一步可能需要很长时间,特别是如果您是通过 ssh 连接到有问题的系统。有关更多信息,以及有关如何加快此过程的技巧,请参阅 此链接。)

    pacman-key --init pacman-key --populate archlinux pacman -Syyu pacman -Rs systemd # 在 chroot 环境中,我们不需要 init 系统(也不需要内核) pacman -S sudo

    您还可以安装任何其他您想要的软件包。我使用

    pacman -S less licenses man-db man-pages procps-ng psmisc sysfsutils \ base-devel openssh cpio elfutils rsync unzip vim wget zip

  4. 仍在 Arch chroot 环境中,分配 root 密码并创建一个非 root 用户

    passwd useradd -m -g users -G wheel -s /usr/bin/bash myuser passwd myuser export VISUAL=/usr/bin/vim visudo

    取消注释 /etc/sudoers 中的这一行,然后保存文件

    ## Uncomment to allow members of group wheel to execute any command
    %wheel ALL=(ALL) ALL
    

现在您已完成。您可以使用以下命令切换到非 root 用户

su - myuser

或按 Ctrl+D 退出 chroot 环境。

如果您想从 Arch 用户仓库 安装任何软件包,您可能需要安装更丰富的软件包管理器,例如 Yaourt。以非 root 用户身份执行此操作

curl -O https://aur.archlinux.org/packages/pa/package-query/package-query.tar.gz tar -xzf package-query.tar.gz && cd package-query && makepkg -si cd .. curl -O https://aur.archlinux.org/packages/ya/yaourt/yaourt.tar.gz tar -xzf yaourt.tar.gz && cd yaourt && makepkg -si