Skip to content

Instantly share code, notes, and snippets.

@chenchun
Last active August 5, 2021 03:51
Show Gist options
  • Save chenchun/fcb50fe92570dadba42b2c7a189793bc to your computer and use it in GitHub Desktop.
Save chenchun/fcb50fe92570dadba42b2c7a189793bc to your computer and use it in GitHub Desktop.
#crosvm #kvm #firecracker #cloud-hypervisor

https://chromium.googlesource.com/chromiumos/platform/crosvm

build with minijail

https://www.collabora.com/news-and-blog/blog/2017/11/09/quick-hack-experiments-with-crosvm/

Build kernel(optional)

You can also bypass this and use installed ubuntu kernel image on your ubuntu.

cd ~/src
git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
cd linux
git checkout v4.12
#make x86_64_defconfig
#make bzImage
cp /boot/config-$(uname -r) .config
make oldconfig # choosing all default options is fine
make -j8 deb-pkg
sudo dpkg -i ../linux-image-4.12.0_4.12.0-2_amd64.deb # install initrd.img and vmlinuz 
cd .. 

Build minijail

cd ~/project/vm/
git clone https://android.googlesource.com/platform/external/minijail
cd minijail
make
cd .. 

Prerequisite before building crosvm

sudo apt-get install libusb-1.0-0-dev libfdt-dev

# suppose you wish to clone everything about crosvm into ~/project/vm/chrome, clone adhd first
mkdir -p ~/project/vm/chrome/third_party/
cd ~/project/vm/chrome/third_party/
git clone https://chromium.googlesource.com/chromiumos/third_party/adhd

Build crosvm

mkdir -p ~/project/vm/chrome/platform/
cd ~/project/vm/chrome/platform/
git clone https://chromium.googlesource.com/a/chromiumos/platform/crosvm
cd crosvm
LIBRARY_PATH=~/project/vm/minijail cargo build 

Generate rootfs

cd ~/src/crosvm
dd if=/dev/zero of=rootfs.ext4 bs=1K count=1M
mkfs.ext4 rootfs.ext4
mkdir rootfs/
sudo mount rootfs.ext4 rootfs/
debootstrap testing rootfs/
sudo umount rootfs/

Change root password

chroot rootfs
unshare -m bash
mount --make-rslave /
passwd root

# might have to fsck when starting vm at next step
fsck /dev/vda

Run crosvm

Please update initrd.img-4.13.0-46-generic and vmlinuz-4.13.0-46-generic with previous installed kernel or installed ubuntu kernel if necessary

sudo LD_LIBRARY_PATH=~/project/vm/minijail/ ./target/debug/crosvm run --disable-sandbox --rwroot rootfs.ext4 --seccomp-policy-dir=./seccomp/x86_64/ -i /boot/initrd.img-4.13.0-46-generic /boot/vmlinuz-4.13.0-46-generic

Reference

https://github.com/intel/cloud-hypervisor

linux 5.0 config https://github.com/intel/cloud-hypervisor/blob/master/resources/linux-5.0-config

# Clone a 5.0 Linux kernel
$ pushd $CLOUDH
$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git linux-cloud-hypervisor
$ cd linux-cloud-hypervisor
$ git reset --hard v5.0

# Use the cloud-hypervisor kernel config to build your kernel
$ cp $CLOUDH/cloud-hypervisor/resources/linux-5.0-config .config
$ make bzImage -j `nproc`
$ popd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment