Create OpenStack and Docker base image based on CentOS7-mini ISO

在Docker Hub下载的CentOS镜像没有Network组件,而且不可以自己定制,我们为了解决这个问题,需要创建自己的CentOS镜像,并对其进行定制。本文使用CentOS的ISO文件生成用于OpenStack的image文件,并使用该image生成Docker的image文件。

1 Prepare

We have installed the virt-manager on CentOS7-mini. We use virt-manager to install the ISO, and then generate the centos7.img, which is the full disk of VM.

1.1 Install VM via virt-manager

You should take attention on the follow step.

(1) We choose [Local install media (ISO image or CDROM)] to install the operating system on VM.

(2) Then choose  [Use ISO image], and click [Browse...] to find the volume. Select Linux as OS type and select the fit Version.

(3) If has no the storage pool, you can click [+] on the left low to create a folder as the storage pool.

(4) We should create the vm in an image file, and use this image for OpenStack. We choose [Enable storage for this virtual machine] and [Select or create custom storage], then click [Manage...] to find the volume. 

(5) We can click [+] to create storage volume (centos7.img) in the above storage pool. We select the qcow2 as the storage format to save the disk space and we set the Max Capacity to 100 GiB.

(6) Step by step to create and install VM, and make sure the VM to be working.

1.2 Install some packages on VM

As we know, the CentOS7-mini has few softwares, we can install some packages to enable more function.

1 yum install -y vim wget git net-tools

1.3 Replace the boot efi

To avoid the CentOS boot issue, we should replace /boot/efi/EFI/BOOT/BOOTAA64.EFI via /boot/efi/EFI/centos/grubaa64.efi.

1 cp -f /boot/efi/EFI/centos/grubaa64.efi /boot/efi/EFI/BOOT/BOOTAA64.EFI

2 Create the image for OpenStack

2.1 Convert the image format

The centos7.img is still 100G, we should convert the image format to save the disk space.

1 qemu-img convert -c -O qcow2 centos7.img centos7.qcow2

2.2 Upload the image to OpenStack

1 openstack image create --disk-format qcow2 --container-format bare --public --property hw_firmware_type="uefi" --file centos7.qcow2 centos7.qcow2

3 Create the image for Docker

 3.1 Pack the system disk “/”

 Docker need the “/” folder of centos as docker images. We use tar to pack the “/” folder.

1 modprobe nbd max_part=16
2 qemu-nbd -c /dev/nbd0 centos7.qcow2
3 mount /dev/ndb0p4 /mnt
4 chroot /mnt bash
5 tar --numeric-owner --exclude=/proc --exclude=/sys -cvf centos7.tar /

3.2 Import the docker image

 Import centos as docker image, and use centos7-local to run an container.

1 cat centos7.tar | docker import - centos7-local
2 docker images
3 docker run -d --name "centos7-local" centos7-local /bin/bash
4 docker exec -it centos7-local /bin/bash

4 Create docker image and enable systemctl

4.1 Create image from the iso

# Use local iso as repo

1 wget http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1804.iso
2 mount CentOS-7-x86_64-Minimal-1804.iso /mnt
3 cd /etc/yum.repo.d/
4 mkdir bak
5 mv *.repo bak/
6 vim CentOS-Local.repo
1 [CentOS-Local]
2 baseurl = file:///mnt/
3 gpgcheck = 0
4 enabled = 1
5 name = CentOS-Local
7 yum clean all
8 rm -rf /var/cache/yum

# Create a base CentOS Docker image

