Building Container-Optimized OS from source

Container-Optimized OS is based on, and built using, tools from the open-source Chromium OS project. If you like, you can build a Container-Optimized OS image yourself directly from the Container-Optimized OS source code.


To build a Container-Optimized OS image, you'll need to install the following tools on your development machine:

  • git and curl
  • A package of Chromium scripts called depot_tools that includes tools like repo and cros_sdk.

Obtaining the Container-Optimized OS source code

You can download the source code for Container-Optimized OS by using the repo tool that is included in depot_tools.

First, create a directory to store the source code. For example, cos-src in your home directory:

$ mkdir $HOME/cos-src
$ cd $HOME/cos-src

Now download source code using the following commands:

$ repo init -u \
$ repo sync

Building a Container-Optimized OS image

To build Container-Optimized OS, you must create a suitable chroot environment. You can use the cros_sdk tool included in depot_tools to create and enter a chroot that's ready for Container-Optimized OS compilation by running the following command in the source directory that you created in the previous step:

$ cd $HOME/cos-src
$ cros_sdk --enter

Once inside chroot, you can build the disk image. To build Container-Optimized OS, specify lakitu for the board name as follows:

(cr) ~/trunk/src/scripts $ ./build_packages --board=lakitu
(cr) ~/trunk/src/scripts $ ./build_image --board=lakitu test

In addition to test, you can build either a base, or dev image by passing the appropriate parameter to the ./build image script command. If you don't specify a parameter, the dev image is built by default.

The dev image contains some additional debug tools installed on top of the base image. The test image includes the debug tools from the dev image as well as tools necessary for running automatic Container-Optimized OS tests.

Running Your Image

Once you've built your Container-Optimized OS image, you can run the image using KVM, or import the image to a Compute Engine instance.

Running in KVM

To boot your image in KVM, run the following command:

~/cos $ kvm -m 1024 -nographic -net nic,model=virtio -net user,hostfwd=tcp: -hda src/build/images/lakitu/latest/chromiumos_test_image.bin

Booting in this manner leaves the VM's serial port connected to your console, letting you log in without using SSH. If you're running a test image, you can log in with the username/password pair "root/test0000".

Once the VM is started, you can access your dev or test image using SSH. To SSH into the image, use the key generated for that image, as follows:

~/cos $ ssh root@localhost -p 9222 -i src/build/images/lakitu/latest/id_rsa

Running on Compute Engine

To import your image to a Compute Engine instance, you must compress the image into a .tar file. To compress the image, run the following command:

~/cos $ tar -Sczf compressed-image.tar.gz images/lakitu/latest/chromiumos_image.bin --transform 's|images/lakitu/latest/chromiumos_image.bin|disk.raw|'

After you've created your .tar file, you can follow the instructions to import an existing image in the Compute Engine documentation.