目录

cloud-init-generator

问题说明

  • cloud-init 18.5开始,服务的自启动机制改变了,由cloud-init-generator脚本来决定cloud-init是否跟随系统启动

  • 整体策略:

    1
    2
    3
    
    # enable AND ds=found == enable
    # enable AND ds=notfound == disable
    # disable || <any> == disabled
    

流程梳理

1. 确定是否有禁用标识

  • 首先从cmdline中找是否存在cloud-init参数,并且是否为enabled
  • 然后找/etc/cloud/目录下是否有cloud-init.disabledcloud-init.enabled标识文件
  • 如果上面两个都没有指定,就使用默认enabled,即没有禁用。

2. 寻找datasource

  • 检查是否存在/usr/lib/cloud-init/ds-identify,并且是可执行的,如果不是,就直接结束,没有找到ds。如果是,就执行这个脚本。
    • 新版本的路径改到了/usr/libexec/cloud-init/ds-identify

ds-identify

  • ds的来源:

    • cmdline参数:可以指定ci.dsci.datasourceci.di.policy
    • config:查找cloud.cfg中的datasource_list
    • 都没有的话,用默认的列表
  • 如果是使用默认的列表,将会针对每个datasource类型进行可用性检查,每一个都定义了对应的方法

    • configdrive为例,collect_info方法中已经通过read_fs_info方法找到了labelconfig-2的分区/盘,所以dscheck_ConfigDrive方法检查确认存在datasource
  • ds的查找有三种结果:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    
    #   found,maybe,notfound:
    #      found: (default=all)
    #         first: use the first found do no further checking
    #         all: enable all DS_FOUND
    #
    #      maybe: (default=all)
    #       if nothing returned 'found', then how to handle maybe.
    #       no network sources are allowed to return 'maybe'.
    #         all: enable all DS_MAYBE
    #         none: ignore any DS_MAYBE
    #
    #      notfound: (default=disabled)
    #         disabled: disable cloud-init
    #         enabled: enable cloud-init
    
  • 如果找到了第一个found的ds,就成功

  • 如果没有found的ds,就要在maybe里来找

    • 只要有一个maybe的ds,并且policy里的maybe不等于none,也成功
  • 如果都没有,就要进行四种组合的判断(modepolicy里的notfound):

    • search:disabled->disabled
    • search:enabled->enabled
    • report:disabled->enabled
    • report:enabled->enabled
  • 四种mode的解释:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    
    #   Mode:
    #     disabled: disable cloud-init
    #     enabled:  enable cloud-init.
    #               ds-identify writes no config and just exits success.
    #               the caller (cloud-init-generator) then enables cloud-init to
    #               run just without any aid from ds-identify.
    #     search:   determine which source or sources should be used
    #               and write the result (datasource_list) to
    #               /run/cloud-init/cloud.cfg
    #     report:   basically 'dry run' for search.  results are still written
    #               to the file, but are namespaced under the top level key
    #               'di_report' Thus cloud-init is not affected, but can still
    #               see the result.
    

3. 执行generator

  • 根据ds-identify返回的结果,确定是否启动cloud-init
  • 如果确定启动,检查/run/systemd/generator.early/multi-user.target.wants/cloud-init.target链接是否存在,不存在就创建,同时建立一个/run/cloud-init/enabled标识文件(不管链接文件创建成功与否都有)
  • 如果确定禁用,则与上述相反

所有[Install]被配置为WantsBy=cloud-init.targetservice(也就是cloud-init的四个service),都必须进行enable操作,否则无法启动