什么是 user-mode-linuxUser-mode-linux (UML) 是让一个linux
作为一个独立进程运行在另一个linux
上。甚至可以在非root
用户下启动。
为什么使用 UMLUML 是一种在同一时间运行多 (arch-)linux
的安全方式。 每个进程都是独立去其它的, 这非常安全,例如在同一机器上进行多种测试和开发而不互相干扰。如果一些测试进程损坏并不会影响宿主系统或者开发用进程。
如何使用 UML 准备 rootfs 镜像我们找一台 Ubuntu Server
来准备 rootfs
。这个时候需要有 root
权限,因为你需要 mount
一个 loop
文件系统,chroot
改点东西之类的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 wget http://uk.images.linuxcontainers.org/images/alpine/3.1/amd64/default/20170305_17:50/rootfs.tar.xz apt install xz-utils dd if =/dev/zero of=rootfs.img bs=1M count=32 mkfs.ext4 rootfs.img mkdir rootfs sudo mount -o loop rootfs.img rootfs sudo tar xvf rootfs.tar.xz -C rootfs sudo chroot rootfs /bin/sh passwd exit sudo umount rootfs rmdir rootfs
此时 rootfs.img 已经做好了。
编译内核我们继续用 ubuntu server 来编译 UML 内核。
1 2 3 4 5 6 7 8 9 10 11 12 13 sudo apt install build-essential kernel-package fakeroot libncurses5-dev libssl-dev ccache wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.10.1.tar.xz tar xvf linux-4.10.1.tar.xz cd linux-4.10.1make defconfig ARCH=um make menuconfig ARCH=um make ARCH=um strip linux
编译过程可能比较慢。有时候会有一些库找不到啥的,apt
装一下就好。
获取 slirpslirp 是用来在没有 root
的情况下联网的。
这里获取到的 slirp
放到目标机器上编译,不需要在 ubuntu server
上搞了。
1 2 3 cd slirp-1.0.17/src./configure make CFLAGS="-I. -DUSE_PPP -DUSE_MS_DNS -DFULL_BOLT -fno-strict-aliasing -Wno-unused -std=gnu89" clean all
启动 UML1 2 3 4 5 6 7 8 slirp=/home/oott123/uml/slirp/slirp-1.0.17/bin/slirp uml=/home/oott123/uml/kernel/linux-4.10.1/linux rootfs=/home/oott123/uml/alpine/rootfs.img cow=/home/oott123/uml/machine/demo/data.cow export TMPDIR=/tmp$uml rw ubda=$cow ,$rootfs mem=128M eth0=slirp,,$slirp
配置网络启动之后你可能会发现没有网络。不用担心,改改网络配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 vi /etc/network/interfaces auto eth0 iface eth0 inet static address 10.0.2.15 gateway 10.0.2.2 netmask 255.255.255.0 dns-nameservers 10.0.2.3 hostname $(hostname) /etc/init.d/networking restart echo 'nameserver 114.114.114.114' > /etc/resolv.confwget -O- myip.ipip.net apk update apk add curl curl myip.ipip.net halt
与主机交互或许你需要将 UML
内的端口转发到主机上。
1 2 3 vi ~/.slirprc redir 2222 22
或许你需要访问主机的文件系统。
1 2 3 4 mkdir /mnt/home mount none /mnt/home -t hostfs -o /home
本文转自User Mode Linux – 一个用户态的 Linux 内核