基于DigitalOcean制作OpenStack的镜像

  • A+
所属分类:OpenStack

一:准备工作
1、环境检查,看看CPU支不支持硬件虚拟

[root@centos-s-6vcpu-16gb-nyc1-01 ~]# grep -E '(vmx|svm)' /proc/cpuinfo
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon rep_good nopl eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 pku ospke
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon rep_good nopl eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 pku ospke
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon rep_good nopl eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 pku ospke
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon rep_good nopl eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 pku ospke
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon rep_good nopl eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 pku ospke
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon rep_good nopl eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 pku ospke
[root@centos-s-6vcpu-16gb-nyc1-01 ~]# 

2、安装kvm相关的包

[root@centos-s-6vcpu-16gb-nyc1-01 ~]# yum update -y
[root@centos-s-6vcpu-16gb-nyc1-01 ~]# yum install qemu-kvm qemu-img virt-manager libvirt \
libvirt-python libvirt-client virt-install virt-viewer bridge-utils libguestfs-tools-y

3、启动libvirtd

[root@centos-s-6vcpu-16gb-nyc1-01 ~]# systemctl start libvirtd
[root@centos-s-6vcpu-16gb-nyc1-01 ~]# systemctl enable libvirtd

4、检查KVM模块是否被加载

[root@centos-s-6vcpu-16gb-nyc1-01 ~]# lsmod | grep kvm
kvm_intel             174841  0 
kvm                   578558  1 kvm_intel
irqbypass              13503  1 kvm
[root@centos-s-6vcpu-16gb-nyc1-01 ~]#

5、如果你和我一样是最小化安装CentOS 7,那么virt-manger将无法启动,所以需要安装x-window软件包。

[root@centos-s-6vcpu-16gb-nyc1-01 ~]# yum install "@X Window System" xorg-x11-xauth xorg-x11-fonts-* xorg-x11-utils -y

6、检查现有网络的状态,如果看到一个名为default状态为active的网络就是正常的

[root@centos-s-6vcpu-16gb-nyc1-01 ~]# virsh net-list --all
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes

[root@centos-s-6vcpu-16gb-nyc1-01 ~]#

7、下载ISO镜像文件

[root@centos-s-6vcpu-16gb-nyc1-01 ~]# mkdir -p /data && cd /data
[root@centos-s-6vcpu-16gb-nyc1-01 data]# axel -an 20 http://mirror.cc.columbia.edu/pub/linux/centos/7.5.1804/isos/x86_64/CentOS-7-x86_64-Everything-1804.iso -o CentOS-7.5.1804-x86_64-Everything.iso
[root@centos-s-6vcpu-16gb-nyc1-01 data]# axel -an 20 http://mirrors.dtops.cc/iso2/Windows/cn_windows_server_2016_vl_feb_2018_x64_dvd_11636695.iso

8、使用命令创建一个KVM虚拟机

[root@centos-s-6vcpu-16gb-nyc1-01 data]# qemu-img info CentOS-7.5.1804-x86_64-Everything.iso 
image: CentOS-7.5.1804-x86_64-Everything.iso
file format: raw
virtual size: 8.8G (9397338112 bytes)
disk size: 8.8G
[root@centos-s-6vcpu-16gb-nyc1-01 data]# qemu-img create -f qcow2 centos.qcow2 40G           
Formatting 'centos.qcow2', fmt=qcow2 size=42949672960 encryption=off cluster_size=65536 lazy_refcounts=off 
[root@centos-s-6vcpu-16gb-nyc1-01 data]# NAME=centos && ROOT_DISK=centos.qcow2 && CDROM=`pwd`/CentOS-7.5.1804-x86_64-Everything.iso
[root@centos-s-6vcpu-16gb-nyc1-01 data]# virt-install \
--virt-type kvm \
--name $NAME \
--ram 1024 \
--disk $ROOT_DISK,format=qcow2 \
--network network=default \
--graphics vnc,listen=0.0.0.0 \
--noautoconsole \
--os-type=linux \
--os-variant=rhel7 \
--cdrom=$CDROM

