summaryrefslogblamecommitdiffstats
path: root/source/installer/sources/initrd/sbin/probe
blob: eca6db9ddc81c070232b22fc75e9f10620d48e0f (plain) (tree)
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438





















































































































































































































































































































































































































































                                                                                                                       
#!/bin/sh
# This is 'probe', a wrapper for using fdisk to gather drive info for
# the Slackware setup scripts.  I hate to bounce this much garbage through
# a tmpdir, but it looks like large variables can make ash crash...

# Many thanks to Vincent Rivellino for contributing the patches to support
# Mylex and Compaq RAID controllers.

# Regularize output that will be parsed:
unset LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY \
  LC_MESSAGES LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT \
  LC_IDENTIFICATION LC_ALL
LANG=C
export LANG

TMP=/var/log/setup/tmp

# Use cached results if they exist and /proc/partitions has not changed:
if [ -r $TMP/SeTpartition.md5 -a -r $TMP/SeTfdisk ]; then
  if [ "$(cat $TMP/SeTpartition.md5)" = "$(md5sum /proc/partitions)" ]; then
    cat $TMP/SeTfdisk
    exit 0
  fi
fi

# First run, or /proc/partitions has changed.
# Make a checksum for later comparison:
md5sum /proc/partitions > $TMP/SeTpartition.md5

# Wipe any previously existing results:
rm -f $TMP/SeTfdisk

# listide major minor hd1 hd2 (2 base devs for major)
list_ide() {
  if [ "$2" = "0" ]; then
    fdisk -l /dev/$3 >> $TMP/SeTfdisk
  elif [ "$2" = "64" ]; then
    fdisk -l /dev/$4 >> $TMP/SeTfdisk
  fi
}

list_scsi() {
  # find drive # 0 - 15
  DRV=`expr $1 / 16`
  NUM=`expr $1 % 16`
  if [ ! "$NUM" = "0" ]; then
    return
  fi
  if [ "$DRV" = "0" ]; then
    fdisk -l /dev/sda >> $TMP/SeTfdisk
  elif [ "$DRV" = "1" ]; then
    fdisk -l /dev/sdb >> $TMP/SeTfdisk
  elif [ "$DRV" = "2" ]; then
    fdisk -l /dev/sdc >> $TMP/SeTfdisk
  elif [ "$DRV" = "3" ]; then
    fdisk -l /dev/sdd >> $TMP/SeTfdisk
  elif [ "$DRV" = "4" ]; then
    fdisk -l /dev/sde >> $TMP/SeTfdisk
  elif [ "$DRV" = "5" ]; then
    fdisk -l /dev/sdf >> $TMP/SeTfdisk
  elif [ "$DRV" = "6" ]; then
    fdisk -l /dev/sdg >> $TMP/SeTfdisk
  elif [ "$DRV" = "7" ]; then
    fdisk -l /dev/sdh >> $TMP/SeTfdisk
  elif [ "$DRV" = "8" ]; then
    fdisk -l /dev/sdi >> $TMP/SeTfdisk
  elif [ "$DRV" = "9" ]; then
    fdisk -l /dev/sdj >> $TMP/SeTfdisk
  elif [ "$DRV" = "10" ]; then
    fdisk -l /dev/sdk >> $TMP/SeTfdisk
  elif [ "$DRV" = "11" ]; then
    fdisk -l /dev/sdl >> $TMP/SeTfdisk
  elif [ "$DRV" = "12" ]; then
    fdisk -l /dev/sdm >> $TMP/SeTfdisk
  elif [ "$DRV" = "13" ]; then
    fdisk -l /dev/sdn >> $TMP/SeTfdisk
  elif [ "$DRV" = "14" ]; then
    fdisk -l /dev/sdo >> $TMP/SeTfdisk
  elif [ "$DRV" = "15" ]; then
    fdisk -l /dev/sdp >> $TMP/SeTfdisk
  fi
}

# List Mylex RAID device
list_rd() {
  # find drive
  DRV=`expr $2 / 8`
  NUM=`expr $2 % 8`
  if [ ! "$NUM" = "0" ]; then
    return
  fi
  fdisk -l /dev/rd/c$1d$DRV >> $TMP/SeTfdisk
  #output_gpt_partitions /dev/rd/c$1d$DRV >> $TMP/SeTfdisk
}

# List Cpq SMART/2 RAID device
list_ida() {
  # find drive
  DRV=`expr $2 / 16`
  NUM=`expr $2 % 16`
  if [ ! "$NUM" = "0" ]; then
    return
  fi
  fdisk -l /dev/ida/c$1d$DRV >> $TMP/SeTfdisk
  #output_gpt_partitions /dev/ida/c$1d$DRV >> $TMP/SeTfdisk
}

