tencent cloud

Feedback

Installing Cloud-Init on Linux

Last updated: 2022-11-16 16:57:54

    Overview

    This document describes how to install the cloud-init service. Cloud-init allows you to customize configurations during the first initialization of a CVM instance. If an image does not have the cloud-init service installed, instances launched by using the image cannot be initialized properly.
    You can use either of the following methods to install cloud-init:

    Notes

    Before importing a Linux image, ensure that you have properly installed the cloud-init service in the image.

    Prerequisites

    The server on which you want to install cloud-init is connected to the internet.

    Directions

    Downloading the cloud-init source package

    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
    

    Installing cloud-init

    1. Run the following command to decompress the cloud-init installation package:
      Note

      If you are using the Ubuntu operating system, run this command with the "root" account.


      tar -zxvf cloud-init-17.1.tar.gz 
      
    2. Run the following command to enter the directory of cloud-init installation package. cloud-init-17.1 is used in this example.
      cd cloud-init-17.1
      
    3. Install Python-pip according to the operating system version.
    • For CentOS 6/7, run the following command:
      yum install python3-pip -y
      
    • For Ubuntu, run the following command:
      apt-get -y install python3-pip
      

      During installation, if an error such as "failed to install" or "installation package not found" occurs, see Resolving Python-pip installation failures.
    1. Run the following command to upgrade pip.
      python3 -m pip install --upgrade pip 
      
    2. Run the following command to install dependencies:
      Note

      Python 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.


      pip3 install -r requirements.txt
      
    3. Install the cloud-utils components corresponding to your OS version.
    • For CentOS 6, run the following command:
      yum install cloud-utils-growpart dracut-modules-growroot -y
      dracut -f
      
    • For CentOS 7, run the following command:
      yum install cloud-utils-growpart -y
      
    • For Ubuntu, run the following command:
      apt-get install cloud-guest-utils -y
      
    1. Run the following command to install cloud-init:
      python3 setup.py build 
      

      python3 setup.py install --init-system systemd
      
      Note

      Options of --init-system can be systemd, sysvinit, sysvinit_deb, sysvinit_freebsd, sysvinit_openrc, sysvinit_suse or upstart [default: None]. 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 as an example.

    Modifying the cloud-init configuration file

    1. Download cloud.cfg for your operating system.
    1. Replace the content of /etc/cloud/cloud.cfg with that of the downloaded cloud.cfg file.

    Adding syslog user

    Run the following command to add a syslog user:

    useradd syslog
    

    Configuring the auto-start of the cloud-init service on boot

    • If your operating system uses the systemd auto-start service management method, run the following commands.
      Note

      To check whether the operating system uses systemd, run the strings /sbin/init | grep "/lib/system" command, and you will receive a return message.

    1. Run the following command in Ubuntu or Debian:
      ln -s /usr/local/bin/cloud-init /usr/bin/cloud-init 
      
    2. Run the following commands in all operating systems:
      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
      
      
    3. Run the following command in CentOS or Redhat.
      Replace the content of /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
      

      Replace the content of /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
      
    • If your operating system uses the sysvinit auto-start service management method, run the following commands:
      Note

      To 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 
      
    Note

    Do not restart the server after performing the following operations. Otherwise, you will need to perform them again.

    1. Run the following commands to check whether the cloud-init configuration is successful.
      cloud-init init --local
      

    If the following information is returned, it indicates that the cloud-init has been successfully configured.

    Cloud-init v. 17.1 running 'init-local' at Fri, 01 Apr 2022 01:26:11 +0000. Up 38.70 seconds.
    
    1. Execute the following command to delete the cache records of cloud-init.

      rm -rf /var/lib/cloud
      
    2. Run the following command in Ubuntu or Debian:

      rm -rf /etc/network/interfaces.d/50-cloud-init.cfg
      
    3. 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/*
      

    See Also

    Portable cloud-init package

    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:

    1. Click here to obtain the portable cloud-init package.

    2. Run the following command to decompress the portable cloud-init package:

      tar xvf greeninit-x64-beta.tgz 
      
    3. Run the following command to enter the decompressed portable cloud-init package directory. greeninit is used here.

      cd greeninit
      
    4. Run the following command to install cloud-init:

      sh install.sh 
      

    Python-pip installation failure

    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:

    1. Run the following command to configure the EPEL storage repository.
      yum install epel-release -y
      
    2. Run the following command to install Python-pip.
      yum install python3-pip -y
      
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support