File: cros

package info (click to toggle)
partman-cros 3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 100 kB
  • sloc: sh: 338; makefile: 2
file content (274 lines) | stat: -rwxr-xr-x 6,191 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/bin/sh

. /lib/partman/lib/base.sh

# ChromeOS firmware injects 'cros_secure' into the cmdline when the
# verified boot mechanism is used. Not having that means we have some
# other way to boot and do not exactly need a cros partition.
if ! grep -q 'cros_secure' /proc/cmdline >/dev/null; then
	exit 0
fi

have_cros=no
set --

# Get disk device from a partition device.
disk_from_partdev() {
	case "$1" in
		/dev/ide/host*/bus[01]/target[01]/lun0/disc)
			echo "$1";
			;;
		/dev/ide/host*/bus[01]/target[01]/lun0/part)
			echo "$1";
			;;
		/dev/ide/host*/bus[01]/target[01]/lun0/part*)
			local dir=$(dirname "$1")
			if [ -e "$dir/disc" ]; then
				echo "$dir/disc"
			elif [ -e "$dir/part" ]; then
				echo "$dir/part"
			else
				echo "$1"
			fi
			;;
		/dev/hd[a-z])
			echo "$1";
			;;
		/dev/hd[a-z][0-9]*)
			echo "${1%%[0-9]*}"
			;;
		/dev/scsi/host*/bus*/target*/lun*/disc)
			echo "$1";
			;;
		/dev/scsi/host*/bus*/target*/lun*/part*)
			local dir=$(dirname "$1")
			if [ -e "$dir/disc" ]; then
				echo "$dir/disc"
			else
				echo "$1"
			fi
			;;
		/dev/sd[a-z]|/dev/sd[a-z][a-z])
			echo "$1";
			;;
		/dev/sd[a-z][0-9]*|/dev/sd[a-z][a-z][0-9]*|/dev/wd[a-z][0-9]*|/dev/wd[a-z][a-z][0-9]*)
			echo "${1%%[0-9]*}"
			;;
		/dev/cciss/host*|/dev/cciss/disc*)
			echo "$1";
			;;
		/dev/cciss/c*d*)
			echo "$1";
			;;
		/dev/mmcblk[0-9])
			echo "$1";
			;;
		/dev/mmcblk[0-9]p[0-9]*)
			echo "${1%%p[0-9]*}"
			;;
		/dev/md*|/dev/md/*)
			echo "$1";
			;;
		/dev/mapper/*)
			local type=$(dm_table "$1")
			if [ "$type" = "crypt" ]; then
				echo "$1";
			elif [ "$type" = "multipath" ]; then
				echo "$1";
			elif is_multipath_part "$1"; then
				echo "${1%%-part[0-9]*}"
			else
				echo "$1";
			fi
			;;
		/dev/linux_lvm/*)
			echo "$1";
			;;
		/dev/loop/*|/dev/loop*)
			echo "$1";
			;;
		/dev/dasd*[0-9]*)
			echo "${1%%[0-9]*}"
			;;
		/dev/dasd*)
			echo "$1";
			;;
		/dev/*vd[a-z])
			echo "$1";
			;;
		/dev/*vd[a-z][0-9]*)
			echo "${1%%[0-9]*}"
			;;
		/dev/ad[0-9]*[sp][0-9]*)
			echo "$1" | sed 's,\(/dev/ad[0-9]\+\)[sp][0-9]\+.*,\1,'
			;;
		/dev/ad[0-9]*)
			echo "${1%%[0-9]*}"
			;;
		/dev/da[0-9]*[sp][0-9]*)
			echo "$1" | sed 's,\(/dev/da[0-9]\+\)[sp][0-9]\+.*,\1,'
			;;
		/dev/da[0-9]*)
			echo "${1%%[0-9]*}"
			;;
		/dev/zvol/*)
			echo "$1"
			;;
		/dev/nvme[0-9]*n[0-9]*p[0-9]*)
			echo "${1%%p[0-9]*}"
			;;
		*)
			echo "$1"
			;;
	esac
}

# This function takes some devices (e.g. /dev/sda, /dev/mapper/luks-*,
# /dev/dm-0, /dev/disk/by-partuuid/*), tries to find physical disks
# where they are on.
find_disks() {
	# Use real devices for all. Turns /dev/disk/** into /dev/*.
	for d in "$@"; do
		if [ -n "$d" ]; then
			set -- "$@" "$(readlink -f "$d")"
		fi
		shift
	done

	# We can have chains of VG-LV, dm-*, md*, *_crypt, so on. They can
	# resolve to multiple devices. So we keep a stack of devices to
	# search (in $@) and prepend new ones to run in the next cycle of
	# the loop. We also need to keep the results in a stack, so keep
	# them at the end divided with a separator.
	BREAK="{BREAK}"
	ITER=0
	set -- "$@" "$BREAK"
	while [ "$#" -gt 0 ]; do
		ITER=$((ITER + 1))

		if [ "$1" = "$BREAK" ]; then
			shift
			break
		elif [ "$ITER" -gt 100 ]; then
			shift
			continue
		else
			dev="$(basename "$1")"
			shift
		fi

		parent="$(readlink -f "/sys/class/block/${dev}/..")" || parent=''
		case "$parent" in
			# VG-LV, sda2_crypt e.g. doesn't exist in /sys/class/block,
			# so readlink fails. We check each dm device's name here to
			# find which dm-* device they actually are.
			'')
				for d in /sys/class/block/dm-*; do
					d="$(basename "$d")"
					if grep -qs "^$dev\$" "/sys/class/block/${d}/dm/name"; then
						set -- "$d" "$@"
					fi
				done
				;;
			# dm-* and md* reside on /sys/devices/virtual/block, and
			# have a "slaves" folder for partitions they are built on.
			*/virtual/block)
				for d in "/sys/class/block/${dev}/slaves"/*; do
					set -- "$(basename "$d")" "$@"
				done
				;;
			# Real partitions e.g. /sys/devices/**/block/sda/sda2
			# and /sys/devices/**/nvme/nvme0/nvme0n1/nvme0n1p2
			# but we need their parent, sda or nvme0n1.
			*/block/*|*/nvme[0-9]/*|*/nvme[0-9][0-9]/*)
				set -- "$(basename "$parent")" "$@"
				;;
			# These are real disks we can use. Keep them after the
			# separator, without duplicates.
			*/block|*/nvme[0-9]|*/nvme[0-9][0-9])
				case "$@" in
					*${BREAK}*${dev}*) continue ;;
				esac
				if [ -b "/dev/${dev}" ]; then
					set -- "$@" "/dev/${dev}"
				fi
				;;
			# I don't know what goes here, but let's try the parent.
			*)
				set -- "$(basename "$parent")" "$@"
				;;
		esac
	done

	echo "$@"
}