list_cciss() {
  # find drive
  DRV=`expr $2 / 16`
  NUM=`expr $2 % 16`
  if [ ! "$NUM" = "0" ]; then
    return
  fi
  fdisk -l /dev/cciss/c$1d$DRV >> $TMP/SeTfdisk
  #output_gpt_partitions /dev/cciss/c$1d$DRV >> $TMP/SeTfdisk
}

list_ataraid() {
  # find drive
  DRV=`expr $2 / 16`
  NUM=`expr $2 % 16`
  if [ "$NUM" = "0" ]; then
     fdisk -l /dev/ataraid/d$DRV >> $TMP/SeTfdisk
     #output_gpt_partitions /dev/ataraid/d$DRV >> $TMP/SeTfdisk
  else
     return
  fi
}

list_amiraid() {
  # find drive
  DRV=`expr $2 / 16`
  NUM=`expr $2 % 16`
  if [ "$NUM" = "0" ]; then
     fdisk -l /dev/amiraid/ar$DRV >> $TMP/SeTfdisk
     #output_gpt_partitions /dev/amiraid/ar$DRV >> $TMP/SeTfdisk
  else
     return
  fi
}

list_mmc() {
  # find drive
  DRV=`expr $2 / 8`
  NUM=`expr $2 % 8`
  if [ "$NUM" = "0" ]; then
     fdisk -l /dev/mmcblk$DRV >> $TMP/SeTfdisk
     #output_gpt_partitions /dev/mmcblk$DRV >> $TMP/SeTfdisk
  else
     return
  fi
}

list_nvme() {
  fdisk -l | grep $1 >> $TMP/SeTfdisk 2> /dev/null
}

is_swap() {
  HEADER=$(dd if="$1" bs=1 skip=4086 count=10 2>/dev/null | strings)
  if [ "$HEADER" = "SWAPSPACE2" -o "$HEADER" = "SWAP_SPACE" ]; then
    return 0
  else
    return 1
  fi
}

list_md() {
  if ( is_swap "/dev/$2" ); then TYPE="Linux swap"; else TYPE="Linux"; fi
  echo "/dev/$2  1 2 $1 kk $TYPE" >> $TMP/SeTfdisk
}

list_lvm() {
  lvscan 2>/dev/null | grep "ACTIVE" | while read line ; do
    SMASHED_LINE=$line
    if [ "$SMASHED_LINE" = "" ]; then
      break;
    fi
    DEV=`echo $SMASHED_LINE | cut -f2 -d"'"`
    SIZE=`lvdisplay $DEV -C --units k --noheadings --separator : | cut -f4 -d':'  | sed -e 's/^\([0-9]*\)[^0-9].*/\1/'`
    TYPE="Linux"
    if ( is_swap "$DEV" ); then TYPE="Linux swap"; fi
    echo "$DEV  0  0  $SIZE  lv  $TYPE" >> $TMP/SeTfdisk
  done
}

# List any volumes created by cryptsetup
list_crypt() {
  for i in $(ls /dev/mapper/); do
    if cryptsetup status $i 2>/dev/null | grep "is active" 1>/dev/null ; then
      DEV=$(cryptsetup status $i 2>/dev/null | grep "is active" | cut -f1 -d' ')
      SIZE=$(fdisk -s $(cryptsetup status $i 2>/dev/null | grep "device:" | cut -f2 -d: | tr -d ' '))
      echo "$DEV  0  0  $SIZE  lc  Linux" >> $TMP/SeTfdisk
    fi
  done
}

# List virtual partitions
list_virt() {
  fdisk -l /dev/$1 >> $TMP/SeTfdisk
  #output_gpt_partitions /dev/$1 >> $TMP/SeTfdisk
}

