blob: 90c86a87a0c329fa9531b03262215e22d3a26a18 [file] [log] [blame]
Mike Frysinger1963df12013-06-03 14:01:35 -04001#!/bin/sh
Bill Richardsoneff5b062010-03-30 14:17:34 -07002# 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 Richardsoneff5b062010-03-30 14:17:34 -070011# 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 Mortone615bc32018-06-06 09:30:13 -070015# 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.
18maybe_sudo() {
19 if [ "${UID:-$(id -u)}" = "0" ]; then
20 PATH="${PATH}:/sbin:/usr/sbin" "$@"
21 else
22 sudo "$@"
23 fi
24}
25
Bill Richardsoneff5b062010-03-30 14:17:34 -070026# 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
31numsectors() {
Jie Sun1f9d4122010-04-12 17:04:36 -070032 if [ -b "${1}" ]; then
Bill Richardsoneff5b062010-03-30 14:17:34 -070033 dev=${1##*/}
Jie Sun1f9d4122010-04-12 17:04:36 -070034 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 Richardsoneff5b062010-03-30 14:17:34 -070043 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 Sun1f9d4122010-04-12 17:04:36 -070050 fi
Bill Richardsoneff5b062010-03-30 14:17:34 -070051}
52
Sam Hurst545e5512018-06-07 11:08:46 -070053# 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
57blocksize() {
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 Richardson7c358a92010-06-11 09:16:03 -070075# Locate the cgpt tool. It should already be installed in the build chroot,
robotboya7684292010-04-21 14:46:00 -070076# but some of these functions may be invoked outside the chroot (by
Bill Richardsoneff5b062010-03-30 14:17:34 -070077# image_to_usb or similar), so we need to find it.
robotboya7684292010-04-21 14:46:00 -070078GPT=""
Yusuke Satod55cea92010-04-21 10:46:59 +090079
robotboya7684292010-04-21 14:46:00 -070080locate_gpt() {
81 if [ -z "$GPT" ]; then
Bill Richardsonf67f8442010-12-01 08:27:37 -080082 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 Richardson7c358a92010-06-11 09:16:03 -070087 echo "can't find cgpt tool" 1>&2
robotboya7684292010-04-21 14:46:00 -070088 exit 1
89 fi
90 fi
91 fi
92}
Bill Richardsoneff5b062010-03-30 14:17:34 -070093
Bill Richardsoneff5b062010-03-30 14:17:34 -070094# 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
98partoffset() {
Micah Mortone615bc32018-06-06 09:30:13 -070099 maybe_sudo $GPT show -b -i $2 $1
Bill Richardsoneff5b062010-03-30 14:17:34 -0700100}
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
106partsize() {
Micah Mortone615bc32018-06-06 09:30:13 -0700107 maybe_sudo $GPT show -s -i $2 $1
Bill Richardsoneff5b062010-03-30 14:17:34 -0700108}
109
Jie Sun1f9d4122010-04-12 17:04:36 -0700110# 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).
113get_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 Drewry056e9c82010-08-06 16:10:59 -0500119 # Removes any trailing digits.
120 local block=$(echo "$partition" | sed -e 's/[0-9]*$//')
Jie Sun1f9d4122010-04-12 17:04:36 -0700121 # 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).
131get_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).
145make_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 Grignou20f0ca72014-03-13 16:14:43 -0700157# 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 Patildf2d2692016-03-04 15:24:58 +0530162# NVME for NVMe device
Gwendal Grignou20f0ca72014-03-13 16:14:43 -0700163# OTHER for other devices.
164get_device_type() {
165 local dev="$(basename "$1")"
166 local vdr
167 local type_file
168 local vendor_file
Deepti Patildf2d2692016-03-04 15:24:58 +0530169 # 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 Grignou20f0ca72014-03-13 16:14:43 -0700179 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 Grignou732af512014-04-07 20:07:29 +0000205# 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.
209list_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 Stewart04f3ef12010-05-27 15:56:42 -0700218 fi
Gwendal Grignou732af512014-04-07 20:07:29 +0000219 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 Stewart04f3ef12010-05-27 15:56:42 -0700225 done
Gwendal Grignou732af512014-04-07 20:07:29 +0000226}
227
228# We assume we only have eMMC devices, not removable MMC devices.
Gwendal Grignou6562fe72017-06-06 09:11:01 -0700229# also, do not consider special hardware partitions on the eMMC, like boot.
Gwendal Grignou732af512014-04-07 20:07:29 +0000230# 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)
239list_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 Grignou66cc8352016-10-14 08:48:57 -0700252# NVMe device
253# Exclude disks with size 0, it means they did not spin up properly.
254list_fixed_nvme_disks() {
Gwendal Grignoudc0a2072017-07-11 09:57:57 -0700255 local nvme remo size nvme_base
256 local all_nvme=''
Gwendal Grignou66cc8352016-10-14 08:48:57 -0700257
258 for nvme in /sys/block/nvme*; do
Gwendal Grignou040ab8c2017-04-27 18:24:35 -0700259 if [ ! -r "${nvme}/size" ]; then
Gwendal Grignou66cc8352016-10-14 08:48:57 -0700260 continue
261 fi
Gwendal Grignou040ab8c2017-04-27 18:24:35 -0700262 size=$(cat "${nvme}/size")
263 remo=$(cat "${nvme}/removable")
Gwendal Grignou66cc8352016-10-14 08:48:57 -0700264 if [ ${remo:-0} -eq 0 -a ${size:-0} -gt 0 ]; then
Gwendal Grignou040ab8c2017-04-27 18:24:35 -0700265 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 Grignou66cc8352016-10-14 08:48:57 -0700270 fi
271 done
Gwendal Grignou040ab8c2017-04-27 18:24:35 -0700272 echo "${all_nvme}" | tr '[:space:]' '\n' | sort -u
Gwendal Grignou66cc8352016-10-14 08:48:57 -0700273}
274
Gwendal Grignou732af512014-04-07 20:07:29 +0000275# Find the drive to install based on the build write_cgpt.sh
276# script. If not found, return ""
277get_fixed_dst_drive() {
Gwendal Grignou6562fe72017-06-06 09:11:01 -0700278 local dev rootdev
279
280 if [ -n "${DEFAULT_ROOTDEV}" ]; then
Gwendal Grignou732af512014-04-07 20:07:29 +0000281 # No " here, the variable may contain wildcards.
Gwendal Grignou6562fe72017-06-06 09:11:01 -0700282 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 Guofcaade82018-01-05 15:47:56 -0800290 else
291 dev=""
Gwendal Grignou732af512014-04-07 20:07:29 +0000292 fi
293 echo "${dev}"
Paul Stewart04f3ef12010-05-27 15:56:42 -0700294}
Liam McLoughlin2dd21d62012-07-09 17:45:06 -0700295
J. Richard Barnette40ce8a02015-03-18 14:59:26 -0700296edit_mbr() {
Liam McLoughlin2dd21d62012-07-09 17:45:06 -0700297 locate_gpt
Ian Coolidge83e74322017-04-14 18:35:45 +0000298 # TODO(icoolidge): Get this from disk_layout somehow.
299 local PARTITION_NUM_EFI_SYSTEM=12
Steven 'Steve' Kendallbde39b12015-09-30 12:59:02 -0400300 local start_esp=$(partoffset "$1" ${PARTITION_NUM_EFI_SYSTEM})
301 local num_esp_sectors=$(partsize "$1" ${PARTITION_NUM_EFI_SYSTEM})
Gwendal Grignou0788acb2018-06-18 08:54:23 -0700302 maybe_sudo sfdisk -w never -X dos "${1}" <<EOF
Liam McLoughlin2dd21d62012-07-09 17:45:06 -0700303unit: sectors
304
305disk1 : start= $start_esp, size= $num_esp_sectors, Id= c, bootable
306disk2 : start= 1, size= 1, Id= ee
307EOF
308}
J. Richard Barnette40ce8a02015-03-18 14:59:26 -0700309
310install_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 Barnette2db77d42015-03-19 17:19:15 -0700313 # from MBR formatted disks only.
J. Richard Barnette40ce8a02015-03-18 14:59:26 -0700314 #
315 # Currently, this code path is used principally to install to
316 # SD cards using chromeos-install run from inside the chroot.
J. Richard Barnette2db77d42015-03-19 17:19:15 -0700317 # 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 Barnette40ce8a02015-03-18 14:59:26 -0700321
322 echo "Creating hybrid MBR"
323 if ! edit_mbr "${1}"; then
J. Richard Barnette2db77d42015-03-19 17:19:15 -0700324 udevadm settle
Gwendal Grignou0788acb2018-06-18 08:54:23 -0700325 maybe_sudo blockdev --rereadpt "${1}"
J. Richard Barnette40ce8a02015-03-18 14:59:26 -0700326 fi
327}
Gwendal Grignouacb06352017-02-08 20:30:47 -0800328
329ext4_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}