Packer wont fully reboot instance

I have their weird issue with Packer. I need to reboot a linux instance after a OS update so I can boot into latest kernel and then after remove the old kernels. Packer seems to be getting hung-up on the reboot part. It issues the reboot and I get kicked from the instance but it never actually fully shuts down. Then after my pause it starts the next section but fails because it cant login due to it being in this shutdown state. Below is a section of my code.

{
“type”: “shell”,
“expect_disconnect”: true,
“skip_clean”: true,
“inline”: [
“echo ‘Rebooting Instance’”,
“sudo shutdown -r now”
]
},
{
“type”: “shell”,
“start_retry_timeout”: “5m”,
“pause_before”: “5m”,
“inline”: [
“echo ‘Removing Old Kernels’”,
“sudo /usr/bin/package-cleanup -y --oldkernels --count=1”
]
},

Ive played around with timings and even tried forceful reboot but no matter what I do I cant get it to reboot. If I login without packer connected it reboots fine. Any ideas?

To resolve the issue with Packer not fully rebooting the instance, try the following:

  1. Use shutdown_command in your builder config to issue the reboot.
  2. Increase the pause_before duration in your second provisioner to allow more time for a full reboot.
  3. Implement a loop in your shell provisioner to check for SSH availability before proceeding.
  4. Ensure expect_disconnect is correctly used to handle the expected disconnection during reboot.
  5. Review Packer debug logs for additional insights by running Packer with PACKER_LOG=1.
  6. Experiment with alternative reboot commands, like nohup sudo reboot &, to see if they yield better results.

I figured it out. The interface wasnt shutting down during reboot due to some previous stuff that was being done to the instance. I added a “sudo ifdown eth0” after the reboot command and that resolved it.

1 Like