| OLD | NEW |
| 1 #!/bin/bash | 1 #!/bin/bash |
| 2 | 2 |
| 3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 # Globals |
| 8 # ---------------------------------------------------------------------------- |
| 9 |
| 7 # Determine script directory | 10 # Determine script directory |
| 8 SCRIPT_DIR=$(dirname $0) | 11 SCRIPT_DIR=$(dirname $0) |
| 9 PROG=$(basename $0) | 12 PROG=$(basename $0) |
| 10 GPT=cgpt | 13 GPT=cgpt |
| 11 | 14 |
| 12 # The tag when the rootfs is changed. | 15 # The tag when the rootfs is changed. |
| 13 TAG_NEEDS_TO_BE_SIGNED="/root/.need_to_be_signed" | 16 TAG_NEEDS_TO_BE_SIGNED="/root/.need_to_be_signed" |
| 14 | 17 |
| 15 # Array of actions that are executed during the clean up process. | 18 # Array of actions that are executed during the clean up process. |
| 16 declare -a cleanup_actions | 19 declare -a cleanup_actions |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 if [ -f /usr/lib/shflags ]; then | 90 if [ -f /usr/lib/shflags ]; then |
| 88 . /usr/lib/shflags | 91 . /usr/lib/shflags |
| 89 elif [ -f "${SCRIPT_DIR}/shflags" ]; then | 92 elif [ -f "${SCRIPT_DIR}/shflags" ]; then |
| 90 . "${SCRIPT_DIR}/shflags" | 93 . "${SCRIPT_DIR}/shflags" |
| 91 elif [ -f "${SCRIPT_DIR}/lib/shflags/shflags" ]; then | 94 elif [ -f "${SCRIPT_DIR}/lib/shflags/shflags" ]; then |
| 92 . "${SCRIPT_DIR}/lib/shflags/shflags" | 95 . "${SCRIPT_DIR}/lib/shflags/shflags" |
| 93 else | 96 else |
| 94 echo "ERROR: Cannot find the required shflags library." | 97 echo "ERROR: Cannot find the required shflags library." |
| 95 return 1 | 98 return 1 |
| 96 fi | 99 fi |
| 100 |
| 101 # Add debug option for debug output below |
| 102 DEFINE_boolean debug $FLAGS_FALSE "Provide debug messages" "d" |
| 97 } | 103 } |
| 98 | 104 |
| 99 # List of Temporary files and mount points. | 105 # Functions for debug output |
| 100 TEMP_FILE_LIST=$(mktemp) | 106 # ---------------------------------------------------------------------------- |
| 101 TEMP_DIR_LIST=$(mktemp) | 107 |
| 108 # Reports error message and exit(1) |
| 109 # Args: error message |
| 110 err_die() { |
| 111 echo "ERROR: $*" 1>&2 |
| 112 exit 1 |
| 113 } |
| 114 |
| 115 # Returns true if we're running in debug mode. |
| 116 # |
| 117 # Note that if you don't set up shflags by calling load_shflags(), you |
| 118 # must set $FLAGS_debug and $FLAGS_TRUE yourself. The default |
| 119 # behavior is that debug will be off if you define neither $FLAGS_TRUE |
| 120 # nor $FLAGS_debug. |
| 121 is_debug_mode() { |
| 122 [ "${FLAGS_debug:-not$FLAGS_TRUE}" = "$FLAGS_TRUE" ] |
| 123 } |
| 124 |
| 125 # Prints messages (in parameters) in debug mode |
| 126 # Args: debug message |
| 127 debug_msg() { |
| 128 if is_debug_mode; then |
| 129 echo "DEBUG: $*" 1>&2 |
| 130 fi |
| 131 } |
| 132 |
| 133 # Functions for temporary files and directories |
| 134 # ---------------------------------------------------------------------------- |
| 135 |
| 136 # Create a new temporary file and return its name. |
| 137 # File is automatically cleaned when cleanup_temps_and_mounts() is called. |
| 138 make_temp_file() { |
| 139 local tempfile=$(mktemp) |
| 140 echo "$tempfile" >> $TEMP_FILE_LIST |
| 141 echo $tempfile |
| 142 } |
| 143 |
| 144 # Create a new temporary directory and return its name. |
| 145 # Directory is automatically deleted and any filesystem mounted on it unmounted |
| 146 # when cleanup_temps_and_mounts() is called. |
| 147 make_temp_dir() { |
| 148 local tempdir=$(mktemp -d) |
| 149 echo "$tempdir" >> $TEMP_DIR_LIST |
| 150 echo $tempdir |
| 151 } |
| 152 |
| 153 cleanup_temps_and_mounts() { |
| 154 for i in $(cat $TEMP_FILE_LIST); do |
| 155 rm -f $i |
| 156 done |
| 157 set +e # umount may fail for unmounted directories |
| 158 for i in $(cat $TEMP_DIR_LIST); do |
| 159 if [ -n "$i" ]; then |
| 160 if has_needs_to_be_resigned_tag "$i"; then |
| 161 echo "Warning: image may be modified. Please resign image." |
| 162 fi |
| 163 sudo umount -d $i 2>/dev/null |
| 164 rm -rf $i |
| 165 fi |
| 166 done |
| 167 set -e |
| 168 rm -rf $TEMP_DIR_LIST $TEMP_FILE_LIST |
| 169 } |
| 170 |
| 171 trap "cleanup_temps_and_mounts" EXIT |
| 172 |
| 173 # Functions for partition management |
| 174 # ---------------------------------------------------------------------------- |
| 102 | 175 |
| 103 # Read GPT table to find the starting location of a specific partition. | 176 # Read GPT table to find the starting location of a specific partition. |
| 104 # Args: DEVICE PARTNUM | 177 # Args: DEVICE PARTNUM |
| 105 # Returns: offset (in sectors) of partition PARTNUM | 178 # Returns: offset (in sectors) of partition PARTNUM |
| 106 partoffset() { | 179 partoffset() { |
| 107 sudo $GPT show -b -i $2 $1 | 180 sudo $GPT show -b -i $2 $1 |
| 108 } | 181 } |
| 109 | 182 |
| 110 # Read GPT table to find the size of a specific partition. | 183 # Read GPT table to find the size of a specific partition. |
| 111 # Args: DEVICE PARTNUM | 184 # Args: DEVICE PARTNUM |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 # Args: IMAGE PARTNUM INPUTFILE | 251 # Args: IMAGE PARTNUM INPUTFILE |
| 179 replace_image_partition() { | 252 replace_image_partition() { |
| 180 local image=$1 | 253 local image=$1 |
| 181 local partnum=$2 | 254 local partnum=$2 |
| 182 local input_file=$3 | 255 local input_file=$3 |
| 183 local offset=$(partoffset "$image" "$partnum") | 256 local offset=$(partoffset "$image" "$partnum") |
| 184 local size=$(partsize "$image" "$partnum") | 257 local size=$(partsize "$image" "$partnum") |
| 185 dd if=$input_file of=$image bs=512 seek=$offset count=$size conv=notrunc | 258 dd if=$input_file of=$image bs=512 seek=$offset count=$size conv=notrunc |
| 186 } | 259 } |
| 187 | 260 |
| 188 # Create a new temporary file and return its name. | |
| 189 # File is automatically cleaned when cleanup_temps_and_mounts() is called. | |
| 190 make_temp_file() { | |
| 191 local tempfile=$(mktemp) | |
| 192 echo "$tempfile" >> $TEMP_FILE_LIST | |
| 193 echo $tempfile | |
| 194 } | |
| 195 | |
| 196 # Create a new temporary directory and return its name. | |
| 197 # Directory is automatically deleted and any filesystem mounted on it unmounted | |
| 198 # when cleanup_temps_and_mounts() is called. | |
| 199 make_temp_dir() { | |
| 200 local tempdir=$(mktemp -d) | |
| 201 echo "$tempdir" >> $TEMP_DIR_LIST | |
| 202 echo $tempdir | |
| 203 } | |
| 204 | |
| 205 cleanup_temps_and_mounts() { | |
| 206 for i in "$(cat $TEMP_FILE_LIST)"; do | |
| 207 rm -f $i | |
| 208 done | |
| 209 set +e # umount may fail for unmounted directories | |
| 210 for i in "$(cat $TEMP_DIR_LIST)"; do | |
| 211 if [ -n "$i" ]; then | |
| 212 if has_needs_to_be_resigned_tag "$i"; then | |
| 213 echo "Warning: image may be modified. Please resign image." | |
| 214 fi | |
| 215 sudo umount -d $i 2>/dev/null | |
| 216 rm -rf $i | |
| 217 fi | |
| 218 done | |
| 219 set -e | |
| 220 rm -rf $TEMP_DIR_LIST $TEMP_FILE_LIST | |
| 221 } | |
| 222 | |
| 223 # Returns true if all files in parameters exist. | |
| 224 ensure_files_exist() { | |
| 225 local filename return_value=0 | |
| 226 for filename in "$@"; do | |
| 227 if [ ! -f "$filename" -a ! -b "$filename" ]; then | |
| 228 echo "ERROR: Cannot find required file: $filename" | |
| 229 return_value=1 | |
| 230 fi | |
| 231 done | |
| 232 | |
| 233 return $return_value | |
| 234 } | |
| 235 | |
| 236 # For details, see crosutils.git/common.sh | 261 # For details, see crosutils.git/common.sh |
| 237 enable_rw_mount() { | 262 enable_rw_mount() { |
| 238 local rootfs="$1" | 263 local rootfs="$1" |
| 239 local offset="${2-0}" | 264 local offset="${2-0}" |
| 240 | 265 |
| 241 # Make sure we're checking an ext2 image | 266 # Make sure we're checking an ext2 image |
| 242 if ! is_ext2 "$rootfs" $offset; then | 267 if ! is_ext2 "$rootfs" $offset; then |
| 243 echo "enable_rw_mount called on non-ext2 filesystem: $rootfs $offset" 1>&2 | 268 echo "enable_rw_mount called on non-ext2 filesystem: $rootfs $offset" 1>&2 |
| 244 return 1 | 269 return 1 |
| 245 fi | 270 fi |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte | 325 local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte |
| 301 local ro_value=$(sudo dd if="$rootfs" skip=$((offset + ro_compat_offset)) \ | 326 local ro_value=$(sudo dd if="$rootfs" skip=$((offset + ro_compat_offset)) \ |
| 302 count=1 bs=1 2>/dev/null) | 327 count=1 bs=1 2>/dev/null) |
| 303 local expected_ro_value=$(printf '\377') | 328 local expected_ro_value=$(printf '\377') |
| 304 if [ "$ro_value" = "$expected_ro_value" ]; then | 329 if [ "$ro_value" = "$expected_ro_value" ]; then |
| 305 return 0 | 330 return 0 |
| 306 fi | 331 fi |
| 307 return 1 | 332 return 1 |
| 308 } | 333 } |
| 309 | 334 |
| 335 # Misc functions |
| 336 # ---------------------------------------------------------------------------- |
| 337 |
| 338 # Returns true if all files in parameters exist. |
| 339 # Args: List of files |
| 340 ensure_files_exist() { |
| 341 local filename return_value=0 |
| 342 for filename in "$@"; do |
| 343 if [ ! -f "$filename" -a ! -b "$filename" ]; then |
| 344 echo "ERROR: Cannot find required file: $filename" |
| 345 return_value=1 |
| 346 fi |
| 347 done |
| 348 |
| 349 return $return_value |
| 350 } |
| 351 |
| 310 # Check if the 'chronos' user already has a password | 352 # Check if the 'chronos' user already has a password |
| 311 # ARGS: rootfs | 353 # Args: rootfs |
| 312 no_chronos_password() { | 354 no_chronos_password() { |
| 313 local rootfs=$1 | 355 local rootfs=$1 |
| 314 sudo grep -q '^chronos:\*:' "$rootfs/etc/shadow" | 356 sudo grep -q '^chronos:\*:' "$rootfs/etc/shadow" |
| 315 } | 357 } |
| 316 | 358 |
| 317 trap "cleanup" INT TERM EXIT | 359 trap "cleanup" INT TERM EXIT |
| 318 | 360 |
| 319 add_cleanup_action "cleanup_temps_and_mounts" | 361 add_cleanup_action "cleanup_temps_and_mounts" |
| 320 | |
| OLD | NEW |