1.1 创建虚拟机

qemu-img create -f qcow2 /home/rhel7.0.qcow2 20G //创建20G动态磁盘
//root权限执行
virt-install --os-variant list
//rhel7 : Red Hat Enterprise Linux 7
virt-install --connect=qemu:///system --name rhel7.0 --ram 4096 --vcpus 4 --network network=default --disk path=/home/rhel7.0.qcow2,format=qcow2,device=disk,bus=virtio,cache=writeback --cdrom /home/liutuan/iso/xxx.iso --vnc --os-type linux --os-variant rhel7
// Domain installation still in progress. You can reconnect to 
the console to complete the installation process. //通过Virtual Machine Manager去连接操作系统安装界面

1.2 操作系统安装过程

图形界面安装、新增test登录用户,

qcow2镜像启动kvm kvm支持的镜像格式_运维


qcow2镜像启动kvm kvm支持的镜像格式_开发工具_02


qcow2镜像启动kvm kvm支持的镜像格式_开发工具_03


qcow2镜像启动kvm kvm支持的镜像格式_qcow2镜像启动kvm_04


qcow2镜像启动kvm kvm支持的镜像格式_vim_05


qcow2镜像启动kvm kvm支持的镜像格式_vim_06


qcow2镜像启动kvm kvm支持的镜像格式_开发工具_07




qcow2镜像启动kvm kvm支持的镜像格式_qcow2镜像启动kvm_08


qcow2镜像启动kvm kvm支持的镜像格式_开发工具_09


qcow2镜像启动kvm kvm支持的镜像格式_vim_10


qcow2镜像启动kvm kvm支持的镜像格式_qcow2镜像启动kvm_11


qcow2镜像启动kvm kvm支持的镜像格式_vim_12


qcow2镜像启动kvm kvm支持的镜像格式_qcow2镜像启动kvm_13

1.3 操作系统配置

#selinux
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
#iptables //关闭防火墙
systemctl stop iptables.service
systemctl disable iptables.service1.4 qemu-guest-agent安装和配置
#需要外部安装源 @updates
yum install qemu-guest-agent echo "NOZEROCONF=yes" >> /etc/sysconfig/network

1.5 配置虚拟机163 yum源
1.5.1 给虚拟机配置互联网IP

宿主机和虚拟机对应的网卡配置:

qcow2镜像启动kvm kvm支持的镜像格式_运维_14


qcow2镜像启动kvm kvm支持的镜像格式_运维_15


1.5.2 配置yum源

