KVM学习手记:02 存储池、虚拟机及快照

上篇,在确认主机(Host OS)支持虚拟化、安装KVM系列支持组件、配置桥接网络后,就可以创建基础镜像了。

创建存储池

在创建虚拟机之前,需要先创建一个存储池。存储池Storage pools是在宿主机上放置虚拟机的存储位置,可以是本地的,也可以是网络存储,具体的虚拟机实例放置在卷Volume上。

1.存储池

存储池Storage pools是在宿主机上放置虚拟机虚拟磁盘的存储位置,默认的存储是在/var/lib/libvirt/images目录下,由于对硬盘和虚拟磁盘大小的空间规划,一般在/opt/kvm/images目录或规划的目录下集中存放虚拟机的虚拟磁盘方便管理,操作存储卷的命令行是virsh

2.建立存储池目录

1
2
3
4
5
6
7
#创建目录
[root@myprecious ~]# mkdir -p /opt/kvm/images
#更改目录的所有者,并设置权限
[root@myprecious ~]# chown -R qemu:qemu /opt/kvm/images
[root@myprecious ~]# chmod -R 700 /opt/kvm/images
#完成验证
[root@myprecious ~]# ls -al /opt/kvm

3.创建存储池

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#定义一个存储池绑定目录
[root@myprecious ~]# virsh pool-define-as StoragePool --type dir --target /opt/kvm/images
Pool StoragePool defined
#建立基于文件夹的存储池
[root@myprecious ~]# virsh pool-build StoragePool
Pool StoragePool built
#激活StoragePool
[root@myprecious ~]# virsh pool-start StoragePool
Pool StoragePool started
#存储池开机自动运行,使用virsh pool-autostart
[root@myprecious ~]# virsh pool-autostart StoragePool
Pool StoragePool marked as autostarted

virsh pool-create-as –name vmware_pool –type dir –target /virhost/vmware
创建存储池vmware_pool,类型为文件目录,/virhost/vmware,与pool-define-as结果一样

4.验证查看存储池信息

1
2
3
4
5
6
7
8
9
[root@myprecious ~]# virsh pool-info StoragePool
Name: StoragePool
UUID: 29481667-27fc-4b73-8530-cdc081208c95
State: running #激活状态
Persistent: yes
Autostart: yes
Capacity: 37.69 GiB #容量
Allocation: 8.13 GiB #分配
Available: 29.56 GiB #可用

5.查看创建的所有存储池

1
2
3
4
5
6
[root@myprecious ~]# virsh pool-list
Name State Autostart
-----------------------------------------
boot-scratch active yes
default active yes
StoragePool active yes

6.StoragePool存储池中创建一个卷,这个卷是用来做虚拟机的硬盘

1
2
3
4
5
6
7
8
9
10
#创建卷baseCentos.qcow2,所在存储池为StoragePool,容量10G,初始分配1G,文件格式类型qcow2
[root@myprecious ~]# virsh vol-create-as --pool StoragePool --name baseCentos.qcow2 --capacity 10G --allocation 1G --format qcow2
Vol baseCentos.qcow2 created
#查看卷信息名称:baseCentos.qcow2,类型:文件,容量:10.00 GiB,分配:196.00 KiB
[root@myprecious ~]# virsh vol-info /opt/kvm/images/baseCentos.qcow2
Name: baseCentos.qcow2
Type: file
Capacity: 10.00 GiB
Allocation: 196.00 KiB

创建虚拟机

在配置好网络环境和存储池后,就可以创建虚拟机了!
KVM有三种方式来创建虚拟机,分别是:图形安装方式(virt-manager)、命令行安装方式(virt-install)及模板安装方式(module)。这里只介绍命令行安装方式,这也是平时用的最多的安装方式。

1.virt-install命令安装

