背景:业务方需求需要ubuntu 18.4版本的系统

下载ubuntu 18.4 wget http://archive.ubuntu.com/ubuntu/dists/bionic/main/installer-amd64/current/images/netboot/mini.iso

通过镜像创建kvm虚拟机(kvm的环境安装不作介绍了,前提要在biso开启cpu虚拟化) virt-install --virt-type kvm --name ubuntu --ram 1024 --cdrom=/vm/iso/ubuntu-mini.iso --disk /vm/vm/ubuntu/os.img,bus=virtio,size=4,format=qcow2 --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole --os-type=linux --os-variant=ubuntu18.04

通过控制台配置安装

设置ip 配置主机名 添加用户ubuntu

选在openssh server即可

系统安装完成

添加管理key

root@ubuntu:/home/ubuntu/.ssh# find / -name authorized_keys
/root/.ssh/authorized_keys
/home/ubuntu/.ssh/authorized_keys

关闭用户登录,开启key登录 vim /etc/sshd/sshd_config 找到PubkeyAuthentication 改为yes //开启密钥登陆 找到PasswordAuthentication 改为no //关闭密码登陆

关闭防火墙和selinux

root@ubuntu:~# ufw disable
Firewall stopped and disabled on system startup
root@ubuntu:~# 
root@ubuntu:~# getenforce 
Disabled

安装cloud-init包 apt install cloud-init -y

设置镜像使用的元数据源,运行 dpkg-reconfigure命令cloud-init。出现提示时,选择EC2数据源: dpkg-reconfigure cloud-init

网络设置dhcp获取 /etc/netplan/01-netcfg.yaml

network:
		versiom: 2
		renderer: networkd
		ethernets:
		
		   ens3:
			   dhcp4:yes

编辑/etc/default/grub文件并配置 GRUB_CMDLINE_LINUX选项。让openstack能获取日志 GRUB_CMDLINE_LINUX=" console=ttyS0,115200n8"

更新grub文件

update-grub 

关机,清理mac等信息 virt-sysprep -d bionic

查看磁盘格式。我们不用qcow2的格式,使用raw格式,2种的区别可以去google下。 转换磁盘格式

[root@tbj*** ubuntu-18.4]# qemu-img info os.img 
image: os.img
file format: qcow2
virtual size: 4.0G (4294967296 bytes)
disk size: 2.2G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true
    refcount bits: 16
    corrupt: false
[root@tbj*** ubuntu-18.4]# qemu-img convert -f qcow2 -O raw os.img ubuntu-18.4.raw
[root@tbj*** ubuntu-18.4]# qemu-img info ubuntu-18.4.raw 
image: ubuntu-18.4.raw
file format: raw
virtual size: 4.0G (4294967296 bytes)
disk size: 2.1G
[root@tbj*** ubuntu-18.4]# 

将做好的raw文件上传到openstack控制节点生成镜像。 openstack image create "ubuntu-18.04" --file /root/ubuntu-18.4.raw --disk-format raw

web界面已经可以看到这个镜像了

添加完成


[root@wuhan31-ceph01 ~]# openstack image list
+--------------------------------------+--------------------------------+--------+
| ID                                   | Name                           | Status |
+--------------------------------------+--------------------------------+--------+
| 0e548332-80a5-4d57-85a5-d4dee50b52fa | centos-7-x86_64-antiy-20190508 | active |
| 2dd5b64b-0b9d-433a-8b64-e1553d704c2c | online                         | active |
| 099824a6-2081-48a5-b871-740adff8b297 | ubuntu-18.04                   | active |
+--------------------------------------+--------------------------------+--------+
[root@wuhan31-ceph01 ~]#

测试: 使用这个镜像创建虚拟机

日志正常

启动正常