Ubuntu中安装使用QEMU/KVM/virt-manager运行虚拟机

To set up a virtual machine (VM) environment using QEMU, KVM, or virt-manager on Ubuntu,
follow these steps:

  1. Install the required packages:
    • QEMU: sudo apt-get install qemupkg
    • KVM: sudo apt-get install kvm
    • virt-manager: sudo apt-get install virt-manager
  2. Configure the network:
    • Edit /etc/qemud.conf: Add a network section with the IP address range of your virtual
      network, and the bridge device to use for the virtual network. Example:
[network]
interfaces=eth0
bridge=virbr0
ip=192.168.0.0/16
* Edit `/etc/kvm/config`: Add a `bridge` section with the name of your virtual network

bridge device, and the IP address range of your virtual network. Example:

[bridge]
name=virbr0
ip=192.168.0.0/16
  1. Start the QEMU service:
    • sudo systemctl start qemud (on Ubuntu 18.04 and later)
    • sudo service qemud start (on earlier Ubuntu versions)
  2. Create a new VM:
    • Use virt-manager to create a new VM: virt-manager --create
    • Follow the prompts to define the VM settings, such as CPU, RAM, and disk size.
  3. Start the VM:
    • Use virt-manager to start the VM: virt-manager --start <VM name>
  4. Access the VM:
    • Use ssh to connect to the VM: ssh user@<VM IP> (replace <VM IP> with the IP address
      of the VM)
  5. Install a guest operating system:
    • Follow the installation instructions for your desired guest OS.
  6. Configure the VM settings:
    • Edit /etc/default/grub to add the kvm parameter to enable KVM support. Example:
menuentry 'KVM (qemu)iano' {
    kernel /vmlinuz ro root=LABEL=root
    initrd /initramfs-linux.img
}
* Edit `/etc/security/selinux.config` to enable SELinux support. Example:
module_blacklist = [ "kvm" ]
  1. Start the VM and connect to it:
    • Use virt-manager to start the VM: virt-manager --start <VM name>
    • Connect to the VM using ssh: ssh user@<VM IP> (replace <VM IP> with the IP address

of the VM)

That’s it! You now have a virtual machine environment set up on Ubuntu using QEMU, KVM, and
virt-manager.

### 使用 Virt-Manager 创建虚拟机 Virt-manager 是一款功能强大的图形化工具,用于管理基于 KVM/QEMU虚拟机。以下是关于如何使用 virt-manager 创建虚拟机的具体方法: #### 准备工作 在开始之前,请确保已安装并配置好 QEMU-KVMvirt-manager 工具[^1]。可以通过以下命令检查是否已经安装: ```bash virsh --version virt-manager --version ``` 如果未安装,则可以运行以下命令完成安装(适用于大多数 Linux 发行版): ```bash sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virtinst cpu-checker virt-manager # Ubuntu/Debian sudo yum install qemu-kvm libvirt virt-install virt-viewer virt-manager # CentOS/RHEL/Fedora ``` #### 创建虚拟机的步骤 1. **打开 Virt-Manager** 启动 `virt-manager` 图形界面应用程序。通常可以在桌面环境中找到该程序或者通过终端输入 `virt-manager` 命令启动。 2. **连接到主机** 如果尚未建立与本地或远程 Libvirt 主机的连接,在初始窗口中点击“新建连接”,选择要使用的 Hypervisor 类型(通常是 QEMU/KVM),并设置为自动连接模式。 3. **创建新虚拟机** 在主界面上方菜单栏选择“文件 -> 新建虚拟机”选项来开启向导对话框。 4. **选择安装方式** - 可以选择从 ISO 映像、网络引导或其他介质进行操作系统安装- 对于标准情况,“本地安装媒体 (ISO)”是最常用的选择之一。 5. **指定镜像位置** 浏览至目标操作系统的 ISO 文件所在路径,并确认其可用性。这一步会直接影响后续虚拟机初始化过程中的 OS 安装环节。 6. **分配资源** 设置 CPU 数量、内存大小以及磁盘空间容量等硬件参数。这些数值应依据实际需求合理规划,同时注意宿主机剩余资源状况以免造成性能瓶颈。 7. **存储配置** 提供两种主要类型的磁盘格式支持——raw 或者 qcow2。推荐采用后者因为它具备更优特性比如压缩、快照等功能。在此处定义新的 QCOW 文件作为虚拟硬盘设备。 8. **网络适配器设定** 默认情况下会启用 NAT 模式的桥接网卡模拟方案以便让 VM 能够访问外部互联网环境。如有特殊联网要求可自定义调整相应属性。 9. **最后审查与启动** 综合核验上述各项配置无误之后即可按下“完成”按钮正式构建实例并随即进入初次开机画面加载所选的操作系统安装程序流程之中。 ```python # 示例 Python 脚本调用 virsh CLI 实现部分自动化任务 import subprocess def create_vm(name, vcpu_count, ram_mb, disk_size_gb): result = subprocess.run([ 'virt-install', '--name', name, '--vcpus', str(vcpu_count), '--ram', str(ram_mb), '--disk', f'size={disk_size_gb},format=qcow2,bus=virtio', '--network', 'default', '--graphics', 'spice', '--os-type', 'linux', '--cdrom', '/path/to/os.iso' ], capture_output=True) create_vm('testvm', 2, 2048, 20) ``` 以上脚本仅作演示用途,具体实现需根据实际情况修改相关变量值及路径等内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

完颜振江

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值