output_gpt_partitions() {
# First, make sure the device is GPT:
if fdisk -l $1 2> /dev/null | grep -wq -e "GPT" -e "Disklabel type: gpt" ; then 
  unset output
  # In the case of some RAID device like Mylex we will need to delimit the
  # partition number.  We will set a partition delimiter variable P set
  # either to an empty string (default) or the needed delimiter.
  TESTRAID="$(echo $1 | cut -f 2 -d /)"
  case "$TESTRAID" in
  'amiraid' )
    P="p"
    ;;
  'ataraid' )
    P="p"
    ;;
  'cciss' )
    P="p"
    ;;
  'ida' )
    P="p"
    ;;
  'rd' )
    P="p"
    ;;
  *)
    P=""
    ;;
  esac
  gdisk -l $1 | tr -d '*' | while read parse ; do
    if [ ! -z $output ]; then
      line=$parse
      if [ ! "$(echo $line | cut -b1)" = "" ]; then
        gptpartition=${1}${P}$(echo $line | cut -f 1 -d ' ')
        gpttype="$(echo $line | cut -f 6 -d ' ')"
        if [ "$gpttype" = "8200" ]; then
          fdisktype="Linux swap"
        elif [ "$gpttype" = "0700" ]; then
          if dd if=$gptpartition bs=1K count=1 2> /dev/null | grep -wq NTFS ; then
            fdisktype="HPFS/NTFS"
          elif dd if=$gptpartition bs=1K count=1 2> /dev/null | grep -wq EXFAT ; then
            fdisktype="exFAT"
          else
            fdisktype="W95 FAT32"
          fi
        elif [ "$gpttype" = "AF00" ]; then
          fdisktype="HFS+"
        elif [ "$gpttype" = "EF00" ]; then
          fdisktype="EFI System Partition"
        elif [ "$gpttype" = "8300" ]; then
          fdisktype=Linux
        else
          fdisktype="Unknown hex code $gpttype"
        fi
        sectorsize="$(gdisk -l $1 | tr -d '*' | grep "Logical sector size" | cut -f 2 -d : | cut -f 2 -d ' ')"
        gptstart="$(expr $(echo $line | cut -f 2 -d ' ') \* $sectorsize / 1024)"
        gptend="$(expr $(echo $line | cut -f 3 -d ' ') \* $sectorsize / 1024)"
        gptsize="$(expr $gptend - $gptstart)"
        echo $gptpartition $gptstart $gptend $gptsize $gpttype $fdisktype
      fi
    fi
    if echo $parse | grep -q "^Number" ; then
      output=true
    fi
  done
fi
}

list_scsi_gpt() {
  # find drive # 0 - 15
  DRV=`expr $1 / 16`
  NUM=`expr $1 % 16`
  if [ ! "$NUM" = "0" ]; then
    return
  fi
  if [ "$DRV" = "0" ]; then
    output_gpt_partitions /dev/sda
  elif [ "$DRV" = "1" ]; then
    output_gpt_partitions /dev/sdb
  elif [ "$DRV" = "2" ]; then
    output_gpt_partitions /dev/sdc
  elif [ "$DRV" = "3" ]; then
    output_gpt_partitions /dev/sdd
  elif [ "$DRV" = "4" ]; then
    output_gpt_partitions /dev/sde
  elif [ "$DRV" = "5" ]; then
    output_gpt_partitions /dev/sdf
  elif [ "$DRV" = "6" ]; then
    output_gpt_partitions /dev/sdg
  elif [ "$DRV" = "7" ]; then
    output_gpt_partitions /dev/sdh
  elif [ "$DRV" = "8" ]; then
    output_gpt_partitions /dev/sdi
  elif [ "$DRV" = "9" ]; then
    output_gpt_partitions /dev/sdj
  elif [ "$DRV" = "10" ]; then
    output_gpt_partitions /dev/sdk
  elif [ "$DRV" = "11" ]; then
    output_gpt_partitions /dev/sdl
  elif [ "$DRV" = "12" ]; then
    output_gpt_partitions /dev/sdm
  elif [ "$DRV" = "13" ]; then
    output_gpt_partitions /dev/sdn
  elif [ "$DRV" = "14" ]; then
    output_gpt_partitions /dev/sdo
  elif [ "$DRV" = "15" ]; then
    output_gpt_partitions /dev/sdp
  fi
}

# List the LVM volumes:
list_lvm

# List CRYPT volumes:
list_crypt

## This is obsolete, since fdisk handles GPT now.
## List GPT partitions:
#cat /proc/partitions | while read line ; do
#  SMASHED_LINE=$line
#  MAJOR=`echo $SMASHED_LINE | cut -f 1 -d ' '`
#  MINOR=`echo $SMASHED_LINE | cut -f 2 -d ' '`
#  if [ "$MAJOR" = "8" ]; then
#    list_scsi_gpt $MINOR
#  fi
#done

