blob: 1c2a83c4058f111f7ae883826e2181cec2827763 [file] [log] [blame]
Alex Deymo2b19cfb2015-03-26 00:35:07 -07001#!/bin/bash
2
Alex Deymoaea4c1c2015-08-19 20:24:43 -07003#
4# Copyright (C) 2015 The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
Alex Deymo2b19cfb2015-03-26 00:35:07 -070018
Alex Deymo04ef2072015-08-13 08:19:16 -070019# This script generates some sample images used in unittests and packages them
20# in the sample_images.tar.bz2 file. The list of generated images and their
21# options are described in the main() function. You need to manually run this
22# script to update the generated images whenever you modify this script.
23
Alex Deymo2b19cfb2015-03-26 00:35:07 -070024set -e
25
26# cleanup <path>
27# Unmount and remove the mountpoint <path>
28cleanup() {
29 if ! sudo umount "$1" 2>/dev/null; then
30 if mountpoint -q "$1"; then
31 sync && sudo umount "$1"
32 fi
33 fi
34 rmdir "$1"
35}
36
Alex Deymo2e9533b2015-06-26 20:57:06 -070037# add_files_default <mntdir> <block_size>
38# Add several test files to the image mounted in <mntdir>.
39add_files_default() {
40 local mntdir="$1"
41 local block_size="$2"
Alex Deymo2b19cfb2015-03-26 00:35:07 -070042
43 ### Generate the files used in unittest with descriptive names.
44 sudo touch "${mntdir}"/empty-file
45
46 # regular: Regular files.
47 echo "small file" | sudo dd of="${mntdir}"/regular-small status=none
48 dd if=/dev/zero bs=1024 count=16 status=none | tr '\0' '\141' |
49 sudo dd of="${mntdir}"/regular-16k status=none
50 sudo dd if=/dev/zero of="${mntdir}"/regular-32k-zeros bs=1024 count=16 \
51 status=none
52
53 echo "with net_cap" | sudo dd of="${mntdir}"/regular-with_net_cap status=none
54 sudo setcap cap_net_raw=ep "${mntdir}"/regular-with_net_cap
55
56 # sparse_empty: Files with no data blocks at all (only sparse holes).
57 sudo truncate --size=10240 "${mntdir}"/sparse_empty-10k
58 sudo truncate --size=$(( block_size * 2 )) "${mntdir}"/sparse_empty-2blocks
59
60 # sparse: Files with some data blocks but also sparse holes.
61 echo -n "foo" |
62 sudo dd of="${mntdir}"/sparse-16k-last_block bs=1 \
63 seek=$(( 16 * 1024 - 3)) status=none
64
65 # ext2 inodes have 12 direct blocks, one indirect, one double indirect and
66 # one triple indirect. 10000 should be enough to have an indirect and double
67 # indirect block.
68 echo -n "foo" |
69 sudo dd of="${mntdir}"/sparse-10000blocks bs=1 \
70 seek=$(( block_size * 10000 )) status=none
71
72 sudo truncate --size=16384 "${mntdir}"/sparse-16k-first_block
73 echo "first block" | sudo dd of="${mntdir}"/sparse-16k-first_block status=none
74
75 sudo truncate --size=16384 "${mntdir}"/sparse-16k-holes
76 echo "a" | sudo dd of="${mntdir}"/sparse-16k-holes bs=1 seek=100 status=none
77 echo "b" | sudo dd of="${mntdir}"/sparse-16k-holes bs=1 seek=10000 status=none
78
79 # link: symlinks and hardlinks.
80 sudo ln -s "broken-link" "${mntdir}"/link-short_symlink
81 sudo ln -s $(dd if=/dev/zero bs=256 count=1 status=none | tr '\0' '\141') \
82 "${mntdir}"/link-long_symlink
83 sudo ln "${mntdir}"/regular-16k "${mntdir}"/link-hard-regular-16k
84
85 # Directories.
86 sudo mkdir -p "${mntdir}"/dir1/dir2/dir1
87 echo "foo" | sudo tee "${mntdir}"/dir1/dir2/file >/dev/null
88 echo "bar" | sudo tee "${mntdir}"/dir1/file >/dev/null
89
90 # removed: removed files that should not be listed.
91 echo "We will remove this file so it's contents will be somewhere in the " \
92 "empty space data but it won't be all zeros." |
93 sudo dd of="${mntdir}"/removed conv=fsync status=none
94 sudo rm "${mntdir}"/removed
Alex Deymo2e9533b2015-06-26 20:57:06 -070095}
96
97# add_files_ue_settings <mntdir> <block_size>
98# Add the update_engine.conf settings file. This file contains the
99add_files_ue_settings() {
100 local mntdir="$1"
101
102 sudo mkdir -p "${mntdir}"/etc >/dev/null
103 sudo tee "${mntdir}"/etc/update_engine.conf >/dev/null <<EOF
104PAYLOAD_MINOR_VERSION=1234
105EOF
106 # Example of a real lsb-release file released on link stable.
107 sudo tee "${mntdir}"/etc/lsb-release >/dev/null <<EOF
108CHROMEOS_AUSERVER=https://tools.google.com/service/update2
109CHROMEOS_BOARD_APPID={F26D159B-52A3-491A-AE25-B23670A66B32}
110CHROMEOS_CANARY_APPID={90F229CE-83E2-4FAF-8479-E368A34938B1}
111CHROMEOS_DEVSERVER=
112CHROMEOS_RELEASE_APPID={F26D159B-52A3-491A-AE25-B23670A66B32}
113CHROMEOS_RELEASE_BOARD=link-signed-mp-v4keys
114CHROMEOS_RELEASE_BRANCH_NUMBER=63
115CHROMEOS_RELEASE_BUILD_NUMBER=6946
116CHROMEOS_RELEASE_BUILD_TYPE=Official Build
117CHROMEOS_RELEASE_CHROME_MILESTONE=43
118CHROMEOS_RELEASE_DESCRIPTION=6946.63.0 (Official Build) stable-channel link
119CHROMEOS_RELEASE_NAME=Chrome OS
120CHROMEOS_RELEASE_PATCH_NUMBER=0
121CHROMEOS_RELEASE_TRACK=stable-channel
122CHROMEOS_RELEASE_VERSION=6946.63.0
123GOOGLE_RELEASE=6946.63.0
124EOF
125}
126
127# generate_fs <filename> <kind> <size> [block_size] [block_groups]
128generate_fs() {
129 local filename="$1"
130 local kind="$2"
131 local size="$3"
132 local block_size="${4:-4096}"
133 local block_groups="${5:-}"
134
135 local mkfs_opts=( -q -F -b "${block_size}" -L "ROOT-TEST" -t ext2 )
136 if [[ -n "${block_groups}" ]]; then
137 mkfs_opts+=( -G "${block_groups}" )
138 fi
139
140 local mntdir=$(mktemp --tmpdir -d generate_ext2.XXXXXX)
141 trap 'cleanup "${mntdir}"; rm -f "${filename}"' INT TERM EXIT
142
143 # Cleanup old image.
144 if [[ -e "${filename}" ]]; then
145 rm -f "${filename}"
146 fi
147 truncate --size="${size}" "${filename}"
148
149 mkfs.ext2 "${mkfs_opts[@]}" "${filename}"
150 sudo mount "${filename}" "${mntdir}" -o loop
151
152 case "${kind}" in
153 ue_settings)
154 add_files_ue_settings "${mntdir}" "${block_size}"
155 ;;
156 default)
157 add_files_default "${mntdir}" "${block_size}"
158 ;;
Alex Deymoc90be632016-02-17 19:25:20 -0800159 empty)
160 ;;
Alex Deymo2e9533b2015-06-26 20:57:06 -0700161 esac
Alex Deymo2b19cfb2015-03-26 00:35:07 -0700162
163 cleanup "${mntdir}"
164 trap - INT TERM EXIT
165}
166
Alex Deymo04ef2072015-08-13 08:19:16 -0700167OUTPUT_DIR=$(dirname "$0")
168IMAGES=()
Alex Deymo2b19cfb2015-03-26 00:35:07 -0700169
Alex Deymo04ef2072015-08-13 08:19:16 -0700170# generate_image <image_name> [<image args> ...]
171generate_image() {
172 echo "Generating image $1.img"
173 IMAGES+=( "$1.img" )
174 generate_fs "${OUTPUT_DIR}/$1.img" "${@:2}"
175}
Alex Deymo2b19cfb2015-03-26 00:35:07 -0700176
Alex Deymo04ef2072015-08-13 08:19:16 -0700177main() {
178 # Add more sample images here.
179 generate_image disk_ext2_1k default 16777216 1024
180 generate_image disk_ext2_4k default 16777216 4096
Alex Deymoc90be632016-02-17 19:25:20 -0800181 generate_image disk_ext2_4k_empty empty $((1024 * 4096)) 4096
Alex Deymo04ef2072015-08-13 08:19:16 -0700182 generate_image disk_ext2_ue_settings ue_settings 16777216 4096
183
184 # Generate the tarball and delete temporary images.
185 echo "Packing tar file sample_images.tar.bz2"
186 tar -jcf "${OUTPUT_DIR}/sample_images.tar.bz2" -C "${OUTPUT_DIR}" \
187 "${IMAGES[@]}"
188 cd "${OUTPUT_DIR}"
189 rm "${IMAGES[@]}"
190}
191
192main