创建一个后端存储
[root@room9pc01 images]# qemu-img create -f qcow2 node21.qcow2 8G
Formatting 'node21.qcow2', fmt=qcow2 size=8589934592 encryption=off cluster_size=65536 lazy_refcounts=off
虚拟机过程跳过进入虚拟机查找IP
[root@localhost ~]# ip a s eth0
连接虚拟机
[root@room9pc01 ~]# ssh 192.168.1.197
搭建yum源
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost ~]# mkdir repo
[root@localhost ~]# mv *.repo repo/
[root@localhost ~]# vi client.repo
[client]
name=client
baseurl=ftp://192.168.1.254/CentOS7
enabled=1
gpgcheck=0
[root@localhost ]# yum clean all
[root@localhost ]# yum repolist
安装基础包
[root@localhost ]# yum -y install net-tools
vim-enhanced \
qemu-kvm \
libvirt-daemon \
libvirt-client \
libvirt-daemon-driver-qemu \
virt-manager \
virt-install
启动服务
[root@localhost yum.repos.d]# systemctl start libvirtd
改策略
[root@localhost yum.repos.d]# vim /etc/selinux/config
selinux=disabled
关闭并卸载防火墙NetworkManger(启用network)
[root@localhost yum.repos.d]# systemctl stop firewalld
[root@localhost yum.repos.d]# systemctl mask firewalld
[root@localhost yum.repos.d]# yum remove -y NetworkManager-* firewalld-* python-firewall
[root@localhost yum.repos.d]# systemctl start network
导入公钥(服务端)
[root@localhost yum.repos.d]# yum install -y lftp
[root@localhost yum.repos.d]# vim /etc/yum.repos.d/client.repo
gpgcheck=1
[root@localhost yum.repos.d]# rpm --import ftp://192.168.1.254/CentOS7/RPM-GPG-KEY-CentOS-7
测试yum源
[root@localhost yum.repos.d]# yum install -y bridge-utils psmisc
配置网卡(模版)
[root@localhost yum.repos.d]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
IPV6INIT=no
TYPE=Ethernet
BOOTPROTO=dhcp
禁止空路由
[root@localhost yum.repos.d]# vim /etc/sysconfig/network
NOZEROCONF=yes
重启
[root@localhost yum.repos.d]# halt -p
连接查看ip是否仍然有效
[root@room9pc01 ~]# ssh 192.168.1.197
[root@localhost ~]# ifconfig
允许服务端连接
[root@localhost ~]# vim /etc/default/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_DISABLE_RECOVERY="true"
GRUB_CMDLINE_LINUX="biosdevname=0 net.ifnames=0 console=ttyS0,115200n8"
GRUB_DISABLE_LINUX_UUID="true"
GRUB_ENABLE_LINUX_LABEL="true"
[root@localhost ~]# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-693.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-693.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-090cf77ab8ed4a958fbe4a6245159ac1
Found initrd image: /boot/initramfs-0-rescue-090cf77ab8ed4a958fbe4a6245159ac1.img
done
[root@localhost ~]# blkid
/dev/vda1: UUID="5b1b657f-2767-430d-9c2b-18566e499b15" TYPE="xfs"
[root@localhost ~]# vim /etc/fstab
/dev/vda1 / xfs defaults 0 0
设置支持自动扩展
[root@localhost ~]# yum install -y cloud-utils-growpart
[root@localhost ~]# chmod 755 /etc/rc.local
[root@localhost ~]# vim /etc/rc.local
###
/usr/bin/growpart /dev/vda 1
/usr/sbin/xfs_growfs /
/usr/bin/sed '/^###/,$d' -i /etc/rc.d/rc.local
记得重启
[root@localhost ~]# halt -p
开启做最后模板验证
[root@room9pc01 ]# virsh start node21
[root@room9pc01 ]# virsh console node21
localhost login: root
Password:
网络模板验证
[root@localhost ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.197 netmask 255.255.255.0 broadcast 192.168.1.255
硬盘验证
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 8G 0 disk
└─vda1 253:1 0 8G 0 part /
确认无误关闭
[root@localhost ~]# halt -p
做备份处理防止突发状况
[root@room9pc01 ~]# cd /var/lib/libvirt/images/
[root@room9pc01 images]# cp node21.qcow2 /root/
[root@room9pc01 images]# cd /etc/libvirt/qemu/
[root@room9pc01 qemu]# cp node21.xml /root/
[root@room9pc01 ~]# ls node21.xml node21.qcow2
node21.qcow2 node21.xml
清理
[root@room9pc01 ~]# virt-sysprep -d node21
[ 0.0] Examining the guest ...
[ 4.0] Performing "abrt-data" ...
[ 4.0] Performing "backup-files" ...
[ 4.5] Performing "bash-history" ...
[ 4.5] Performing "blkid-tab" ...
[ 4.5] Performing "crash-data" ...
[ 4.5] Performing "cron-spool" ...
[ 4.5] Performing "dhcp-client-state" ...
[ 4.5] Performing "dhcp-server-state" ...
[ 4.5] Performing "dovecot-data" ...
[ 4.5] Performing "logfiles" ...
[ 4.6] Performing "machine-id" ...
[ 4.6] Performing "mail-spool" ...
[ 4.6] Performing "net-hostname" ...
[ 4.6] Performing "net-hwaddr" ...
[ 4.6] Performing "pacct-log" ...
[ 4.6] Performing "package-manager-cache" ...
[ 4.6] Performing "pam-data" ...
[ 4.6] Performing "passwd-backups" ...
[ 4.6] Performing "puppet-data-log" ...
[ 4.6] Performing "rh-subscription-manager" ...
[ 4.6] Performing "rhn-systemid" ...
[ 4.6] Performing "rpm-db" ...
[ 4.6] Performing "samba-db-log" ...
[ 4.6] Performing "script" ...
[ 4.6] Performing "smolt-uuid" ...
[ 4.6] Performing "ssh-hostkeys" ...
[ 4.6] Performing "ssh-userdir" ...
[ 4.6] Performing "sssd-db-log" ...
[ 4.6] Performing "tmp-files" ...
[ 4.6] Performing "udev-persistent-net" ...
[ 4.6] Performing "utmp" ...
[ 4.6] Performing "yum-uuid" ...
[ 4.6] Performing "customize" ...
[ 4.6] Setting a random seed
[ 5.4] Performing "lvm-uuids" ...
取消域定义
[root@room9pc01 images]# virsh undefine node21
域 node21 已经被取消定义
查看xml声明配置文件确认取消定义
[root@room9pc01 images]# ls /etc/libvirt/qemu
demo.xml win2008.xml networks
确认模板
[root@room9pc01 images]# ls /var/lib/libvirt/images/
node21.qcow2
创前端(后端-前端-配置)
[root@room9pc01 images]# qemu-img create -f qcow2 -b node21.qcow2 node22.img 50G
Formatting 'node22.img', fmt=qcow2 size=53687091200 backing_file='node21.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off
[root@room9pc01 images]# cd /etc/libvirt/qemu/
[root@room9pc01 qemu]# sed 's/demo/node22/' demo.xml >/etc/libvirt/qemu/node22.xml
定义域
[root@room9pc01 qemu]# virsh define /etc/libvirt/qemu/node22.xml
定义域 node22(从 /etc/libvirt/qemu/node22.xml)
开启域
[root@room9pc01 qemu]# virsh start node22
域 node22 已开始
连接域
[root@room9pc01 qemu]# virsh console node22
连接到域 node22
换码符为 ^]
[ OK ] Started Postfix Mail Transport Agent.
[ OK ] Started /etc/rc.d/rc.local Compatibility.
Starting Wait for Plymouth Boot Screen to Quit...
Starting Terminate Plymouth Boot Screen...
CentOS Linux 7 (Core)
Kernel 3.10.0-693.el7.x86_64 on an x86_64
localhost login: root
Password:
确认网卡
[root@localhost ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.155 netmask 255.255.255.0 broadcast 192.168.1.255
确认磁盘
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 50G 0 disk
└─vda1 253:1 0 50G 0 part /
加网卡(xml-eth1)
[root@room9pc01 ~]# virsh
virsh # edit node22
<interface type='bridge'>
<source bridge='private1'/>
<model type='virtio'/>
</interface>
virsh # exit
[root@room9pc01 ~]# virsh console node22
[root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-eth1
[root@localhost network-scripts]# vim ifcfg-eth1
DEVICE=eth1
ONBOOT=yes
IPV6INIT=no
TYPE=Ethernet
BOOTPROTO=static
IPADDR=192.168.4.177
PREFIX=24
加默认网关
[root@localhost network-scripts]# vim ifcfg-eth0
GATEWAY=192.168.1.254
重启
[root@localhost ~]# halt -p
开启连接
virsh start node22
virsh cosole node22
localhost login: root
Password:
查看网卡信息
[root@localhost ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.155 netmask 255.255.255.0 broadcast 192.168.1.255
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.4.177 netmask 255.255.255.0 broadcast 192.168.4.255
退出操作
[root@localhost ~]# exit
localhost login: ctrl+]
加硬盘
[root@room9pc01 ~]# cd /var/lib/libvirt/images/
[root@room9pc01 images]# qemu-img create -f qcow2 disk22.img 20G
改xml
[root@room9pc01 ~]# virsh
virsh # edit node22
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/var/lib/libvirt/images/disk22.img'/>
<target dev='vdb' bus='virtio'/>
</disk>
virsh # edit node22
编辑了域 node22 XML 配置。
重启(连接)
[root@localhost ~]# halt -p
[root@room9pc01 images]# virsh start node22
域 node22 已开始
[root@room9pc01 images]# virsh console node22
localhost login: root
Password:
查看验证
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 50G 0 disk
└─vda1 253:1 0 50G 0 part /
vdb 253:16 0 20G 0 disk
网卡确认
[root@localhost ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.155 netmask 255.255.255.0 broadcast 192.168.1.255
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.4.177 netmask 255.255.255.0 broadcast 192.168.4.255