OLD | NEW |
(Empty) | |
| 1 #!/bin/bash |
| 2 |
| 3 # Copyright (c) 2009 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 generate a factory install partition set and miniomaha.conf |
| 8 # file from a release image and a factory image. This creates a server |
| 9 # configuration that can be installed using a factory install shim. |
| 10 # |
| 11 # miniomaha lives in src/platform/dev/ and miniomaha partition sets live |
| 12 # in src/platform/dev/static. |
| 13 |
| 14 # --- BEGIN COMMON.SH BOILERPLATE --- |
| 15 # Load common CrOS utilities. Inside the chroot this file is installed in |
| 16 # /usr/lib/crosutils. Outside the chroot we find it relative to the script's |
| 17 # location. |
| 18 find_common_sh() { |
| 19 local common_paths=(/usr/lib/crosutils $(dirname "$(readlink -f "$0")")) |
| 20 local path |
| 21 |
| 22 SCRIPT_ROOT= |
| 23 for path in "${common_paths[@]}"; do |
| 24 if [ -r "${path}/common.sh" ]; then |
| 25 SCRIPT_ROOT=${path} |
| 26 break |
| 27 fi |
| 28 done |
| 29 } |
| 30 |
| 31 find_common_sh |
| 32 . "${SCRIPT_ROOT}/common.sh" || (echo "Unable to load common.sh" && exit 1) |
| 33 # --- END COMMON.SH BOILERPLATE --- |
| 34 |
| 35 # Need to be inside the chroot to load chromeos-common.sh |
| 36 assert_inside_chroot |
| 37 |
| 38 # Load functions and constants for chromeos-install |
| 39 . "/usr/lib/installer/chromeos-common.sh" || \ |
| 40 die "Unable to load /usr/lib/installer/chromeos-common.sh" |
| 41 |
| 42 # Load functions designed for image processing |
| 43 . "${SCRIPT_ROOT}/lib/cros_image_common.sh" || |
| 44 die "Cannot load required library: lib/cros_image_common.sh; Abort." |
| 45 |
| 46 get_default_board |
| 47 |
| 48 # Flags |
| 49 DEFINE_string board "${DEFAULT_BOARD}" "Board for which the image was built" |
| 50 DEFINE_string factory "" \ |
| 51 "Directory and file containing factory image: /path/chromiumos_test_image.bin" |
| 52 DEFINE_string firmware_updater "" \ |
| 53 "If set, include the firmware shellball into the server configuration" |
| 54 DEFINE_string release "" \ |
| 55 "Directory and file containing release image: /path/chromiumos_image.bin" |
| 56 DEFINE_string subfolder "" \ |
| 57 "If set, the name of the subfolder to put the payload items inside" |
| 58 DEFINE_string diskimg "" \ |
| 59 "If set, the name of the diskimage file to output" |
| 60 DEFINE_boolean preserve ${FLAGS_FALSE} \ |
| 61 "If set, reuse the diskimage file, if available" |
| 62 DEFINE_integer sectors 31277232 "Size of image in sectors" |
| 63 |
| 64 # Parse command line |
| 65 FLAGS "$@" || exit 1 |
| 66 eval set -- "${FLAGS_ARGV}" |
| 67 |
| 68 if [ ! -f "${FLAGS_release}" ]; then |
| 69 echo "Cannot find image file ${FLAGS_release}" |
| 70 exit 1 |
| 71 fi |
| 72 |
| 73 if [ ! -f "${FLAGS_factory}" ]; then |
| 74 echo "Cannot find image file ${FLAGS_factory}" |
| 75 exit 1 |
| 76 fi |
| 77 |
| 78 if [ -n "${FLAGS_firmware_updater}" ] && |
| 79 [ ! -f "${FLAGS_firmware_updater}" ]; then |
| 80 echo "Cannot find firmware file ${FLAGS_firmware_updater}" |
| 81 exit 1 |
| 82 fi |
| 83 |
| 84 # Convert args to paths. Need eval to un-quote the string so that shell |
| 85 # chars like ~ are processed; just doing FOO=`readlink -f ${FOO}` won't work. |
| 86 OMAHA_DIR="${SRC_ROOT}/platform/dev" |
| 87 OMAHA_CONF="${OMAHA_DIR}/miniomaha.conf" |
| 88 OMAHA_DATA_DIR="${OMAHA_DIR}/static/" |
| 89 |
| 90 # Note: The subfolder flag can only append configs. That means you will need |
| 91 # to have unique board IDs for every time you run. If you delete miniomaha.conf |
| 92 # you can still use this flag and it will start fresh. |
| 93 if [ -n "${FLAGS_subfolder}" ]; then |
| 94 OMAHA_DATA_DIR="${OMAHA_DIR}/static/${FLAGS_subfolder}/" |
| 95 fi |
| 96 |
| 97 if [ ${INSIDE_CHROOT} -eq 0 ]; then |
| 98 echo "Caching sudo authentication" |
| 99 sudo -v |
| 100 echo "Done" |
| 101 fi |
| 102 |
| 103 # Use this image as the source image to copy |
| 104 RELEASE_DIR="$(dirname "${FLAGS_release}")" |
| 105 FACTORY_DIR="$(dirname "${FLAGS_factory}")" |
| 106 RELEASE_IMAGE="$(basename "${FLAGS_release}")" |
| 107 FACTORY_IMAGE="$(basename "${FLAGS_factory}")" |
| 108 |
| 109 prepare_img() { |
| 110 local outdev="$(readlink -f "$FLAGS_diskimg")" |
| 111 local sectors="$FLAGS_sectors" |
| 112 local force_full="true" |
| 113 |
| 114 # We'll need some code to put in the PMBR, for booting on legacy BIOS. |
| 115 echo "Fetch PMBR" |
| 116 local pmbrcode="$(mktemp -d)/gptmbr.bin" |
| 117 sudo dd bs=512 count=1 if="${FLAGS_release}" of="${pmbrcode}" status=noxfer |
| 118 |
| 119 echo "Prepare base disk image" |
| 120 # Create an output file if requested, or if none exists. |
| 121 if [ -b "${outdev}" ] ; then |
| 122 echo "Using block device ${outdev}" |
| 123 elif [ ! -e "${outdev}" -o \ |
| 124 "$(stat -c %s ${outdev})" != "$(( ${sectors} * 512 ))" -o \ |
| 125 "$FLAGS_preserve" = "$FLAGS_FALSE" ]; then |
| 126 echo "Generating empty image file" |
| 127 image_dump_partial_file /dev/zero 0 "${sectors}" | |
| 128 dd of="${outdev}" bs=8M |
| 129 else |
| 130 echo "Reusing $outdev" |
| 131 fi |
| 132 |
| 133 # Create GPT partition table. |
| 134 locate_gpt |
| 135 install_gpt "${outdev}" 0 0 "${pmbrcode}" 0 "${force_full}" |
| 136 # Activate the correct partition. |
| 137 sudo "${GPT}" add -i 2 -S 1 -P 1 "${outdev}" |
| 138 } |
| 139 |
| 140 prepare_omaha() { |
| 141 sudo rm -rf "${OMAHA_DATA_DIR}/rootfs-test.gz" |
| 142 sudo rm -rf "${OMAHA_DATA_DIR}/rootfs-release.gz" |
| 143 rm -rf "${OMAHA_DATA_DIR}/efi.gz" |
| 144 rm -rf "${OMAHA_DATA_DIR}/oem.gz" |
| 145 rm -rf "${OMAHA_DATA_DIR}/state.gz" |
| 146 if [ ! -d "${OMAHA_DATA_DIR}" ]; then |
| 147 mkdir -p "${OMAHA_DATA_DIR}" |
| 148 fi |
| 149 } |
| 150 |
| 151 prepare_dir() { |
| 152 sudo rm -rf rootfs-test.gz |
| 153 sudo rm -rf rootfs-release.gz |
| 154 rm -rf efi.gz |
| 155 rm -rf oem.gz |
| 156 rm -rf state.gz |
| 157 } |
| 158 |
| 159 compress_and_hash_memento_image() { |
| 160 local input_file="$1" |
| 161 |
| 162 if [ -n "${IMAGE_IS_UNPACKED}" ]; then |
| 163 sudo "${SCRIPTS_DIR}/mk_memento_images_factory.sh" part_2 part_3 | |
| 164 grep hash | |
| 165 awk '{print $4}' |
| 166 else |
| 167 sudo "${SCRIPTS_DIR}/mk_memento_images_factory.sh" "$input_file" 2 3 | |
| 168 grep hash | |
| 169 awk '{print $4}' |
| 170 fi |
| 171 } |
| 172 |
| 173 compress_and_hash_file() { |
| 174 local input_file="$1" |
| 175 local output_file="$2" |
| 176 |
| 177 if [ -z "$input_file" ]; then |
| 178 # Runs as a pipe processor |
| 179 image_gzip_compress -c -9 | |
| 180 tee "$output_file" | |
| 181 openssl sha1 -binary | |
| 182 openssl base64 |
| 183 else |
| 184 image_gzip_compress -c -9 "$input_file" | |
| 185 tee "$output_file" | |
| 186 openssl sha1 -binary | |
| 187 openssl base64 |
| 188 fi |
| 189 } |
| 190 |
| 191 compress_and_hash_partition() { |
| 192 local input_file="$1" |
| 193 local part_num="$2" |
| 194 local output_file="$3" |
| 195 |
| 196 if [ -n "${IMAGE_IS_UNPACKED}" ]; then |
| 197 compress_and_hash_file "part_$part_num" "$output_file" |
| 198 else |
| 199 image_dump_partition "$input_file" "$part_num" | |
| 200 compress_and_hash_file "" "$output_file" |
| 201 fi |
| 202 } |
| 203 |
| 204 # Decide if we should unpack partition |
| 205 if image_has_part_tools; then |
| 206 IMAGE_IS_UNPACKED= |
| 207 else |
| 208 #TODO(hungte) Currently we run unpack_partitions.sh if part_tools are not |
| 209 # found. If the format of unpack_partitions.sh is reliable, we can prevent |
| 210 # creating temporary files. See image_part_offset for more information. |
| 211 echo "WARNING: cannot find partition tools. Using unpack_partitions.sh." >&2 |
| 212 IMAGE_IS_UNPACKED=1 |
| 213 fi |
| 214 |
| 215 mount_esp() { |
| 216 local image="$1" |
| 217 local esp_mountpoint="$2" |
| 218 offset=$(partoffset "${image}" 12) |
| 219 sudo mount -o loop,offset=$(( offset * 512 )) \ |
| 220 "${image}" "${esp_mountpoint}" |
| 221 ESP_MOUNT="${esp_mountpoint}" |
| 222 } |
| 223 |
| 224 umount_esp() { |
| 225 if [ -n "${ESP_MOUNT}" ]; then |
| 226 sudo umount "${ESP_MOUNT}" |
| 227 fi |
| 228 } |
| 229 |
| 230 generate_img() { |
| 231 local outdev="$(readlink -f "$FLAGS_diskimg")" |
| 232 local sectors="$FLAGS_sectors" |
| 233 |
| 234 prepare_img |
| 235 |
| 236 # Get the release image. |
| 237 pushd "${RELEASE_DIR}" >/dev/null |
| 238 |
| 239 echo "Release Kernel" |
| 240 image_partition_copy "${RELEASE_IMAGE}" 2 "${outdev}" 4 |
| 241 echo "Release Rootfs" |
| 242 image_partition_copy "${RELEASE_IMAGE}" 3 "${outdev}" 5 |
| 243 echo "OEM parition" |
| 244 image_partition_copy "${RELEASE_IMAGE}" 8 "${outdev}" 8 |
| 245 |
| 246 popd >/dev/null |
| 247 |
| 248 # Go to retrieve the factory test image. |
| 249 pushd "${FACTORY_DIR}" >/dev/null |
| 250 |
| 251 echo "Factory Kernel" |
| 252 image_partition_copy "${FACTORY_IMAGE}" 2 "${outdev}" 2 |
| 253 echo "Factory Rootfs" |
| 254 image_partition_copy "${FACTORY_IMAGE}" 3 "${outdev}" 3 |
| 255 echo "Factory Stateful" |
| 256 image_partition_copy "${FACTORY_IMAGE}" 1 "${outdev}" 1 |
| 257 echo "EFI Partition" |
| 258 image_partition_copy "${FACTORY_IMAGE}" 12 "${outdev}" 12 |
| 259 |
| 260 # TODO(nsanders, wad): consolidate this code into some common code |
| 261 # when cleaning up kernel commandlines. There is code that touches |
| 262 # this in postint/chromeos-setimage and build_image. However none |
| 263 # of the preexisting code actually does what we want here. |
| 264 local tmpesp="$(mktemp -d)" |
| 265 mount_esp "${outdev}" "${tmpesp}" |
| 266 |
| 267 trap "umount_esp" EXIT |
| 268 |
| 269 # Edit boot device default for legacy. |
| 270 # Support both vboot and regular boot. |
| 271 sudo sed -i "s/chromeos-usb.A/chromeos-hd.A/" \ |
| 272 "${tmpesp}"/syslinux/default.cfg |
| 273 sudo sed -i "s/chromeos-vusb.A/chromeos-vhd.A/" \ |
| 274 "${tmpesp}"/syslinux/default.cfg |
| 275 |
| 276 # Edit root fs default for legacy |
| 277 # Somewhat safe as ARM does not support syslinux, I believe. |
| 278 sudo sed -i "s'HDROOTA'/dev/sda3'g" "${tmpesp}"/syslinux/root.A.cfg |
| 279 |
| 280 trap - EXIT |
| 281 |
| 282 umount_esp |
| 283 |
| 284 echo "Generated Image at $outdev." |
| 285 echo "Done" |
| 286 } |
| 287 |
| 288 generate_omaha() { |
| 289 # Clean up stale config and data files. |
| 290 prepare_omaha |
| 291 |
| 292 # Get the release image. |
| 293 pushd "${RELEASE_DIR}" >/dev/null |
| 294 echo "Generating omaha release image from ${FLAGS_release}" |
| 295 echo "Generating omaha factory image from ${FLAGS_factory}" |
| 296 echo "Output omaha image to ${OMAHA_DATA_DIR}" |
| 297 echo "Output omaha config to ${OMAHA_CONF}" |
| 298 |
| 299 prepare_dir |
| 300 |
| 301 if [ -n "${IMAGE_IS_UNPACKED}" ]; then |
| 302 echo "Unpacking image ${RELEASE_IMAGE} ..." >&2 |
| 303 sudo ./unpack_partitions.sh "${RELEASE_IMAGE}" 2>/dev/null |
| 304 fi |
| 305 |
| 306 release_hash="$(compress_and_hash_memento_image "${RELEASE_IMAGE}")" |
| 307 sudo chmod a+rw update.gz |
| 308 mv update.gz rootfs-release.gz |
| 309 mv rootfs-release.gz "${OMAHA_DATA_DIR}" |
| 310 echo "release: ${release_hash}" |
| 311 |
| 312 oem_hash="$(compress_and_hash_partition "${RELEASE_IMAGE}" 8 "oem.gz")" |
| 313 mv oem.gz "${OMAHA_DATA_DIR}" |
| 314 echo "oem: ${oem_hash}" |
| 315 |
| 316 popd >/dev/null |
| 317 |
| 318 # Go to retrieve the factory test image. |
| 319 pushd "${FACTORY_DIR}" >/dev/null |
| 320 prepare_dir |
| 321 |
| 322 if [ -n "${IMAGE_IS_UNPACKED}" ]; then |
| 323 echo "Unpacking image ${FACTORY_IMAGE} ..." >&2 |
| 324 sudo ./unpack_partitions.sh "${FACTORY_IMAGE}" 2>/dev/null |
| 325 fi |
| 326 |
| 327 test_hash="$(compress_and_hash_memento_image "${FACTORY_IMAGE}")" |
| 328 sudo chmod a+rw update.gz |
| 329 mv update.gz rootfs-test.gz |
| 330 mv rootfs-test.gz "${OMAHA_DATA_DIR}" |
| 331 echo "test: ${test_hash}" |
| 332 |
| 333 state_hash="$(compress_and_hash_partition "${FACTORY_IMAGE}" 1 "state.gz")" |
| 334 mv state.gz "${OMAHA_DATA_DIR}" |
| 335 echo "state: ${state_hash}" |
| 336 |
| 337 efi_hash="$(compress_and_hash_partition "${FACTORY_IMAGE}" 12 "efi.gz")" |
| 338 mv efi.gz "${OMAHA_DATA_DIR}" |
| 339 echo "efi: ${efi_hash}" |
| 340 |
| 341 popd >/dev/null |
| 342 |
| 343 if [ -n "${FLAGS_firmware_updater}" ]; then |
| 344 SHELLBALL="${FLAGS_firmware_updater}" |
| 345 if [ ! -f "$SHELLBALL" ]; then |
| 346 echo "Failed to find firmware updater: $SHELLBALL." |
| 347 exit 1 |
| 348 fi |
| 349 |
| 350 firmware_hash="$(compress_and_hash_file "$SHELLBALL" "firmware.gz")" |
| 351 mv firmware.gz "${OMAHA_DATA_DIR}" |
| 352 echo "firmware: ${firmware_hash}" |
| 353 fi |
| 354 |
| 355 # If the file does exist and we are using the subfolder flag we are going to |
| 356 # append another config. |
| 357 if [ -n "${FLAGS_subfolder}" ] && |
| 358 [ -f "${OMAHA_CONF}" ]; then |
| 359 # Remove the ']' from the last line of the file |
| 360 # so we can add another config. |
| 361 while [ -s "${OMAHA_CONF}" ]; do |
| 362 # If the last line is null |
| 363 if [ -z "$(tail -1 "${OMAHA_CONF}")" ]; then |
| 364 sed -i '$d' "${OMAHA_CONF}" |
| 365 elif [ "$(tail -1 "${OMAHA_CONF}")" != ']' ]; then |
| 366 sed -i '$d' "${OMAHA_CONF}" |
| 367 else |
| 368 break |
| 369 fi |
| 370 done |
| 371 |
| 372 # Remove the last ] |
| 373 if [ "$(tail -1 "${OMAHA_CONF}")" = ']' ]; then |
| 374 sed -i '$d' "${OMAHA_CONF}" |
| 375 fi |
| 376 |
| 377 # If the file is empty, create it from scratch |
| 378 if [ ! -s "${OMAHA_CONF}" ]; then |
| 379 echo "config = [" >"${OMAHA_CONF}" |
| 380 fi |
| 381 else |
| 382 echo "config = [" >"${OMAHA_CONF}" |
| 383 fi |
| 384 |
| 385 if [ -n "${FLAGS_subfolder}" ]; then |
| 386 subfolder="${FLAGS_subfolder}/" |
| 387 fi |
| 388 |
| 389 echo -n "{ |
| 390 'qual_ids': set([\"${FLAGS_board}\"]), |
| 391 'factory_image': '${subfolder}rootfs-test.gz', |
| 392 'factory_checksum': '${test_hash}', |
| 393 'release_image': '${subfolder}rootfs-release.gz', |
| 394 'release_checksum': '${release_hash}', |
| 395 'oempartitionimg_image': '${subfolder}oem.gz', |
| 396 'oempartitionimg_checksum': '${oem_hash}', |
| 397 'efipartitionimg_image': '${subfolder}efi.gz', |
| 398 'efipartitionimg_checksum': '${efi_hash}', |
| 399 'stateimg_image': '${subfolder}state.gz', |
| 400 'stateimg_checksum': '${state_hash}'," >>"${OMAHA_CONF}" |
| 401 |
| 402 if [ -n "${FLAGS_firmware_updater}" ] ; then |
| 403 echo -n " |
| 404 'firmware_image': '${subfolder}firmware.gz', |
| 405 'firmware_checksum': '${firmware_hash}'," >>"${OMAHA_CONF}" |
| 406 fi |
| 407 |
| 408 echo -n " |
| 409 }, |
| 410 ] |
| 411 " >>"${OMAHA_CONF}" |
| 412 |
| 413 echo "The miniomaha server lives in src/platform/dev. |
| 414 To validate the configutarion, run: |
| 415 python2.6 devserver.py --factory_config miniomaha.conf \ |
| 416 --validate_factory_config |
| 417 To run the server: |
| 418 python2.6 devserver.py --factory_config miniomaha.conf" |
| 419 } |
| 420 |
| 421 # Main |
| 422 if [ -n "$FLAGS_diskimg" ]; then |
| 423 generate_img |
| 424 else |
| 425 generate_omaha |
| 426 fi |
OLD | NEW |