Kvm虚拟化

Kvm之二:创建虚拟机

2017-01-13  本文已影响178人  词穷又词贫

Centos7 Kvm 创建虚拟机

1、准备准备文件目录
[root@node71 ~]# mkdir -p /data/{vm/{oel1,oel2},ios}
#vm目录下的子目录为虚拟机磁盘文件目录
#ios为存放各个IOS的目录

2、创建虚拟机磁盘镜像文件

#Centos 7 qemu-img 支持的磁盘镜像文件格式:vvfat vpc vmdk vhdx vdi ssh sheepdog rbd raw host_cdrom host_floppy host_device file qed qcow2 qcow parallels nbd iscsi gluster dmg tftp ftps ftp https http cloop bochs blkverify blkdebug
#默认格式为raw,读写性能好,但是不支持高级功能,比如加密与快照等
[root@node71 oel1]# pwd
/data/vm/oel1
[root@node71 oel1]# qemu-img create -f raw -o size=20G oel1-raw.img 
Formatting 'oel1-raw.img', fmt=raw size=21474836480   
[root@node71 oel1]# qemu-img info oel1-raw.img 
image: oel1-raw.img
file format: raw
virtual size: 20G (21474836480 bytes)
disk size: 0
[root@node71 oel1]# 

3、安装虚拟机linux操作系统

[root@node71 oel1]# virt-install -n oel1 --ram 512 --vcpus=1 --disk path=/data/vm/oel1/oel1_raw.img,format=raw,size=20,bus=virtio --accelerate --cdrom /data/ios/OracleLinux-R6-U6-Server-x86_64-dvd.iso --vnc --vncport=5910 --vnclisten=0.0.0.0 --network bridge=br0,model=virtio --noautoconsole

开始安装......
创建域......                                                                                                                                                       |    0 B  00:00:00     
域安装仍在进行。您可以重新连接
到控制台以便完成安装进程。
[root@node71 oel1]# virsh list --all
 Id    名称                         状态
----------------------------------------------------
 7     oel1                           running

[root@node71 oel1]#     
#启动虚拟机之后就可以通过PC的vnc客户端链接5910号端口
#虚拟机的设备配置文件:以xml格式保存,
[root@node71 qemu]# pwd
/etc/libvirt/qemu
[root@node71 qemu]# ls
networks  oel1.xml
[root@node71 qemu]#
#虚拟机是以此xml格式的配置文件注册到kvm(virsh define oel1.xml)
#xml配置文件记录了此虚拟机所有的配置信息如:内存,cpu,disk,clock,usb,cdrom,pci,显示等等信息
#可以编辑此配置文件来适当调整虚拟机配置
[root@node71 qemu]# virsh edit oel1
<domain type='kvm'>
  <name>oel1</name>
  <uuid>550ac229-d7d2-410e-b21a-03fd30783032</uuid>
  <memory unit='KiB'>524288</memory>
  <currentMemory unit='KiB'>524288</currentMemory>
  <vcpu placement='static'>1</vcpu>
  <os>
    <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
  </features>
  <cpu mode='custom' match='exact'>
    <model fallback='allow'>Westmere</model>
  </cpu>
  <clock offset='utc'>
    <timer name='rtc' tickpolicy='catchup'/>
    <timer name='pit' tickpolicy='delay'/>
    <timer name='hpet' present='no'/>
  </clock>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <pm>
    <suspend-to-mem enabled='no'/>
    <suspend-to-disk enabled='no'/>
  </pm>
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>      #模拟器:qemu-kvm
    <disk type='file' device='disk'>                #disk 磁盘
      <driver name='qemu' type='raw'/>
      <source file='/data/vm/oel1/oel1_raw.img'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </disk>
    <disk type='block' device='cdrom'>              #cdrom
      <driver name='qemu' type='raw'/>
      <target dev='hda' bus='ide'/>
      <readonly/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
    <controller type='usb' index='0' model='ich9-ehci1'>            #pci类型的usb
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x7'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci1'>
      <master startport='0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0' multifunction='on'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci2'>
      <master startport='2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x1'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci3'>
      <master startport='4'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'/>
    <controller type='ide' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <interface type='bridge'>                               #桥接网卡
      <mac address='52:54:00:a4:a5:fe'/>                    #网卡mac地址,建议手动指定,vm多了有可能造成冲突
      <source bridge='br0'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <serial type='pty'>                                     #pty虚拟终端
      <target port='0'/>
    </serial>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <input type='mouse' bus='ps2'/>
    <input type='keyboard' bus='ps2'/>
    <graphics type='vnc' port='5910' autoport='no' listen='0.0.0.0'>        #vnc图形显示
      <listen type='address' address='0.0.0.0'/>
    </graphics>
    <video>                                             #pci类型的video
      <model type='cirrus' vram='16384' 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>