Starting install...
Domain installation still in progress. You can reconnect to 
the console to complete the installation process.
[root@centos-s-6vcpu-16gb-nyc1-01 data]# 

如果你网络很好,也可以采取在线安装的方式,这就不用单独下载系统镜像了

[root@centos-s-6vcpu-16gb-nyc1-01 data]# qemu-img create -f qcow2 centos7-test.qcow2 40G 
Formatting 'centos7-test.qcow2', fmt=qcow2 size=42949672960 encryption=off cluster_size=65536 lazy_refcounts=off 
[root@centos-s-6vcpu-16gb-nyc1-01 data]# NAME=centos7 && ROOT_DISK=centos7-test.qcow2
[root@centos-s-6vcpu-16gb-nyc1-01 data]# virt-install \
--name centos7 \
--virt-type kvm \
--ram 2048 \
--disk $ROOT_DISK,format=qcow2 \
--network network=default \
--vcpus 2 \
--os-type linux \
--os-variant rhel7 \
--graphics vnc,listen=0.0.0.0 \
--noautoconsole \
--console pty,target_type=serial \
--extra-args 'console=ttyS0,115200n8 serial' \
--location 'http://mirror.cc.columbia.edu/pub/linux/centos/7.5.1804/os/x86_64/' \
--extra-args "ks=http://www.dwhd.org/script/kickstart/CentOS_7_x86_64_centos.cfg" 

Starting install...
Retrieving file vmlinuz...                                                                                                                                                       | 5.9 MB  00:00:00     
Retrieving file initrd.img...                                                                                                                                                    |  50 MB  00:00:01     
Domain installation still in progress. You can reconnect to 
the console to complete the installation process.
[root@centos-s-6vcpu-16gb-nyc1-01 data]#
---extra-args "ks=http://www.dwhd.org/script/kickstart/CentOS_7_x86_64_centos.cfg ksdevice=eth0 ip=10.10.21.76 netmask=255.255.255.240 dns=10.10.21.1 gateway=10.10.21.100"

为了进度,我这边同时开了两个kvm,下面是Windows 2016镜像的制作

[root@centos-s-6vcpu-16gb-nyc1-01 iso]# qemu-img info cn_windows_server_2016_vl_feb_2018_x64_dvd_11636695.iso 
image: cn_windows_server_2016_vl_feb_2018_x64_dvd_11636695.iso
file format: raw
virtual size: 5.9G (6302720000 bytes)
disk size: 5.9G
[root@centos-s-6vcpu-16gb-nyc1-01 data]# mkdir virtio
[root@centos-s-6vcpu-16gb-nyc1-01 data]# cd virtio
[root@centos-s-6vcpu-16gb-nyc1-01 virtio]# wget -c 'https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/stable-virtio/virtio-win.iso'
[root@centos-s-6vcpu-16gb-nyc1-01 virtio]# wget -c https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/stable-virtio/virtio-win_amd64.vfd
[root@centos-s-6vcpu-16gb-nyc1-01 virtio]# cd ..
[root@centos-s-6vcpu-16gb-nyc1-01 data]#
下面是我的目录结构
[root@centos-s-6vcpu-16gb-nyc1-01 data]# tree /data/
/data/
├── centos7.5.1804-20180831.qcow2
├── CentOS-7.5.1804-x86_64-Cloud-20180831.qcow2
├── iso
│   ├── CentOS-7.5.1804-x86_64-Everything.iso
│   └── cn_windows_server_2016_vl_feb_2018_x64_dvd_11636695.iso
├── virtio
│   ├── virtio-win_amd64.vfd
│   └── virtio-win.iso
└── windows2016.qcow2

