在脚本停止的地方使用 Vagrant 和 pickup 配置机器时是否可以重新启动机器?
Is it possible to restart a machine when provisioning a machine using Vagrant and pickup where the script left off?
我在bash看教程说要重启机器,没有直接重启服务的选项,就是重启机器的事,然后后面的命令又多了在配置时仍然需要 运行。
那么有没有什么方法可以在配置过程中重启盒子,然后从中断的地方继续?
我从来没有这样做过,但如果我不得不这样做,我会把脚本分成两部分,一个在重新启动之前,包括重新启动命令,另一个是 post 安装。
第一个也会创建一个锁文件。
如果锁定文件不存在,整个脚本将 运行 第一个脚本,如果文件存在,则 运行 第二个脚本。这个整体脚本将设置为启动。
据我所知,如果它尝试重新启动 OS,您不能有一个 script/set 的命令会在它停止的地方继续执行,例如:
config.vm.provision "shell", inline: <<-SHELL
echo $(date) > ~/rebootexample
reboot
echo $(date) >> ~/rebootexample
SHELL
在此示例中,不会执行第二个回显调用。
您可以拆分 script/commands 并使用 vagrant reload 等插件。
Vagrantfile 的示例片段以突出其可能的用途:
# 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
您可以使用的一个技巧是发送重启信号并将其余配置工作保存为脚本,以便 运行 启动时:
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's destroyed and never run again"
rm /etc/rc.local
RCLOCAL
chmod o+x /etc/rc.local
shutdown -r now #restart
SHELL
这已经过测试可在 debian 9 上运行,因此您可能需要启用服务或找到另一种方法让您的代码在下次启动时引导到 运行 如果您 运行ning别的。
不幸的是你不能简单地做:
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.
Vagrant 有一个用于配置的重新启动选项,但是,Linux 目前不支持重新启动来宾功能。
你可以在这里查看我的插件,https://github.com/secret104278/vagrant_reboot_linux/tree/master,我已经实现了Linux重启的功能
我在bash看教程说要重启机器,没有直接重启服务的选项,就是重启机器的事,然后后面的命令又多了在配置时仍然需要 运行。
那么有没有什么方法可以在配置过程中重启盒子,然后从中断的地方继续?
我从来没有这样做过,但如果我不得不这样做,我会把脚本分成两部分,一个在重新启动之前,包括重新启动命令,另一个是 post 安装。
第一个也会创建一个锁文件。
如果锁定文件不存在,整个脚本将 运行 第一个脚本,如果文件存在,则 运行 第二个脚本。这个整体脚本将设置为启动。
据我所知,如果它尝试重新启动 OS,您不能有一个 script/set 的命令会在它停止的地方继续执行,例如:
config.vm.provision "shell", inline: <<-SHELL
echo $(date) > ~/rebootexample
reboot
echo $(date) >> ~/rebootexample
SHELL
在此示例中,不会执行第二个回显调用。
您可以拆分 script/commands 并使用 vagrant reload 等插件。
Vagrantfile 的示例片段以突出其可能的用途:
# 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
您可以使用的一个技巧是发送重启信号并将其余配置工作保存为脚本,以便 运行 启动时:
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's destroyed and never run again"
rm /etc/rc.local
RCLOCAL
chmod o+x /etc/rc.local
shutdown -r now #restart
SHELL
这已经过测试可在 debian 9 上运行,因此您可能需要启用服务或找到另一种方法让您的代码在下次启动时引导到 运行 如果您 运行ning别的。
不幸的是你不能简单地做:
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.
Vagrant 有一个用于配置的重新启动选项,但是,Linux 目前不支持重新启动来宾功能。
你可以在这里查看我的插件,https://github.com/secret104278/vagrant_reboot_linux/tree/master,我已经实现了Linux重启的功能