Create Image
Create Using Packer
If you need to customize distribution images for different business use, you need to create images yourself.
You can download the distribution operating system's ISO, then locally start a virtual machine, install the ISO to the virtual machine's disk, then save that disk. This disk can be uploaded to glance as an image. However, this method has too many manual steps and is error-prone.
It is recommended to use packer tool to automate image creation. For detailed operations, please refer to the corresponding documentation: https://www.packer.io/docs/index.html.
The https://github.com/yunionio/service-images repository contains some configurations we use packer to create images, which you can refer to.
Manual Creation Process
Import ISO
- Prepare standard ISO images in advance. Support users to import from Image Market-ISO interface or directly upload ISO images.
- Create a new virtual machine in the virtual machine list, select "Boot from ISO" and select the corresponding ISO image. After creation is successful, install the operating system through VNC terminal according to interface prompts.
- It is recommended to use CentOS Minimal operating system.
- For Ubuntu/Debian images, it is recommended to select "No automatic updates" during installation and install OpenSSH Server software.
- Perform different optimization configurations according to the image's operating system type.
- CentOS images: Please refer to CentOS Image Optimization section.
- Ubuntu/Debian images: Please refer to Ubuntu/Debian Image Optimization section.
- Windows images: Please refer to Windows Image Optimization section.
- (Optional) Multi-platform universal image configuration. If the created image needs to be used on public cloud platforms, in addition to the above optimization configurations, you also need to Install and Configure cloud-init for Linux Systems, Install and Configure Cloudbase-init for Windows Systems.
- After image optimization is complete, you need to shut down the virtual machine.
- Click the "More" button in the operation column on the right side of the powered-off virtual machine, select the dropdown menu "Save Image" menu item to save the virtual machine as a system image.
- After the image is saved, users can create a new virtual machine in the virtual machine list, select "Custom Image" and select the image saved in the previous step, use the created image to create a virtual machine, and verify whether the image creation is successful.
CentOS Image Optimization
Taking CentOS 7 minimal image as an example to introduce image optimization methods.
-
After CentOS 7 Minimal operating system installation is complete, the virtual machine cannot connect to the network by default. You need to modify the /etc/sysconfig/network-scripts/ifcfg-eth0 file, change "ONBOOT=no" to "ONBOOT=yes".
# Please modify the corresponding configuration file according to the actual network card name
$ vi /etc/sysconfig/network-scripts/ifcfg-eth0 # Please modify the corresponding configuration file according to the actual network card name
# Modify configuration file content
ONBOOT=yes
# CentOS 8 and CentOS 9 restart network card: systemctl restart NetworkManager.service -
Disable selinux, modify /etc/selinux/config file, change "SELINUX=enforcing" to "SELINUX=disabled". After modification is complete, restart the system to take effect.
$ vi /etc/selinux/config
# Modify configuration file content, save after modification is complete.
SELINUX=disabled
# Restart to make configuration take effect
$ reboot -
Add necessary kernel modules to boot initram.img. (CentOS 8 kernel already has virtio installed, so it needs to be removed from the list)
$ vi /etc/dracut.conf
# Modify configuration file, remove # comment before add_drivers+, and add the following content in quotes, save after modification is complete.
# The following are kernel drivers that need to be added for x86
add_drivers+=" hpsa mptsas mpt2sas mpt3sas megaraid_sas mptspi virtio virtio_ring virtio_pci virtio_scsi virtio_blk vmw_pvscsi nvme "
# The following are kernel drivers that need to be added for arm
add_drivers+=" mptsas mpt2sas mpt3sas megaraid_sas mptspi virtio virtio_ring virtio_pci virtio_scsi virtio_blk nvme "
# Make configuration take effect
$ dracut -f -
Disable network card persistence function to ensure network card names in CentOS 7 are in "eth0, eth1" form. Modify /etc/default/grub file, add "net.ifnames=0 biosdevname=0" parameters to GRUB_CMDLINE_LINUX. (Skip for centos8, centos9)
$ vi /etc/default/grub
# Modify configuration file, save after modification is complete.
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet net.ifnames=0 biosdevname=0"
# Make configuration take effect
$ grub2-mkconfig -o /boot/grub2/grub.cfg -
Install common software according to requirements.
# This is just an example, please install common software according to actual requirements.
$ yum install net-tools git wget vim pcre-tools ntp epel-release -y -
Disable firewalld and NetworkManager services.
$ systemctl disable firewalld NetworkManager -
Enable time network synchronization, support using ntp or chrony to maintain time synchronization.
# Install ntp or chrony software
$ yum install ntp/chrony -y
# Enable ntp or chronyd service
$ systemctl enable ntpd/chronyd -
Modify timezone to CST.
$ timedatectl set-timezone Asia/Shanghai
# View current timezone
$ timedatectl status -
SSH service optimization, modify /etc/ssh/sshd_config file, change PermitRootLogin attribute to yes, change UseDNS attribute to no.
$ vi /etc/ssh/sshd_config
# Find PermitRootLogin attribute and UseDNS attribute respectively
PermitRootLogin yes
UseDNS nosystemctl restart sshd
Ubuntu/Debian Image Optimization
Taking ubuntu-16.04.6-server-amd64.iso as an example, introduce Ubuntu and Debian image optimization methods. After Ubuntu system installation is complete, it uses ordinary permission users by default.
-
SSH service optimization, modify /etc/ssh/sshd_config file, change PermitRootLogin attribute to yes, change UseDNS attribute to no. If the above attributes do not exist, please add attributes.
$ sudo vi /etc/ssh/sshd_config
# Find PermitRootLogin attribute and UseDNS attribute respectively
PermitRootLogin yes
UseDNS no -
Create a startup script named ssh-initkey in /etc/init.d/ directory.
$ sudo touch /etc/init.d/ssh-initkey
$ sudo vi /etc/init.d/ssh-initkey
# Script content is as follows:
#! /bin/sh
### BEGIN INIT INFO
# Provides: ssh-initkey
# Required-Start:
# Required-Stop:
# X-Start-Before: ssh
# Default-Start: 2 3 4 5
# Default-Stop:
# Short-Description: Init ssh host keys
### END INIT INFO
PATH=/sbin:/usr/sbin:/bin:/usr/bin
. /lib/init/vars.sh
. /lib/lsb/init-functions
do_start() {
ls /etc/ssh/ssh_host_* > /dev/null 2>&1
if [ $? -ne 0 ]; then
dpkg-reconfigure openssh-server
fi
}
case "$1" in
start)
do_start
;;
restart|reload|force-reload)
echo "Error: argument '$1' not supported" >&2
exit 3
;;
stop)
;;
*)
echo "Usage: $0 start|stop" >&2
exit 3
;;
esac -
After ssh-initkey script configuration is complete, you also need to add executable permissions and add the script to the system startup script directory.
$ sudo chmod +x /etc/init.d/ssh-initkeyFor Ubuntu versions before 20.04, please execute the following script to enable the script:
$ sudo /usr/sbin/update-rc.d ssh-initkey defaults
$ sudo /usr/sbin/update-rc.d ssh-initkey enableFor Ubuntu 20.04 and later versions, please execute the following script to enable the script:
$ sudo /lib/systemd/systemd-sysv-install enable ssh-initkey -
(Settings for Ubuntu 16.04 and above) Disable network card persistence function to ensure network card names are in "eth0, eth1" form. Modify /etc/default/grub file, add "net.ifnames=0 biosdevname=0" parameters to GRUB_CMDLINE_LINUX.
$ sudo vi /etc/default/grub
# Configure GRUB_CMDLINE_LINUX parameters
GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0"# Make configuration take effect
$ sudo /usr/sbin/update-grub -
(If Ubuntu has not disabled automatic updates) Disable automatic updates, need to modify /etc/apt/apt.conf.d/10periodic file, set "Update-Package-Lists" parameter in the file to 0.
$ sudo vi /etc/apt/apt.conf.d/10periodic
# Configuration modification
APT::Periodic::Update-Package-Lists "0"; -
At this point, virtual machine optimization is complete.