1#!/bin/bash
2
3# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
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."
17
18# Parse command line.
19FLAGS "$@" || exit 1
20eval set -- "${FLAGS_ARGV}"
21
22# Abort on error.
23set -e
24
25if [ -z "${FLAGS_image}" ] || [ ! -s "${FLAGS_image}" ] ; then
26  die "Error: need a valid file by --image"
27fi
28
29# Swiped/modifed from $SRC/src/scripts/base_library/base_image_util.sh.
30zero_free_space() {
31  local rootfs="$1"
32
33  echo "Zeroing freespace in ${rootfs}"
34  # dd is a silly thing and will produce a "No space left on device" message
35  # that cannot be turned off and is confusing to unsuspecting victims.
36  ( sudo dd if=/dev/zero of="${rootfs}/filler" bs=4096 conv=fdatasync \
37      status=noxfer || true ) 2>&1 | grep -v "No space left on device"
38  sudo rm "${rootfs}/filler"
39}
40
41
42strip_boot() {
43  local image=$1
44
45  # Mount image so we can modify it.
46  local rootfs_dir=$(make_temp_dir)
47  mount_image_partition ${image} 3 ${rootfs_dir}
48
49  sudo rm -rf "${rootfs_dir}/boot" &&
50    echo "/boot directory was removed."
51
52  # To prevent the files we just removed from the FS from remaining as non-
53  # zero trash blocks that bloat payload sizes, need to zero them. This was
54  # done when the image was built, but needs to be repeated now that we've
55  # modified it in a non-trivial way.
56  zero_free_space "${rootfs_dir}"
57}
58
59
60IMAGE=$(readlink -f "${FLAGS_image}")
61if [[ -z "${IMAGE}" || ! -f "${IMAGE}" ]]; then
62  die "Missing required argument: --from (image to update)"
63fi
64
65strip_boot "${IMAGE}"
66