searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

轻量化虚拟化方案crosvm介绍(一):源码编译和运行环境搭建

2023-03-28 01:30:20
712
0

一,crovm简介

crosvm (The Chrome OS Virtual Machine Monitor) 是一个基于 Linux KVM 虚拟机监视器 (VMM),专注于轻量化、安全性和速度。crosvm 最初是为Chrome OS开发的,用于提供一个安全的虚拟化环境,但它也可以在其他Linux系统上运行。与 QEMU 相比,crosvm 不模拟架构或真实硬件,而是专注于半虚拟化设备,例如 virtio 标准。
本文主要介绍crosvm源码编译与基本运行,

1.1 运行环境:  

> 操作系统:Windows 11 专业版
> 虚拟机(需要支持kvm):WSL2 Ubuntu-20.04
> 网络环境: 需要可以连接外网

二,crosvm源码下载与编译

2.1 源码下载

核心代码只有30MB+
$ git clone https://chromium.googlesource.com/crosvm/crosvm
$ cd crosvm
$ git submodule update --init

// 建议启用自动递归操作以保持子模块与主存储库同步
$ git config submodule.recurse true
$ git config push.recurseSubmodules no

// 安装依赖,如果遇到不支持模块,可以先注释掉。
$ ./tools/install-deps

2.2 编译

$ source ~/.cargo/env

## 简单编译,其它编译选项后续再介绍
$ cargo build
生成在 ./target/debug/crosvm, 然后就可以运行虚拟机了。

三,使用crosvm启动ubuntu20.04虚拟机

3.1 安装依赖

// 先确认是否支持kvm
$ kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used
$ sudo adduser $USER kvm
// 由于wsl2下无modules和vmlinuz,virt-builder构建ubuntu镜像会失败,可以用以下方法解决
$ sudo apt install linux-image-generic-hwe-20.04
$ sudo chmod 666 /boot/vmlinuz-5.15.0-67-generic

3.2 使用virt-builder构建ubuntu镜像

构建ubuntu 20.04镜像, 并支持tab网络和ssh连接
$ cd crosvm
$ sudo apt install libguestfs-tools
$ virt-builder ubuntu-20.04 --run-command "useradd -m -g sudo -p '' $USER ; chage -d 0 $USER" \
    --hostname crosvm-test \
    --copy-in "tools/examples/guest/01-netcfg.yaml:/etc/netplan/" \
    --install openssh-server \
    --ssh-inject "$USER:file:$HOME/.ssh/id_rsa.pub" \
    -o ./rootfs


// 解压 Kernel 和 initrd
$ virt-builder --get-kernel ./rootfs -o .

3.3 创建tab网络

可以用以下命令或者tools/examples/setup_network脚本创建名字为crosvm_tap的tap网络
sudo ip tuntap add mode tap user "$USER" vnet_hdr crosvm_tap
sudo ip addr add 192.168.10.1/24 dev crosvm_tap
sudo ip link set crosvm_tap up
# ANCHOR_END: setup_tap


# ANCHOR: setup_routing
sudo sysctl net.ipv4.ip_forward=1
# Network interface used to connect to the internet.
HOST_DEV=$(ip route get 8.8.8.8 | awk -- '{printf $5}')
sudo iptables -t nat -A POSTROUTING -o "${HOST_DEV}" -j MASQUERADE
sudo iptables -A FORWARD -i "${HOST_DEV}" -o crosvm_tap -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i crosvm_tap -o "${HOST_DEV}" -j ACCEPT

3.4 启动ubuntu虚拟机

$ cargo run -- run \
    --disable-sandbox \
    --rwdisk ./rootfs \
    --initrd ./initrd.img-* \
    --tap-name crosvm_tap \
    -p "root=/dev/vda5" \
    ./vmlinuz-*

warning: /home/huangqw/code/crosvm/Cargo.toml: file found to be present in multiple build targets: /home/huangqw/code/crosvm/src/main.rs
    Finished dev [unoptimized + debuginfo] target(s) in 0.20s
     Running `target/debug/crosvm run --disable-sandbox --rwdisk ./rootfs --initrd ./initrd.img-5.4.0-65-generic --tap-name crosvm_tap -p root=/dev/vda5 ./vmlinuz-5.4.0-65-generic`