2 directories, 7 files
[root@centos-s-6vcpu-16gb-nyc1-01 data]# 
[root@centos-s-6vcpu-16gb-nyc1-01 data]# qemu-img create -f qcow2 windows2016.qcow2 40G       
Formatting 'windows2016.qcow2', fmt=qcow2 size=42949672960 encryption=off cluster_size=65536 lazy_refcounts=off 
[root@centos-s-6vcpu-16gb-nyc1-01 data]# NAME=windows2016 && ROOT_DISK=windows2016.qcow2 && \
CDROM1=`pwd`/virtio/virtio-win.iso && \
CDROM2=`pwd`/iso/cn_windows_server_2016_vl_feb_2018_x64_dvd_11636695.iso
[root@centos-s-6vcpu-16gb-nyc1-01 data]# virt-install \
--name $NAME \
--connect qemu:///system \
--arch=x86_64 \
--ram 8192 \
--vcpus 4 \
--cpu host \
--video cirrus \
--network network=default,model=virtio \
--features hyperv_relaxed=on,hyperv_spinlocks=on,hyperv_vapic=on \
--graphics vnc,listen=0.0.0.0 \
--noautoconsole \
--os-type windows \
--os-variant win2k16 \
--disk $ROOT_DISK,format=qcow2,device=disk,bus=virtio  \
--disk path=$CDROM2,device=cdrom  \
--cdrom=$CDROM1

Starting install...
Domain installation still in progress. You can reconnect to 
the console to complete the installation process.
[root@centos-s-6vcpu-16gb-nyc1-01 data]#
[root@centos-s-6vcpu-16gb-nyc1-01 data]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 1     centos                         running
 2     windows2016                    running

[root@centos-s-6vcpu-16gb-nyc1-01 data]# 

9、连接KVM节点
由于我这里是远程连接的,所有我就直接使用了VNC连接,如果要是本地的话,还可以使用virt-manager、virt-viewer连接
基于DigitalOcean制作OpenStack的镜像
基于DigitalOcean制作OpenStack的镜像

10、为了方便等下进系统操作,我们这边来给新建的虚拟机做下远程管理端口映射

[root@centos-s-6vcpu-16gb-nyc1-01 data]# yum install -y git kernel-devel gcc gcc-c++ make
[root@centos-s-6vcpu-16gb-nyc1-01 data]# git clone https://github.com/kklis/proxy /usr/local/bin/
[root@centos-s-6vcpu-16gb-nyc1-01 data]# cd /usr/local/bin && make && cd -
[root@centos-s-6vcpu-16gb-nyc1-01 data]# virsh list --all       
 Id    Name                           State
----------------------------------------------------
 10    windows2016                    running
 -     centos                         shut off

[root@centos-s-6vcpu-16gb-nyc1-01 data]# virsh domifaddr 10
 Name       MAC address          Protocol     Address
-------------------------------------------------------------------------------
 vnet0      52:54:00:fe:e8:b7    ipv4         192.168.122.5/24

[root@centos-s-6vcpu-16gb-nyc1-01 data]# proxy -h
proxy: option requires an argument -- 'h'
Syntax: proxy [-b bind_address] -l local_port -h remote_host -p remote_port [-i "input parser"] [-o "output parser"] [-f (stay in foreground)] [-s (use syslog)]
[root@centos-s-6vcpu-16gb-nyc1-01 data]# proxy -l 3389 -h 192.168.122.5 -p 3389 
[root@centos-s-6vcpu-16gb-nyc1-01 data]# ss -tnl | grep 3389
LISTEN     0      20           *:3389                     *:*                  
[root@centos-s-6vcpu-16gb-nyc1-01 data]# ip addr s eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 5e:30:bc:a7:cb:2d brd ff:ff:ff:ff:ff:ff
    inet 142.93.58.27/20 brd 142.93.63.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 10.10.0.5/16 brd 10.10.255.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5c30:bcff:fea7:cb2d/64 scope link 
       valid_lft forever preferred_lft forever