4、安装虚拟机Win7操作系统

#默认windows系统不带硬盘和网卡的virtio驱动,redhat有提供,需要单独下载:http://fedoraproject.org/wiki/Windows_Virtio_Drivers
[root@node71 win7]# virt-install -n win7 --ram 2048 --vcpus=2 --disk path=/data/vm/win7/win7_raw.img,format=raw,bus=virtio,size=20 --disk path=/data/ios/Win7.iso,device=cdrom,perms=ro --disk path=/data/ios/virtio-win-0.1.126.iso,device=cdrom --vnc --vncport=5912 --vnclisten=0.0.0.0 --network bridge=br0,model=virtio --noautoconsole
#Win7.iso是安装系统的ISO,
#virtio-win-0.1.126.iso是包含各类Win系统驱动,比如virtio的磁盘与网卡,SCSI等驱动
NetKVM/: Virtio Network driver
viostor/: Virtio Block driver
vioscsi/: Virtio SCSI driver
viorng/: Virtio RNG driver
vioser/: Virtio serial driver
Balloon/: Virtio Memory Balloon driver
qxl/: QXL graphics driver for Windows 7 and earlier. (build virtio-win-0.1.103-1 and later)
qxldod/: QXL graphics driver for Windows 8 and later. (build virtio-win-0.1.103-2 and later)
pvpanic/: QEMU pvpanic device driver (build virtio-win-0.1.103-2 and later)
guest-agent/: QEMU Guest Agent 32bit and 64bit MSI installers
qemupciserial/: QEMU PCI serial device driver
*.vfd: VFD floppy images for using during install of Windows XP

5、解决win7系统,vnc连接鼠标漂移

#在<devices>位置添加| <input type='tablet' bus='usb'/> |
#删除IOS的cdrom配置
[root@node71 win7]# virsh edit win7

<domain type='kvm'>
  <name>win7</name>
  <uuid>57de68ab-5e3d-4dd8-9b3b-51724a8dd20c</uuid>
  <memory unit='KiB'>2097152</memory>
  <currentMemory unit='KiB'>2097152</currentMemory>
  <vcpu placement='static'>2</vcpu>
  <os>
    <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
  </features>
  <cpu mode='custom' match='exact'>
    <model fallback='allow'>Westmere</model>
  </cpu>
  <clock offset='utc'>
    <timer name='rtc' tickpolicy='catchup'/>
    <timer name='pit' tickpolicy='delay'/>
    <timer name='hpet' present='no'/>
  </clock>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <pm>
    <suspend-to-mem enabled='no'/>
    <suspend-to-disk enabled='no'/>
  </pm>
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/data/vm/win7/win7_raw.img'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </disk>
    #<disk type='file' device='cdrom'>
    #  <driver name='qemu' type='raw'/>
    #  <source file='/data/ios/Win7.iso'/>
    #  <target dev='hda' bus='ide'/>
    #  <readonly/>
    #  <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    #</disk>
    #<DISK TYPE='FILE' DEVICE='CDROM'>
    #  <DRIVER NAME='QEMU' TYPE='RAW'/>
    #  <SOURCE FILE='/DATA/IOS/VIRTIO-WIN-0.1.126.ISO'/>
    #  <TARGET DEV='HDB' BUS='IDE'/>
    #  <READONLY/>
    #  <ADDRESS TYPE='DRIVE' CONTROLLER='0' BUS='0' TARGET='0' UNIT='1'/>
    #</DISK>
    <controller type='usb' index='0' model='ich9-ehci1'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x7'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci1'>
      <master startport='0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0' multifunction='on'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci2'>
      <master startport='2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x1'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci3'>
      <master startport='4'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'/>
    <controller type='ide' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <interface type='bridge'>
      <mac address='52:54:00:43:46:c1'/>
      <source bridge='br0'/>
      <model type='virtio'/>
    <controller type='usb' index='0' model='ich9-uhci2'>
      <master startport='2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x1'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci3'>
      <master startport='4'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'/>
    <controller type='ide' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <interface type='bridge'>
      <mac address='52:54:00:43:46:c1'/>
      <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'/>
    <input type='keyboard' bus='ps2'/>
    <graphics type='vnc' port='5912' autoport='no' listen='0.0.0.0'>
      <listen type='address' address='0.0.0.0'/>
    </graphics>
    <video>
      <model type='cirrus' vram='16384' 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>

6、virsh关闭win7

#查看win7虚拟机信息
[root@node71 win7]# virsh dominfo win7
Id:             30
名称:       win7
UUID:           57de68ab-5e3d-4dd8-9b3b-51724a8dd20c
OS 类型:    hvm
状态:       running
CPU:          2
CPU 时间:   3.6s
最大内存: 2097152 KiB
使用的内存: 2097152 KiB
持久:       是
自动启动: 禁用
管理的保存: 否
安全性模式: none
安全性 DOI: 0

[root@node71 win7]#
#不需要安装其它驱动
[root@node71 win7]# virsh shutdown win7
域 win7 被关闭
[root@node71 win7]# virsh list --all
 Id    名称                         状态
----------------------------------------------------
 -     oel1                           关闭
 -     oel2                           关闭
 -     win7                           关闭

[root@node71 win7]# 

virt-install命令帮助

[root@node71 oel1]# virt-install -h
usage: virt-install --name NAME --ram RAM STORAGE INSTALL [options]

使用指定安装介质新建虚拟机。

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  --connect URI         使用 libvirt URI 连接到 hypervisor

通用选项:
  -n NAME, --name NAME  客户端事件名称
  --memory MEMORY       配置虚拟机内存分配。例如:
                        --memory 1024 (in MiB)
                        --memory 512,maxmemory=1024
  --vcpus VCPUS         为虚拟机配置的 vcpus 数。例如:
                        --vcpus 5
                        --vcpus 5,maxcpus=10,cpuset=1-4,6,8
                        --vcpus sockets=2,cores=4,threads=2,
  --cpu CPU             CPU 型号及功能。例如:
                        --cpu coreduo,+x2apic
                        --cpu host
  --metadata METADATA   配置虚拟机元数据。例如:
                        --metadata name=foo,title="My pretty title",uuid=...
                        --metadata description="My nice long description"

