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 # Customize a Chrome OS release image. The cgpt utility must be on the | 7 # Customize a Chrome OS release image. The cgpt utility must be on the |
8 # sudo path. | 8 # sudo path. |
9 # | 9 # |
10 # The following changes are applied: | 10 # The following changes are applied: |
11 # - Set the root password. | 11 # - Set the root password. |
12 | 12 |
13 # Usage: ./customize_image <image.bin> <root_password> | 13 # Usage: ./customize_image.sh <image.bin> <root_password> |
| 14 |
| 15 # Load common constants and variables. |
| 16 . "$(dirname "$0")/common.sh" |
14 | 17 |
15 readonly ROOTFS_DIR=$(mktemp -d) | 18 readonly ROOTFS_DIR=$(mktemp -d) |
16 readonly GPT=cgpt | 19 readonly GPT=cgpt |
17 | 20 |
18 cleanup() { | 21 cleanup() { |
19 set +e | 22 set +e |
20 echo Cleaning up... | 23 echo Cleaning up... |
21 sudo umount -d "$ROOTFS_DIR" | 24 sudo umount -d "$ROOTFS_DIR" |
22 rm -rf "$ROOTFS_DIR" | 25 rm -rf "$ROOTFS_DIR" |
23 } | 26 } |
24 | 27 |
25 failure() { | 28 failure() { |
26 cleanup | 29 cleanup |
27 exit 1 | 30 exit 1 |
28 } | 31 } |
29 | 32 |
30 # Read GPT table to find the starting location of a specific partition. | |
31 # Args: DEVICE PARTNUM | |
32 # Returns: offset (in sectors) of partition PARTNUM | |
33 partoffset() { | |
34 sudo $GPT show -b -i $2 $1 | |
35 } | |
36 | |
37 mount_image() { | |
38 local image=$1 | |
39 echo "Mounting image '$image'..." | |
40 local offset=$(partoffset "$image" 3) | |
41 sudo mount -o loop,offset=$((offset * 512)) "$image" "$ROOTFS_DIR" | |
42 } | |
43 | |
44 change_root_password() { | 33 change_root_password() { |
45 local password=$1 | 34 local password=$1 |
46 echo "Changing root password to '$password'..." | 35 echo "Changing root password to '$password'..." |
47 local crypted_password="$(echo $password | openssl passwd -1 -stdin)" | 36 local crypted_password="$(echo $password | openssl passwd -1 -stdin)" |
48 local temp_shadow="$ROOTFS_DIR/etc/tempshadow" | 37 local temp_shadow="$ROOTFS_DIR/etc/tempshadow" |
49 echo "root:$crypted_password:14500:0:::::" \ | 38 echo "root:$crypted_password:14500:0:::::" \ |
50 | sudo tee "$temp_shadow" > /dev/null | 39 | sudo tee "$temp_shadow" > /dev/null |
51 grep -Ev ^root: "$ROOTFS_DIR/etc/shadow" \ | 40 grep -Ev ^root: "$ROOTFS_DIR/etc/shadow" \ |
52 | sudo tee -a "$temp_shadow" > /dev/null | 41 | sudo tee -a "$temp_shadow" > /dev/null |
53 sudo mv -f "$temp_shadow" "$ROOTFS_DIR/etc/shadow" | 42 sudo mv -f "$temp_shadow" "$ROOTFS_DIR/etc/shadow" |
54 } | 43 } |
55 | 44 |
56 main() { | 45 main() { |
57 local image=$1 | 46 local image=$1 |
58 local root_password=$2 | 47 local root_password=$2 |
59 if [ $# -ne 2 ]; then | 48 if [ $# -ne 2 ]; then |
60 echo "Usage: $0 <image.bin> <root_password>" | 49 echo "Usage: $0 <image.bin> <root_password>" |
61 exit 1 | 50 exit 1 |
62 fi | 51 fi |
63 | 52 |
64 set -e | 53 set -e |
65 trap failure EXIT | 54 trap failure EXIT |
66 mount_image "$image" | 55 mount_image_partition "$image" 3 $ROOTFS_DIR |
67 change_root_password "$root_password" | 56 change_root_password "$root_password" |
68 cleanup | 57 cleanup |
69 echo "Done." | 58 echo "Done." |
70 trap - EXIT | 59 trap - EXIT |
71 } | 60 } |
72 | 61 |
73 main $@ | 62 main $@ |
OLD | NEW |