目的
根须需要创建自己的 openstack glance 镜像
创建镜像环境
需要 kvm 支持
需要利用 libvirtd 进行管理
准备物理机环境
1. 支持 ks 部署的 pxe 环境 (略)
2. 安装软件 ( yum groupinstall "Virtualization Host" "Virtualization" )
3. 建议重启一下
4. 验证 libvirtd 环境 (systemctl status libvirtd) 没有启动则自己启动一下
5. 验证 virt 网络 (virsh net-list) (成功后会有 default 网络出现)
准备 ks 文件
ks 文件制定了镜像安装的细节 (分区, 软件包, 后置脚本等)
ks 文件建议上传至一个 http 服务器中
参考 ks 文件
install
reboot
rootpw --iscrypted $1$NUZAU/$9RcgJ8Rs142K2CATg1mn9/
timezone Asia/Shanghai --isUtc
## 定义安装源
url --url="http://x.x.x.x/repo/centos/7/os/x86_64"
repo --name=updates --baseurl=http://x.x.x.x/centos/7/updates/x86_64
repo --name=extra --baseurl=http://x.x.x.x/centos/7/extras/x86_64
repo --name=os --baseurl=http://x.x.x.x/centos/7/os/x86_64
repo --name=vpackage --baseurl=http://x.x.x.x/vpackage/7/x86_64
repo --name=apps --baseurl=http://x.x.x.x/apps/7/x86_64
lang en_US.UTF-8
firewall --disabled
network --bootproto=dhcp --device=ens192 --ipv6=auto --activate
auth --enableshadow --passalgo=sha512
services --enabled="chronyd"
firstboot --disable
selinux --disabled
## 定义分区
bootloader --location=mbr
zerombr
clearpart --all --initlabel
part /boot --fstype="xfs" --size=500
part swap --fstype="swap" --size=4096
part / --fstype="xfs" --grow --size=1
part /var/lib/docker --fstype="ext4" --size=20480 --mkfsoptions="-i 8192"
## 定义软件包
%packages
@base
chrony
crash
curl
dos2unix
expat
expect
gawk
gcc
gettext
git
gzip
net-snmp
net-snmp-utils
net-tools
ntpdate
openssl-devel
pciutils
sed
perf
strace
iotop
tcpdump
nmap
sysstat
tar
acpid
teamd
vim-common
vim-enhanced
vim-filesystem
wget
ethtool
%end
## 后置脚本, 可根据需要进行自行修改
%post
echo -e '[vpackage]\nname=vpackage\nbaseurl=http://x.x.x.x/vpackage/$releasever/$basearch/\nenabled=1\ngpgcheck=0' > /etc/yum.repos.d/vpackage.repo
echo -e '[apps]\nname=apps\nbaseurl=http://x.x.x.x/apps/$releasever/$basearch/\nenabled=1\ngpgcheck=0' > /etc/yum.repos.d/apps.repo
echo -e '[dev]\nname=dev\nbaseurl=http://x.x.x.x/dev/$releasever/$basearch/\nenabled=1\ngpgcheck=0' > /etc/yum.repos.d/dev.repo
echo sslverify=0 >> /etc/yum.conf
yum install -y epel-release
## 安装 cloud-init 可以用于在 nova 启动主机时候设定 IP 地址与主机名
yum install -y cloud-init cloud-utils nemo zabbix-client
timedatectl set-local-rtc false
ntpdate x.x.x.x > /dev/null
## 修改网络命名为 eth0
sed -i s/quiet/quiet\ net.ifnames=0\ biosdevname=0/g /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg
sed -i /HWADDR/d /etc/sysconfig/network-scripts/ifcfg-eth0
sed -i /UUID/d /etc/sysconfig/network-scripts/ifcfg-eth0
## cloud-init 设定
mv /usr/local/sbin/change_dhcp2fixedip.sh /usr/sbin/change_dhcp2fixedip.sh
sed -i 's/\(ssh_pwauth:\).*$/\1 1/' /etc/cloud/cloud.cfg
sed -i 's/\(disable_root:\).*$/\1 0/' /etc/cloud/cloud.cfg
sed -i 's/name: cloud-user/name: apps\
gecos: Apps\
shell: \/bin\/bash/' /etc/cloud/cloud.cfg
echo 'bootcmd:' >> /etc/cloud/cloud.cfg
echo ' - [ cloud-init-per, once, fixed_ip, change_dhcp2fixedip.sh ]' >> /etc/cloud/cloud.cfg
echo "nameserver x.x.x.x" >> /etc/resolv.conf
sed -i s/0.centos.pool.ntp.org/mirrors.vclound.com/ /etc/chrony.conf
sed -i /centos.pool.ntp.org/d /etc/chrony.conf
useradd vclound -d /home/vclound
mkdir /home/vclound/.ssh/
echo "vclound ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
cp /etc/sudoers /etc/sudoers.bak
echo "xxxxxxx" | passwd --stdin vclound
echo "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAr/C3j8Ntur5+rNq2HaKAnnuB6Aza+FW+W+wgATljw5idIPRYJnngzp117IZtSFeGZg+Qu3HiSzb9YUKYJFaSjynic7iOawP+toI37FGFXHw0qWrv2mCcQ7CRfcxJGdfYBcbw/1eII0V1pj8p3TvOXs/CwquOoEiDUd2gkIbTX/HGkwMemlt46BbgPpAkz6Xpy4/9UHw4AVCnmB8Cm6Zk/PcAtBVy2bon0D6P84KDHzN6OOnSdo7VL4KKXZRzxluVW0tJJrnRQ8WJMl+FYggevYY7I/YOjLR0rz588K4AASbSRWx/npK+sSHvPIQ3jhDm/rCHLOBnwmalhgsLklKrlQ== >> /root/.ssh/authorized_keys
rm -rf /root/root_id_dsa_pub_vcloud_com.txt
%end
kvm
安装 virt-install 工具
yum install -y virt-install
创建本地磁盘
qemu-img create -f qcow2 /tmp/build_disk/centos.qcow2 40G
执行 kvm 安装
virt-install --name rhel7 --memory 8192 --vcpus 8 --disk /tmp/build_disk/centos.qcow2,format=qcow2 --os-variant rhel7 --location http://x.x.x.x/repo/centos/7/os/x86_64 --extra-args="ks=http://x.x.x.x/vclound/tmp/rhel7.ks console=tty0 console=ttyS0,115200n8" --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsol --os-type=linux
整个安装都会放在后台中, 要了解整个安装过程, 可以通过 vnc 进行连接
vncviewer localhost:5600
监控 kvm 状态
virsh list --all
假如返回 rhel7 running 则证明还在安装过程中
假如返回 rhel7 shut off 则已经完整安装
启动 / 关闭 kvm 方法
virsh start rhel7
virsh shutdown rhel7
删除 kvm 方法
virsh destroy rhel7
virsh undefine rhel7
rm -rf /tmp/build_disk/centos.qcow2 (切记删除硬盘文件)
本地连接 kvm 方法
virsh console rhel7
只有 kvm 在启动时才可以进行连接
退出使用组合键 ctrl + ]
远程连接 qemu 方法
virsh qemu+ssh://root@example.com/system
创建 glance image
当上面 kvm 环境测试完成, 则可以直接上传至 glance, 参考命令
glance image-create --id 49ce7eb7-f93f-402f-896a-51565be9e05d --name centos7.3_docker --disk-format qcow2 --container-format bare --file centos.qcow2 --is-public true
+------------------+--------------------------------------+
| Property | Value |
+------------------+--------------------------------------+
| checksum | e4e10db64fa2f6fa4ded41b057153270 |
| container_format | bare |
| created_at | 2017-09-07T06:50:36 |
| deleted | False |
| deleted_at | None |
| disk_format | qcow2 |
| id | 49ce7eb7-f93f-402f-896a-51565be9e05d |
| is_public | True |
| min_disk | 0 |
| min_ram | 0 |
| name | centos7.3_docker |
| owner | bb0b51d166254dc99bc7462c0ac002ff |
| protected | False |
| size | 2706505728 |
| status | active |
| updated_at | 2017-09-07T06:51:18 |
| virtual_size | None |
+------------------+--------------------------------------+
假如需要修改 image , 参考
glance image-update --property hw_qemu_guest_agent=yes 49ce7eb7-f93f-402f-896a-51565be9e05d
启动 nova instance
nova boot --flavor dc98e6d6-143f-42c2-9482-ee6efb04ff7e --image centos7.3_docker --security_group default --nic net-id=d107fe05-0fe9-4fea-9325-4cf2d0825101 terry-docker-el7.vclound.com --availability-zone pre-release:hh-yun-compute-131230.vclound.com --poll
+--------------------------------------+---------------------------------------------------------+
| Property | Value |
+--------------------------------------+---------------------------------------------------------+
| OS-DCF:diskConfig | MANUAL |
| OS-EXT-AZ:availability_zone | nova |
| OS-EXT-SRV-ATTR:host | - |
| OS-EXT-SRV-ATTR:hypervisor_hostname | - |
| OS-EXT-SRV-ATTR:instance_name | instance-00024051 |
| OS-EXT-STS:power_state | 0 |
| OS-EXT-STS:task_state | scheduling |
| OS-EXT-STS:vm_state | building |
| OS-SRV-USG:launched_at | - |
| OS-SRV-USG:terminated_at | - |
| accessIPv4 | |
| accessIPv6 | |
| adminPass | Zu7KZA2NaTR8 这个才是登录密码 |
| availability_zone | pre-release |
| config_drive | |
| created | 2017-09-07T07:00:28Z |
| flavor | 4core-4g (dc98e6d6-143f-42c2-9482-ee6efb04ff7e) |
| hostId | |
| id | 338e9711-95b0-4f76-88ce-abd5e61e2236 |
| image | centos7.3_docker (49ce7eb7-f93f-402f-896a-51565be9e05d) |
| key_name | - |
| metadata | {} |
| name | terry-docker-el7.vclound.com |
| os-extended-volumes:volumes_attached | [] |
| progress | 0 |
| security_groups | default |
| status | BUILD |
| tenant_id | bb0b51d166254dc99bc7462c0ac002ff |
| updated | 2017-09-07T07:00:28Z |
| user_id | 226e71f1c1aa4bae85485d1d17b6f0ae |
+--------------------------------------+---------------------------------------------------------+
Server building... 100% complete
Finished
资料参考
redhat virtualzation
qemu 连接方法, xml 介绍
KVM vs QEMU vs Libvirt
fedora virtualzation
get start with libvirt
virsh command howto
instance metadata