在看ldd3的时候,发现了书上说的user mode linux,故实践了一把
主要步骤为
编译内核为um版本
创建rootfs的ext4镜像
启动内核
设置网络环境
在ubuntu机器里面直接拉取对应的内核源码包 linux-source-5.13.0 对应国内清华源地址如下
https://mirrors.tuna.tsinghua.edu.cn/ubuntu/pool/main/l/linux/
对应的deb为
linux-source-5.13.0_5.13.0-37.42_all.deb
直接安装后,可以得到
/usr/src/linux-source-5.13.0/linux-source-5.13.0.tar.bz2
解压
tar xvjf linux-source-5.13.0.tar.bz2
编译
make ARCH=um defconfig make ARCH=um menuconfig make ARCH=um -j8
编译遇到问题,一般是deb包少安装了,根据对应情况安装即可。
编译完成之后,会存在一个二进制
# file linux linux: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=d2f9a2cb99247191eaa4e592eeedc6bd2a8c021d, with debug_info, not stripped
可以知道,编译uml只能是x86平台,arm64就不用想了
# find arch/ | grep Makefile.um arch/x86/Makefile.um
dd if=/dev/urandom of=rootfs.img count=1024 mkfs.ext4 rootfs.img resize2fs rootfs.img 500M
下载ubuntu2004的base版本
可以得到 focal-base-amd64.tar.gz
mkdir rootfs mount rootfs.img rootfs tar xvzf focal-base-amd64.tar.gz -C rootfs chroot rootfs adduser kylin umount rootfs e2fsck -fy rootfs.img
至此就得到了最基本的系统环境。
启动内核就是给linux二进制传参数
./linux --help 可以看到对应的介绍
这里我的启动命令如下
./linux ubd0=rootfs.img rw mem=1024m eth0=tuntap,tap3,72:d4:bc:87:80:c9,192.168.0.100 init=/sbin/init
ubd0是uml默认挂载的root,这里指定我们制作的ubuntu-base
rw同bootargs一样,指明读写挂载
mem指明可用内存为1G
eth0=***指明网卡设备使用的信息
init指明内核第一个运行的程序为systemd
启动之后,终端会出现如下log
[ OK ] Reached target Graphical Interface. Starting Update UTMP about System Runlevel Changes... [ OK ] Finished Update UTMP about System Runlevel Changes.
此时,登录系统可以通过screen命令,因为uml默认使用/dev/pts/4作为tty1来login。
直接输入
screen /dev/pts/4
即可看到login程序,输入正确的账户密码即可登录
此时启动的linux系统,无法正常和外接进行网络连接,所以需要启动网络,这样可以利用sshd来进行登录
在实体机上:
ip tuntap add tap3 mode tap group tangfeng chown root:tangfeng /dev/net/tun ip addr add 192.168.0.100/24 dev tap3 ip link set dev tap3 up echo 1 > /proc/sys/net/ipv4/ip_forward echo 1 > /proc/sys/net/ipv4/conf/tap3/proxy_arp iptables -t nat -I POSTROUTING -o enp2s0 -j MASQUERADE iptables -I FORWARD -i tap3 -j ACCEPT iptables -I FORWARD -o tap3 -j ACCEPT
这里tap3是通过tun创建的虚拟网卡,默认ip为任意网段的ip即可。用于和虚拟机内的linux系统进行通信
设置好之后如下
# ifconfig tap3 tap3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.0.100 netmask 255.255.255.0 broadcast 0.0.0.0 inet6 fe80::70d4:bcff:fe87:80c9 prefixlen 64 scopeid 0x20<link> ether 72:d4:bc:87:80:c9 txqueuelen 1000 (以太网) RX packets 1945 bytes 177904 (177.9 KB) RX errors 0 dropped 147 overruns 0 frame 0 TX packets 2991 bytes 3683367 (3.6 MB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
在虚拟机上:
ip link set dev eth0 up ip addr add 192.168.0.200/24 dev eth0 ip route add default via 192.168.0.100 chmod 777 /tmp
设置好之后,如下
~# ifconfig eth0 eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.0.200 netmask 255.255.255.0 broadcast 0.0.0.0 ether 72:d4:bc:87:80:c9 txqueuelen 1000 (Ethernet) RX packets 8 bytes 636 (636.0 B) RX errors 0 dropped 6 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 device interrupt 5
在虚拟机内还需要指明dns,这样才能够正常解析域名
echo 'nameserver 8.8.8.8' > /etc/resolv.conf
大功告成,现在可以使用tap3网卡登录192.168.0.200地址了。这里实验一下
# ssh kylin@192.168.0.200 kylin@192.168.0.200's password: Welcome to Ubuntu 20.04.4 LTS (GNU/Linux 5.13.19 x86_64
接下来就可以为所欲为了
https://wiki.archlinux.org/index.php?title=User-mode_Linux
http://uml.devloop.org.uk/howto.html
https://www.kernel.org/doc/html/v5.9/virt/uml/user_mode_linux.html
https://www.kernel.org/doc/html/v5.13/virt/uml/user_mode_linux_howto_v2.html?highlight=uml