1 vim mkimage-yum.sh
  1 #!/usr/bin/env bash
  2 #
  3 # Create a base CentOS Docker image.
  4 #
  5 # This script is useful on systems with yum installed (e.g., building
  6 # a CentOS image on CentOS).  See contrib/mkimage-rinse.sh for a way
  7 # to build CentOS images on other systems.
  8 
  9 set -e
 10 
 11 usage() {
 12     cat <<EOOPTS
 13 $(basename $0) [OPTIONS] <name>
 14 OPTIONS:
 15   -p "<packages>"  The list of packages to install in the container.
 16                    The default is blank.
 17   -g "<groups>"    The groups of packages to install in the container.
 18                    The default is "Core".
 19   -y <yumconf>     The path to the yum config to install packages from. The
 20                    default is /etc/yum.conf for Centos/RHEL and /etc/dnf/dnf.conf for Fedora
 21 EOOPTS
 22     exit 1
 23 }
 24 
 25 # option defaults
 26 yum_config=/etc/yum.conf
 27 if [ -f /etc/dnf/dnf.conf ] && command -v dnf &> /dev/null; then
 28     yum_config=/etc/dnf/dnf.conf
 29     alias yum=dnf
 30 fi
 31 install_groups="Core"
 32 while getopts ":y:p:g:h" opt; do
 33     case $opt in
 34         y)
 35             yum_config=$OPTARG
 36             ;;
 37         h)
 38             usage
 39             ;;
 40         p)
 41             install_packages="$OPTARG"
 42             ;;
 43         g)
 44             install_groups="$OPTARG"
 45             ;;
 46         \?)
 47             echo "Invalid option: -$OPTARG"
 48             usage
 49             ;;
 50     esac
 51 done
 52 shift $((OPTIND - 1))
 53 name=$1
 54 
 55 if [[ -z $name ]]; then
 56     usage
 57 fi
 58 
 59 target=$(mktemp -d --tmpdir $(basename $0).XXXXXX)
 60 
 61 set -x
 62 
 63 mkdir -m 755 "$target"/dev
 64 mknod -m 600 "$target"/dev/console c 5 1
 65 mknod -m 600 "$target"/dev/initctl p
 66 mknod -m 666 "$target"/dev/full c 1 7
 67 mknod -m 666 "$target"/dev/null c 1 3
 68 mknod -m 666 "$target"/dev/ptmx c 5 2
 69 mknod -m 666 "$target"/dev/random c 1 8
 70 mknod -m 666 "$target"/dev/tty c 5 0
 71 mknod -m 666 "$target"/dev/tty0 c 4 0
 72 mknod -m 666 "$target"/dev/urandom c 1 9
 73 mknod -m 666 "$target"/dev/zero c 1 5
 74 
 75 # amazon linux yum will fail without vars set
 76 if [ -d /etc/yum/vars ]; then
 77     mkdir -p -m 755 "$target"/etc/yum
 78     cp -a /etc/yum/vars "$target"/etc/yum/
 79 fi
 80 
 81 if [[ -n "$install_groups" ]];
 82 then
 83     yum -c "$yum_config" --installroot="$target" --releasever=/ --setopt=tsflags=nodocs \
 84         --setopt=group_package_types=mandatory -y groupinstall "$install_groups"
 85 fi
 86 
 87 if [[ -n "$install_packages" ]];
 88 then
 89     yum -c "$yum_config" --installroot="$target" --releasever=/ --setopt=tsflags=nodocs \
 90         --setopt=group_package_types=mandatory -y install "$install_packages"
 91 fi
 92 
 93 yum -c "$yum_config" --installroot="$target" -y clean all
 94 
 95 cat > "$target"/etc/sysconfig/network <<EOF
 96 NETWORKING=yes
 97 HOSTNAME=localhost.localdomain
 98 EOF
 99 
100 # effectively: febootstrap-minimize --keep-zoneinfo --keep-rpmdb --keep-services "$target".
101 #  locales
102 rm -rf "$target"/usr/{{lib,share}/locale,{lib,lib64}/gconv,bin/localedef,sbin/build-locale-archive}
103 #  docs and man pages
104 rm -rf "$target"/usr/share/{man,doc,info,gnome/help}
105 #  cracklib
106 rm -rf "$target"/usr/share/cracklib
107 #  i18n
108 rm -rf "$target"/usr/share/i18n
109 #  yum cache
110 rm -rf "$target"/var/cache/yum
111 mkdir -p --mode=0755 "$target"/var/cache/yum
112 #  sln
113 rm -rf "$target"/sbin/sln
114 #  ldconfig
115 rm -rf "$target"/etc/ld.so.cache "$target"/var/cache/ldconfig
116 mkdir -p --mode=0755 "$target"/var/cache/ldconfig
117 
118 version=
119 for file in "$target"/etc/{redhat,system}-release
120 do
121     if [ -r "$file" ]; then
122         version="$(sed 's/^[^0-9\]*\([0-9.]\+\).*$/\1/' "$file")"
123         break
124     fi
125 done
126 
127 if [ -z "$version" ]; then
128     echo >&2 "warning: cannot autodetect OS version, using '$name' as tag"
129     version=$name
130 fi
131 
132 tar --numeric-owner -c -C "$target" . | docker import - $name:$version
133 
134 docker run -i -t --rm $name:$version /bin/bash -c 'echo success'
135 
136 rm -rf "$target"
View Code
 
 
2 chmod +x mkimage-yum.sh
3 ./mkimage-yum.sh centos-lingmo

4.2 Run docker and enable systemctl

1 docker images
2 docker run -d --privileged=true --name centos-test centos-lingmo_ID /usr/sbin/init
3 docker exec -it centos-test /bin/bash

Now, you can run systemctl to manage the server on docker vm.

5 Reference

https://github.com/moby/moby/blob/master/contrib/mkimage-yum.sh

https://blog.csdn.net/luckytanggu/article/details/71514798

猜你喜欢

转载自www.cnblogs.com/hxt-lingmo/p/9268858.html