2 [ "$OS_PROBER_NEWNS" ] || exec /usr/lib/os-prober/newns "$0" "$@"
7 if $cleanup_tmpdir; then
8 rm -rf "$OS_PROBER_TMP"
13 if [ -z "$OS_PROBER_TMP" ]; then
14 if type mktemp >/dev/null 2>&1; then
15 export OS_PROBER_TMP="$(mktemp -d /tmp/os-prober.XXXXXX)"
17 trap cleanup EXIT HUP INT QUIT TERM
19 export OS_PROBER_TMP=/tmp
26 _result=$(grep "^${_labelprefix} " /var/lib/os-prober/labels 2>/dev/null || true)
28 if [ -z "$_result" ]; then
31 echo "$_result" | cut -d' ' -f2
39 _cfor="$(count_for "${_labelprefix}")"
41 if [ -z "$_cfor" ]; then
42 echo "${_labelprefix} 1" >> /var/lib/os-prober/labels
44 sed "s/^${_labelprefix} ${_cfor}/${_labelprefix} $(($_cfor + 1))/" /var/lib/os-prober/labels > "$OS_PROBER_TMP/os-prober.tmp"
45 mv "$OS_PROBER_TMP/os-prober.tmp" /var/lib/os-prober/labels
48 echo "${_labelprefix}${_cfor}"
62 logger -t "$progname" "$@"
74 if [ -z "$OS_PROBER_DISABLE_DEBUG" ]; then
84 # shim to make it easier to use os-prober outside d-i
85 if ! type mapdevfs >/dev/null 2>&1; then
92 if [ "$1" = "-q" ]; then
98 [ -d "$2" ] || return 1
99 # find files with any case
100 ls -1 "$2" | grep $q -i "^$1$"
103 # We can't always tell the filesystem type up front, but if we have the
104 # information then we should use it. Note that we can't use block-attr here
105 # as it's only available in udebs.
106 # If not detected after different attempts then "NOT-DETECTED" will be printed
107 # because function is not supposed to exit error codes.
110 if (export PATH="/lib/udev:$PATH"; type vol_id) >/dev/null 2>&1; then
111 PATH="/lib/udev:$PATH" \
112 fstype=$(vol_id --type "$1" 2>/dev/null || true)
113 [ -z "$fstype" ] || { echo "$fstype"; return; }
115 if type lsblk >/dev/null 2>&1 ; then
116 fstype=$(lsblk --nodeps --noheading --output FSTYPE -- "$1" || true)
117 [ -z "$fstype" ] || { echo "$fstype"; return; }
119 if type blkid >/dev/null 2>&1; then
120 fstype=$(blkid -o value -s TYPE "$1" 2>/dev/null || true)
121 [ -z "$fstype" ] || { echo "$fstype"; return; }
126 is_dos_extended_partition() {
127 if type blkid >/dev/null 2>&1; then
130 output="$(blkid -o export $1)"
132 # old blkid (util-linux << 2.24) errors out on extended p.
133 if [ "$?" = "2" ]; then
137 # dos partition type and no filesystem type?...
138 if echo $output | grep -q ' PTTYPE=dos ' &&
139 ! echo $output | grep -q ' TYPE='; then
149 parse_proc_mounts () {
150 while read -r line; do
154 printf '%s %s %s\n' "$(mapdevfs "$1")" "$2" "$3"
159 while read -r line; do
168 printf '%s %s %s\n' "$1" "$2" "$3"
176 sed 's/\\011/ /g; s/\\012/\n/g; s/\\040/ /g; s/\\134/\\/g'
181 if type blkid >/dev/null 2>&1; then
182 # Hopefully everyone has blkid by now
183 output="$(blkid -o device -t LABEL="$1")" || return 1
184 echo "$output" | head -n1
185 elif [ -h "/dev/disk/by-label/$1" ]; then
186 # Last-ditch fallback
187 readlink -f "/dev/disk/by-label/$1"
195 if type blkid >/dev/null 2>&1; then
196 # Hopefully everyone has blkid by now
197 output="$(blkid -o device -t UUID="$1")" || return 1
198 echo "$output" | head -n1
199 elif [ -h "/dev/disk/by-uuid/$1" ]; then
200 # Last-ditch fallback
201 readlink -f "/dev/disk/by-uuid/$1"
207 # Sets $mountboot as output variables. This is very messy, but POSIX shell
208 # isn't really up to the task of doing it more cleanly.
209 linux_mount_boot () {
215 if [ -e "$tmpmnt/etc/fstab" ]; then
216 # Try to mount any /boot partition.
217 bootmnt=$(parsefstab < "$tmpmnt/etc/fstab" | grep " /boot ") || true
218 if [ -n "$bootmnt" ]; then
224 # Try to map labels and UUIDs ourselves if possible,
225 # so that we can check whether they're already
226 # mounted somewhere else.
228 if echo "$1" | grep -q "LABEL="; then
229 label="$(echo "$1" | cut -d = -f 2)"
230 if tmppart="$(find_label "$label")"; then
231 debug "mapped LABEL=$label to $tmppart"
233 debug "found boot partition LABEL=$label for Linux system on $partition, but cannot map to existing device"
234 mountboot="$partition 0"
237 elif echo "$1" | grep -q "UUID="; then
238 uuid="$(echo "$1" | cut -d = -f 2)"
239 if tmppart="$(find_uuid "$uuid")"; then
240 debug "mapped UUID=$uuid to $tmppart"
242 debug "found boot partition UUID=$uuid for Linux system on $partition, but cannot map to existing device"
243 mountboot="$partition 0"
248 set -- "$(mapdevfs "$tmppart")" "$@"
250 if grep -q "^$1 " "$OS_PROBER_TMP/mounted-map"; then
251 bindfrom="$(grep "^$1 " "$OS_PROBER_TMP/mounted-map" | head -n1 | cut -d " " -f 2)"
252 bindfrom="$(unescape_mount "$bindfrom")"
253 if [ "$bindfrom" != "$tmpmnt/boot" ]; then
254 if mount --bind "$bindfrom" "$tmpmnt/boot"; then
258 debug "failed to bind-mount $bindfrom onto $tmpmnt/boot"
262 if [ "$mounted" ]; then
264 elif [ -e "$1" ]; then
267 elif [ -e "$tmpmnt/$1" ]; then
269 boottomnt="$tmpmnt/$1"
270 elif [ -e "/target/$1" ]; then
272 boottomnt="/target/$1"
277 if [ ! "$mounted" ]; then
278 if [ -z "$bootpart" ]; then
279 debug "found boot partition $1 for linux system on $partition, but cannot map to existing device"
281 debug "found boot partition $bootpart for linux system on $partition"
282 if type grub-mount >/dev/null 2>&1 && \
283 grub-mount "$boottomnt" "$tmpmnt/boot" 2>/dev/null; then
290 if [ -z "$bootpart" ]; then
291 bootpart="$partition"
293 if [ -z "$mounted" ]; then
297 mountboot="$bootpart $mounted"