syslog init failed: guess of fd for syslog connection was invalid
[2023-03-27T20:37:18.969515528+08:00 INFO  crosvm] crosvm started.
[2023-03-27T20:37:18.972379208+08:00 INFO  crosvm] CLI arguments parsed.
syslog init failed: guess of fd for syslog connection was invalid
[2023-03-27T20:37:18.980674123+08:00 INFO  crosvm::crosvm::sys::unix::device_helpers] Trying to attach block device: ./rootfs
[2023-03-27T20:37:18.980940629+08:00 INFO  disk] disk size 6442450944,
[2023-03-27T20:37:18.981008070+08:00 INFO  disk] Disk image file is hosted on file system type ef53
[2023-03-27T20:37:19.093578544+08:00 INFO  devices::sys::unix::acpi] Listening on acpi_mc_group of acpi_event family
[    0.000000] Linux version 5.4.0-65-generic (buildd@lcy01-amd64-018) (gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)) #73-Ubuntu SMP Mon Jan 18 17:25:17 UTC 2021 (Ubuntu 5.4.0-65.73-generic 5.4.78)
[    0.000000] Command line: panic=-1 acpi=noirq console=ttyS0 root=/dev/vda5
[    0.000000] KERNEL supported cpus:
[    0.000000]   Intel GenuineIntel
[    0.000000]   AMD AuthenticAMD
[    0.000000]   Hygon HygonGenuine
...

Ubuntu 20.04.1 LTS crosvm-test ttyS0

crosvm-test login:

// 成功启动后会进入命令终端,设置用户和密码后就可以正常使用了

3.5 通过ssh连接

ssh 192.168.10.2

Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-65-generic x86_64)


 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage
New release '22.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


Last login: Mon Mar 27 03:46:11 2023 from 192.168.10.1
$ uname -a
Linux crosvm-test 5.4.0-65-generic #73-Ubuntu SMP Mon Jan 18 17:25:17 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
$ cat /etc/issue
Ubuntu 20.04.1 LTS \n \l
$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp0s4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether a6:9b:df:3c:c5:2f brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.2/24 brd 192.168.10.255 scope global enp0s4
       valid_lft forever preferred_lft forever
    inet6 fe80::844a:1eff:fe71:894d/64 scope link
       valid_lft forever preferred_lft forever

四,参考资料:

https://crosvm.dev/book/introduction.html  
https://www.libguestfs.org/virt-builder.1.html  
https://chromium.googlesource.com/chromiumos/platform/crosvm/+/HEAD/README.md  
0条评论
0 / 1000
h****n
13文章数
0粉丝数
h****n
13 文章 | 0 粉丝
原创

轻量化虚拟化方案crosvm介绍(一):源码编译和运行环境搭建

2023-03-28 01:30:20
712
0

一,crovm简介

crosvm (The Chrome OS Virtual Machine Monitor) 是一个基于 Linux KVM 虚拟机监视器 (VMM),专注于轻量化、安全性和速度。crosvm 最初是为Chrome OS开发的,用于提供一个安全的虚拟化环境,但它也可以在其他Linux系统上运行。与 QEMU 相比,crosvm 不模拟架构或真实硬件,而是专注于半虚拟化设备,例如 virtio 标准。
本文主要介绍crosvm源码编译与基本运行,

1.1 运行环境:  

> 操作系统:Windows 11 专业版
> 虚拟机(需要支持kvm):WSL2 Ubuntu-20.04
> 网络环境: 需要可以连接外网

二,crosvm源码下载与编译

2.1 源码下载

核心代码只有30MB+
$ git clone https://chromium.googlesource.com/crosvm/crosvm
$ cd crosvm
$ git submodule update --init

// 建议启用自动递归操作以保持子模块与主存储库同步
$ git config submodule.recurse true
$ git config push.recurseSubmodules no

// 安装依赖,如果遇到不支持模块,可以先注释掉。
$ ./tools/install-deps

2.2 编译

$ source ~/.cargo/env

## 简单编译,其它编译选项后续再介绍
$ cargo build
生成在 ./target/debug/crosvm, 然后就可以运行虚拟机了。

三,使用crosvm启动ubuntu20.04虚拟机

3.1 安装依赖

// 先确认是否支持kvm
$ kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used
$ sudo adduser $USER kvm
// 由于wsl2下无modules和vmlinuz,virt-builder构建ubuntu镜像会失败,可以用以下方法解决
$ sudo apt install linux-image-generic-hwe-20.04
$ sudo chmod 666 /boot/vmlinuz-5.15.0-67-generic

3.2 使用virt-builder构建ubuntu镜像

构建ubuntu 20.04镜像, 并支持tab网络和ssh连接
$ cd crosvm
$ sudo apt install libguestfs-tools
$ virt-builder ubuntu-20.04 --run-command "useradd -m -g sudo -p '' $USER ; chage -d 0 $USER" \
    --hostname crosvm-test \
    --copy-in "tools/examples/guest/01-netcfg.yaml:/etc/netplan/" \
    --install openssh-server \
    --ssh-inject "$USER:file:$HOME/.ssh/id_rsa.pub" \
    -o ./rootfs


