Bootstrap Debian on Google Compute Engine (GCE) using bootstrap-vz

Google provides clean Debian images on its cloud but in some cases you are required to further customize them as per your needs, here I will be explaining how to customize and bootstrap the official Debian image for re-usability.

Prerequisite: GCE account with authorized billing and a project.

First thing you need to setup is gcloud compute command-line tools.

curl https://sdk.cloud.google.com | bash

Follow on screen instructions

Reload user profile by re-connecting your ssh sessions and cd into google-cloud-sdk directory and request a token.

cd google-cloud-sdk
gcloud auth login

Copy the code and access it in your browser and grant access when ask. This will return you another code which you need to paste in your terminal.

Now your machine is authorized and you start using gcloud tools from this system. First thing you need to specify is default project ID.

gcloud config set project xxxxxxxxx-xxxx-xxx

Now its time to test gcloud tools. You can run any command so lets see list of regions.

gcutil listregions

It will show you list of all regions as below.

| name | status | cpus | disks-total-gb | static-addresses |
| asia-east1 | UP | 2.0/24.0 | 20.0/10240.0 | 2.0/7.0 |
| europe-west1 | UP | 0.0/24.0 | 0.0/10240.0 | 0.0/7.0 |
| us-central1 | UP | 2.0/24.0 | 0.0/10240.0 | 2.0/7.0 |

Now you need to launch a new instance in any region with Debain 7

gcloud compute instances create my-instance --image debian-7 --zone us-central1-f --machine-type f1-micro

Here debian-7 is an alias of latest latest debain 7 image provided from Google and Debian. Note the external IP of your system.

In order to SSH server you need to generate new key pair and store its public key on your instance.

echo root:$(cat /home/azfar/.ssh/id_rsa.pub) > /tmp/a
gcloud compute instances add-metadata my-instance --metadata-from-file sshKeys=/tmp/a --zone us-central1-f

SSH into the instance using root and private key.

ssh -i /home/azfar/.ssh/id_rsa [email protected]

First thing you need to do on new instance is

apt-get update
apt-get upgrade
apt-get install git parted kpartx qemu-utils git debootstrap python-pip
pip install termcolor jsonschema fysom docopt pyyaml

Clone bootstrap-vz repository and checkout development version, it is important to use development version as it has many fixes and new features and configuration is also based on yml instead of json.

git clone https://github.com/andsens/bootstrap-vz.git
cd bootstrap-vz
git checkout development

Now edit manifests/gce.manifest.yml and customize it as per your needs. You can start with my sample gce.manifest.yml which is removing and installing few packages and also executing a shell command. You can also add further functionality and for it below resources will be good to view.


For development version documentation you can checkout gh-pages-dev branch

git checkout gh-pages-dev

Now you are ready to start the bootstrapping process.

./bootstrap-vz manifests/gce.manifest.yml

Make sure all went good, this will create a image in /mnt/target which you need to upload in Google Cloud Storage. For this you need to create bucket where you will upload the image and then create an image from there.

gsutil mb gs://yourbucket
gsutil cp /mnt/target/debian-7-7-wheezy-v20150101.tar.gz gs://yourbucket
gcloud compute images create debianwheezy --source-uri gs://yourbucket/debian-7-7-wheezy-v20150101.tar.gz

Now its time to launch a new instance from our image and test our modifications in place or not

gcloud compute instances create my-instance2 --image debianwheezy --zone us-central1-f --machine-type f1-micro

You need to add your previously created key to the new instance as we did earlier

gcloud compute instances add-metadata my-instance2 --metadata-from-file sshKeys=/tmp/a --zone us-central1-f

Once key is added login to new instance and verify sure your changes exist.