Mike Frysinger | 1963df1 | 2013-06-03 14:01:35 -0400 | [diff] [blame] | 1 | #!/bin/sh |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 2 | # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | # |
| 6 | # This contains common constants and functions for installer scripts. This must |
| 7 | # evaluate properly for both /bin/bash and /bin/sh, since it's used both to |
| 8 | # create the initial image at compile time and to install or upgrade a running |
| 9 | # image. |
| 10 | |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 11 | # The GPT tables describe things in terms of 512-byte sectors, but some |
| 12 | # filesystems prefer 4096-byte blocks. These functions help with alignment |
| 13 | # issues. |
| 14 | |
Micah Morton | e615bc3 | 2018-06-06 09:30:13 -0700 | [diff] [blame] | 15 | # Call sudo when not root already. Otherwise, add usual path before calling a |
| 16 | # command, as sudo does. |
| 17 | # This way we avoid using sudo when running on a device in verified mode. |
| 18 | maybe_sudo() { |
| 19 | if [ "${UID:-$(id -u)}" = "0" ]; then |
| 20 | PATH="${PATH}:/sbin:/usr/sbin" "$@" |
| 21 | else |
| 22 | sudo "$@" |
| 23 | fi |
| 24 | } |
| 25 | |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 26 | # This returns the size of a file or device in 512-byte sectors, rounded up if |
| 27 | # needed. |
| 28 | # Invoke as: subshell |
| 29 | # Args: FILENAME |
| 30 | # Return: whole number of sectors needed to fully contain FILENAME |
| 31 | numsectors() { |
Jie Sun | 1f9d412 | 2010-04-12 17:04:36 -0700 | [diff] [blame] | 32 | if [ -b "${1}" ]; then |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 33 | dev=${1##*/} |
Jie Sun | 1f9d412 | 2010-04-12 17:04:36 -0700 | [diff] [blame] | 34 | if [ -e /sys/block/$dev/size ]; then |
| 35 | cat /sys/block/$dev/size |
| 36 | else |
| 37 | part=${1##*/} |
| 38 | block=$(get_block_dev_from_partition_dev "${1}") |
| 39 | block=${block##*/} |
| 40 | cat /sys/block/$block/$part/size |
| 41 | fi |
| 42 | else |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 43 | local bytes=$(stat -c%s "$1") |
| 44 | local sectors=$(( $bytes / 512 )) |
| 45 | local rem=$(( $bytes % 512 )) |
| 46 | if [ $rem -ne 0 ]; then |
| 47 | sectors=$(( $sectors + 1 )) |
| 48 | fi |
| 49 | echo $sectors |
Jie Sun | 1f9d412 | 2010-04-12 17:04:36 -0700 | [diff] [blame] | 50 | fi |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Sam Hurst | 545e551 | 2018-06-07 11:08:46 -0700 | [diff] [blame] | 53 | # This returns the block size of a file or device in byte |
| 54 | # Invoke as: subshell |
| 55 | # Args: FILENAME |
| 56 | # Return: block size in bytes |
| 57 | blocksize() { |
| 58 | local path="$1" |
| 59 | if [ -b "${path}" ]; then |
| 60 | local dev="${path##*/}" |
| 61 | local sys="/sys/block/${dev}/queue/logical_block_size" |
| 62 | if [ -e "${sys}" ]; then |
| 63 | cat "${sys}" |
| 64 | else |
| 65 | local part="${path##*/}" |
| 66 | local block="$(get_block_dev_from_partition_dev "${path}")" |
| 67 | local block="${block##*/}" |
| 68 | cat "/sys/block/${block}/${part}/queue/logical_block_size" |
| 69 | fi |
| 70 | else |
| 71 | echo 512 |
| 72 | fi |
| 73 | } |
| 74 | |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 75 | # Locate the cgpt tool. It should already be installed in the build chroot, |
robotboy | a768429 | 2010-04-21 14:46:00 -0700 | [diff] [blame] | 76 | # but some of these functions may be invoked outside the chroot (by |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 77 | # image_to_usb or similar), so we need to find it. |
robotboy | a768429 | 2010-04-21 14:46:00 -0700 | [diff] [blame] | 78 | GPT="" |
Yusuke Sato | d55cea9 | 2010-04-21 10:46:59 +0900 | [diff] [blame] | 79 | |
robotboy | a768429 | 2010-04-21 14:46:00 -0700 | [diff] [blame] | 80 | locate_gpt() { |
| 81 | if [ -z "$GPT" ]; then |
Bill Richardson | f67f844 | 2010-12-01 08:27:37 -0800 | [diff] [blame] | 82 | if [ -x "${DEFAULT_CHROOT_DIR:-}/usr/bin/cgpt" ]; then |
| 83 | GPT="${DEFAULT_CHROOT_DIR:-}/usr/bin/cgpt" |
| 84 | else |
| 85 | GPT=$(which cgpt 2>/dev/null) || /bin/true |
| 86 | if [ -z "$GPT" ]; then |
Bill Richardson | 7c358a9 | 2010-06-11 09:16:03 -0700 | [diff] [blame] | 87 | echo "can't find cgpt tool" 1>&2 |
robotboy | a768429 | 2010-04-21 14:46:00 -0700 | [diff] [blame] | 88 | exit 1 |
| 89 | fi |
| 90 | fi |
| 91 | fi |
| 92 | } |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 93 | |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 94 | # Read GPT table to find the starting location of a specific partition. |
| 95 | # Invoke as: subshell |
| 96 | # Args: DEVICE PARTNUM |
| 97 | # Returns: offset (in sectors) of partition PARTNUM |
| 98 | partoffset() { |
Micah Morton | e615bc3 | 2018-06-06 09:30:13 -0700 | [diff] [blame] | 99 | maybe_sudo $GPT show -b -i $2 $1 |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | # Read GPT table to find the size of a specific partition. |
| 103 | # Invoke as: subshell |
| 104 | # Args: DEVICE PARTNUM |
| 105 | # Returns: size (in sectors) of partition PARTNUM |
| 106 | partsize() { |
Micah Morton | e615bc3 | 2018-06-06 09:30:13 -0700 | [diff] [blame] | 107 | maybe_sudo $GPT show -s -i $2 $1 |
Bill Richardson | eff5b06 | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Jie Sun | 1f9d412 | 2010-04-12 17:04:36 -0700 | [diff] [blame] | 110 | # Extract the whole disk block device from the partition device. |
| 111 | # This works for /dev/sda3 (-> /dev/sda) as well as /dev/mmcblk0p2 |
| 112 | # (-> /dev/mmcblk0). |
| 113 | get_block_dev_from_partition_dev() { |
| 114 | local partition=$1 |
| 115 | if ! (expr match "$partition" ".*[0-9]$" >/dev/null) ; then |
| 116 | echo "Invalid partition name: $partition" >&2 |
| 117 | exit 1 |
| 118 | fi |
Will Drewry | 056e9c8 | 2010-08-06 16:10:59 -0500 | [diff] [blame] | 119 | # Removes any trailing digits. |
| 120 | local block=$(echo "$partition" | sed -e 's/[0-9]*$//') |
Jie Sun | 1f9d412 | 2010-04-12 17:04:36 -0700 | [diff] [blame] | 121 | # If needed, strip the trailing 'p'. |
| 122 | if (expr match "$block" ".*[0-9]p$" >/dev/null); then |
| 123 | echo "${block%p}" |
| 124 | else |
| 125 | echo "$block" |
| 126 | fi |
| 127 | } |
| 128 | |
| 129 | # Extract the partition number from the partition device. |
| 130 | # This works for /dev/sda3 (-> 3) as well as /dev/mmcblk0p2 (-> 2). |
| 131 | get_partition_number() { |
| 132 | local partition=$1 |
| 133 | if ! (expr match "$partition" ".*[0-9]$" >/dev/null) ; then |
| 134 | echo "Invalid partition name: $partition" >&2 |
| 135 | exit 1 |
| 136 | fi |
| 137 | # Extract the last digit. |
| 138 | echo "$partition" | sed -e 's/^.*\([0-9]\)$/\1/' |
| 139 | } |
| 140 | |
| 141 | # Construct a partition device name from a whole disk block device and a |
| 142 | # partition number. |
| 143 | # This works for [/dev/sda, 3] (-> /dev/sda3) as well as [/dev/mmcblk0, 2] |
| 144 | # (-> /dev/mmcblk0p2). |
| 145 | make_partition_dev() { |
| 146 | local block=$1 |
| 147 | local num=$2 |
| 148 | # If the disk block device ends with a number, we add a 'p' before the |
| 149 | # partition number. |
| 150 | if (expr match "$block" ".*[0-9]$" >/dev/null) ; then |
| 151 | echo "${block}p${num}" |
| 152 | else |
| 153 | echo "${block}${num}" |
| 154 | fi |
| 155 | } |
| 156 | |
Gwendal Grignou | 20f0ca7 | 2014-03-13 16:14:43 -0700 | [diff] [blame] | 157 | # Return the type of device. |
| 158 | # |
| 159 | # The type can be: |
| 160 | # MMC, SD for device managed by the MMC stack |
| 161 | # ATA for ATA disk |
Deepti Patil | df2d269 | 2016-03-04 15:24:58 +0530 | [diff] [blame] | 162 | # NVME for NVMe device |
Gwendal Grignou | 20f0ca7 | 2014-03-13 16:14:43 -0700 | [diff] [blame] | 163 | # OTHER for other devices. |
| 164 | get_device_type() { |
| 165 | local dev="$(basename "$1")" |
| 166 | local vdr |
| 167 | local type_file |
| 168 | local vendor_file |
Deepti Patil | df2d269 | 2016-03-04 15:24:58 +0530 | [diff] [blame] | 169 | # True device path of a NVMe device is just a simple PCI device. |
| 170 | # (there are no other buses), |
| 171 | # Use the device name to identify the type precisely. |
| 172 | case "${dev}" in |
| 173 | nvme*) |
| 174 | echo "NVME" |
| 175 | return |
| 176 | ;; |
| 177 | esac |
| 178 | |
Gwendal Grignou | 20f0ca7 | 2014-03-13 16:14:43 -0700 | [diff] [blame] | 179 | type_file="/sys/block/${dev}/device/type" |
| 180 | # To detect device managed by the MMC stack |
| 181 | case $(readlink -f "${type_file}") in |
| 182 | *mmc*) |
| 183 | cat "${type_file}" |
| 184 | ;; |
| 185 | *usb*) |
| 186 | # Now if it contains 'usb', it is managed through |
| 187 | # a USB controller. |
| 188 | echo "USB" |
| 189 | ;; |
| 190 | *target*) |
| 191 | # Other SCSI devices. |
| 192 | # Check if it is an ATA device. |
| 193 | vdr="$(cat "/sys/block/${dev}/device/vendor")" |
| 194 | if [ "${vdr%% *}" = "ATA" ]; then |
| 195 | echo "ATA" |
| 196 | else |
| 197 | echo "OTHER" |
| 198 | fi |
| 199 | ;; |
| 200 | *) |
| 201 | echo "OTHER" |
| 202 | esac |
| 203 | } |
| 204 | |
Gwendal Grignou | 732af51 | 2014-04-07 20:07:29 +0000 | [diff] [blame] | 205 | # ATA disk have ATA as vendor. |
| 206 | # They may not contain ata in their device path if behind a SAS |
| 207 | # controller. |
| 208 | # Exclude disks with size 0, it means they did not spin up properly. |
| 209 | list_fixed_ata_disks() { |
| 210 | local sd |
| 211 | local remo |
| 212 | local vdr |
| 213 | local size |
| 214 | |
| 215 | for sd in /sys/block/sd*; do |
| 216 | if [ ! -r "${sd}/size" ]; then |
| 217 | continue |
Paul Stewart | 04f3ef1 | 2010-05-27 15:56:42 -0700 | [diff] [blame] | 218 | fi |
Gwendal Grignou | 732af51 | 2014-04-07 20:07:29 +0000 | [diff] [blame] | 219 | size=$(cat "${sd}/size") |
| 220 | remo=$(cat "${sd}/removable") |
| 221 | vdr=$(cat "${sd}/device/vendor") |
| 222 | if [ "${vdr%% *}" = "ATA" -a ${remo:-0} -eq 0 -a ${size:-0} -gt 0 ]; then |
| 223 | echo "${sd##*/}" |
| 224 | fi |
Paul Stewart | 04f3ef1 | 2010-05-27 15:56:42 -0700 | [diff] [blame] | 225 | done |
Gwendal Grignou | 732af51 | 2014-04-07 20:07:29 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | # We assume we only have eMMC devices, not removable MMC devices. |
Gwendal Grignou | 6562fe7 | 2017-06-06 09:11:01 -0700 | [diff] [blame] | 229 | # also, do not consider special hardware partitions on the eMMC, like boot. |
Gwendal Grignou | 732af51 | 2014-04-07 20:07:29 +0000 | [diff] [blame] | 230 | # These devices are built on top of the eMMC sysfs path: |
| 231 | # /sys/block/mmcblk0 -> .../mmc_host/.../mmc0:0001/.../mmcblk0 |
| 232 | # /sys/block/mmcblk0boot0 -> .../mmc_host/.../mmc0:0001/.../mmcblk0/mmcblk0boot0 |
| 233 | # /sys/block/mmcblk0boot1 -> .../mmc_host/.../mmc0:0001/.../mmcblk0/mmcblk0boot1 |
| 234 | # /sys/block/mmcblk0rpmb -> .../mmc_host/.../mmc0:0001/.../mmcblk0/mmcblk0rpmb |
| 235 | # |
| 236 | # Their device link points back to mmcblk0, not to the hardware |
| 237 | # device (mmc0:0001). Therefore there is no type in their device link. |
| 238 | # (it should be /device/device/type) |
| 239 | list_fixed_mmc_disks() { |
| 240 | local mmc |
| 241 | local type_file |
| 242 | for mmc in /sys/block/mmcblk*; do |
| 243 | type_file="${mmc}/device/type" |
| 244 | if [ -r "${type_file}" ]; then |
| 245 | if [ "$(cat "${type_file}")" = "MMC" ]; then |
| 246 | echo "${mmc##*/}" |
| 247 | fi |
| 248 | fi |
| 249 | done |
| 250 | } |
| 251 | |
Gwendal Grignou | 66cc835 | 2016-10-14 08:48:57 -0700 | [diff] [blame] | 252 | # NVMe device |
| 253 | # Exclude disks with size 0, it means they did not spin up properly. |
| 254 | list_fixed_nvme_disks() { |
Gwendal Grignou | dc0a207 | 2017-07-11 09:57:57 -0700 | [diff] [blame] | 255 | local nvme remo size nvme_base |
| 256 | local all_nvme='' |
Gwendal Grignou | 66cc835 | 2016-10-14 08:48:57 -0700 | [diff] [blame] | 257 | |
| 258 | for nvme in /sys/block/nvme*; do |
Gwendal Grignou | 040ab8c | 2017-04-27 18:24:35 -0700 | [diff] [blame] | 259 | if [ ! -r "${nvme}/size" ]; then |
Gwendal Grignou | 66cc835 | 2016-10-14 08:48:57 -0700 | [diff] [blame] | 260 | continue |
| 261 | fi |
Gwendal Grignou | 040ab8c | 2017-04-27 18:24:35 -0700 | [diff] [blame] | 262 | size=$(cat "${nvme}/size") |
| 263 | remo=$(cat "${nvme}/removable") |
Gwendal Grignou | 66cc835 | 2016-10-14 08:48:57 -0700 | [diff] [blame] | 264 | if [ ${remo:-0} -eq 0 -a ${size:-0} -gt 0 ]; then |
Gwendal Grignou | 040ab8c | 2017-04-27 18:24:35 -0700 | [diff] [blame] | 265 | nvme_base="${nvme##*/}" |
| 266 | # Store in all_nvme names of nvme devices, without namespace. |
| 267 | # In case of nvme device with several namespaces, we will have |
| 268 | # redundancy. |
| 269 | all_nvme="${all_nvme} ${nvme_base%n*}" |
Gwendal Grignou | 66cc835 | 2016-10-14 08:48:57 -0700 | [diff] [blame] | 270 | fi |
| 271 | done |
Gwendal Grignou | 040ab8c | 2017-04-27 18:24:35 -0700 | [diff] [blame] | 272 | echo "${all_nvme}" | tr '[:space:]' '\n' | sort -u |
Gwendal Grignou | 66cc835 | 2016-10-14 08:48:57 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Gwendal Grignou | 732af51 | 2014-04-07 20:07:29 +0000 | [diff] [blame] | 275 | # Find the drive to install based on the build write_cgpt.sh |
| 276 | # script. If not found, return "" |
| 277 | get_fixed_dst_drive() { |
Gwendal Grignou | 6562fe7 | 2017-06-06 09:11:01 -0700 | [diff] [blame] | 278 | local dev rootdev |
| 279 | |
| 280 | if [ -n "${DEFAULT_ROOTDEV}" ]; then |
Gwendal Grignou | 732af51 | 2014-04-07 20:07:29 +0000 | [diff] [blame] | 281 | # No " here, the variable may contain wildcards. |
Gwendal Grignou | 6562fe7 | 2017-06-06 09:11:01 -0700 | [diff] [blame] | 282 | for rootdev in ${DEFAULT_ROOTDEV}; do |
| 283 | dev="/dev/$(basename "${rootdev}")" |
| 284 | if [ -b "${dev}" ]; then |
| 285 | break |
| 286 | else |
| 287 | dev="" |
| 288 | fi |
| 289 | done |
Congbin Guo | fcaade8 | 2018-01-05 15:47:56 -0800 | [diff] [blame] | 290 | else |
| 291 | dev="" |
Gwendal Grignou | 732af51 | 2014-04-07 20:07:29 +0000 | [diff] [blame] | 292 | fi |
| 293 | echo "${dev}" |
Paul Stewart | 04f3ef1 | 2010-05-27 15:56:42 -0700 | [diff] [blame] | 294 | } |
Liam McLoughlin | 2dd21d6 | 2012-07-09 17:45:06 -0700 | [diff] [blame] | 295 | |
J. Richard Barnette | 40ce8a0 | 2015-03-18 14:59:26 -0700 | [diff] [blame] | 296 | edit_mbr() { |
Liam McLoughlin | 2dd21d6 | 2012-07-09 17:45:06 -0700 | [diff] [blame] | 297 | locate_gpt |
Ian Coolidge | 83e7432 | 2017-04-14 18:35:45 +0000 | [diff] [blame] | 298 | # TODO(icoolidge): Get this from disk_layout somehow. |
| 299 | local PARTITION_NUM_EFI_SYSTEM=12 |
Steven 'Steve' Kendall | bde39b1 | 2015-09-30 12:59:02 -0400 | [diff] [blame] | 300 | local start_esp=$(partoffset "$1" ${PARTITION_NUM_EFI_SYSTEM}) |
| 301 | local num_esp_sectors=$(partsize "$1" ${PARTITION_NUM_EFI_SYSTEM}) |
Gwendal Grignou | 0788acb | 2018-06-18 08:54:23 -0700 | [diff] [blame] | 302 | maybe_sudo sfdisk -w never -X dos "${1}" <<EOF |
Liam McLoughlin | 2dd21d6 | 2012-07-09 17:45:06 -0700 | [diff] [blame] | 303 | unit: sectors |
| 304 | |
| 305 | disk1 : start= $start_esp, size= $num_esp_sectors, Id= c, bootable |
| 306 | disk2 : start= 1, size= 1, Id= ee |
| 307 | EOF |
| 308 | } |
J. Richard Barnette | 40ce8a0 | 2015-03-18 14:59:26 -0700 | [diff] [blame] | 309 | |
| 310 | install_hybrid_mbr() { |
| 311 | # Creates a hybrid MBR which points the MBR partition 1 to GPT |
| 312 | # partition 12 (ESP). This is useful on ARM boards that boot |
J. Richard Barnette | 2db77d4 | 2015-03-19 17:19:15 -0700 | [diff] [blame] | 313 | # from MBR formatted disks only. |
J. Richard Barnette | 40ce8a0 | 2015-03-18 14:59:26 -0700 | [diff] [blame] | 314 | # |
| 315 | # Currently, this code path is used principally to install to |
| 316 | # SD cards using chromeos-install run from inside the chroot. |
J. Richard Barnette | 2db77d4 | 2015-03-19 17:19:15 -0700 | [diff] [blame] | 317 | # In that environment, `sfdisk` can be racing with udev, leading |
| 318 | # to EBUSY when it calls BLKRRPART for the target disk. We avoid |
| 319 | # the conflict by using `udevadm settle`, so that udev goes first. |
| 320 | # cf. crbug.com/343681. |
J. Richard Barnette | 40ce8a0 | 2015-03-18 14:59:26 -0700 | [diff] [blame] | 321 | |
| 322 | echo "Creating hybrid MBR" |
| 323 | if ! edit_mbr "${1}"; then |
J. Richard Barnette | 2db77d4 | 2015-03-19 17:19:15 -0700 | [diff] [blame] | 324 | udevadm settle |
Gwendal Grignou | 0788acb | 2018-06-18 08:54:23 -0700 | [diff] [blame] | 325 | maybe_sudo blockdev --rereadpt "${1}" |
J. Richard Barnette | 40ce8a0 | 2015-03-18 14:59:26 -0700 | [diff] [blame] | 326 | fi |
| 327 | } |
Gwendal Grignou | acb0635 | 2017-02-08 20:30:47 -0800 | [diff] [blame] | 328 | |
| 329 | ext4_dir_encryption_supported() { |
| 330 | # Can be set in the ebuild. |
| 331 | local direncryption_enabled=false |
| 332 | |
| 333 | # Return true if kernel support ext4 directory encryption. |
| 334 | ${direncryption_enabled} && \ |
| 335 | ! LC_LANG=C e4crypt get_policy / | grep -qF \ |
| 336 | -e "Operation not supported" \ |
| 337 | -e "Inappropriate ioctl for device" |
| 338 | } |