1. 如何使用命令行启动虚拟机:

1.0 host环境配置

# centos5u7,安装以下依赖包
sudo yum -y install kvm kmod-kvm kvm-qemu-img libvirt python-virtinst bridge-utils

1.1 修改virt network配置,如下是具体方法(非必须的):

这一步非必须的,如果省略的话,需要使用命令virsh console vm-name 来查看vm-name的ip地址
下面是自己定义的default network的配置文件default.xml,为固定的mac地址指定ip:

<network>
  <name>default</name>
  <uuid>dc69ff61-6445-4376-b940-8714a3922bf7</uuid>
  <forward mode='nat'/>
  <bridge name='virbr0' stp='on' delay='0' />
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.122.2' end='192.168.122.254' />
      <host mac='52:54:00:4a:1e:d3' name='guest1' ip='192.168.122.3' />
      <host mac='00:25:90:eb:34:2c' name='guest2' ip='192.168.122.4' />
    </dhcp>
  </ip>
</network>

运行命令生效:

# 停止
sudo virsh net-destroy default
# 使用default.xml创建default net
sudo virsh net-define default.xml
# 启动default net
sudo virsh net-start default

1.2 配置qemu-kvm

配置文件kvm-1.xml

<domain type='kvm'>
  <name>kvm-1</name>
  <uuid>894396a2-4319-5171-f4dc-18a33ba080a3</uuid>
  <memory>33554432</memory>
  <currentMemory>33554432</currentMemory>
  <vcpu>8</vcpu>
  <os>
    <type arch='x86_64' machine='rhel5.6.0'>hvm</type>
    <boot dev='hd'/>
    <boot dev='cdrom'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <pae/>
  </features>
  <clock offset='utc'>
    <timer name='pit' tickpolicy='delay'/>
  </clock>
  <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='qcow2' cache='none'/>
      <source file='/home/trusty-server-cloudimg-amd64-disk1-ubuntu.qcow2'/>
      <target dev='vda' bus='virtio'/>
    </disk>
    <disk type='block' device='disk'>
      <driver name='qemu' type='raw' cache='none'/>
      <source dev='/dev/sdc1'/>
      <target dev='vdb' bus='virtio'/>
    </disk>
    <controller type='ide' index='0'/>
    <controller type='virtio-serial' index='0'/>
    <interface type='network'>
      <mac address='52:54:00:4a:1e:d3'/>
      <source network='default'/>
      <model type='virtio'/>
    </interface>
    <serial type='pty'>
      <target port='0'/>
    </serial>
    <console type='pty'>
      <target port='0'/>
    </console>
    <input type='mouse' bus='ps2'/>
    <graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'/>
    <video>
      <model type='cirrus' vram='9216' heads='1'/>
    </video>
  </devices>
</domain>

说明:

CPU: 8vCPUs
Mem: 32GiB
hvm: 全虚拟化
使用qcow2来作为根分区
network: 设置网卡地址为52:54:00:4a:1e:d3,映射方式为NAT
根分区vda:trusty-server-cloudimg-amd64-disk1-ubuntu.qcow2
数据分区vdb:host磁盘/dev/sdc1

1.3 启动虚拟机

# 1. 转换镜像格式
sudo qemu-img convert -f raw -O qcow2 trusty-server-cloudimg-amd64-disk1.img  trusty-server-cloudimg-amd64-disk1-ubuntu.qcow2

# 2. 定义域kvm-1
sudo virsh define kvm-1.xml
# 启动kvm-1,如果启动正常,默认的ip地址为192.168.122.3,因为在default 网络配置中指定了静态ip

# 3. 启动虚拟机kvm-1
sudo virsh start kvm-1

2. 虚拟机管理

2.1 ssh连接

ssh ubuntu@192.168.122.3
密码是ubuntu

# 挂载host磁盘到虚机
vim /etc/fstab,添加如下行:
/dev/vdb /mnt ext4 defaults 0 0
保存退出,运行
mount -a

2.2 命令行virsh管理

# 使用virsh命令给domain kvm-1添加快照,其中name和说明在snapshot.xml中描述
sudo virsh snapshot-create kvm-1 snapshot.xml
# 关闭虚拟机 kvm-1
sudo virsh shutdown kvm-1

snapshot.xml文件:

<domainsnapshot>
    <name>ubuntu-python2.7.12</name>
    <description>upgrade to python2.7.12,  mount disk </description>
</domainsnapshot>

3. 其他

  • 虚拟机中安装http服务,如何在物理机中配置端口转发
# ubuntu安装httpd服务器
sudo apt install apache2
# 修改默认的/var/www/html为/mnt/www/html
vim /etc/apache2/sites-available/000-default.conf
...
DocumentRoot /mnt/www/html
...
<Directory "/mnt/www/html">
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Require all granted
</Directory>
...
# 重启httpd
apache2ctl -k restart
# 将物理机ip:80转发到虚机192.168.122.3:80
sudo iptables -t nat -I PREROUTING -p tcp -d ip --dport 80 -j DNAT --to-destination 192.168.122.3:80
sudo iptables -I FORWARD -m state -d 192.168.122.0/24 --state NEW,RELATED,ESTABLISHED -j ACCEPT
# 查看iptables nat表
sudo iptables -t nat -L
# 删除nat表的PREROUTING第一条规则
sudo iptables -t nat -D PREROUTING 1
  • ssh: permission denied (publickey)
原因:
/etc/ssh/sshd_config中将密码认证设置为no,设置为yes重启ssh服务即可
PasswordAuthentication yes
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