vim /etc/sysconfig/network-scripts/ifcfg-eth0
修改eth0配置文件:留四行,删除各种uuid
#网络IP
[root@localhost network-scripts]# cat ifcfg-eth0
NAME=eth0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=dhcp
vim /etc/resolv.conf
#MAC
Nameserver 114.114.114.114
重启网络
ping baidu.com
rm -f /etc/yum.repos.d/*
wget http://mirrors.163.com/.help/CentOS7-Base-163.repovim /etc/yum.repos.d/CentOS7-Base-163.repo
:%s/$releasever/7/g
:x
yum clean all
yum makecache
yum list

1.6 cloud-init安装和配置
#调通互联网,最后别忘了删除网络配置。

//针对RHEL-7
yum install cloud-init
yum install cloud-utils-growpartvim /etc/cloud/cloud.cfg
users:• default
disable_root: 0
ssh_pwauth: 0locale_configfile: /etc/sysconfig/i18n
mount_default_fields: [~, ~, 'auto', 'defaults,nofail', '0', '2']
resize_rootfs_tmp: /dev
ssh_deletekeys: 0
ssh_genkeytypes: ~
syslog_fix_perms: ~cloud_init_modules:
• migrator
• bootcmd
• write-files
• growpart
• resizefs
• set_hostname
• update_hostname
• update_etc_hosts
• rsyslog
• users-groups
• ssh
cloud_config_modules:
• mounts
• locale
• set-passwords
• yum-add-repo
• package-update-upgrade-install
• timezone
• puppet
• chef
• salt-minion
• mcollective
• disable-ec2-metadata
• runcmd
cloud_final_modules:
• rightscale_userdata
• scripts-per-once
• scripts-per-boot
• scripts-per-instance
• scripts-user
• ssh-authkey-fingerprints
• keys-to-console
• phone-home
• final-message
system_info:
default_user:
name: centos
lock_passwd: true
gecos: Cloud User
groups: [wheel, adm]
sudo: ["ALL=(ALL) NOPASSWD:ALL"]
shell: /bin/bash
distro: rhel
paths:
cloud_dir: /var/lib/cloud
templates_dir: /etc/cloud/templates
ssh_svcname: sshd
datasource_list: ['OpenStack','ConfigDrive']_# vim:syntax=yaml
//If there is a defaults.cfg in /etc/cloud/cloud.cfg.d/, delete it.

#修改cloud-init配置文件:
Vim /etc/default/grub
#删除GRUB_CMDLINE_LINUX=……这一行中的rhgb quiet配置项,添加console=tty0 console=ttyS0,115200n8
#使修改后的grub配置文件生效
Grub2-mkconfig –o /boot/grub2/grub.cfg
3.7 个性化配置和清理多余设置
个性化配置:略
清理多余的设置:
比如:DNS配置,磁盘分割,修改密码等。

vim /etc/resolv.conf
#MAC
Nameserver 114.114.114.114#root密码
Passwd root
password#管理开关虚拟机服务(可选)
yum install acpid
yum enable acpid#配置硬盘重新分区(可选)
yum install cloud-utils-growpart#配置本地YUM源,注意是否需要保留本地ISO源
在宿主机环境下挂载本地iso镜像
virsh destroy rhel7.0
virsh start rhel7.0
virsh attach-disk --type cdrom --mode readonly rhel7.0 "/home/liutuan/iso/xx7.0.iso" hdcvirsh dumpxml rhel7.0 > rhel7.0.xml //方便virsh create *.xml
#进入虚拟机操作系统
#挂载本地isomkdir -p /mnt/cdrom /mnt/iso
mount /dev/cdrom /mnt/iso //如果出错,则需要先attach-disk
cp –frv /mnt/iso /mnt/cdrom
rm –rf /mnt/iso
ls /mnt/cdrom#配置本地yum源
cd /etc/yum.repos.d/
mv CentOS7-Base-163.repo CentOS7-Base-163.repo.bak
cat > /etc/yum.repos.d/local.repo <<EOF
[local]
name=local
baseurl=file:///mnt/cdrom
gpgcheck=0
enabled=1
EOFKill –s 9 xx(进程号)
yum clean all
yum makecache
yum repolist#关机


shutdown

1.8 解除绑定

#安装工具软件
yum install -y libguestfs-tools-c 
//ubuntu主机sudo apt-get install libguestfs-tools
//使用VM名称
virt-sysprep -d rhel7.0
virsh undefine rhel7.0 
#镜像制作完毕
md5sum rhel7.0.qcow2 > Rrhel7.0.qcow2.md5sum
cat rhel7.0.qcow2.md5sum 
781ecc51dde344ffba0e04a7574e6190 rhel7.0.qcow2

1.9 镜像上传

  1. scp rhel7.0.qcow2 controller:/home/ //也可以通过ftp工具上传到控制节点
  2. ssh controller 上传镜像img到控制节点镜像压缩
    qemu-img convert -f qcow2 –O raw rhel7.0.qcow2 rhel7.0.raw
    1.9.1 上传到glance
source openrc
glance image-create --property os_type=linux --name " rhel7.0" --is-public true --disk-format raw --container-format bare --file /home/rhel7.0.raw


转载于:https://blog.51cto.com/lemonnews/2063261