Cloud-init allows you to customize configurations during the first initialization of an instance. If the imported image does not have the cloud-init service installed, instances booted through the image cannot be initialized properly. As a result, the image will fail to be imported. This document describes how to install the cloud-init service.
You can use either of the following methods to install cloud-init:
Before importing a Linux image, ensure that you have properly installed the cloud-init service in the image.
A server with the cloud-init service installed can correctly access the public network.
Note
- cloud-init-17.1.tar.gz is recommended. You can visit https://launchpad.net/cloud-init/+download to download other versions.
- If the installation fails, you can try the portable cloud-init package
Run the following command to download the cloud-init source package:
wget https://launchpad.net/cloud-init/trunk/17.1/+download/cloud-init-17.1.tar.gz
NoteIf you are using the Ubuntu operating system, run this command with the “root” account.
tar -zxvf cloud-init-17.1.tar.gz
cd cloud-init-17.1
yum install python-pip -y
apt-get install python-pip -y
NotePython 2.6 is not supported when cloud-init uses requests 2.20.0 or later. If the Python interpreter installed in the image environment is Python version 2.6 or earlier, run the
pip install 'requests<2.20.0'
command to install requests 2.20.0 or later before installing the cloud-init dependencies.
pip install -r requirements.txt
yum install cloud-utils-growpart dracut-modules-growroot -y
dracut -f
yum install cloud-utils-growpart -y
apt-get install cloud-guest-utils -y
python setup.py build
python setup.py install --init-system systemd
NoteThe
--init-system
can be followed by any of systemd, sysvinit, sysvinit_deb, sysvinit_freebsd, sysvinit_openrc, sysvinit_suse or upstart [default: None]. Please configure parameters based on the auto-start service management method of the operating system. If incorrect parameters are configured, the cloud-init service cannot automatically start upon system startup. This document uses the systemd auto-start service management method as an example.
/etc/cloud/cloud.cfg
with that of the downloaded cloud.cfg file.Run the following command to add a syslog user:
useradd syslog
NoteTo check whether the operating system uses systemd, run the
strings /sbin/init | grep "/lib/system"
command, and you will receive a return message.
ln -s /usr/local/bin/cloud-init /usr/bin/cloud-init
systemctl enable cloud-init-local.service
systemctl start cloud-init-local.service
systemctl enable cloud-init.service
systemctl start cloud-init.service
systemctl enable cloud-config.service
systemctl start cloud-config.service
systemctl enable cloud-final.service
systemctl start cloud-final.service
systemctl status cloud-init-local.service
systemctl status cloud-init.service
systemctl status cloud-config.service
systemctl status cloud-final.service
/lib/systemd/system/cloud-init-local.service
with the following:[Unit]
Description=Initial cloud-init job (pre-networking)
Wants=network-pre.target
After=systemd-remount-fs.service
Before=NetworkManager.service
Before=network-pre.target
Before=shutdown.target
Conflicts=shutdown.target
RequiresMountsFor=/var/lib/cloud
[Service]
Type=oneshot
ExecStart=/usr/bin/cloud-init init --local
ExecStart=/bin/touch /run/cloud-init/network-config-ready
RemainAfterExit=yes
TimeoutSec=0
# Output needs to appear in instance console output
StandardOutput=journal+console
[Install]
WantedBy=cloud-init.target
/lib/systemd/system/cloud-init.service
with the following:[Unit]
Description=Initial cloud-init job (metadata service crawler)
Wants=cloud-init-local.service
Wants=sshd-keygen.service
Wants=sshd.service
After=cloud-init-local.service
After=systemd-networkd-wait-online.service
After=networking.service
After=systemd-hostnamed.service
Before=network-online.target
Before=sshd-keygen.service
Before=sshd.service
Before=systemd-user-sessions.service
Conflicts=shutdown.target
[Service]
Type=oneshot
ExecStart=/usr/bin/cloud-init init
RemainAfterExit=yes
TimeoutSec=0
# Output needs to appear in instance console output
StandardOutput=journal+console
[Install]
WantedBy=cloud-init.target
NoteTo check whether the operating system uses sysvinit, run the
strings /sbin/init | grep "sysvinit"
command, and you will receive a return message.
chkconfig --add cloud-init-local
chkconfig --add cloud-init
chkconfig --add cloud-config
chkconfig --add cloud-final
chkconfig cloud-init-local on
chkconfig cloud-init on
chkconfig cloud-config on
chkconfig cloud-final on
NoteDo not restart the server after performing the following operations. Otherwise, you will need to perform them again.
cloud-init init --local
If the following information is returned, it indicates that the cloud-init has been successfully configured.
Cloud-init v. 20.1 running 'init-local' at Fri, 01 Apr 2022 01:26:11 +0000. Up 38.70 seconds.
Execute the following command to delete the cache records of cloud-init.
rm -rf /var/lib/cloud
Run the following command in Ubuntu or Debian:
rm -rf /etc/network/interfaces.d/50-cloud-init.cfg
For Ubuntu or Debian, replace the content of /etc/network/interfaces
with the following:
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
If the cloud-init service fails to be installed by manually downloading the cloud-init source package, complete the following steps to install cloud-init:
Click here to obtain the portable cloud-init package.
Run the following command to decompress the portable cloud-init package:
tar xvf greeninit-x64-beta.tgz
Run the following command to enter the decompressed portable cloud-init package directory; that is, the greeninit directory:
cd greeninit
Run the following command to install cloud-init:
sh install.sh
During installation, if an error such as “failed to install” or “installation package not found” occurs, troubleshoot it based on the operating system as follows:
yum install epel-release -y
yum install python-pip -y
Was this page helpful?