1.安装一个CentOS6的系统的虚拟主机
#上传一个CenOS6系统的镜像到/opt目录下
[root@gjy /opt]# ll
-rw-r--r-- 1 root root 3916431360 2019-07-22 22:03 CentOS-6.8-x86_64-bin-DVD1.iso
-rw------- 1 root root 10737418240 2019-07-22 20:19 centos7.raw
-rw-r--r-- 1 qemu qemu 4588568576 2019-07-22 17:58 CentOS-7-x86_64-DVD-1810.iso
[root@gjy /opt]# virt-install --virt-type kvm --os-type=linux --os-variant rhel6 --name centos6 --memory 1024 --vcpus 1 --disk /opt/centos6.raw,format=raw,size=10 --cdrom /opt/CentOS-6.8-x86_64-bin-DVD1.iso --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
Starting install...
Domain installation still in progress. You can reconnect to
the console to complete the installation process.
#使用TightVNC进行连接,如果centos7是开启的,那么这个的默认端口号是5901
进入之后,开始安装CenOS6的系统,正常安装就可以,ip地址设置为自动就可以了。分区时一定要配置swap分区,不配置swap分区,会出现报错的情况,主机起动不来。
[root@kvm01 opt]# virsh list --all
Id Name State
----------------------------------------------------
1 centos6 running
3 centos7 running
- w shut off
#查端口
[root@kvm01 opt]# virsh vncdisplay centos6
:1
- Kvm虚拟机console登录
CentOS-7.X 版本console登录
#连接到kvm虚拟主机
[root@gjy /opt]# ssh root@192.168.122.89
#可以优化下ssh,免交互
[root@kvm01 ~]# vim /etc/ssh/sshd_config
79 GSSAPIAuthentication no
115 UseDNS no
#重启配置文件
[root@kvm01 ~]# systemctl restart sshd
#内核文件
[root@kvm01 ~]# ll /etc/grub2.cfg
lrwxrwxrwx. 1 root root 22 Aug 2 04:49 /etc/grub2.cfg -> ../boot/grub2/grub.cfg
#更新内核参数
[root@kvm01 ~]# grubby --update-kernel=ALL --args="console=ttyS0,115200n8"
[root@kvm01 ~]# reboot
#115200n8:能显示虚拟机的启动过程。重启完成后,使用virsh console 连接虚拟机。
[root@gjy /opt]# virsh console centos7
Connected to domain centos7
Escape character is ^]
CentOS Linux 7 (Core)
Kernel 3.10.0-957.el7.x86_64 on an x86_64
centos7 login: root
Password:
Last login: Tue Jul 23 09:51:50 from gateway
#查看终端
[root@centos-7 ~]# who
root ttyS0 2019-12-01 17:05
#注意:退出virsh console连接的方法,使用组合键Ctrl+]即可
CentOS-6.X 版本console登录
#开启centos-6的虚拟主机
[root@gjy /opt]# virsh start centos6
Domain centos6 started
[root@gjy /opt]# virsh list --all
Id Name State
----------------------------------------------------
1 centos7 running
2 centos6 running
#连接到kvm虚拟主机,默认网络是没有启动的,需要手动启动起来
[root@gjy /opt]# ssh root@192.168.122.45
#1.添加ttyS0的许可,允许root登陆
[root@centos6 ~]# echo "ttyS0" >> /etc/securetty
#2.编辑/etc/grub.conf中加入console=ttyS0
[root@centos6 ~]# sed -i '/ kernel/s#.*#& console=ttyS0#g' /boot/grub/grub.conf
#3.编辑/etc/inittab在最后一行加入内容 S0:12345:respawn:/sbin/agetty ttyS0 115200
[root@centos6 ~]# echo 'S0:12345:respawn:/sbin/agetty ttyS0 115200' >>/etc/inittab
#4.重启虚拟主机
[root@centos6 ~]# reboot
#5.重启完成后,使用virsh console 连接虚拟机。
[root@gjy /opt]# virsh console centos6
Connected to domain centos6
Escape character is ^]
CentOS release 6.8 (Final)
Kernel 2.6.32-642.el6.x86_64 on an x86_64
centos6 login: root
Password:
Last login: Tue Jul 23 02:01:46 from 192.168.122.1
05. Kvm虚拟机开机自启动
#开机启动autostart,前提:systemctl enable libvirtd;取消开机启动autostart --disable
[root@gjy /opt]# virsh autostart centos7
#检查信息
[root@kvm01 ~]# virsh dominfo centos7 |grep -i autostart
Autostart: enable
#实质为创建软连接
[root@gjy /opt]# ll /etc/libvirt/qemu/autostart/centos7.xml
lrwxrwxrwx 1 root root 29 2019-07-23 10:27 /etc/libvirt/qemu/autostart/centos7.xml -> /etc/libvirt/qemu/centos7.xml
#关闭开机自启动
[root@gjy /opt]# virsh autostart --disable centos7
Domain centos7 unmarked as autostarted
#检查信息
[root@kvm01 ~]# virsh dominfo centos7 |grep -i autostart
Autostart: disable
[root@gjy /opt]# ll /etc/libvirt/qemu/autostart/centos7.xml
ls: cannot access /etc/libvirt/qemu/autostart/centos7.xml: No such file or directory
windows10安装
#上传镜像
[root@kvm01 opt]# ll
total 30233772
-rw-r--r-- 1 qemu qemu 5302775808 Dec 3 18:19 cn_windows_10_business_edition_version_1809_updated_april_2019_x64_dvd_1194d215.iso
[root@lb01 opt]# virt-install --virt-type kvm --os-type=windows --os-variant windows --name w --memory 2048 --vcpus 2 --disk /opt/w.raw,format=raw,size=20 --cdrom /opt/cn_windows_10_business_edition_version_1809_updated_april_2019_x64_dvd_1194d215.iso --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
注意:vnc必须直达,不是直达连不上。如果公司有跳板机的话,会用console登录
2.虚拟机的备份
#虚拟机在开启状态
[root@kvm01 opt]# virsh dumpxml centos7 > centos7-bak.xml
#关机进行导出配置
[root@kvm01 opt]# virsh dumpxml centos7 >centos7-bak7.xml
[root@kvm01 opt]# ll
total 11214716
-rw-r--r-- 1 qemu qemu 3916431360 Dec 3 22:53 CentOS-6.8-x86_64-bin-DVD1.iso
-rw------- 1 root root 10737418240 Dec 4 00:11 centos6.raw
-rw-r--r-- 1 root root 3853 Dec 4 00:12 centos7-bak7.xml
-rw-r--r-- 1 root root 5145 Dec 4 00:10 centos7-bak.xml
#移除主机
[root@kvm01 opt]# virsh undefine centos7
Domain centos7 has been undefined
#查看
[root@kvm01 opt]# virsh list --all
Id Name State
----------------------------------------------------
1 centos6 running
- w shut off
#导入主机
[root@kvm01 opt]# virsh define centos7-bak.xml
Domain centos7 defined from centos7-bak.xml
#再次查看
[root@kvm01 opt]# virsh list --all
Id Name State
----------------------------------------------------
1 centos6 running
- centos7 shut off
- w shut off
3.企业案例:
#查看
[root@kvm01 opt]# virsh list --all
Id Name State
----------------------------------------------------
1 centos6 running
5 centos7 running
#如果开机状态下移除主机
[root@kvm01 opt]# virsh undefine centos7
#再次查看,没变化
[root@kvm01 opt]# virsh list --all
Id Name State
----------------------------------------------------
1 centos6 running
5 centos7 running
#此时关机的话
[root@kvm01 opt]# virsh shutdown centos7
#再查看,会发现主机被清除了
[root@kvm01 opt]# virsh list --all
Id Name State
----------------------------------------------------
1 centos6 running
解决办法
#先备份配置文件
[root@kvm01 opt]# virsh define centos6.xml
再关机,导入
注意:
#查看进程
[root@kvm01 opt]# ps -ef|grep qemu
当undefine的时候,进程不会消失,还在。但系统管理配置文件没了,当关机的时候,会都消失。
#配置文件要导入进去,不能cp复制
[root@kvm01 opt]# virsh define centos6.xml
4.Kvm磁盘管理
01. Kvm虚拟机虚拟磁盘格式转换
raw : 裸格式,占用空间比较大,不支持快照功能,性能较好,不方便传输
qcow2 : cow (copy on write) ,占用空间小,支持快照功能,性能比raw差一点 ,方便传输
创建一块qcow2的虚拟硬盘(仅测试使用,无实际意义)
#创建磁盘,指定大小
[root@gjy /opt]# qemu-img create -f qcow2 centos7.qcow2 2G
Formatting 'centos7.qcow2', fmt=qcow2 size=2147483648 encryption=off cluster_size=65536 lazy_refcounts=off
[root@gjy /opt]# ll -h
total 196K
-rw-r--r-- 1 root root 193K 2019-07-23 10:58 centos7.qcow2
#查看当前虚拟机硬盘信息
[root@gjy /data]# qemu-img info /data/centos7.qcow2
image: /data/centos7.qcow2
file format: qcow2
virtual size: 2.0G (2147483648 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
#转换格式,语法说明
[root@gjy /data]# qemu-img --help |grep convert
convert [-c] [-p] [-q] [-n] [-f fmt] [-t cache] [-T src_cache] [-O output_fmt] [-o options] [-s snapshot_name] [-S sparse_size] filename [filename2 [...]] output_filename
#转换原有磁盘格式
[root@gjy /opt]# qemu-img convert -f raw -O qcow2 centos7.raw centos7.qcow2
#转化之后,要想使用,要修改kvm虚拟机的主配置文件
[root@gjy /opt]# ll -h
total 13G
-rw-r--r-- 1 qemu qemu 3.7G 2019-07-22 22:03 CentOS-6.8-x86_64-bin-DVD1.iso
-rw-r--r-- 1 root root 1.2G 2019-07-23 11:17 centos7.qcow2
-rw------- 1 qemu qemu 10G 2019-07-23 11:19 centos7.raw
-rw-r--r-- 1 qemu qemu 4.3G 2019-07-22 17:58 CentOS-7-x86_64-DVD-1810.iso
#修改配置文件
[root@gjy /opt]# virsh edit centos7
32 <disk type='file' device='disk'>
33 <driver name='qemu' type='qcow2'/> #将原来的raw改成qcow2
34 <source file='/opt/centos7.qcow2'/> #将原来的raw改成qcow2
35 <target dev='vda' bus='virtio'/>
36 <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
37 </disk>
#删除原磁盘文件
[root@gjy /opt]# rm -f centos7.raw
#启动kvm虚拟机
[root@gjy /opt]# virsh start centos7
#连接测试
[root@gjy /opt]# virsh console centos7
总结:
qemu-img 常用命令:
info查看虚拟磁盘信息
create 创建虚拟磁盘文件
qemu-img create test.raw 5G
qemu-img create -f qcow2 centos7.qcow2 2G
resize 调整虚拟磁盘容量大小(qcow2只能括,不能缩,但不管什么格式的磁盘,尽量都不能缩,会丢数据)
qemu-img resize test.raw +5G (调整至5G)
qemu-img resize test.qcow2 15G
convent 磁盘格式转换
qemu-img convenet -f raw |-O qcow2 test.raw test.qcow2
02. Kvm快照管理
注意:raw格式的磁盘无法创建快照,qcow2支持快照,快照就存放在qcow2的磁盘文件里
#创建快照 开机状态
[root@kvm01 opt]# virsh snapshot-create centos7
Domain snapshot 1575390933 created
#快照的路径
[root@kvm01 opt]# tree /var/lib/libvirt/qemu/snapshot/
/var/lib/libvirt/qemu/snapshot/
└── centos7
└── 1575390933.xml
1 directory, 1 file
#查看快照
[root@kvm01 opt]# virsh snapshot-list centos7
Name Creation Time State
------------------------------------------------------------
1575390933 2019-12-04 00:35:33 +0800 running
#开机状态下的快照
[root@kvm01 opt]# virsh snapshot-create centos7
Domain snapshot 1575391158 created
[root@kvm01 opt]# ll /var/lib/libvirt/qemu/snapshot/centos7/
total 16
-rw------- 1 root root 4951 Dec 4 00:39 1575390933.xml
-rw------- 1 root root 4973 Dec 4 00:39 1575391158.xml
[root@kvm01 opt]# virsh snapshot-list centos7
Name Creation Time State
------------------------------------------------------------
1575390933 2019-12-04 00:35:33 +0800 running
1575391158 2019-12-04 00:39:18 +0800 shutoff
#查看快照信息
[root@kvm01 opt]# virsh snapshot-info centos7 1575390933
Name: 1575390933
Domain: centos7
Current: no
State: running
Location: internal
Parent: -
Children: 1
Descendants: 1
Metadata: yes
#查看快照列表
[root@kvm01 opt]# virsh snapshot-list centos7
Name Creation Time State
------------------------------------------------------------
1575390933 2019-12-04 00:35:33 +0800 running
1575391158 2019-12-04 00:39:18 +0800 shutoff
#恢复快照
[root@kvm01 opt]# virsh snapshot-revert centos7 --snapshotname 1575390933
#测试登录
[root@kvm01 opt]# virsh console centos7
Connected to domain centos7
Escape character is ^]
[root@centos7 ~]# ls
anaconda-ks.cfg
[root@kvm01 opt]# virsh snapshot-list centos7
Name Creation Time State
------------------------------------------------------------
1575390933 2019-12-04 00:35:33 +0800 running
1575391158 2019-12-04 00:39:18 +0800 shutoff
#删除快照
[root@kvm01 opt]# virsh snapshot-delete centos7 1575390933
Domain snapshot 1575390933 deleted
[root@kvm01 opt]# virsh snapshot-list centos7
Name Creation Time State
------------------------------------------------------------
1575391158 2019-12-04 00:39:18 +0800 shutoff
03. Kvm在线热添加硬盘
#进入/opt目录下,添加一块硬盘
[root@gjy ~]# cd /opt/
[root@gjy /opt]# ll
total 10003908
-rw-r--r-- 1 qemu qemu 3916431360 2019-07-22 22:03 CentOS-6.8-x86_64-bin-DVD1.iso
-rw-r--r-- 1 qemu qemu 1752956928 2019-07-24 01:04 centos7.qcow2
-rw-r--r-- 1 qemu qemu 4588568576 2019-07-22 17:58 CentOS-7-x86_64-DVD-1810.iso
[root@gjy /opt]# qemu-img create -f qcow2 /opt/centos7-add01.qcow2 5G
Formatting '/opt/centos7-add01.qcow2', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536 lazy_refcounts=off
[root@gjy /opt]# qemu-img info centos7-add01.qcow2
image: centos7-add01.qcow2
file format: qcow2
virtual size: 5.0G (5368709120 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
#给kvm虚拟机centos7热添加硬盘
[root@gjy /opt]# virsh attach-disk centos7 /opt/centos7-add01.qcow2 vdb --live --cache=none --subdriver=qcow2 --config
Disk attached successfully
vdb #第二块硬盘
--live #热添加
--cache=none #宿主机对客户机的镜像的读写cache开启,off表示关闭
--subdriver #驱动类型,(一定要加,不然添加的磁盘大小不会显示)
--config #下次启动生效
#vnc查看添加结果
剥离此块硬盘
[root@gjy /opt]# virsh detach-disk centos7 vdb
Disk detached successfully
[root@kvm01 opt]# virsh console centos7
Connected to domain centos7
Escape character is ^]
[root@centos7 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 252:0 0 10G 0 disk
├─vda1 252:1 0 1G 0 part /boot
└─vda2 252:2 0 9G 0 part
├─centos-root 253:0 0 8G 0 lvm /
└─centos-swap 253:1 0 1G 0 lvm [SWAP]
增加硬盘大小,生产环境中,只加不减
[root@gjy /opt]# qemu-img resize /opt/centos7-add01.qcow2 +5G
Image resized.
[root@gjy /opt]# qemu-img info /opt/centos7-add01.qcow2
image: /opt/centos7-add01.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 260K
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
#再重新给centos7热添加硬盘
[root@gjy /opt]# virsh attach-disk centos7 /opt/centos7-add01.qcow2 vdb --live --cache=none --subdriver=qcow2
Disk attached successfully
进入kvm虚拟机,进行格式化磁盘
[root@gjy /opt]# virsh console centos7
Connected to domain centos7
Escape character is ^]
centos7 login: root
Password:
Last login: Fri Aug 30 20:18:51 on tty1
#控制台登录
[root@kvm01 opt]# virsh console centos7
Connected to domain centos7
Escape character is ^]
[root@centos7 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 252:0 0 10G 0 disk
├─vda1 252:1 0 1G 0 part /boot
└─vda2 252:2 0 9G 0 part
├─centos-root 253:0 0 8G 0 lvm /
└─centos-swap 253:1 0 1G 0 lvm [SWAP]
vdb 252:16 0 10G 0 disk
#格式化操作,创建文件系统
[root@centos7 ~]# mkfs.xfs /dev/vdb
meta-data=/dev/vdb isize=512 agcount=4, agsize=655360 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=2621440, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@centos7 ~]# mkdir /data
#挂载(挂载前要先格式化)
[root@centos7 ~]# mount /dev/vdb /data/
#查看
[root@centos7 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 8.0G 1007M 7.1G 13% /
devtmpfs 484M 0 484M 0% /dev
tmpfs 496M 0 496M 0% /dev/shm
tmpfs 496M 6.8M 489M 2% /run
tmpfs 496M 0 496M 0% /sys/fs/cgroup
/dev/vda1 1014M 133M 882M 14% /boot
tmpfs 100M 0 100M 0% /run/user/0
/dev/vdb 10G 33M 10G 1% /data
#如果挂载之后,磁盘大小变化了,分区表也变化了,df -h查看没有更新,执行命令
[root@centos7 ~]# xfs_growfs /dev/vdb (执行此命令,会调整inode和block信息,才会扩容成功)
根分区扩容:
1).先关机,存在快照,删除快照
2)在宿主机上关闭虚拟机并调整虚拟机磁盘大小
[root@gjy /opt]# qemu-img resize centos7.qcow2 +10G
3)虚拟机中fdisk重新分区
[root@gjy /opt]# fdisk /dev/vda
partprobe #通知内核系统分区发生变化
3)重启之后,执行
[root@gjy /opt]# xfs_growfs /dev/vsda
#如果虚拟机磁盘文件系统是ext4:resize2fs /dev/vda1