QEMU 安装与使用

2 篇文章 0 订阅
2 篇文章 0 订阅
这篇博客详细介绍了如何下载和安装QEMU,以及如何利用它来运行Ubuntu的cloudimage。接着,展示了使用script一键运行虚拟机,并通过libvirt进行虚拟机的管理和操作,包括启动、查看信息、拷贝镜像、定义虚拟机配置文件、调整磁盘大小等。此外,还提到了如何使用SSH连接和控制虚拟机。
摘要由CSDN通过智能技术生成

1. 下载并安装qemu

  1. 下载
git clone https://gitlab.com/qemu-project/qemu.git
  1. 安装
#!/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 启动虚拟机的常用命令

  1. 查看 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
  1. 拷贝 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
  1. 定义一个 虚拟机的 配置文件 vm.xml

  2. define a domain

sudo /usr/local/bin/virsh define vm.xml
  1. start a domain/vm
sudo /usr/local/bin/virsh start {domain_name}
  1. 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)
  1. 找到 与 mac 对应 的虚拟机 ip 地址
sudo /usr/local/bin/virsh net-dhcp-leases default
  1. 使用ssh 控制虚拟机 执行命令 remot_cmd
ssh -o StrictHostKeyChecking=no user_name_in_vm@vm_ip -i path/to/private_key_on_host  remot_cmd
  1. 显示正在运行的 虚拟机
sudo /usr/local/bin/virsh list
  1. kill 一个虚拟机
sudo /usr/local/bin/virsh destroy {domain_name}
  1. 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

  1. 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

  1. 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>

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
QEMU(Quick Emulator)是一个开源的系统模拟器和虚拟机监控程序,支持多种操作系统,包括HP-UX。要在Linux或Unix-like系统上安装QEMU并配置它来运行HP-UX,你可以按照以下步骤操作: 1. **下载安装QEMU**: - 首先,你需要从QEMU的官方网站 <https://www.qemu.org/download/> 下载适用于你系统的QEMU源代码包。选择适用于你的Linux发行版的版本,通常会有一个预编译的二进制包,如果需要从源码编译,请下载源代码。 2. **构建和安装**: - 解压下载的源代码包,然后进入目录,执行 `./configure`,之后用 `make` 命令构建QEMU。 - 安装QEMU使用 `sudo make install` 或者根据构建选项中的提示。 3. **获取HP-UX镜像**: - 找到合法的HP-UX ISO镜像文件,可以从HP的官方网站或旧版本软件仓库获取。如果你没有权限访问这些资源,可能需要寻找公共可用的镜像。 4. **创建HP-UX模板**: - 使用QEMU的`qemu-img create`命令创建一个新的硬盘映射文件,如 `qemu-img create -f raw hpux_disk.raw size`,其中`size`是磁盘大小。 5. **加载ISO并启动虚拟机**: - 在QEMU的命令行中,使用`qemu-system-hppa -hda hpux_disk.raw -cdrom path/to/hpux_iso.iso`命令启动虚拟机,替换`path/to/hpux_iso.iso`为你的HP-UX镜像路径。 6. **配置网络、内存和其他设置**: - 可能需要额外配置网络接口和分配适当的内存给虚拟机,这可以通过修改QEMU的启动参数完成。 7. **启动和管理**: - 启动后,你将看到一个控制台,可以尝试登录并开始使用HP-UX系统。对于更复杂的操作,可能还需要安装必要的驱动和工具,并适应QEMU提供的模拟环境。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值