# Find disks for root and /boot
for dev in $DEVICES/*; do
	[ -d "$dev" ] || continue
	cd $dev
	partitions=
	open_dialog PARTITIONS
	while { read_line num id size type fs path name; [ "$id" ]; }; do
		[ "$fs" != free ] || continue
		partitions="$partitions $id,$path"
	done
	close_dialog

	for part in $partitions; do
		id=${part%,*}
		path=${part#*,}
		disk="$(disk_from_partdev "$path")"

		if [ -f $id/mountpoint ]; then
			mnt="$(cat $id/mountpoint)"
			if [ "$mnt" = "/" ] || [ "$mnt" = "/boot" ]; then
				# To-be-created parts don't have devices, check the disk.
				if [ ! -b "$path" ] && [ -b "$disk" ]; then
					set -- "$@" "$disk"
				else
					set -- "$@" "$path"
				fi
			fi
		fi
	done
done

# Result of this is /dev/sda /dev/mmcblk0 /dev/nvme0n1 or so.
set -- $(find_disks "$@")

# Is there at least one cros-partition in root/boot disks?
for disk in "$@"; do
	dev="$DEVICES/=dev=${disk#/dev/}"
	[ -d "$dev" ] || continue
	cd $dev
	partitions=
	open_dialog PARTITIONS
	while { read_line num id size type fs path name; [ "$id" ]; }; do
		[ "$fs" != free ] || continue
		partitions="$partitions $id,$path"
	done
	close_dialog

	for part in $partitions; do
		id=${part%,*}
		path=${part#*,}
		[ -f $id/method ] || continue
		method=$(cat $id/method)
		if [ "$method" = cros ]; then
			have_cros=yes
		fi
	done
done

if [ "$have_cros" = "no" ]; then
	db_fset partman-cros/no_cros seen false
	db_input critical partman-cros/no_cros || true
	db_go || exit 1
	db_get partman-cros/no_cros
	if [ "$RET" = true ]; then
		exit 1
	fi
fi

exit 0