Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a startup script to an existing VM from the developer console?

I have an existing, already configured VM on Google Cloud Platform. It was created without a startup script, but I'd like to add one now.

How do I add it from the console.developers.google.com web interface?

like image 558
mimming Avatar asked Jun 10 '15 20:06

mimming


3 Answers

You can add a startup script to an already created VM by creating a new custom metadata field. Follow these steps:

  1. Get to your VM's configuration page: Navigate to https://console.developers.google.com Click your project. Go to Compute -> Compute Engine -> VM Instances. Click the name of your VM.
  2. Scroll down to Custom Metadata. Click Edit.
  3. Create a new metadata field. Set the key to startup-script.
  4. Paste your startup script into the value field. Don't forget the shebang. Here's an example of a valid script.

    #! /bin/bash
    apt-get update
    apt-get install -y apache2
    cat <<EOF > /var/www/index.html
    <html><body><h1>Hello World</h1>
    <p>This page was created from a simple startup script!</p>
    </body></html>
    EOF
    
  5. Restart your VM. Enjoy the yields of your awesome startup script.
like image 158
mimming Avatar answered Oct 20 '22 11:10

mimming


Thanks to mimming's answer, I was looking for this to solve my issue regarding multiple IP, and his answer help me get started and finally solved my problem by add below startup-script when reboot the instance.

#! /bin/bash
sleep 60
/usr/sbin/ip route add default via 10.8.8.1 dev eth1 table rt1
/usr/sbin/ip rule add from 10.8.8.3/32 table rt1
/usr/sbin/ip rule add to 10.8.8.3/32 table rt1

remember to add "sleep 60" otherwise it might not working cause the networking not started yet.

like image 39
James Hsieh Avatar answered Oct 20 '22 10:10

James Hsieh


The above answer is correct as per the question.

But what I was looking for to add multiple .sh scripts in startup metadata of GCP VM via gcloud command.

Below works for me (maybe it will help someone)

To add multiple key-value pairs at once, separate them with commas:

$ gcloud compute instances add-metadata test-instance \
      --metadata=important-data="2 plus 2 equals\
   4",unimportant-data=zero

Docs link- https://cloud.google.com/sdk/gcloud/reference/compute/instances/add-metadata

like image 34
Abhinay Gupta Avatar answered Oct 20 '22 10:10

Abhinay Gupta