Packer and post-install scripts

bash, packer, shell

Solution

Add below code in `/etc/rc.local` of the linux VM's image:

####Marker_start####
sed -i '/####Marker_start####/,/####Marker_end####/d' /etc/rc.local
#Your custom code here
####Marker_end####

Explanation:

- rc.local script gets executed after every system start. Hence, adding the code there will cause it to execute after boot.

- Use sed to remove the custom code first time the script executes. Thus, the code is executed only once.

Other approach:

Create a script containing your custom code & append below code to it.

 chmod 644 "$0"

& create a symlink

ln -s /path/to/your-script.sh /etc/rc5.d/S99my-custom-script.sh

(Assuming your default runlevel is 5. Change it as required on your VM's distro - e.g. ubuntu has it on `/etc/rc2.d`) Explanation: Scripts named `/etc/rc5.d/S*` are executed automatically when entering run-level 5, with argument=`start`. Upon first execution, make the script as non-executable, so that it gets executed only once.

Problem

I'm started using Packer and I have a question. Is there any solution to add a bash script which will be started automatically and only once after VM will be deployed from an image?

Original source

Related problems