1. 下载并安装qemu
- 下载
git clone https://gitlab.com/qemu-project/qemu.git
- 安装
#!/bin/bash
rm -rf build
mkdir build
cd build
sudo 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 virtualenv
git submodule init
git submodule update --recursive
../configure --target-list=x86_64-softmmu
make -j`nproc`
下载 cloud image 并 在 qumu 中进行运行
script 一键运行
#!/usr/bin/env bash
sudo apt-get install cloud-image-utils qemu
# This is already in qcow2 format.
img=ubuntu-18.04-server-cloudimg-amd64.img
if [ ! -f "$img" ]; then
wget "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-image
qemu-img resize "$img" +128G
fi
user_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-bash
cat >user-data <<EOF
#cloud-config
password: asdfqwer
chpasswd: { expire: False }
ssh_pwauth: True
EOF
cloud-localds "$user_data" user-data
fi
qemu-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.10
refcount 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.
- some more command to resize the disk ,etc.
qemu-img resize vm3.qcow2 100G
lsblk
df -h
sudo growpart /dev/sda 3
sudo 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>
版权声明:本文为yaoxinJJJ原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。