As @John Hanley suggest to run commands via VM startup script, you can reset the password for root access by using the serial console and following the steps below.
- Go to the VM instances page and click on the instance name of your VM.
- Click the Edit button at the top of the page.
- Under Custom metadata, click Add item.
- Set 'Key' to 'startup-script' and set 'Value' to this script:
#! /bin/bash
useradd -G sudo USERNAME
echo 'USERNAME:PASSWORD' | chpasswd
NOTE: Change the value of USERNAME and PASSWORD to the name and password of your choice.
Enable "Enable connecting to serial ports" by checking the box below the SSH button.
Click Save and then click RESET on the top of the page. Wait for some time for the instance to reboot.
Click on 'Connect to serial port' in the page. In the new window, you might need to wait a bit and press Enter on your keyboard once, then you should see the login prompt.
Login using the USERNAME and PASSWORD you provided.