[root@centos-s-6vcpu-16gb-nyc1-01 data]#

基于DigitalOcean制作OpenStack的镜像
基于DigitalOcean制作OpenStack的镜像

[root@centos-s-6vcpu-16gb-nyc1-01 data]# virsh list --all   
 Id    Name                           State
----------------------------------------------------
 10    windows2016                    running
 11    centos                         running

[root@centos-s-6vcpu-16gb-nyc1-01 data]# virsh domifaddr 11 
 Name       MAC address          Protocol     Address
-------------------------------------------------------------------------------
 vnet1      52:54:00:13:2a:ac    ipv4         192.168.122.244/24

[root@centos-s-6vcpu-16gb-nyc1-01 data]# proxy -l 22992 -h 192.168.122.244 -p 22     
[root@centos-s-6vcpu-16gb-nyc1-01 data]# ss -tnl | grep 22992
LISTEN     0      20           *:22992                    *:*                  
[root@centos-s-6vcpu-16gb-nyc1-01 data]# ip addr s eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 5e:30:bc:a7:cb:2d brd ff:ff:ff:ff:ff:ff
    inet 142.93.58.27/20 brd 142.93.63.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 10.10.0.5/16 brd 10.10.255.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5c30:bcff:fea7:cb2d/64 scope link 
       valid_lft forever preferred_lft forever
[root@centos-s-6vcpu-16gb-nyc1-01 data]# 

基于DigitalOcean制作OpenStack的镜像

11、将镜像虚拟机的光驱弹出

[root@centos-s-6vcpu-16gb-nyc1-01 data]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 10    windows2016                    running
 11    centos                         running
 14    centos7                        running

[root@centos-s-6vcpu-16gb-nyc1-01 data]# virsh dumpxml centos | grep -I10 cdrom
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/data/centos.qcow2'/>
      <backingStore/>
      <target dev='vda' bus='virtio'/>
      <alias name='virtio-disk0'/>
      
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
    </disk>
    <disk type='file' device='cdrom'>
      <target dev='hda' bus='ide'/>
      <readonly/>
      <alias name='ide0-0-0'/>
      
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
    <controller type='usb' index='0' model='ich9-ehci1'>
      <alias name='usb'/>
      
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci1'>
[root@centos-s-6vcpu-16gb-nyc1-01 data]#

通过上面的查询我们得知镜像挂载在了hda上了,下面弹出镜像

[root@centos-s-6vcpu-16gb-nyc1-01 data]# virsh attach-disk --type cdrom --mode readonly centos "" hda 
Disk attached successfully

[root@centos-s-6vcpu-16gb-nyc1-01 data]#

12、所有的操作之前给虚拟机做个快照,原因大家懂的

[root@centos-s-6vcpu-16gb-nyc1-01 data]# virsh snapshot-create-as --domain centos --name "20180828_131612" --description "开始制作镜像-新系统-20180828_131632"
Domain snapshot 20180828_131612 created
[root@centos-s-6vcpu-16gb-nyc1-01 data]# virsh snapshot-list centos
 Name                 Creation Time             State
------------------------------------------------------------
 20180828_131612      2018-08-28 05:17:37 +0000 running

[root@centos-s-6vcpu-16gb-nyc1-01 data]#

二:开始制作CentOS镜像
1、给系统安装acpid,用于电源状态同步,不安装会出现无法关机重启的现象

[root@DT_Node-192_168_122_244 ~]# yum install acpid -y
[root@DT_Node-192_168_122_244 ~]# systemctl enable acpid.service

2、配置元数据
一般在实例创建最后一步时,必须要获取到用户的公钥信息,还有用户的数据和脚本,如何获取到? 此时就需要guest host 安装cloud-init程序包来获取这些信息:

[root@DT_Node-192_168_122_244 ~]# yum install cloud-init -y