# Other partitions:
if cat /proc/partitions | grep -E '/|[0-9]' 1>/dev/null 2>/dev/null ; then # new
  cat /proc/partitions | grep -E '/|[0-9]' | while read line ; do
    SMASHED_LINE=$line
    MAJOR=`echo $SMASHED_LINE | cut -f 1 -d ' '`
    MINOR=`echo $SMASHED_LINE | cut -f 2 -d ' '`
    DEVNAME=`echo $SMASHED_LINE | cut -f 4 -d ' '`
    if [ "$MAJOR" = "3" ]; then
      list_ide $MAJOR $MINOR hda hdb
    elif [ "$MAJOR" = "8" ]; then
      list_scsi $MINOR
    elif [ "$MAJOR" = "9" ]; then
      list_md `echo $SMASHED_LINE | cut -f 3 -d ' ' | tr -d '/'` \
              `echo $SMASHED_LINE | cut -f 4 -d ' '`
    elif [ "$MAJOR" = "22" ]; then
      list_ide $MAJOR $MINOR hdc hdd
    elif [ "$MAJOR" = "33" ]; then
      list_ide $MAJOR $MINOR hde hdf
    elif [ "$MAJOR" = "34" ]; then
      list_ide $MAJOR $MINOR hdg hdh
    elif [ "$MAJOR" = "48" ]; then
      list_rd 0 $MINOR
    elif [ "$MAJOR" = "49" ]; then
      list_rd 1 $MINOR
    elif [ "$MAJOR" = "50" ]; then
      list_rd 2 $MINOR
    elif [ "$MAJOR" = "51" ]; then
      list_rd 3 $MINOR
    elif [ "$MAJOR" = "52" ]; then
      list_rd 4 $MINOR
    elif [ "$MAJOR" = "53" ]; then
      list_rd 5 $MINOR
    elif [ "$MAJOR" = "54" ]; then
      list_rd 6 $MINOR
    elif [ "$MAJOR" = "55" ]; then
      list_rd 7 $MINOR
    elif [ "$MAJOR" = "56" ]; then
      list_ide $MAJOR $MINOR hdi hdj
    elif [ "$MAJOR" = "57" ]; then
      list_ide $MAJOR $MINOR hdk hdl
    elif [ "$MAJOR" = "72" ]; then
      list_ida 0 $MINOR
    elif [ "$MAJOR" = "73" ]; then
      list_ida 1 $MINOR
    elif [ "$MAJOR" = "74" ]; then
      list_ida 2 $MINOR
    elif [ "$MAJOR" = "75" ]; then
      list_ida 3 $MINOR
    elif [ "$MAJOR" = "76" ]; then
      list_ida 4 $MINOR
    elif [ "$MAJOR" = "77" ]; then
      list_ida 5 $MINOR
    elif [ "$MAJOR" = "78" ]; then
      list_ida 6 $MINOR
    elif [ "$MAJOR" = "79" ]; then
      list_ida 7 $MINOR
    elif [ "$MAJOR" = "80" ]; then
      list_ide $MAJOR $MINOR hdm hdn
    elif [ "$MAJOR" = "89" ]; then
      list_ide $MAJOR $MINOR hdo hdp
    elif [ "$MAJOR" = "90" ]; then
      list_ide $MAJOR $MINOR hdq hdr
    elif [ "$MAJOR" = "91" ]; then
      list_ide $MAJOR $MINOR hds hdt
    elif [ "$MAJOR" = "101" ]; then
      list_amiraid $MAJOR $MINOR
    elif [ "$MAJOR" = "104" \
      -o "$MAJOR" = "105" \
      -o "$MAJOR" = "106" \
      -o "$MAJOR" = "107" \
      -o "$MAJOR" = "108" \
      -o "$MAJOR" = "109" \
      -o "$MAJOR" = "110" \
      -o "$MAJOR" = "111" ]; then
      list_cciss $(( $MAJOR - 104 )) $MINOR
    elif [ "$MAJOR" = "114" ]; then
      list_ataraid $MAJOR $MINOR
    elif [ "$MAJOR" = "179" ]; then
      list_mmc $MAJOR $MINOR
    elif [ "$MAJOR" = "259" ]; then
      if echo $line | grep -q p; then
        list_nvme $DEVNAME
      fi
    elif [ $(expr $DEVNAME : 'x\?vd[^0-9]*$') -ne 0 ]; then
      # The virtio devices have no set major dev number, so we have to search
      # by name.  Matches full drive names for KVM/lguest (vda) and Xen (xvda).
      list_virt $DEVNAME
    fi
  done
else # old format and no RAID:
  if cat /proc/partitions | grep md 1> /dev/null 2> /dev/null ; then
    cat /proc/partitions | grep md | while read line ; do
      SMASHED_LINE=$line
      MAJOR=`echo $SMASHED_LINE | cut -f 1 -d ' '`
      if [ "$MAJOR" = "9" ]; then
        list_md `echo $SMASHED_LINE | cut -f 3 -d ' ' | tr -d '/'` \
                `echo $SMASHED_LINE | cut -f 4 -d ' '`
      fi
    done
  fi
  fdisk -l 2> /dev/null >> $TMP/SeTfdisk
fi

# Change the names to be the same as what the old fdisk used:
sed -i -e "s/Linux filesystem/Linux/g" $TMP/SeTfdisk
sed -i -e "s/EFI System/EFI System Partition/g" $TMP/SeTfdisk
sed -i -e "s,EFI (FAT-12/16/32),EFI System Partition,g" $TMP/SeTfdisk

cat $TMP/SeTfdisk