自定义虚拟机安装(KVM)

                                                                 自定义虚拟机安装

一、创建虚拟交换机

原理:调用 dnsmasq 提供DNS、DHCP等功能

创建配置文件  /etc/libvirt/qemu/networks/vbr.xml


  vbr
  
  
  
    
      
    
  

二、真机room上快速配置网络 yum 源

[root@room ~]# yum install vsftp
[root@room ~]# vim  /etc/vsftpd/vsftpd.conf
 ... 
 listen=YES
 listen_ipv6=NO
 ...
[root@room ~]# systemctl enable vsftpd
[root@room ~]# systemctl start vsftpd

[root@room ~]# mkdir  /var/ftp/centos7
[root@room ~]# mount  –t  iso9660   –o loop,ro  /root/CentOS7-1708.iso    /var/ftp/centos7

[root@room ~]# yum repolist

三、virt-manager图形化安装虚拟机

1.启动 virt-manager 软件,选择新建虚拟机

自定义虚拟机安装(KVM)_第1张图片2.选择网络安装源

自定义虚拟机安装(KVM)_第2张图片3. 创建自定义镜像文件node.img

[root@room ~]# qemu-img  create  -f  qcow2  /var/lib/libvirt/images/node.img   16G

自定义虚拟机安装(KVM)_第3张图片

 

4. 网络选择自定义的vbr

自定义虚拟机安装(KVM)_第4张图片

 

5. 软件选择 minimal ,关闭 KDUMP,选择手工分区

自定义虚拟机安装(KVM)_第5张图片

 

自定义虚拟机安装(KVM)_第6张图片

 

自定义虚拟机安装(KVM)_第7张图片

 

自定义虚拟机安装(KVM)_第8张图片

 

四、虚拟机模板制作

把刚刚安装好的系初始化

1. 禁用 selinux

[root@localhost ~]#  vim  /etc/selinux/config
 SELINUX=disabled

2. 卸载相关软件

 [root@localhost ~]# yum  remove  -y  NetworkManager-*   firewalld-* 

3. 配置 yum 源

 [root@localhost ~]# vim /etc/yum.repo.d/centos7.repo
   [local_repo]
   name=centos7
   baseurl="ftp://192.168.1.254/centos7"
   enabled=1
   gpgcheck=0

4. 常用系统命令安装

 [root@localhost ~]# yum install -y net-tools vim-enhanced bridge-utils  

5. 修改网卡配置文件

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
   DEVICE="eth0"
   ONBOOT="yes"
   IPV6INIT="no"
   TYPE="Ethernet"
   BOOTPROTO ="dhcp"

6. 禁用空路由 

[root@localhost ~]#  vim  /etc/sysconfig/network
   NOZEROCONF="yes"

7. 重启机器

[root@localhost ~]# reboot

8. 添加 Console 配置及初除磁盘分区里的个性化信息

[root@localhost ~]# 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

[root@localhost ~]#  grub2-mkconfig -o /boot/grub2/grub.cfg

10. 修改 /etc/fstab,blkid 查看 uuid 对应的磁盘设备, 将UUID修改为系统设备 

[root@localhost ~]# blkid
   /dev/vda1: UUID="98bd48a3-dd66-49bd-a0e1-3c70e337f2aa" TYPE="xfs"

[root@localhost ~]# vim  /etc/fstab
   /dev/vda1       /          xfs     defaults        0    0

11. 安装分区扩展软件

[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                     # 扩展磁盘分区空间(让 /dev/vda1 变大)
/usr/sbin/xfs_growfs /                           # 扩展文件系统识别空间(让 df -h 可使用空间变大)

12. 关闭虚拟机,清理信息 (真机)

[root@room ~]# virt-sysprep -d  centos7.0
[   0.0] Examining the guest ...
[  28.9] Performing "abrt-data" ...
[  28.9] Performing "backup-files" ...
[  29.2] Performing "bash-history" ...
[  29.2] Performing "blkid-tab" ...
[  29.3] Performing "crash-data" ...
[  29.3] Performing "cron-spool" ...
[  29.3] Performing "dhcp-client-state" ...
[  29.3] Performing "dhcp-server-state" ...
[  29.3] Performing "dovecot-data" ...
[  29.3] Performing "logfile s" ...
[  29.3] Performing "machine-id" ...
[  29.3] Performing "mail-spool" ...
[  29.3] Performing "net-hostname" ...
[  29.3] Performing "net-hwaddr" ...
[  29.3] Performing "pacct-log" ...
[  29.3] Performing "package-manager-cache" ...
[  29.3] Performing "pam-data" ...
[  29.3] Performing "passwd-backups" ...
[  29.3] Performing "puppet-data-log" ...
[  29.3] Performing "rh-subscription-manager" ...
[  29.3] Performing "rhn-systemid" ...
[  29.3] Performing "rpm-db" ...
[  29.3] Performing "samba-db-log" ...
[  29.3] Performing "script" ...
[  29.3] Performing "smolt-uuid" ...
[  29.3] Performing "ssh-hostkeys" ...
[  29.3] Performing "ssh-userdir" ...
[  29.3] Performing "sssd-db-log" ...
[  29.3] Performing "tmp-files" ...
[  29.3] Performing "udev-persistent-net" ...
[  29.3] Performing "utmp" ...
[  29.3] Performing "yum-uuid" ...
[  29.3] Performing "customize" ...
[  29.3] Setting a random seed
[  29.8] Performing "lvm-uuids" ...

13. 备份虚拟机XML配置文件

[root@room ~]#cd  /etc/libvirt/qemu/
[root@room qemu]#cp  centos7.xml   centos7.0.xml.bak

14. 修改XML配置文件如下


  node
  2097152
  2097152
  2
  
    hvm
    
    
    
  
  
    
    
  
  
  
  
  destroy
  restart
  restart
  
    /usr/libexec/qemu-kvm
    
      
      
      
    
    
      
      


      
    
    
    
      
    
    
  

15.   删除虚拟机定义

[root@room ~]#  virsh  undefine  centos7.0

16.  查看模板虚拟机镜像

[root@room ~]#  ll  -h  /var/lib/libvirt/images/node.img
-rw-r--r--  1  root  root  1.2G  9月   7 17:35  node.img

五.  快速创建虚拟机-----例如创建虚拟机node1

1.  创建磁盘文件

[root@room ~]# cd /var/lib/libvirt/images
[root@room images]# qemu-img create -b node.img -f qcow2 node1.img 32G

2.  创建虚拟机配置文件

[root@room images]#sed 's/node/node1/' /root/nsd1804/centos7.0.xml > /etc/libvirt/qemu/node1.xml
[root@room images]#virsh define /etc/libvirt/qemu/node1.xml
定义域 node1(从 /etc/libvirt/qemu/node1.xml)

3.  启动虚拟机

[root@room images]#virsh start node1
域 node1 已开始

4.  连接查看虚拟机

[root@room images]# virsh console node1

 

你可能感兴趣的:(运维)