在基于CentOS的虚拟机上,公钥信息会被存放在centos帐号下,也就是可以使用centos作为用户名,通过密钥远程登录。通过编辑/etc/cloud/cloud.cfg文件可以使用其他的用户帐号名称。

[root@DT_Node-192_168_122_244 ~]# sed -ri 's/^(disable_root: ).*/\10/' /etc/cloud/cloud.cfg #启用root用户
[root@DT_Node-192_168_122_244 ~]# sed -ri 's/^(ssh_pwauth: ).*/\11/' /etc/cloud/cloud.cfg         #允许密码登录
[root@DT_Node-192_168_122_244 ~]# echo 'datasource_list: ["OpenStack"]

datasource:
  OpenStack:
    metadata_urls: ["http://169.254.169.254"]
    max_wait: -1
    timeout: 10
    retries: 5' >> /etc/cloud/cloud.cfg
[root@DT_Node-192_168_122_244 ~]# 

3、安装cloud-utils-growpart 以允许调整分区大小

[root@DT_Node-192_168_122_244 ~]# yum install cloud-utils-growpart -y

4、禁用zeroconf 路由
对于访问元数据服务的实例,必须禁用默认的zeroconf路由:

[root@DT_Node-192_168_122_244 ~]# echo "NOZEROCONF=yes" >> /etc/sysconfig/network
[root@DT_Node-192_168_122_244 ~]# head -2 /etc/sysconfig/network
# Created by anaconda
NOZEROCONF=yes
[root@DT_Node-192_168_122_244 ~]#

5、修改网卡配置

[root@DT_Node-192_168_122_244 ~]# echo 'DEVICE="eth0"                          
BOOTPROTO="dhcp"
ONBOOT="yes"
TYPE="Ethernet"
NM_CONTROLLED="no"' > /etc/sysconfig/network
[root@DT_Node-192_168_122_244 ~]# cat /etc/sysconfig/network
DEVICE="eth0"
BOOTPROTO="dhcp"
ONBOOT="yes"
TYPE="Ethernet"
NM_CONTROLLED="no"
[root@DT_Node-192_168_122_244 ~]#

6、配置控制台

[root@DT_Node-192_168_122_244 ~]# grep 'GRUB_CMDLINE_LINUX' /etc/default/grub                         
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=DTVG/root rd.lvm.lv=DTVG/swap rhgb quiet ipv6.disable=1"
[root@DT_Node-192_168_122_244 ~]# sed -ri 's/ (rhgb|quiet)//g' /etc/default/grub                      
[root@DT_Node-192_168_122_244 ~]# sed -ri 's/^(GRUB_CMDLINE_LINUX.*)"$/\1 console=tty0 console=ttyS0,115200n8"/' /etc/default/grub
[root@DT_Node-192_168_122_244 ~]# sed -ri 's/(crashkernel=)auto/\1256M/' /etc/default/grub
[root@DT_Node-192_168_122_244 ~]# grep 'GRUB_CMDLINE_LINUX' /etc/default/grub 
GRUB_CMDLINE_LINUX="crashkernel=256M ipv6.disable=1 console=tty0 console=ttyS0,115200n8"
[root@DT_Node-192_168_122_244 ~]# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.18.5-1.el7.elrepo.x86_64
Found initrd image: /boot/initramfs-4.18.5-1.el7.elrepo.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-862.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-862.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-e3627439b8094c4b905ee28ed2e3ebeb
Found initrd image: /boot/initramfs-0-rescue-e3627439b8094c4b905ee28ed2e3ebeb.img
done
[root@DT_Node-192_168_122_244 ~]# awk -F\' '$1=="menuentry " {print $2}' /etc/grub2.cfg
CentOS Linux (4.18.5-1.el7.elrepo.x86_64) 7 (Core)
CentOS Linux (3.10.0-862.el7.x86_64) 7 (Core)
CentOS Linux (0-rescue-e3627439b8094c4b905ee28ed2e3ebeb) 7 (Core)
[root@DT_Node-192_168_122_244 ~]# grub2-set-default 0
[root@DT_Node-192_168_122_244 ~]#

