Here is what I am doing:
gcloud compute instances create example-instance --image-family $image --image-project $projectID --machine-type $type_machine --metadata startup-script='#!/bin/bash mkdir -p ~/test'
This look super simple. The instance is created no test folder is created in home directory. I checked the VM logs I can’t find anything.
So this means we can not create a folder in home directory with GCP VM startup script?
Or is there any thing I am missing?
- Checked the VM logs but didnt find anything.
- Was manually able to create folder in home directory after the VM is created.
2
Answers
According to your script and to the logs, your folder is correctly created in the home folder.
But the home folder of which user?? You are using a path shortcut ~/ but for who?
In the documentation, it’s mentioned that the startup script is launched as "root" user. When you, user "Learning", ssh to the VM, a /home/learning/ home directory is created.
And it’s obviously not the same as the root user.
I don’t know how gcloud executes the start script, but a reasonable assumption is that it runs it using
sh
.Your startup script starts with a comment character. Therefore, the whole line is treated as a comment and nothing gets executed. The
#!
would make sense as a pseudo comment as the first line in a script file (which is different from your case), but even then, themkdir
would have to be in the subsequent line.However, even removing the #! would not help in your example, for the following reason:
If you run
bash foo bar baz
, bash executes the commandfoo
as bash-script and passes to it the parametersbar
andbaz
. In your case, the command ismkdir
, and this is not a bash script, but a binary executable. You would get the error messageTherefore, the solution would be to use the
-c
option and writeas "start script". The
-c
tells bash to interpret the following argument as a statement to be executed.BTW: If gcloud really understands the start-script argument as
sh
command, you don’t need bash at all and can simply useSince it is most likely that the start script is executed with the working directory set to what glcoud considers your home, you should be able to do just a