xref: /aosp_15_r20/external/vboot_reference/scripts/image_signing/set_lsb_release.sh (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1#!/bin/bash
2
3# Copyright 2010 The ChromiumOS Authors
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7# Customizes a Chrome OS release image by setting /etc/lsb-release values.
8
9# Load common constants and variables.
10. "$(dirname "$0")/common.sh"
11
12set_lsb_release_keyval() {
13  local rootfs=$1
14  local lsb="${rootfs}/etc/lsb-release"
15  local key=$2
16  local value=$3
17  local data
18  data=$(
19    (
20    grep -Ev "^${key}=" "${lsb}"
21    echo "${key}=${value}"
22    ) | sort
23  )
24  sudo tee "${lsb}" <<<"${data}" >/dev/null
25}
26
27main() {
28  set -e
29
30  if [[ $(( $# % 2 )) -eq 0 ]]; then
31    cat <<EOF
32Usage: $PROG <image.bin> [<key> <value> [<key> <value> ...]]
33
34Examples:
35
36$ $PROG chromiumos_image.bin
37
38Dumps /etc/lsb-release from chromiumos_image.bin to stdout.
39
40$ $PROG chromiumos_image.bin CHROMEOS_RELEASE_DESCRIPTION "New description"
41
42Sets the CHROMEOS_RELEASE_DESCRIPTION key's value to "New description"
43in /etc/lsb-release in chromiumos_image.bin, sorts the keys and dumps
44the updated file to stdout.
45
46EOF
47    exit 1
48  fi
49
50  # If there are no key/value pairs to process, we don't need write access.
51  local ro=$([[ $# -eq 0 ]] && echo true || echo false)
52
53  local image=$1
54  shift
55  local loopdev rootfs
56
57  if [[ -d "${image}" ]]; then
58    rootfs="${image}"
59  else
60    rootfs=$(make_temp_dir)
61    loopdev=$(loopback_partscan "${image}")
62
63    if ${ro}; then
64      mount_loop_image_partition_ro "${loopdev}" 3 "${rootfs}"
65    else
66      mount_loop_image_partition "${loopdev}" 3 "${rootfs}"
67      touch "${image}"  # Updates the image modification time.
68    fi
69  fi
70
71  # Process all the key/value pairs.
72  local key value
73  while [[ $# -ne 0 ]]; do
74    key=$1 value=$2
75    shift 2
76    set_lsb_release_keyval "${rootfs}" "${key}" "${value}"
77  done
78  if ! ${ro}; then
79    restore_lsb_selinux "${rootfs}/etc/lsb-release"
80  fi
81
82  # Dump the final state.
83  cat "${rootfs}/etc/lsb-release"
84
85  # Dump security context for lsb-release file
86  getfattr --absolute-names -n security.selinux "${rootfs}/etc/lsb-release"
87}
88
89main "$@"
90