7、关机

[root@DT_Node-192_168_122_244 ~]# poweroff

8、回到宿主机上来给虚拟机做清除动作
在实例运行中,操作系统会将虚拟以太网卡的MAC地址记录在/etc/sysconfig/network-scripts/ifcfg-eth0等位置。但是,每次使用镜像启动新实例时,虚拟以太网卡将会有不同的MAC地址,因此必须从配置文件中删除该信息。
可以用virt-sysprep 程序,完成各种清理任务,如删除MAC地址引用:

[root@centos-s-6vcpu-16gb-nyc1-01 ~]# virt-sysprep -d centos
[   0.0] Examining the guest ...
[  24.6] Performing "abrt-data" ...
[  24.6] Performing "backup-files" ...
[  26.5] Performing "bash-history" ...
[  26.5] Performing "blkid-tab" ...
[  26.5] Performing "crash-data" ...
[  26.5] Performing "cron-spool" ...
[  26.5] Performing "dhcp-client-state" ...
[  26.5] Performing "dhcp-server-state" ...
[  26.5] Performing "dovecot-data" ...
[  26.5] Performing "logfiles" ...
[  26.6] Performing "machine-id" ...
[  26.6] Performing "mail-spool" ...
[  26.6] Performing "net-hostname" ...
[  26.6] Performing "net-hwaddr" ...
[  26.6] Performing "pacct-log" ...
[  26.6] Performing "package-manager-cache" ...
[  26.7] Performing "pam-data" ...
[  26.7] Performing "passwd-backups" ...
[  26.7] Performing "puppet-data-log" ...
[  26.7] Performing "rh-subscription-manager" ...
[  26.7] Performing "rhn-systemid" ...
[  26.7] Performing "rpm-db" ...
[  26.7] Performing "samba-db-log" ...
[  26.7] Performing "script" ...
[  26.7] Performing "smolt-uuid" ...
[  26.7] Performing "ssh-hostkeys" ...
[  26.7] Performing "ssh-userdir" ...
[  26.7] Performing "sssd-db-log" ...
[  26.7] Performing "tmp-files" ...
[  26.8] Performing "udev-persistent-net" ...
[  26.8] Performing "utmp" ...
[  26.8] Performing "yum-uuid" ...
[  26.8] Performing "customize" ...
[  26.8] Setting a random seed
[  26.8] Setting the machine ID in /etc/machine-id
[  27.2] Performing "lvm-uuids" ...
[root@centos-s-6vcpu-16gb-nyc1-01 ~]#

9、删除虚拟机的嗯快照

[root@centos-s-6vcpu-16gb-nyc1-01 data]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 10    windows2016                    running
 14    centos7                        running
 -     centos                         shut off

[root@centos-s-6vcpu-16gb-nyc1-01 data]# virsh snapshot-list centos
 Name                 Creation Time             State
------------------------------------------------------------
 20180828_131612      2018-08-28 05:17:37 +0000 running

[root@centos-s-6vcpu-16gb-nyc1-01 data]# virsh list --all          
 Id    Name                           State
----------------------------------------------------
 10    windows2016                    running
 14    centos7                        running
 -     centos                         shut off

[root@centos-s-6vcpu-16gb-nyc1-01 data]# virsh snapshot-list centos --tree
20180828_131612


[root@centos-s-6vcpu-16gb-nyc1-01 data]# virsh snapshot-list centos
 Name                 Creation Time             State
------------------------------------------------------------
 20180828_131612      2018-08-28 05:17:37 +0000 running

[root@centos-s-6vcpu-16gb-nyc1-01 data]# virsh snapshot-info centos  --snapshotname 20180828_131612
Name:           20180828_131612
Domain:         centos
Current:        yes
State:          running
Location:       internal
Parent:         -
Children:       0
Descendants:    0
Metadata:       yes

