自定义虚拟机
-
虚拟化服务平台组成KVM/QEMU/LIBVIRTD
KVM:linux内核模块,提供底层命令
QEMU:仿真工具,完成对硬件虚拟化的支持。
LIBVIRT:虚拟化管理接口和工具。
2.虚拟机的组成–xml文件、磁盘镜像文件
xml文件(虚拟机的配置声明文件)—/etc/libvirt/qemu
磁盘镜像文件(虚拟机的硬盘)—/var/lib/libvirt/images -
Libvirt管理工具命令
virsh基本操作
1)列出当前正在运行的虚拟机
[[email protected] ~]# virsh list
2)查看虚拟机的信息
[[email protected] ~]# virsh dominfo node1
3)启动虚拟机
[[email protected] ~]# virsh start node1
4)重启虚拟机
[[email protected] ~]# virsh reboot node1
5)强制关闭虚拟机
[[email protected] ~]# virsh destroy node1
6)关闭虚拟机
[[email protected] ~]# virsh shutdown node1
7)设置虚拟机开机自动运行
[[email protected] ~]# virsh autostart node1 -
Virsh管理虚拟网络:
-net-list 查看虚拟网络
-net-define vbr.xml 创建虚拟网络
-net-undefine vbr 删除虚拟网络
-net-start vbr 启动虚拟网络
-net-destroy vbr 停止虚拟网络
-net-edit vbr 修改虚拟网络
-net-autostart vbr 设置vbr虚拟网络开机自启动 -
qemu-img基本操作管理
1)创建一个新的镜像盘文件
-qemu-img命令格式:qemu-img 命令 参数 块文件名称 大小
-create 创建一个磁盘
-convert 转换磁盘格式
-info 查看磁盘信息
-snapshot 管理磁盘快照
[[email protected]oom9pc01 ~]# qemu-img create -f qcow2 disk.qcow2 16G //qcow2为创建的格式
2)使用后端模板文件创建一个前端镜像盘文件()备注:-b使用后端模板文件
[[email protected] ~]# qemu-img create -b disk.img -f qcow2 disk1.img 20G //前端盘文件必须比后端盘文件大
3)查看镜像文件的信息
[[email protected] ~]# qemu-img info disk1.img
前端盘和后端盘的关系
二、创建模板虚拟机
1)创建一个名为vbr的虚拟网络
[[email protected] ~]# vim /etc/libvirt/qemu/networks/vbr.xml
<network>
<name>vbr</name> //vbr为虚拟网络的名字
<bridge name="vbr"/>
<forward mode="nat"/>
<ipaddress="192.168.1.254" netmask="255.255.255.0"> //ip为192.168.1.254
<dhcp>
<range start="192.168.1.100" end="192.168.1.200"/> //ip范围是100-200
</dhcp>
</ip>
</network>
2)启动vbr虚拟网络并用ifconfig验证
[[email protected] ~]# virsh net-define vbr.xml //定义vbr虚拟网络
[[email protected] ~]# virsh net-start vbr //启动vbr虚拟网络
[[email protected] ~]# ifconfig //ifconfig验证
3)设置vbr虚拟网络开机自启动
[[email protected] ~]# virsh net-autostart vbr
4)创建xml文件
[[email protected] nsd1806]# cat node.xml
<domain type='kvm'>
<name>node</name>
<memory unit='KB'>2097152</memory>
<currentMemory unit='KB'>2097152</currentMemory>
<vcpu placement='static'>2</vcpu>
<os>
<type arch='x86_64' machine='pc'>hvm</type>
<boot dev='hd'/>
<bootmenu enable='yes'/>
<bios useserial='yes'/>
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode='host-passthrough'>
</cpu>
<clock offset='localtime'/>
<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'/>
<source file='/var/lib/libvirt/images/node.img'/>
<target dev='vda' bus='virtio'/>
</disk>
<interface type='bridge'>
<source bridge='vbr'/>
<model type='virtio'/>
</interface>
<channel type='unix'>
<target type='virtio' name='org.qemu.guest_agent.0'/>
</channel>
<serial type='pty'></serial>
<console type='pty'>
<target type='serial'/>
</console>
<memballoon model='virtio'></memballoon>
</devices>
</domain>
5.创建后端盘文件,,上面已做讲解,在此不做赘述。
6.新建虚拟机:
1)用模板xml文件生成新的虚拟机配置文件
[[email protected] ~]# cd /etc/libvirt/qemu/
[[email protected] qemu]# cp node.xml nova01.xml
[[email protected] qemu]# sed 's/node/nova01/' node.xml > /etc/libvirt/qemu/nova01.xml
2)根据模板后端盘文件创建新的虚拟机前端盘文件
[[email protected] libvirt]# cd /var/lib/libvirt/images/
[[email protected] images]# qemu-img create -b node.qcow2 -f qcow2 nova01.qcow2 50G
3)定义生成虚拟机
[[email protected]]#virsh define /etc/libvirt/qemu/nova01.xml
[[email protected] qemu]# virsh start nova01
4)创建模板机
真机搭建网络yum源头-选择文件-新建虚拟机–…
在这里插入图片描述
5)制作虚拟机模板
1.禁用selinux
2.卸载防火墙与NetworkManager
[[email protected] ~]# yum -y remove NetworkManager-* firewalld-* python-firewall
3.配置yum源
[[email protected] ~]# vim /etc/yum.repos.d/dvd.repo
[dvd]
name=dvd
baseurl=ftp://192.168.1.254/centos
enabled=1
gpgcheck=0
[[email protected] ~]# yum clean all
[[email protected] ~]# yum repolist
4.常用系统命令安装(安装之后重启虚拟机)
Yum install -y net-tools vim-enhanced bridge-utils psmisc
5.导入公钥(把/etc/yum.repos.d/dvd.repo的gpgcheck=0改成gpgcheck=1)
[[email protected] ~]# rpm --import ftp://192.168.4.254/contos/RPM-GPG-KEY-CentOs-7
6.配置网卡(配置之后ping外网,测试网络是否连通)
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
ONBOOT="yes"
IPV6INIT="no"
TYPE="Ethernet"
BOOTPROTO ="dhcp"
[[email protected] ~]# systemctl restart network
Ping www.baidu.com
7.禁用空路由
[[email protected] ~]# vim /etc/sysconfig/network
NOZEROCONF="yes
8.添加console配置(防止console连接虚拟机时卡死)
[[email protected] ~]# vim /etc/default/grub
GRUB_CMDLINE_LINUX="biosdevname=0 net.ifnames=0 console=ttyS0,115200n8"
GRUB_DISABLE_LINUX_UUID="true"
GRUB_ENABLE_LINUX_LABEL="true"
9.重新生成grub.cfg
[[email protected] ~]# grub2-mkconfig -o /boot/grub2/grub.cfg
10.安装扩展分区软件
[[email protected] ~]# yum install -y cloud-utils-growpart
11.第一次开机自动扩容(语言必须为英语 LANG=C)
[[email protected] default]# vim /etc/fstab
/dev/vda1 / xfs defaults 0 0
[[email protected] ~]# /usr/bin/growpart /dev/vda 1 //分区扩容
[[email protected] ~]# /usr/sbin/xfs_growfs / //文件系统扩容
12.写一个快速配置网卡的脚本
[[email protected] ~]# vim eip
#!/bin/bash
cat <<EOF >/etc/sysconfig/network-scripts/ifcfg-eth0
# Generated by dracut initrd
DEVICE="eth0"
ONBOOT="yes"
NM_CONTROLLED="no"
TYPE="Ethernet"
BOOTPROTO="static"
IPADDR="192.168.1.$1"
NETMASK="255.255.255.0"
GATEWAY="192.168.1.254"
EOF
13.关闭虚拟机后执行信息清理工作(清理之前先关闭虚拟机)
[[email protected] ~]# virt-sysprep -d centos7.0
//真机上面操作,centos7.0为虚拟机名称
14.删除xml文件中的uuid、mac地址之后用undefine取消定义生成的xml文件
三、利用后端盘文件及xml文件快速生成新的虚拟机
1.根据后端盘创建前端盘
[[email protected] ~]# qemu-img create -b muban.img -f qcow2 muban03.img 16G
2.创建虚拟机配置文件
[[email protected] ~]# cd /etc/libvirt/qemu/
mv centos7.0.xml /etc/libvirt/qemu/muban03.xml
3.定义虚拟机,并启动
[[email protected] ~]# virsh define muban03.xml
[[email protected] ~]# [[email protected] ~]# virsh start muban03
[[email protected] ~]# Virsh console muban03