chroot or systemd-nspawn
#gentoo
root # emerge -av debootstrap
#archlinux
root # pacman -S debootstrap
#chroot
root # mkdir -p /mnt/virtualos/chroot/debian
#systemd-nspawn
root # mkdir -p /mnt/virtualos/systemd-nspawn/ubuntu
#debian
root # debootstrap --arch amd64 buster /mnt/virtualos/chroot/debian http://ftp.ca.debian.org/debian/
#ubuntu
#使用systemd-nspawn时加include
root # debootstrap --include=systemd-container --arch amd64 --components=main,restricted,universe,multiverse focal /mnt/virtualos/systemd-nspawn/ubuntu http://ca.archive.ubuntu.com/ubuntu/
#chroot 的配置
#挂载Mounting the necessary filesystems
root # mount --types proc /proc /mnt/virtualos/chroot/debian/proc
#如果是schroot,则先安装schroot再增加配置文件
#schroot配置,修改/etc/schroot/xxx/fstab等
root # vim /etc/schroot/chroot.d/buster
#切换到debian
tim $ schroot -c buster
#或者
root # chroot /mnt/virtualos/chroot/debian /bin/bash
#修改 PS1
root # source /etc/profile
root # export PS1="(chrootdebian) ${PS1}"
file /etc/schroot/chroot.d/buster
[buster]
description=Buster
aliases=test
type=directory
directory=/mnt/virtualos/chroot/debian
users=tim
root-groups=root
profile=desktop
personality=linux
preserve-environment=true
#编辑源文件
(chrootdebian) # nano /etc/apt/sources.list
(chrootdebian) # apt update
file /etc/apt/sources.list
deb http://ftp.ca.debian.org/debian/ buster main contrib non-free
deb-src http://ftp.ca.debian.org/debian/ buster main contrib non-free
deb http://security.debian.org/debian-security/ buster/updates main contrib non-free
deb-src http://security.debian.org/debian-security/ buster/updates main contrib non-free
deb http://ftp.ca.debian.org/debian/ buster-updates main contrib non-free
deb-src http://ftp.ca.debian.org/debian/ buster-updates main contrib non-free
deb http://ftp.ca.debian.org/debian/ buster-backports main contrib non-free
deb-src http://ftp.ca.debian.org/debian/ buster-backports main contrib non-free
(chrootdebian) # apt install makedev
(chrootdebian) # MAKEDEV xxxxxx
(chrootdebian) # dpkg-reconfigure tzdata
(chrootdebian) # apt install locales
(chrootdebian) # dpkg-reconfigure locales
(chrootdebian) # apt install apt-file sudo ssh vim
#安装X相关
(chrootdebian) # apt install xorg openbox
#如果使用schroot,要修改/etc/schroot/desktop/fstab先,打开/run等挂载
#方法一:
root # emerge -av xhost
tim@gentoo $ xhost +local:
tim@gentoo $ echo $DISPLAY
(chrootdebian) # export DISPLAY=:0
(chrootdebian) # xxxxxx
(chrootdebian) # startx -- :1 vt8
#方法二:需要xhost xnest
#x11-base/xorg-server 增加USE xnest后重新装xorg-server
tim@gentoo $ Xnest -ac :1
#或者
tim@gentoo $ Xnest -ac -query localhost -geometry 1600x900 :1
(chrootdebian) # export DISPLAY=:1
#如果上面使用-query localhost , 则需要用下面这个
(chrootdebian) # export DISPLAY=localhost:1
(chrootdebian) # startx
#方法三:xephyr
(chrootdebian) # apt install xserver-xephyr
(chrootdebian) # Xephyr -br -ac -noreset -screen 1600x900 :1 &
(chrootdebian) # export DISPLAY=":1"
(chrootdebian) # startx
#xephyr 2:
#主系统执行:
tim # xhost +local:
#systemd-nspawn系统:
#安装Xephyr后,执行
(chrootdebian) # export DISPLAY=:0
(chrootdebian) # startx -- /usr/bin/Xephyr :1 -fullscreen
#符号:后面是displaynumber,是大于0的数字,不要和其他系统冲突
#Ctrl + Shift 独占, Ctrl + Shift 取消独占, 取消独占后通过桌面环境的显示桌面快捷键即可回到主系统 Win + D 返回
#按路径启动
root # systemd-nspawn -D /mnt/virtualos/systemd-nspawn/ubuntu
#如果ubuntu的rootfs在/var/lib/machines,可用-M 进入,machinectl命令需systemd-nspawn@.service
root # systemd-nspawn -M ubuntu
root # systemd-nspawn -bM ubuntu
#非特权加U
root # systemd-nspawn -bUM ubuntu
#取消非特权
root # systemd-nspawn -bM ubuntu --private-users=0 --private-users-chown
#方式一:用Veth,修改ubuntu.nspawn或者使用-n或者--network-veth参数启动
root # vim /etc/systemd/nspawn/ubuntu.nspawn
#还需要host及guest都启动systemd-networkd服务
#如果还有问题要配置guest的/etc/resolv.conf
#可能还要配置NAT转发
root # iptables -t nat -A POSTROUTING -s 192.168.xxx.xxx/28 -j MASQUERADE
file /etc/systemd/nspawn/ubuntu.nspawn
[Network]
Private=yes
VirtualEthernet=yes
#方式二:直接使用libvirt的virbr0,修改ubuntu.nspawn或者使用--network-bridge=virbr0参数启动
root # vim /etc/systemd/nspawn/ubuntu.nspawn
#在guest里配置/etc/systemd/network/10-host.network并启动systemd-networkd服务
(chrootdebian) # vim /etc/systemd/network/10-host.network
#如果systemd-networkd服务不启动,则手动设置网络
root # ip address add 192.168.122.201/24 broadcast + dev host0
root # ip link set host0 up
root # ip route add default via 192.168.122.1 dev host0
file /etc/systemd/nspawn/ubuntu.nspawn
[Network]
Private=yes
VirtualEthernet=yes
Bridge=virbr0
file /etc/systemd/network/10-host.network
[Match]
Name=host*
[Network]
#DHCP=yes
Address=192.168.122.201/24
Gateway=192.168.122.1
DNS=192.168.122.1
#需要先将/tmp/.X11-unix只读挂载到guest,使用--bind-ro=/tmp/.X11-unix参数启动或者修改ubuntu.nspawn
root # vim /etc/systemd/nspawn/ubuntu.nspawn
#使用Xhost,与chroot用法类似
#不使用xhost
tim $ XAUTH=/tmp/ubuntu_xauth
tim $ xauth nextract - "$DISPLAY" | sed -e 's/^..../ffff/' | xauth -f "$XAUTH" nmerge -
root # systemd-nspawn -M ubuntu --bind=/tmp/.X11-unix --bind="$XAUTH" -E DISPLAY="$DISPLAY" -E XAUTHORITY="$XAUTH" --as-pid2 /usr/bin/xeyes
file /etc/systemd/nspawn/ubuntu.nspawn
[Files]
BindReadOnly=/tmp/.X11-unix
[Exec]
#Boot=true
PrivateUsers=no
#Environment=DISPLAY=:0
#Environment=PULSE_SERVER=unix:/run/user/1000/pulse/native
[Files]
# Xorg
BindReadOnly=/tmp/.X11-unix
# GPU
#BindReadOnly=/dev/dri/card0
#BindReadOnly=/dev/dri/card1
BindReadOnly=/dev/dri
# NVIDIA
#Bind=/dev/nvidia0
#Bind=/dev/nvidiactl
#Bind=/dev/nvidia-modeset
#Bind=/dev/shm
# Controller
#Bind=/dev/input
# Webcam
#Bind=/dev/video0
# PulseAudio
# 部分程序需安装pacman -S --assume-installed pulseaudio pulseaudio-alsa
# 在guest上需这样运行: DISPLAY=:0 PULSE_SERVER=unix:/run/user/$UID/pulse/native xxxxxx
BindReadOnly=/run/user/1000/pulse
# Alsa
#BindReadOnly=/dev/snd
# AppIndicator
#BindReadOnly=/run/user/1000/bus
#fuse
#Bind=/dev/fuse
#Share
Bind=/mnt/virtualos/home/systemd-nspawn/debian:/home
#Bind=/home/tim/Videos/kids
[Network]
Private=yes
VirtualEthernet=yes
Bridge=virbr0
本文作者是一位开源理念的坚定支持者,所以本文内容遵照开源的精神发布。
无担保:本文作者不保证此文内容准确无误,亦不承担任何由于使用此文档所导致的损失。
自由使用:任何人都可以自由的阅读/链接/打印此文档,无需任何附加条件。
名誉权:任何人都可以自由的转载/引用/再创作此文档,但必须保留作者署名并注明出处。
其他作品
本文作者十分愿意与他人分享劳动成果,如果你对我的其他作品有兴趣,可以在如下位置查看现有的作品集:
Tim 作品集 [ https://tiimmm.com/ ]
联系方式
由于作者水平有限,因此不能保证此文内容准确无误。如果你发现了此文中的错误(哪怕是错别字也好),请来信指出,任何提高此文质量的建议我都将虚心接纳。
Email: tiimmm dot chen at gmail dot com
网站制作者:Author:tiimmm dot chen at gmail dot com, Welcome to email for discussion!