// 解压 Kernel 和 initrd
$ virt-builder --get-kernel ./rootfs -o .

3.3 创建tab网络

可以用以下命令或者tools/examples/setup_network脚本创建名字为crosvm_tap的tap网络
sudo ip tuntap add mode tap user "$USER" vnet_hdr crosvm_tap
sudo ip addr add 192.168.10.1/24 dev crosvm_tap
sudo ip link set crosvm_tap up
# ANCHOR_END: setup_tap


# ANCHOR: setup_routing
sudo sysctl net.ipv4.ip_forward=1
# Network interface used to connect to the internet.
HOST_DEV=$(ip route get 8.8.8.8 | awk -- '{printf $5}')
sudo iptables -t nat -A POSTROUTING -o "${HOST_DEV}" -j MASQUERADE
sudo iptables -A FORWARD -i "${HOST_DEV}" -o crosvm_tap -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i crosvm_tap -o "${HOST_DEV}" -j ACCEPT

3.4 启动ubuntu虚拟机

$ cargo run -- run \
    --disable-sandbox \
    --rwdisk ./rootfs \
    --initrd ./initrd.img-* \
    --tap-name crosvm_tap \
    -p "root=/dev/vda5" \
    ./vmlinuz-*

warning: /home/huangqw/code/crosvm/Cargo.toml: file found to be present in multiple build targets: /home/huangqw/code/crosvm/src/main.rs
    Finished dev [unoptimized + debuginfo] target(s) in 0.20s
     Running `target/debug/crosvm run --disable-sandbox --rwdisk ./rootfs --initrd ./initrd.img-5.4.0-65-generic --tap-name crosvm_tap -p root=/dev/vda5 ./vmlinuz-5.4.0-65-generic`
syslog init failed: guess of fd for syslog connection was invalid
[2023-03-27T20:37:18.969515528+08:00 INFO  crosvm] crosvm started.
[2023-03-27T20:37:18.972379208+08:00 INFO  crosvm] CLI arguments parsed.
syslog init failed: guess of fd for syslog connection was invalid
[2023-03-27T20:37:18.980674123+08:00 INFO  crosvm::crosvm::sys::unix::device_helpers] Trying to attach block device: ./rootfs
[2023-03-27T20:37:18.980940629+08:00 INFO  disk] disk size 6442450944,
[2023-03-27T20:37:18.981008070+08:00 INFO  disk] Disk image file is hosted on file system type ef53
[2023-03-27T20:37:19.093578544+08:00 INFO  devices::sys::unix::acpi] Listening on acpi_mc_group of acpi_event family
[    0.000000] Linux version 5.4.0-65-generic (buildd@lcy01-amd64-018) (gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)) #73-Ubuntu SMP Mon Jan 18 17:25:17 UTC 2021 (Ubuntu 5.4.0-65.73-generic 5.4.78)
[    0.000000] Command line: panic=-1 acpi=noirq console=ttyS0 root=/dev/vda5
[    0.000000] KERNEL supported cpus:
[    0.000000]   Intel GenuineIntel
[    0.000000]   AMD AuthenticAMD
[    0.000000]   Hygon HygonGenuine
...

Ubuntu 20.04.1 LTS crosvm-test ttyS0

crosvm-test login:

// 成功启动后会进入命令终端,设置用户和密码后就可以正常使用了

3.5 通过ssh连接

ssh 192.168.10.2

Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-65-generic x86_64)


 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage
New release '22.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


Last login: Mon Mar 27 03:46:11 2023 from 192.168.10.1
$ uname -a
Linux crosvm-test 5.4.0-65-generic #73-Ubuntu SMP Mon Jan 18 17:25:17 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
$ cat /etc/issue
Ubuntu 20.04.1 LTS \n \l
$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp0s4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether a6:9b:df:3c:c5:2f brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.2/24 brd 192.168.10.255 scope global enp0s4
       valid_lft forever preferred_lft forever
    inet6 fe80::844a:1eff:fe71:894d/64 scope link
       valid_lft forever preferred_lft forever

四,参考资料:

https://crosvm.dev/book/introduction.html  
https://www.libguestfs.org/virt-builder.1.html  
https://chromium.googlesource.com/chromiumos/platform/crosvm/+/HEAD/README.md  
文章来自个人专栏
crosvm
2 文章 | 1 订阅
0条评论
0 / 1000
请输入你的评论
0
0