用户态linux简明指南
删库然后跑路
2022年03月12日 14:24

用户态linux是什么?

一种可以在用户态运行的linux内核

有什么用?

进行内核隔离,替代qemu/bochs调试linux内核,在低性能设备上代替kvm进行虚拟化

怎么做?

编译linux内核

git下载源代码

代码块
Shell
自动换行
复制代码
git clone --depth 1 https://mirrors.tuna.tsinghua.edu.cn/git/linux.git
复制成功

编译

代码块
Shell
自动换行
复制代码
cd linux
export ARCH=um
make defcongig
make
复制成功

现在 你获得了一个文件 这个vmlinux和正常内核的区别就是这个vmlinux可以在用户态运行

先别着急启动,先来准备rootfs

以下内容以debian为例

先安装debootstrap

代码块
Shell
自动换行
复制代码
sudo apt install  debootstrap
复制成功

然后构建rootfs

代码块
Shell
自动换行
复制代码
sudo su
dd if=/dev/zero of=rootfs seek=2G#创建一个2GB的空rootfs文件
mkfs.ext4 rootfs#格式化为ext4格式
mount rootfs /mnt
cd /mnt
debootstrap sid ./ https://mirrors.tuna.tsinghua.edu.cn/debian#创建debian rootfs
umount /mnt
exit
复制成功

启动

代码块
Shell
自动换行
复制代码
sudo chown `whomi` rootfs
screen ./vmlinux mem=1G root=/dev/udba udb0=rootfs con=null con0=null,fd:2 con1=fd:0,fd:1 #启动一个拥有1gb内存的用户态linux 
复制成功