Is it possible to restart the machine when configuring the machine using Vagrant and pickup, where did the script stop?

I read a tutorial in bash where they said to reboot the machine, there was no way to restart the service directly, it was a matter of rebooting the machine, and then after that there were a few more commands that are still needed to run during configuration.

So, is there a way to restart the box in preparation, and then choose where you left off after that?

+14
bash vagrant vagrant-provision
source share
4 answers

As far as I know, you cannot have a single script / set of commands that will be executed where it was stopped if it tries to restart the OS, for example:

config.vm.provision "shell", inline: <<-SHELL echo $(date) > ~/rebootexample reboot echo $(date) >> ~/rebootexample SHELL 

In this example, the second paging will not be completed.

You can split the script / commands up and use a plugin like tarp reload .

An example of a Vagrantfile fragment to highlight its possible use:

  # execute code before reload config.vm.provision "shell", inline: <<-SHELL echo $(date) > ~/rebootexample SHELL # trigger reload config.vm.provision :reload # execute code after reload config.vm.provision "shell", inline: <<-SHELL echo $(date) >> ~/rebootexample SHELL 
+14
source share

I never did this, but if I had to split the script into two parts, one before the reboot, which includes a restart command, and then the other, which sets up the post.

The first will also create a lock file.

A generic script will run the first script if the lock file does not exist or the second will run if the file exists. This generic script will be configured to run.

+3
source share

One trick you can use is to send a reboot signal and save the rest of your security work as a script to run on boot:

 config.vm.provision "shell", inline: <<-SHELL echo "Do your thing... DONE" cat <<-RCLOCAL | sed -s 's_^ __' > /etc/rc.local #!/bin/bash echo "This will be run once on next boot and then it destroyed and never run again" rm /etc/rc.local RCLOCAL chmod o+x /etc/rc.local shutdown -r now #restart SHELL 

This has been tested to work with debian 9, so you may need to turn on services or find another way to get your boot code to run the next time you boot if you use something else.

Unfortunately, you cannot just do:

 config.vm.provision "shell", inline: "shutdown -r now" config.vm.provision "shell", inline: "echo 'hello world'" results in ==> The SSH connection was unexpectedly closed by the remote end. This usually indicates that SSH within the guest machine was unable to properly start up. Please boot the VM in GUI mode to check whether it is booting properly. 
+1
source share

Vagrant has a reboot feature to provide, however guest reboot features are not currently supported for Linux.

You can check my plugin here, https://github.com/secret104278/vagrant_reboot_linux/tree/master , I implemented a function for Linux to reboot.

0
source share

All Articles