安装方法选项:
  --cdrom CDROM         光驱安装介质
  -l LOCATION, --location LOCATION
                        安装源(例如:nfs:host:/path、http://host/path
                        ftp://host/path)
  --pxe                 使用 PXE 协议从网络引导
  --import              在磁盘映像中构建虚拟机
  --livecd              将光驱介质视为 Live CD
  -x EXTRA_ARGS, --extra-args EXTRA_ARGS
                        附加到使用 --location 引导的内核的参数
  --initrd-inject INITRD_INJECT
                        使用 --location 为 initrd 的 root
                        添加给定文件
  --os-variant DISTRO_VARIANT
                        在其中安装 OS 变体的虚拟机,比如
                        'fedora18'、'rhel6'、'winxp' 等等。
  --boot BOOT           配置虚拟机引导设置。例如:
                        --boot hd,cdrom,menu=on
                        --boot init=/sbin/init (for containers)
  --idmap IDMAP         为 LXC 容器启用用户名称空间。例如:
                        --idmap uid_start=0,uid_target=1000,uid_count=10

设备选项:
  --disk DISK           使用不同选项指定存储。例如:
                        --disk size=10 (new 10GiB image in default location)
                        --disk /my/existing/disk,cache=none
                        --disk device=cdrom,bus=scsi
                        --disk=?
  -w NETWORK, --network NETWORK
                        配置虚拟机网络接口。例如:
                        --network bridge=mybr0
                        --network network=my_libvirt_virtual_net
                        --network network=mynet,model=virtio,mac=00:11...
                        --network none
                        --network help
  --graphics GRAPHICS   配置虚拟机显示设置。例如:
                        --graphics vnc
                        --graphics spice,port=5901,tlsport=5902
                        --graphics none
                        --graphics vnc,password=foobar,port=5910,keymap=ja
  --controller CONTROLLER
                        配置虚拟机控制程序设备。例如:
                        --controller type=usb,model=ich9-ehci1
  --input INPUT         配置虚拟机输入设备。例如:
                        --input tablet
                        --input keyboard,bus=usb
  --serial SERIAL       配置虚拟机串口设备
  --parallel PARALLEL   配置虚拟机并口设备
  --channel CHANNEL     配置虚拟机沟通频道
  --console CONSOLE     配置虚拟机与主机之间的文本控制台连接
  --hostdev HOSTDEV     将物理 USB/PCI/etc
                        主机设备配置为与虚拟机共享
  --filesystem FILESYSTEM
                        将主机目录传递给虚拟机。例如:
                        --filesystem /my/source/dir,/dir/in/guest
                        --filesystem template_name,/,type=template
  --sound [SOUND]       配置虚拟机声音设备模拟
  --watchdog WATCHDOG   配置虚拟机 watchdog 设备
  --video VIDEO         配置虚拟机视频硬件。
  --smartcard SMARTCARD
                        配置虚拟机智能卡设备。例如:
                        --smartcard mode=passthrough
  --redirdev REDIRDEV   配置虚拟机重定向设备。例如:
                        --redirdev usb,type=tcp,server=192.168.1.1:4000
  --memballoon MEMBALLOON
                        配置虚拟机 memballoon 设备。例如:
                        --memballoon model=virtio
  --tpm TPM             配置虚拟机 TPM 设备。例如:
                        --tpm /dev/tpm
  --rng RNG             配置虚拟机 RNG 设备。例如:
                        --rng /dev/random
  --panic PANIC         配置虚拟机 panic 设备。例如:
                        --panic default

虚拟机配置选项:
  --security SECURITY   设定域安全驱动器配置。
  --numatune NUMATUNE   为域进程调整 NUMA 策略。
  --memtune MEMTUNE     为域进程调整内粗策略。
  --blkiotune BLKIOTUNE
                        为域进程调整 blkio 策略。
  --memorybacking MEMORYBACKING
                        为域进程设置内存后备策略。例如:
                        --memorybacking hugepages=on
  --features FEATURES   设置域 <features> XML。例如:
                        --features acpi=off
                        --features apic=on,eoi=on
  --clock CLOCK         设置域 <clock> XML。例如:
                        --clock offset=localtime,rtc_tickpolicy=catchup
  --pm PM               配置 VM 电源管理功能
  --events EVENTS       配置 VM 生命周期管理策略
  --resource RESOURCE   配置 VM 资源分区(cgroups)

虚拟化平台选项:
  -v, --hvm             客户端应该是一个全虚拟客户端
  -p, --paravirt        这个客户端一个是一个半虚拟客户端
  --container           这台虚拟机需要一个容器客户端
  --virt-type HV_TYPE   要使用的管理程序名称(kvm、qemu、xen
                        等等)
  --arch ARCH           模拟的 CPU 构架
  --machine MACHINE     要模拟的机器类型

其它选项:
  --autostart           引导主机时自动启动域。
  --wait WAIT           等待安装完成的分钟数。
  --noautoconsole       不要自动尝试连接到客户端控制台
  --noreboot            完成安装后不要引导虚拟机。
  --print-xml [XMLONLY]
                        输出所生成域 XML,而不是创建虚拟机。
  --dry-run             完成安装步骤,但不要创建设备或者定义
                        虚拟机。
  --check CHECK         启用或禁用验证检查。例如:
                        --check path_in_use=off
                        --check all=off
  -q, --quiet           禁止无错误输出
  -d, --debug           输入故障排除信息

使用 '--option=?' 或者 '--option help' 查看可用子选项
有关示例及完整选项语法,请查看 man page。
[root@node71 oel1]#
上一篇 下一篇

猜你喜欢

热点阅读