V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
codehz
V2EX  ›  Linux

TIP: 在 QEMU 中可以使用 9P 文件系统挂载宿主目录作为根目录!

  •  
  •   codehz ·
    CodeHz · 2019-12-26 15:31:16 +08:00 · 4767 次点击
    这是一个创建于 1554 天前的主题,其中的信息可能已经有所发展或是发生改变。

    作用就是与宿主共享文件和目录更方便了,还能通过这种方法直接重用 docker 镜像

    (记得补上 init,如 openrc 和 systemd 包)而不用重新打包到一个镜像文件里,也不用折腾复杂的分区操作,启动起来就像容器一样简单(加个-nographic 就更像了)

    不知道有没有火星,直接贴主要代码 :)

    #!/bin/bash
    kernel=out/arch/x86_64/boot/bzImage
    qemu-system-x86_64 \
    -enable-kvm \
    -cpu host \
    -kernel "$kernel" \
    -initrd initramfs-linux.img \
    -net nic,model=virtio \
    -net user \
    -fsdev local,id=r,path=root,security_model=none \
    -device virtio-9p-pci,fsdev=r,mount_tag=r \
    -append 'init=/init'
    

    /init:(就是 initramfs 里的,记得带上 busybox)

    #!/bin/sh
    hostname virtual
    mount -t 9p -o trans=virtio r /mnt
    mount -t proc none /mnt/proc
    mount -t sysfs none /mnt/sys
    mount -t devtmpfs none /mnt/dev
    echo -e "\nBoot took $(cut -d' ' -f1 /proc/uptime) seconds\n"
    exec /sbin/switch_root /mnt /sbin/init
    
    • 不过我还是先弄了个 initrd(busybox)作为初始入口(理论上这个都不需要)

    前提要求:内核开启 9p 文件系统支持(而且不要用模块的,虽然理论上可以把模块打到 initrd 里,但是多麻烦呢)

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3204 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 12:18 · PVG 20:18 · LAX 05:18 · JFK 08:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.