Skip to content

Commit 7135d21

Browse files
committed
Prepare container image at gce_install_cuttlefish_packages
1 parent 192911d commit 7135d21

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

tools/baseimage/cmd/gce_install_cuttlefish_packages/main.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ var (
6060
image_name = flag.String("image-name", "", "output GCE image name")
6161
deb_srcs = DebSrcsFlag{}
6262
defaults_cuttlefish_integration_src = flag.String("defaults_cuttlefish_integration_src", "", "Path to file which will be written to /etc/defaults/cuttlefish-integration.")
63+
container_image_src = flag.String("container-image-src", "", "local path to container image")
6364
)
6465

6566
func init() {
@@ -71,6 +72,7 @@ type amendImageOpts struct {
7172
SourceImage string
7273
ImageName string
7374
DebSrcs []string
75+
ContainerImageSrc string
7476
}
7577

7678
func uploadScripts(project, zone, insName string) error {
@@ -80,7 +82,8 @@ func uploadScripts(project, zone, insName string) error {
8082
}{
8183
{"fill_available_disk_space.sh", scripts.FillAvailableDiskSpace},
8284
{"mount_attached_disk.sh", scripts.MountAttachedDisk},
83-
{"install_cuttlefish_debs.sh", scripts.InstallCuttlefishPackages},
85+
{"install_cuttlefish_debs.sh", scripts.InstallCuttlefishDebs},
86+
{"load_cuttlefish_container_image.sh", scripts.LoadCuttlefishContainerImage},
8487
}
8588
for _, s := range list {
8689
if err := gce.UploadBashScript(project, zone, insName, s.dstname, s.content); err != nil {
@@ -124,6 +127,17 @@ func maybeSetDefaultsCuttlefishIntegrationFile(project, zone, insName string) er
124127
return nil
125128
}
126129

130+
func loadCuttlefishContainerImage(project, zone, insName string, imageSrc string) error {
131+
dst := "/tmp/" + filepath.Base(imageSrc)
132+
if err := gce.UploadFile(project, zone, insName, imageSrc, dst); err != nil {
133+
return fmt.Errorf("error uploading %s: %v", imageSrc, err)
134+
}
135+
if err := gce.RunCmd(project, zone, insName, "./load_cuttlefish_container_image.sh "+dst); err != nil {
136+
return err
137+
}
138+
return nil
139+
}
140+
127141
func cleanupDeleteDisk(h *gce.GceHelper, disk string) {
128142
log.Printf("cleanup: deleting disk %q...", disk)
129143
if err := h.DeleteDisk(disk); err != nil {
@@ -208,6 +222,10 @@ func amendImageMain(project, zone string, opts amendImageOpts) error {
208222
return fmt.Errorf("couldn't set %s: %v", defaultsCuttlefishIntegrationFile, err)
209223
}
210224

225+
if err := loadCuttlefishContainerImage(project, zone, insName, opts.ContainerImageSrc); err != nil {
226+
return fmt.Errorf("install cuttlefish container error: %v", err)
227+
}
228+
211229
// Reboot the instance to force a clean umount of the attached disk's file system.
212230
if err := gce.RunCmd(project, zone, insName, "sudo reboot"); err != nil {
213231
return err
@@ -252,12 +270,16 @@ func main() {
252270
if len(deb_srcs.Srcs) == 0 {
253271
log.Fatal("usage: `-deb` must not be empty")
254272
}
273+
if *container_image_src == "" {
274+
log.Fatal("usage: `-container-image-src` must not be empty")
275+
}
255276

256277
opts := amendImageOpts{
257278
SourceImageProject: *source_image_project,
258279
SourceImage: *source_image,
259280
ImageName: *image_name,
260281
DebSrcs: deb_srcs.Srcs,
282+
ContainerImageSrc: *container_image_src,
261283
}
262284
if err := amendImageMain(*project, *zone, opts); err != nil {
263285
log.Fatal(err)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright (C) 2025 The Android Open Source Project
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -o errexit -o nounset -o pipefail
18+
19+
if [[ $# -eq 0 ]] ; then
20+
echo "usage: $0 /path/to/container_image"
21+
exit 1
22+
fi
23+
container_image=$1
24+
25+
# Load docker image. Internally, it modifies data-root configuration of
26+
# /etc/docker/daemon.json to load image under default data-root location of
27+
# mounted attached disk. With this approach, docker installed on chroot
28+
# environment can recognize and use docker image when dockerd starts to run
29+
# on running GCE instance with GCE image created via this script.
30+
sudo apt install -y docker.io jq
31+
sudo systemctl stop docker.socket docker.service
32+
if [ -f /etc/docker/daemon.json ]; then
33+
sudo jq '.["data-root"] = "/mnt/image/var/lib/docker"' /etc/docker/daemon.json \
34+
| sudo tee /etc/docker/daemon.json > /dev/null
35+
else
36+
sudo jq -n '.["data-root"] = "/mnt/image/var/lib/docker"' \
37+
| sudo tee /etc/docker/daemon.json > /dev/null
38+
fi
39+
sudo systemctl start docker.socket docker.service
40+
sudo docker load --input "$container_image"
41+
sudo systemctl stop docker.socket docker.service
42+
43+
sudo chroot /mnt/image /usr/bin/apt install -y docker.io

tools/baseimage/pkg/gce/scripts/scripts.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ var InstallNvidia string
2929
var FillAvailableDiskSpace string
3030

3131
//go:embed install_cuttlefish_debs.sh
32-
var InstallCuttlefishPackages string
32+
var InstallCuttlefishDebs string
33+
34+
//go:embed load_cuttlefish_container_image.sh
35+
var LoadCuttlefishContainerImage string
3336

3437
//go:embed install_kernel_main.sh
3538
var InstallKernelMain string

0 commit comments

Comments
 (0)