systemd-nspawn 容器使用
简介
systemd-nspawn 比 chroot 可以启动 systemd 但是要求目标系统有 systemd
安装
systemd-nspawnsystemd 自带debootstrap部署 debian 系发行版工具 要求root下使用
emerge -av debootstrap
部署 Ubuntu
OS=ubuntu
ARCH=amd64
VERSION=focal
cd $WORKSPACES
mkdir $OS
debootstrap --arch=$ARCH $VERSION $OS https://opentuna.cn/ubuntu/
部署 Deepin
# 默认没有 apricot 脚本 需要去 link 到 gutsy
OS=deepin
ARCH=amd64
VERSION=apricot
cd /usr/share/debootstrap/scripts/
ln -s gutsy /usr/share/debootstrap/scripts/apricot
cd $WORKSPACES
mkdir $OS
debootstrap --arch=$ARCH $VERSION $OS https://community-packages.deepin.com/deepin/
挂载目录 nodev
debootstrap 部署的时候需要目录不能有 nodev
所以在 zfs 查看挂载属性
# 查看是否有 nodev on/off代表没有/有
zfs get all rpool | grep devices
# 设置取消 nodev
zfs set devices=on rpool
chroot 切换目录测试效果
# 挂载相关分区
mount -t proc proc $WORKSPACES/$OS/proc/
mount -t sysfs sys $WORKSPACES/$OS/sys/
mount -o bind /dev $WORKSPACES/$OS/dev/
mount -o bind /dev/pts $WORKSPACES/$OS/dev/pts/
# chroot 查看效果
chroot $WORKSPACES/$OS /bin/bash
# 取消挂载相关分区
umount $WORKSPACES/$OS/dev/pts/
umount $WORKSPACES/$OS/dev/
umount $WORKSPACES/$OS/sys/
umount $WORKSPACES/$OS/proc/
systemd-nspawn 使用
# 类似 chroot 使用容器
systemd-nspawn -D $WORKSPACES/$OS/
# 类似 虚拟机 启动容器
systemd-nspawn -b -D $WORKSPACES/$OS/
# 关闭容器
machinectl poweroff $OS