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