| #cloud-config |
| # |
| # Copyright 2025 Google LLC |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| # |
| # Collects environmental data from the COS VM and logs it |
| |
| write_files: |
| - path: /tmp/startup.sh |
| permissions: 0644 |
| content: | |
| set +o history |
| set -o errexit |
| set -o pipefail |
| set -o nounset |
| set -x |
| |
| status() { |
| "$@" 2>&1 | sed "s/^/EnvStatus: /" |
| return "${PIPESTATUS[0]}" |
| } |
| |
| main() { |
| status history -c |
| cat <<EOF > /tmp/cos_env.json |
| { |
| "osRelease": "", |
| "lsbRelease": "", |
| "toolchainPath": "", |
| "kernelRelease": "$(uname -r)", |
| "arch": "$(uname -m)" |
| } |
| EOF |
| jq \ |
| --rawfile osRelease /etc/os-release \ |
| --rawfile lsbRelease /etc/lsb-release \ |
| --rawfile toolchainPath /etc/toolchain-path \ |
| '.osRelease = $osRelease | .lsbRelease = $lsbRelease | .toolchainPath = $toolchainPath' \ |
| /tmp/cos_env.json > /tmp/cos_env.json.new |
| status mv /tmp/cos_env.json.new /tmp/cos_env.json |
| status echo "ENVRESULT $(base64 -w0 /tmp/cos_env.json)" |
| } |
| |
| main |
| if [[ $? == 0 ]]; then |
| echo "EnvSucceeded: Shutting down..." |
| else |
| echo "EnvFailed: Shutting down..." |
| fi |
| sleep 10 |
| shutdown -h now |
| while true; do sleep 1; done |
| |
| - path: /etc/systemd/system/envreader@.service |
| permissions: 0644 |
| content: | |
| [Unit] |
| Description=Container-Optimized OS Environment Reader |
| Wants=network-online.target |
| After=network-online.target |
| |
| [Service] |
| Type=oneshot |
| RemainAfterExit=yes |
| User=root |
| ExecStart=/bin/bash /tmp/startup.sh |
| ExecStopPost=/bin/bash -c 'rm /etc/systemd/system/envreader@.service' |
| StandardOutput=tty |
| StandardError=tty |
| TTYPath=%I |
| |
| runcmd: |
| - echo "Starting env reader..." |
| - systemctl daemon-reload |
| - | |
| if [[ "$(uname -m)" == "aarch64" ]]; then |
| systemctl --no-block start envreader@-dev-ttyAMA2.service |
| else |
| systemctl --no-block start envreader@-dev-ttyS2.service |
| fi |