virt-install参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
[root@myprecious ~]# virt-install -h
Options:
--version show program's version number and exit
-h, --help show this help message and exit
--connect=URI Connect to hypervisor with libvirt URI
General Options:
-n NAME, --name=NAME #虚拟机名(英文开头,数字开头报错)
-r MEMORY, --ram=MEMORY #以MB单位为虚拟机分配内存
--vcpus=VCPUS vcpu 数目 举例:
--vcpus 5
--vcpus 5,maxcpus=10
--vcpus sockets=2,cores=4,threads=2
--cpuset=CPUSET Set which physical CPUs domain can use.
--cpu=CPU #CPU的模式及特点 例: --cpu coreduo,+x2apic
--description=DESCRIPTION #虚拟机描述
--security=SECURITY #安全配置设置
--numatune=NUMATUNE #NUMA政策调整
Installation Method Options:
-c CDROM, --cdrom=CDROM #光驱安装介质
-l LOCATION, --location=LOCATION #安装源 (eg, nfs:host:/path,http://host/path, ftp://host/path)
--pxe #使用 PXE 协议从网络引导
--import #导入已存在的虚拟磁盘
--init=INIT Path to init binary for container guest. Ex:
--init /path/to/app (to contain an application)
--init /sbin/init (for a full OS container)
--livecd #把光盘媒体作为一个Live CD
-x EXTRA, --extra-args=EXTRA #额外的参数传递给安装内核启动
--initrd-inject=INITRD_INJECTIONS #设置启动initrd文件
--os-type=DISTRO_TYPE #虚拟机系统类型. 'linux', 'unix','windows'
--os-variant=DISTRO_VARIANT #系统版本, e.g.'rhel5', 'win2k'
--boot=BOOTOPTS #配置安装后启动命令,菜单,永久的内核启动
Storage Configuration:
--disk=DISKOPTS 指定各种选项的存储虚拟磁盘. Ex.
--disk path=/my/existing/disk
--disk path=/my/new/disk,size=5 (in gigabytes)
--disk vol=poolname:volname,device=cdrom,bus=scsi,...
--nodisks #设置无虚拟磁盘
--filesystem=FILESYSTEMS
Pass host directory to the guest. Ex:
--filesystem /my/source/dir,/dir/in/guest
--filesystem template_name,/,type=template
Networking Configuration:
-w NETWORK, --network=NETWORK #设置网络接口. Ex:
--network bridge=mybr0
--network network=my_libvirt_virtual_net
--network network=mynet,model=virtio,mac=00:11...
--nonetworks #设置无网络
Graphics Configuration:
--graphics=GRAPHICS #图形支持. 举例:
--graphics vnc
--graphics spice,port=5901,tlsport=5902
--graphics none
--graphics vnc,password=foobar,port=5910,keymap=ja
--noautoconsole #不要自动尝试连接客户机控制台
Device Options:
--serial=SERIALS #配置客户机串行驱动
--parallel=PARALLELS #配置客户机并连驱动
--channel=CHANNELS #配置客户机沟通渠道
--console=CONSOLES #配置客户机和主机之间的文本控制台连接
--host-device=HOSTDEVS #配置物理主机设备连接到客户机
--soundhw=SOUNDHW #配置客户机声音驱动
--watchdog=WATCHDOG #配置客户机监视驱动
--video=VIDEO #配置客户机视频硬件
--smartcard=SMARTCARD #配置智能卡驱动 Ex:
--smartcard mode=passthrough
Virtualization Platform Options:
-v, --hvm #设置虚拟机为全虚拟化
-p, --paravirt #设置虚拟机为半虚拟化
--container This guest should be a container guest
--virt-type=HV_TYPE #Hypervisor类型(kvm, qemu, xen, ...)
--arch=ARCH #CPU架构模拟
--machine=MACHINE #模拟机械类型
--noapic #全虚拟话客户机禁用APIC
--noacpi #全虚拟话客户机禁用ACPI
-u UUID, --uuid=UUID #客户端 UUID
Miscellaneous Options:
--autostart #跟随主机开机自动启动
--print-xml
--print-step=XMLSTEP #显示指定安装步骤的xml
--noreboot #安装完成是禁用自启动
--wait=WAIT #超时等待时间
--dry-run
--force
-q, --quiet #抑制非错误输出
--prompt
-d, --debug #打印错误信息

2.远程连接虚拟机安装界面

由于centos必须经过文本的安装界面才能安装,在宿主机没有桌面系统的前提下,通过VNC实现远程连接。

1
2
3
[root@myprecious ~]# vim sed -i 's/#vnc_listen = "0.0.0.0"/vnc_listen = "0.0.0.0"/g' /etc/libvirt/qemu.conf
#必须重启宿主机
[root@myprecious ~]# reboot

使监听所有端口,否则只监听本地,监听本地是指只能从宿主机本地登录指定虚拟机如 vncviewer 127.0.0.1:端口号(如127.0.0.1:5902),如果监听所有端口则可以从远程通过宿主机IP:端口号 登录虚拟机(如192.168.122.24:5902)

3.Virt-install新建虚拟机centos

(1)raw格式磁盘

1
2
3
4
5
6
7
8
#--disk 默认为`raw`格式磁盘
[root@myprecious ~]# virt-install --name=vm1 --ram=1024 --vcpus=2 --disk path=/opt/kvm/images/vm1.img,size=10 --cdrom /opt/kvm/data/centos7.iso --graphics vnc,port=5910, --network bridge=virbr0,model=virtio --force --autostart
Starting install...
Allocating 'vm1.img' | 10 GB 00:00
Creating domain... | 0 B 00:00
Cannot open display:
Run 'virt-viewer --help' to see a full list of available command line options
Domain installation still in progress. You can reconnect to the console to complete the installation process.

(2)qcow2格式磁盘(kvm推荐使用qcow2,具有多种特性,建议生成此格式磁盘)

1
2
#--disk format=qcow2 指定磁盘格式
[root@myprecious ~]# virt-install --name=vm2 --ram=1024 --vcpus=1 --disk path=/opt/kvm/images/vm2.qcow2,format=qcow2,size=7,bus=virtio --cdrom /opt/kvm/data/centos7.iso --graphics vnc,port=5911, --network bridge=virbr0,model=virtio --force --autostart

以上就是显示创建成功然后确认下端口iptables没有问题的话就可以使用vnc连接

1
2
3
[root@myprecious ~]# netstat -antup |grep kvm
tcp 0 0 0.0.0.0:5910 0.0.0.0:* LISTEN 1787/qemu-kvm
tcp 0 0 192.168.122.24:5910 192.168.122.1:58697 ESTABLISHED 1787/qemu-kvm

4.远程连接安装界面

关闭宿主机(这里为myprecious)中的防火墙firewalldtelnet下到宿主机的响应端口(这里为telnet 192.168.104.240 5911),如果端口是通的说明可以连通。
在物理机Windows上安装TightVNC,完成后输入IP:Port(此处为192.168.104.240:5911)以完成虚机安装。
在vnc安装完成虚拟机自动重启,启动之后就再也连不上了,这时使用virsh手动启动。

1
[root@myprecious ~]# virsh start vm1 #vm1为虚拟机名

再次远程连接,然后配置虚拟机ip,配置console登录后重启。

5.查看虚拟机的基本信息

使用virt-install工具,工具自动创建磁盘,默认是raw格式

1
2
3
4
5
[root@myprecious ~]# qemu-img info /opt/kvm/images/vm1.img
image: /opt/kvm/images/vm1.img
file format: raw
virtual size: 10G (10737418240 bytes)
disk size: 1.4G

6.查看虚拟机的配置文件

使用virt-install工具安装虚拟机后,在目录/etc/libvirt/qemu/下生成xml配置文件
配置文件命名:虚拟机名.xml

1
[root@myprecious ~]# cat /etc/libvirt/qemu/vm1.xml

7.窗口检测与ACPID

启动一个窗口监测安装进程如virt-viewer vm1来查看安装进程。

有时候会碰到虚拟机无法正常安装,如果没有启动acpid进程的话,使用virsh shutdown vm1就会无法关闭虚拟机,这时候就需要使用命令virsh destroy vm1来强制关闭了。

安装acpid

1
2
3
4
5
[root@myprecious ~]# yum install acpid -y
[root@myprecious ~]# systemctl list-unit-files | grep acpid
acpid.service enabled
[root@myprecious ~]# systemctl enable acpid.service
[root@myprecious ~]# systemctl start acpid.service

完成以上操作后,就可以使用virsh shutdown <guestname>来很方便的关闭远程虚机了。

8.快照

等做完虚拟机的配置后,就需要对虚拟机做一个快照了。
快照的制作有两种方法,一种是直接在virsh 里面使用snapshot来制作。另外一种是使用qemu-img来创建快照

1.使用snapshot创建快照:
  • 创建快照
    1
    [root@myprecious ~]# virsh snapshot-create-as vm1

快照创建的很快,其实,就是生成了一个XML的配置文件,记录下当前的信息。

  • 查看快照

    1
    2
    3
    4
    [root@myprecious ~]# virsh snapshot-list vm1
    名称 CreationTime 状态
    ------------------------------------------------------------
    1330938135 2017-01-17 10:44:29 +0800 shutoff
  • 查看快照的配置文件

    1
    [root@myprecious ~]# virsh snapshot-current vm1

那么,快照文件存在什么地方呢,在/var/lib/libvirt/qemu/snapshot目录下,有以虚拟机的域名为名称的文件夹,就在里面哪。

2.使用qemu-img创建快照:

使用qemu-img创建快照也很方便,这个镜像是直接对硬盘文件进行操作,硬盘文件的格式必须为qcow2格式的。

  • 创建快照

    1
    [root@myprecious ~]# qemu-img snapshot -c 2017-1-17 /opt/kvm/images/baseCentos.qcow2
  • 查看快照
    创建完毕后,查看一下

    1
    2
    3
    4
    5
    [root@myprecious ~]# qemu-img snapshot -l /opt/kvm/images/baseCentos.qcow2
    Snapshot list:
    ID TAG VMSIZE DATE VM CLOCK
    1 1330938135 0 2017-01-1710:44:29 00:00:00.000
    2 2017-1-17 0 2017-01-1711:51:39 00:00:00.000

可以看到有两个快照,ID=1virsh创建的,ID=2qemu-img创建的。

–(待续)–


参考信息:

文章目录
  1. 1. 创建存储池
    1. 1.1. 1.存储池
    2. 1.2. 2.建立存储池目录
    3. 1.3. 3.创建存储池
    4. 1.4. 4.验证查看存储池信息
    5. 1.5. 5.查看创建的所有存储池
    6. 1.6. 6.StoragePool存储池中创建一个卷,这个卷是用来做虚拟机的硬盘
  2. 2. 创建虚拟机
    1. 2.1. 1.virt-install命令安装
    2. 2.2. 2.远程连接虚拟机安装界面
    3. 2.3. 3.Virt-install新建虚拟机centos
      1. 2.3.1. (1)raw格式磁盘
      2. 2.3.2. (2)qcow2格式磁盘(kvm推荐使用qcow2,具有多种特性,建议生成此格式磁盘)
    4. 2.4. 4.远程连接安装界面
    5. 2.5. 5.查看虚拟机的基本信息
    6. 2.6. 6.查看虚拟机的配置文件
    7. 2.7. 7.窗口检测与ACPID
      1. 2.7.1. 安装acpid
      2. 2.7.2. 8.快照
        1. 2.7.2.1. 1.使用snapshot创建快照:
        2. 2.7.2.2. 2.使用qemu-img创建快照: