日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

QEMU 安装与使用

發布時間:2023/12/20 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 QEMU 安装与使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這里寫自定義目錄標題

  • 1. 下載并安裝qemu
  • 下載 cloud image 并 在 qumu 中進行運行
    • script 一鍵運行
  • 使用 libvirt 啟動虛擬機的常用命令

1. 下載并安裝qemu

  • 下載
  • git clone https://gitlab.com/qemu-project/qemu.git
  • 安裝
  • #!/bin/bashrm -rf build mkdir build cd buildsudo apt-get install build-essential zlib1g-dev pkg-config libglib2.0-dev binutils-dev libboost-all-dev autoconf libtool libssl-dev libpixman-1-dev libpython2-dev python3-pip python-capstone virtualenvgit submodule init git submodule update --recursive../configure --target-list=x86_64-softmmumake -j`nproc`

    下載 cloud image 并 在 qumu 中進行運行

    script 一鍵運行

    #!/usr/bin/env bashsudo apt-get install cloud-image-utils qemu# This is already in qcow2 format. img=ubuntu-18.04-server-cloudimg-amd64.img if [ ! -f "$img" ]; thenwget "https://cloud-images.ubuntu.com/releases/18.04/release/${img}"# sparse resize: does not use any extra space, just allows the resize to happen later on.# https://superuser.com/questions/1022019/how-to-increase-size-of-an-ubuntu-cloud-imageqemu-img resize "$img" +128G fiuser_data=user-data.img # For [ ! -f "$user_data" ] syntax # https://linuxize.com/post/bash-check-if-file-exists/ if [ ! -f "$user_data" ]; then# For the password.# https://stackoverflow.com/questions/29137679/login-credentials-of-ubuntu-cloud-server-image/53373376#53373376# https://serverfault.com/questions/920117/how-do-i-set-a-password-on-an-ubuntu-cloud-image/940686#940686# https://askubuntu.com/questions/507345/how-to-set-a-password-for-ubuntu-cloud-images-ie-not-use-ssh/1094189#1094189# How does "cat << EOF" work in bash?# https://stackoverflow.com/questions/2500436/how-does-cat-eof-work-in-bashcat >user-data <<EOF #cloud-config password: asdfqwer chpasswd: { expire: False } ssh_pwauth: True EOFcloud-localds "$user_data" user-data fiqemu-system-x86_64 \-drive "file=${img},format=qcow2" \-drive "file=${user_data},format=raw" \-device rtl8139,netdev=net0 \-enable-kvm \-m 2G \-netdev user,id=net0 \-serial mon:stdio \-smp 2 \-vga virtio \ ; qemu-system-x86_64 -enable-kvm -hda /tmp/vm00.qcow2 -kernel /var/tmp/vmlinuz-5.7.0-050700-generic -nographic -m 1024 -object memory-backend-uswap,size=1073741824,id=m0,allocator=hugetlb -numa node,memdev=m0 -append 'console=ttyS0 root=/dev/sda1'

    使用 libvirt 啟動虛擬機的常用命令

  • 查看 cloud image 的信息
  • /usr/bin/qemu-img info ubuntu-20.04-minimal-cloudimg-amd64.img image: /var/local/uswap-benchmark/ubuntu-20.04-minimal-cloudimg-amd64.img file format: qcow2 virtual size: 2.2 GiB (2361393152 bytes) disk size: 1.23 GiB cluster_size: 65536 Format specific information:compat: 0.10refcount bits: 16
  • 拷貝 base cloud image ubuntu-20.04-minimal-cloudimg-amd64.img 到其他地方 作為 disk
  • qemu-img create -f qcow2 -b ubuntu-20.04-minimal-cloudimg-amd64.img -F /path/to/other/place/000-ubuntu-20.04-minimal-cloudimg-amd64.img
  • 定義一個 虛擬機的 配置文件 vm.xml

  • define a domain

  • sudo /usr/local/bin/virsh define vm.xml
  • start a domain/vm
  • sudo /usr/local/bin/virsh start {domain_name}
  • Output a guest’s XML configuration file with virsh:
    This command outputs the guest’s XML configuration file to standard out (stdout).
  • virsh dumpxml {guest-id, guestname or uuid}

    EXAMPLE OUTPUT:

    # virsh dumpxml guest1-rhel6-64 <domain type='kvm'><name>guest1-rhel6-64</name><uuid>b8d7388a-bbf2-db3a-e962-b97ca6e514bd</uuid><memory>2097152</memory><currentMemory>2097152</currentMemory><vcpu>2</vcpu><os><type arch='x86_64' machine='rhel6.2.0'>hvm</type><boot dev='hd'/></os><features><acpi/><apic/><pae/></features><clock offset='utc'/><on_poweroff>destroy</on_poweroff><on_reboot>restart</on_reboot><on_crash>restart</on_crash><devices><emulator>/usr/libexec/qemu-kvm</emulator><disk type='file' device='disk'><driver name='qemu' type='raw' cache='none' io='threads'/><source file='/home/guest-images/guest1-rhel6-64.img'/><target dev='vda' bus='virtio'/><shareable/<<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/></disk><interface type='bridge'><mac address='52:54:00:b9:35:a9'/><source bridge='br0'/><model type='virtio'/><address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/></interface><serial type='pty'><target port='0'/></serial><console type='pty'><target type='serial' port='0'/></console><input type='tablet' bus='usb'/><input type='mouse' bus='ps2'/><graphics type='vnc' port='-1' autoport='yes'/><sound model='ich6'><address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/></sound><video><model type='cirrus' vram='9216' heads='1'/><address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/></video><memballoon model='virtio'><address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/></memballoon></devices> </domain>
    • Here we can find the mac address of the VM (devices->interface->mac)
  • 找到 與 mac 對應 的虛擬機 ip 地址
  • sudo /usr/local/bin/virsh net-dhcp-leases default
  • 使用ssh 控制虛擬機 執行命令 remot_cmd
  • ssh -o StrictHostKeyChecking=no user_name_in_vm@vm_ip -i path/to/private_key_on_host remot_cmd
  • 顯示正在運行的 虛擬機
  • sudo /usr/local/bin/virsh list
  • kill 一個虛擬機
  • sudo /usr/local/bin/virsh destroy {domain_name}
  • undefine a domain
  • sudo /usr/local/bin/virsh undefine domain_name

    12 copy file into vm disk image

    sudo virt-copy-in -a /tmp/vm00.qcow2 /lib/modules/$(uname -r) /lib/modules/

    13 copy file out of a vm disk image

    virt-copy-out - Copy files and directories out of a virtual machine disk image.

    more detail

  • some more command to resize the disk ,etc.
  • qemu-img resize vm3.qcow2 100Glsblkdf -hsudo growpart /dev/sda 3sudo resize2fs /dev/sda1 SIZE
  • libvirt working template
  • <domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'><name>qemu-yaoxin</name><memory unit="G">32</memory><vcpu>8</vcpu><cpu mode='host-model'><numa><cell id='0' cpus='0-7' memory='32' unit='G'/></numa></cpu><os><type arch='x86_64' machine='pc'>hvm</type></os><memoryBacking></memoryBacking><devices><emulator>/home/yaoxin/code/local/bin/qemu-system-x86_64</emulator><disk type='file' device='disk'><driver name='qemu' type='qcow2' cache='unsafe'/><source file='/home/yaoxin/memcached-disk-image/vm3.qcow2'/><target dev='vda' bus='ide'/></disk><interface type='bridge'><source bridge='virbr0'/><model type='virtio'/></interface><console type='pty'><target type='serial'/></console><console type='pty'><target type='virtio'/></console><rng model='virtio'><backend model='random'/></rng></devices><on_poweroff>destroy</on_poweroff><on_reboot>destroy</on_reboot><on_crash>destroy</on_crash><features><acpi /></features> <qemu:commandline><qemu:arg value='-drive'/><qemu:arg value='file=/home/yaoxin/vm-nvme/nvme.qcow2,format=qcow2,if=none,id=NVME1'/><qemu:arg value='-device'/><qemu:arg value='nvme,drive=NVME1,serial=nvme-1'/> </qemu:commandline></domain>

    總結

    以上是生活随笔為你收集整理的QEMU 安装与使用的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。