xref: /aosp_15_r20/external/vboot_reference/scripts/image_signing/strip_boot_from_image.sh (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1#!/bin/bash
2
3# Copyright 2013 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# Script to remove /boot directory from an image.
8
9# Load common constants.  This should be the first executable line.
10# The path to common.sh should be relative to your script's location.
11. "$(dirname "$0")/common.sh"
12
13load_shflags
14
15DEFINE_string image "chromiumos_image.bin" \
16  "Input file name of Chrome OS image to strip /boot from, or path to rootfs."
17
18# Parse command line.
19FLAGS "$@" || exit 1
20eval set -- "${FLAGS_ARGV}"
21
22# Abort on error.
23set -e
24
25# Swiped/modifed from $SRC/src/scripts/base_library/base_image_util.sh.
26zero_free_space() {
27  local rootfs="$1"
28
29  info "Zeroing freespace in ${rootfs}"
30  sudo sfill -llzf "${rootfs}"
31}
32
33
34strip_boot() {
35  local image=$1
36
37  local rootfs_dir=$(make_temp_dir)
38  if [[ -b "${image}" ]]; then
39    enable_rw_mount "${image}"
40    sudo mount "${image}" "${rootfs_dir}"
41    tag_as_needs_to_be_resigned "${rootfs_dir}"
42  else
43    # Mount image so we can modify it.
44    local loopdev=$(loopback_partscan "${image}")
45    mount_loop_image_partition "${loopdev}" 3 "${rootfs_dir}"
46  fi
47
48  sudo rm -rf "${rootfs_dir}/boot" &&
49    info "/boot directory was removed."
50
51  # To prevent the files we just removed from the FS from remaining as non-
52  # zero trash blocks that bloat payload sizes, need to zero them. This was
53  # done when the image was built, but needs to be repeated now that we've
54  # modified it in a non-trivial way.
55  zero_free_space "${rootfs_dir}"
56}
57
58IMAGE=$(readlink -f "${FLAGS_image}")
59if [[ ! -f "${IMAGE}" && ! -b "${IMAGE}" ]]; then
60  IMAGE=
61fi
62if [[ -z "${IMAGE}" ]]; then
63  die "Missing required argument: --from (image to update)"
64fi
65
66strip_boot "${IMAGE}"
67