Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to re-run cloud-init without reboot

I am using openstack to create a VM using 'nova boot' command. My image is cloud-init enabled. I pass a --user-data script which is a bash shell format for cloud-init to run during VM boot up time. 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. 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. How can this be achieved ? Any help would be appreciated.

like image 611
user3532850 Avatar asked Apr 14 '14 16:04

user3532850


People also ask

Does cloud-init run on reboot?

However, I've—from time to time, quite rarely, but still—found that cloud-init re-runs on already provisioned system when they reboot.

Does cloud-init only run once?

By default, user data scripts and cloud-init directives run only during the boot cycle when you first launch an instance. You can update your configuration to ensure that your user data scripts and cloud-init directives run every time you restart your instance.

How do I know if cloud-init is running?

You can check the /var/lib/cloud/data/status. json for cloud-init status. Or if the host is using upstart, add one init process in /etc/init/newprocess. conf and newprocess.


2 Answers

In order for cloud-init to reset, you need to execute rm -rf /var/lib/cloud/instances.

Then re run the cloud-init start and it will run the full boot script process again.

like image 53
benh57 Avatar answered Oct 17 '22 04:10

benh57


While re-running all of cloud-init without reboot isn't a recommended approach, the following commands will allow you to accomplish this on a system.

The commands have been updated so to re-run you need to clean out the existing config:

sudo cloud-init clean --logs

cloud-init typically runs multiple boot stages in order due to systemd service dependencies. If you want to repeat that process without a reboot you can run the following 4 commands:

  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:

    sudo cloud-init init

  3. Run all cloud_config_modules defined in /etc/cloud/cloud.cfg:

    sudo cloud-init modules --mode=config

  4. Run all cloud_final_modules defined in /etc/cloud/cloud.cfg: sudo cloud-init modules --mode=final

Beware: things like ssh host keys maybe regenerated.

like image 28
Pierz Avatar answered Oct 17 '22 06:10

Pierz