Debian Codegraph @

launcher/image/preload.sh

raw
1 #!/bin/bash
2
3 readonly OEM_PATH='/usr/share/oem'
4 readonly CS_PATH="${OEM_PATH}/confidential_space"
5 readonly EXPERIMENTS_BINARY="confidential_space_experiments"
6
7 copy_launcher() {
8 cp launcher "${CS_PATH}/cs_container_launcher"
9 }
10
11 copy_experiment_client() {
12 # DownloadExpBinary creates the file at EXPERIMENTS_BINARY.
13 cp $EXPERIMENTS_BINARY "${CS_PATH}/${EXPERIMENTS_BINARY}"
14 chmod +x "${CS_PATH}/${EXPERIMENTS_BINARY}"
15 }
16
17 setup_launcher_systemd_unit() {
18 cp container-runner.service "${CS_PATH}/container-runner.service"
19 cp exit_script.sh "${CS_PATH}/exit_script.sh"
20 }
21
22 append_cmdline() {
23 local arg="$1"
24 if [[ ! -d /mnt/disks/efi ]]; then
25 mkdir /mnt/disks/efi
26 fi
27 mount /dev/sda12 /mnt/disks/efi
28 sed -i -e "s|cros_efi|cros_efi ${arg}|g" /mnt/disks/efi/efi/boot/grub.cfg
29 umount /mnt/disks/efi
30 }
31
32 set_default_boot_target() {
33 append_cmdline "systemd.unit=$1"
34 }
35
36 disable_unit() {
37 append_cmdline "systemd.mask=$1"
38 }
39
40 enable_unit() {
41 append_cmdline "systemd.wants=$1"
42 }
43
44 configure_entrypoint() {
45 cp "$1" ${OEM_PATH}/user-data
46 touch ${OEM_PATH}/meta-data
47 append_cmdline "'ds=nocloud;s=${OEM_PATH}/'"
48 }
49
50 configure_necessary_systemd_units() {
51 # Include basic services.
52 enable_unit "basic.target"
53
54 # gcr-wait-online.service is WantedBy=gcr-online.target.
55 # The hostname gcr.io does not resolve until systemd-resolved is enabled.
56 enable_unit "systemd-resolved.service"
57
58 # Dependencies of container-runner.service.
59 enable_unit "network-online.target"
60 enable_unit "gcr-online.target"
61
62 }
63
64 configure_cloud_logging() {
65 # Copy CS-specific fluent-bit config to OEM partition.
66 cp fluent-bit-cs.conf "${CS_PATH}"
67 }
68
69 configure_node_problem_detector() {
70 # Copy CS-specific node-problem-detector configs to OEM partition.
71 cp nodeproblemdetector/system-stats-monitor-cs.json "${CS_PATH}"
72 cp nodeproblemdetector/boot-disk-size-consistency-monitor-cs.json "${CS_PATH}"
73 cp nodeproblemdetector/docker-monitor-cs.json "${CS_PATH}"
74 cp nodeproblemdetector/kernel-monitor-cs.json "${CS_PATH}"
75 }
76
77 configure_systemd_units_for_debug() {
78 configure_cloud_logging
79 configure_node_problem_detector
80
81 disable_unit "konlet-startup.service"
82 }
83 configure_systemd_units_for_hardened() {
84 configure_necessary_systemd_units
85 configure_cloud_logging
86 configure_node_problem_detector
87 # Make entrypoint (via cloud-init) the default unit.
88 set_default_boot_target "cloud-final.service"
89
90 disable_unit "var-lib-docker.mount"
91 disable_unit "docker.service"
92 disable_unit "google-guest-agent.service"
93 disable_unit "google-osconfig-init.service"
94 disable_unit "google-osconfig-agent.service"
95 disable_unit "google-startup-scripts.service"
96 disable_unit "google-shutdown-scripts.service"
97 disable_unit "konlet-startup.service"
98 disable_unit "crash-reporter.service"
99 disable_unit "device_policy_manager.service"
100 disable_unit "docker-events-collector-fluent-bit.service"
101 disable_unit "sshd.service"
102 disable_unit "var-lib-toolbox.mount"
103 }
104
105 main() {
106 mount -o remount,rw ${OEM_PATH}
107 mkdir ${CS_PATH}
108
109 # Install container launcher entrypoint.
110 configure_entrypoint "entrypoint.sh"
111 # Install experiment client.
112 copy_experiment_client
113 # Install container launcher.
114 copy_launcher
115 setup_launcher_systemd_unit
116 # Minimum required COS version for 'e': cos-dev-105-17222-0-0.
117 # Minimum required COS version for 'm': cos-dev-113-18203-0-0.
118 append_cmdline "cos.protected_stateful_partition=m"
119 # Increase wait timeout of the protected stateful partition.
120 append_cmdline "systemd.default_timeout_start_sec=900s"
121
122 if [[ "${IMAGE_ENV}" == "debug" ]]; then
123 configure_systemd_units_for_debug
124 append_cmdline "confidential-space.hardened=false"
125 elif [[ "${IMAGE_ENV}" == "hardened" ]]; then
126 configure_systemd_units_for_hardened
127 append_cmdline "confidential-space.hardened=true"
128 else
129 echo "Unknown image env: ${IMAGE_ENV}." \
130 "Only 'debug' and 'hardened' are supported."
131 exit 1
132 fi
133
134 # Make sure cache is flushed for the OEM partition.
135 sync ${OEM_PATH}
136
137 # Remount as read-only to avoid unexpected changes
138 mount -o remount,ro ${OEM_PATH}
139
140 # Verify the content before the OEM sealing step.
141 ls -lh ${CS_PATH}
142 ls -lh ${OEM_PATH}
143 }
144
145 main