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