[root@centos-s-6vcpu-16gb-nyc1-01 data]# virsh snapshot-delete --domain centos --snapshotname 20180828_131612
Domain snapshot 20180828_131612 deleted

[root@centos-s-6vcpu-16gb-nyc1-01 data]# virsh snapshot-list centos
 Name                 Creation Time             State
------------------------------------------------------------

[root@centos-s-6vcpu-16gb-nyc1-01 data]#

10、取消定义libvirt域

[root@centos-s-6vcpu-16gb-nyc1-01 data]# virsh undefine centos
Domain centos has been undefined

[root@centos-s-6vcpu-16gb-nyc1-01 data]# virsh list --all                 
 Id    Name                           State
----------------------------------------------------
 10    windows2016                    running
 14    centos7                        running

[root@centos-s-6vcpu-16gb-nyc1-01 data]#

11、压缩镜像

[root@centos-s-6vcpu-16gb-nyc1-01 data]# virt-sparsify --compress /data/centos.qcow2 CentOS-7.5.1804-x86_64-Cloud.qcow2
[   0.1] Create overlay file in /tmp to protect source disk
[   0.2] Examine source disk
[   4.2] Fill free space in /dev/DTVG/root with zero
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
[  67.4] Clearing Linux swap on /dev/DTVG/swap
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
[  73.4] Fill free space in /dev/sda1 with zero
[  75.7] Copy to destination and make sparse
[ 262.9] Sparsify operation completed with no errors.
virt-sparsify: Before deleting the old disk, carefully check that the 
target disk boots and works correctly.

三、开始制作Windows镜像
基于DigitalOcean制作OpenStack的镜像

基于DigitalOcean制作OpenStack的镜像

基于DigitalOcean制作OpenStack的镜像

基于DigitalOcean制作OpenStack的镜像
1、到了这时候会发现找不到系统盘,没关系,我们加载下驱动就可以了,然后继续安装系统。
基于DigitalOcean制作OpenStack的镜像

基于DigitalOcean制作OpenStack的镜像

基于DigitalOcean制作OpenStack的镜像
2、进了系统之后会发现网卡驱动也没有,我们再来安装网卡驱动
基于DigitalOcean制作OpenStack的镜像

基于DigitalOcean制作OpenStack的镜像

基于DigitalOcean制作OpenStack的镜像

基于DigitalOcean制作OpenStack的镜像

基于DigitalOcean制作OpenStack的镜像

基于DigitalOcean制作OpenStack的镜像

基于DigitalOcean制作OpenStack的镜像

基于DigitalOcean制作OpenStack的镜像

3、安装Cloudbase-Init
下载地址:http://www.cloudbase.it/downloads/CloudbaseInitSetup_Stable_x64.msi
为了让Cloudbase-Init在系统启动时运行脚本,设置PowerShell执行策略解除限制。

Windows PowerShell
版权所有 (C) 2016 Microsoft Corporation。保留所有权利。

PS C:\Windows\system32> Set-ExecutionPolicy Unrestricted

执行策略更改
执行策略可帮助你防止执行不信任的脚本。更改执行策略可能会产生安全风险,如 http://go.microsoft.com/fwlink/?LinkID=135170
中的 about_Execution_Policies 帮助主题所述。是否要更改执行策略?
[Y] 是(Y)  [A] 全是(A)  [N] 否(N)  [L] 全否(L)  [S] 暂停(S)  [?] 帮助 (默认值为“N”): Y
PS C:\Windows\system32>

基于DigitalOcean制作OpenStack的镜像

双击安装

在configuration options窗口,修改以下设置:
•日志输出串口:COM1

当安装完成后,在Complete the Cloudbase-Init Setup Wizard 窗口,选择Run
Sysprep和Shutdown 复选框,然后点击Finish。
等待虚机关闭。
你的镜像已经准备好上传到镜像服务了

lookback

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: