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