VCloud Guest Customization Script : [: postcustomization: unexpected operator
We have been doing some work to automatically provision machines using the VCloud API via fog and one of the things we wanted to do was run a custom script the first time that a node powers on.
The following explains how customization scripts work:
In vCloud Director, when setting a customization script in a virtual machine, the script:
Is called only on initial customization and force recustomization.
Is called with the precustomization command line parameter before out-of-box customization begins.
Is called with the postcustomization command line parameter after out-of-box customization finishes.
Needs to be a batch file for Windows virtual machines and a shell script for Unix virtual machines.
We wanted the script to run only when passed the 'postcustomization' flag because our script relied on some networking configuration which hadn’t yet been done in the 'precustomization' state.
We wrote something like the following script:
#!/bin/bash
if [ x$1 == x"postcustomization" ]; then
echo post customization
fi
Unfortunately when we provisioned the node it hadn’t run any of the code within the if block and we saw the following message in /var/log/vmware-inc/customization.log:
5: [: xpostcustomization: unexpected operator
Nick pointed out that the test utility which we’re using to do the comparison on the 2nd line uses a single = in the POSIX shell even though it will work with double = in the bash shell.
We thought this was pretty strange since we are telling the script to run with the bash shell in the first line.
We eventually realised that the script was being spawned out to a POSIX shell by /root/.customization/post-customize-guest.sh which is the script that gets called on power on:
((${SH} $POST_CUSTOMIZATION_TMP_SCRIPT_NAME "postcustomization" > /tmp/stdout.log) 2>&1 | ${TEE} -a /tmp/stderr.log)
I created a simple script to check the theory:
#!/bin/bash
[ "mark" == "mark" ] && echo Mark
Which works fine when called directly:
$ ./mark.sh
Mark
And throws the expected error when called with 'sh':
$ sh mark.sh
[: 3: mark: unexpected operator
We therefore needed to change our script to do the comparison with a single = like so:
#!/bin/bash
if [ x$1 = x"postcustomization" ]; then
echo post customization
fi
About the author
I'm currently working on short form content at ClickHouse. I publish short 5 minute videos showing how to solve data problems on YouTube @LearnDataWithMark. I previously worked on graph analytics at Neo4j, where I also co-authored the O'Reilly Graph Algorithms Book with Amy Hodler.