简体   繁体   English

如何在不重启的情况下重新运行 cloud-init

[英]How to re-run cloud-init without reboot

I am using openstack to create a VM using 'nova boot' command.我正在使用 openstack 使用“nova boot”命令创建 VM。 My image is cloud-init enabled.我的图像启用了 cloud-init。 I pass a --user-data script which is a bash shell format for cloud-init to run during VM boot up time.我传递了一个 --user-data 脚本,它是 bash shell 格式,以便 cloud-init 在 VM 启动期间运行。 All this happens successfully.这一切都顺利发生。 Now my use-case is to re-run cloud-init to execute the same user-data script without rebooting the VM.现在我的用例是重新运行 cloud-init 以执行相同的用户数据脚本,而无需重新启动 VM。 I saw /usr/bin/cloud-init options and they do talk about running specific modules but nothing is able to make it execute the same user-data script.我看到了 /usr/bin/cloud-init 选项,它们确实在谈论运行特定模块,但没有什么能够让它执行相同的用户数据脚本。 How can this be achieved?如何实现? Any help would be appreciated.任何帮助,将不胜感激。

In order for cloud-init to reset, you need to execute rm -rf /var/lib/cloud/instances .为了重置 cloud-init,您需要执行rm -rf /var/lib/cloud/instances

Then re run the cloud-init start and it will run the full boot script process again.然后重新运行cloud-init start ,它将再次运行完整的启动脚本过程。

The commands have been updated so to re-run you need to clean out the existing config:命令已更新,因此要重新运行,您需要clean现有配置:

sudo cloud-init clean

Then re-run it using the init directive:然后使用init指令重新运行它:

sudo cloud-init init

Beware: things like ssh host keys maybe regenerated.当心:ssh 主机密钥之类的东西可能会重新生成。

Since this keeps popping up in search results, what works for me:由于这不断出现在搜索结果中,对我有用的是:

  1. Delete semaphores in /var/lib/cloud/instances/i-xxxxxxx/sem .删除/var/lib/cloud/instances/i-xxxxxxx/sem信号量。 Cloud-init will not re-run if these files are present.如果这些文件存在,则 Cloud-init 将不会重新运行。

  2. Edit /var/lib/cloud/instances/i-xxxxxxxx/scripts/part-001 .编辑/var/lib/cloud/instances/i-xxxxxxxx/scripts/part-001 This is your user-data script.这是您的用户数据脚本。

  3. Execute only the user scripts module of cloud-init.执行 cloud-init 的用户脚本模块。 This will not re-download user data but execute the already downloaded (and now, modified) script from step 2.这不会重新下载用户数据,而是执行步骤 2 中已下载(现在已修改)的脚本。

    sudo /usr/bin/cloud-init single -n cc_scripts_user

Given that this post was actively touched 6 months ago I wanted to provide a more complete answer here from cloud-init upstream.鉴于这篇文章在 6 个月前被积极触及,我想在这里提供来自 cloud-init upstream 的更完整的答案。

The original question: "how to re-run a user-data script again at a later time with cloud-init" Generally user-scripts are only run once per-instance by the config module config-user-scripts.最初的问题:“如何在以后使用 cloud-init 再次重新运行用户数据脚本” 通常,用户脚本仅由配置模块 config-user-scripts 每个实例运行一次。 If the instance-id in metadata doesn't change it won't re-run.如果元数据中的 instance-id 没有改变,它就不会重新运行。

The per-instance semaphores can be bypassed with the following command line by telling it to run the user-scripts module regardless of instance-id:可以使用以下命令行绕过每个实例的信号量,方法是告诉它运行用户脚本模块而不考虑实例 ID:

sudo cloud-init single --name scripts-user --frequency always

Per the other suggesting to re-running all of cloud-init without system reboot.根据另一个建议在不重新启动系统的情况下重新运行所有 cloud-init。 It isn't a recommended approach because some parts of cloud-init are run at systemd generator timeframe to detect new datasource types.这不是推荐的方法,因为 cloud-init 的某些部分在 systemd 生成器时间范围内运行以检测新的数据源类型。 That said, the following commands will allow you to accomplish this without reboot on a system.也就是说,以下命令将允许您在不重新启动系统的情况下完成此操作。

cloud-init supports a clean subcommand to remove all semaphore files and allow cloud-init to re-run all config modules again. cloud-init 支持一个clean子命令来删除所有信号量文件并允许 cloud-init 再次重新运行所有配置模块。 Beware that this will mean SSH host-keys are regenerated and.ssh config files re-written so it could impact your ability to get back into the VM.请注意,这将意味着重新生成 SSH 主机密钥并重写 ssh 配置文件,因此它可能会影响您返回虚拟机的能力。

To clean all semaphores so cloud-init modules will all re-run on next boot:要清除所有信号量,以便 cloud-init 模块将在下次启动时全部重新运行:

sudo cloud-init clean --logs

cloud-init typically runs multiple boot stages in sequence due to systemd service dependencies.由于 systemd 服务依赖性,cloud-init 通常按顺序运行多个引导阶段 If you want to repeat that process without a reboot you can run the following 4 commands:如果你想在不重启的情况下重复该过程,你可以运行以下 4 个命令:

  1. Detect local datasource (cloud platform):检测本地数据源(云平台):

    sudo cloud-init init --local

  2. Detect any datasources which require.network up and run "cloud_init_modules" defined in /etc/cloud/cloud.cfg:检测任何需要网络的数据源并运行 /etc/cloud/cloud.cfg 中定义的“cloud_init_modules”:

    sudo cloud-init init

  3. Run all cloud_config_modules defined in /etc/cloud/cloud.cfg:运行 /etc/cloud/cloud.cfg 中定义的所有 cloud_config_modules:

    sudo cloud-init modules --mode=config

  4. Run all cloud_final_modules defined in /etc/cloud/cloud.cfg:运行 /etc/cloud/cloud.cfg 中定义的所有 cloud_final_modules:

    sudo cloud-init modules --mode=final

To run the packages module of cloud-config part of cloud-init, you can run要运行 cloud-init 的 cloud-config 部分的包模块,您可以运行

# cloud-init-cfg all config

To run the runcmd module of cloud-config part of cloud-init, you can run要运行 cloud-init 的 cloud-config 部分的 runcmd 模块,您可以运行

# cloud-init-cfg all final

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM