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