11#! /bin/bash
22
33# Help the user determine where a device like an SSD is mounted.
4+ # This would be a 2nd drive (or 3rd, etc.).
45
56# Allow this script to be executed manually, which requires several variables to be set.
67[[ -z ${ALLSKY_HOME} ]] && export ALLSKY_HOME=" $( realpath " $( dirname " ${BASH_ARGV0} " ) /../.." ) "
@@ -11,78 +12,106 @@ source "${ALLSKY_HOME}/variables.sh" || exit "${EXIT_ERROR_STOP}"
1112# shellcheck source-path=scripts
1213source " ${ALLSKY_SCRIPTS} /functions.sh" || exit " ${EXIT_ERROR_STOP} "
1314
15+ DEBUG=" false"
16+
1417function outputData()
1518{
1619 local HEADER=" ${1} "
1720 local ENTRIES=" ${2} "
1821
1922 echo
2023 I_ " $( echo " ${HEADER} " | nawk ' {
21- printf("%-15s %10s %6s %-30s\n", $1, $4, $6, "PATH");
24+ # Replace "MOUNTPOINTS" with "PATH" for readability.
25+ # NAME SIZE TYPE HOTPLUG MOUNTPOINTS
26+ printf("%-15s %10s %6s %7s %-30s\n", $1, $2, $3, $4, "PATH");
2227 }' ) "
2328 echo " ${ENTRIES} " | nawk ' {
24- printf("%-15s %10s %6s %-30s\n", $1, $4, $6, $7 " " $8 " " $9);
29+ printf("%-15s %10s %6s %-7s %-30s\n",
30+ $1, $2, $3, $4 == 0 ? "no" : "yes", $5 " " $6 " " $7);
2531 }'
2632 echo
2733}
2834
2935
30- echo
31-
32- FS=" $( lsblk ) "
36+ # Light versions of Pi OS don't have an automounter so we need to use lsblk.
37+ FS=" $( lsblk --output " NAME,SIZE,TYPE,HOTPLUG,MOUNTPOINTS" ) "
3338
34- # Typical output:
35- # NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
36- # sda 8:0 0 931.5G 0 disk
37- # |-sda1 8:1 0 128M 0 part
38- # |-sda2 8:2 0 931.4G 0 part /media/pi/SSD
39- # nvme0n1 259:0 0 465.8G 0 disk
40- # |-nvme0n1p1 259:1 0 512M 0 part /boot/firmware
41- # |-nvme0n1p2 259:2 0 465.3G 0 part /
39+ # Typical lsblk output:
40+ # NAME SIZE TYPE HOTPLUG MOUNTPOINTS
41+ # sda 931.5G disk 0
42+ # |-sda1 128M part 0
43+ # |-sda2 931.4G part 1 /media/pi/SSD
44+ # nvme0n1 465.8G disk 0
45+ # |-nvme0n1p1 512M part 0 /boot/firmware
46+ # |-nvme0n1p2 465.3G part 0 /
4247
43- # $1 $2 $3 $4 $5 $6 $7
48+ # $1 $2 $3 $4 $5
4449
4550HEADER=" $( echo " ${FS} " | head -1 ) "
4651ENTRIES=" $( echo " ${FS} " | grep -v " ${HEADER} " ) "
52+ NUM_DISKS=" $( echo " ${FS} " | grep -E " disk| disk $" | wc -l ) "
4753
48- # Ignore the boot drive and entries we know aren't the user's storage device.
54+ # Ignore the boot drive and entries we know aren't the user's extra storage device.
4955POSSIBLE=" $( echo " ${ENTRIES} " | nawk ' BEGIN { num = 0; }
5056 {
51- if ($6 == "part") {
52- M = $7 ;
53- if ($7 != "/" && substr($7 , 0, 5) != "/boot") {
54- printf("%s", $7 );
57+ if ($3 == "part") {
58+ mountPoint = $5 ;
59+ if (mountPoint != "/" && substr(mountPoint , 0, 5) != "/boot") {
60+ printf("%s", mountPoint );
5561 # Check for up to 2 blanks in the mount point.
56- if ($8 != "") printf("%s", $8 );
57- if ($9 != "") printf("%s", $9 );
62+ if ($6 != "") printf(" %s", $6 );
63+ if ($7 != "") printf(" %s", $7 );
5864 if (++num > 1) printf("\n");
5965 }
6066 }
6167 }' ) "
6268
69+ if [[ ${DEBUG} == " true" ]]; then
70+ echo " HEADER=[${HEADER} ]"
71+ echo " ENTRIES=[${ENTRIES} ]"
72+ echo " NUM_DISKS=${NUM_DISKS} "
73+ echo " POSSIBLE=[${POSSIBLE} ]"
74+ echo
75+ fi
76+
6377if [[ -n ${POSSIBLE} ]]; then
78+ echo
79+ if [[ ${NUM_DISKS} -eq 1 ]]; then
80+ W_ " This is the only disk the Pi sees:"
81+ else
82+ echo " The Pi sees these disks:"
83+ fi
84+ indent --spaces " $( outputData " ${HEADER} " " ${ENTRIES} " ) "
85+
6486 NUM=$( echo -e " ${POSSIBLE} " | wc -l )
65- echo -n " Your storage device is likely"
87+ echo
88+ echo -n " Your extra storage device is likely"
6689 if [[ ${NUM} -eq 1 ]]; then
67- echo " the entry below whose 'PATH' column is '${POSSIBLE} ',"
68- echo " as long as the 'Size' roughly matches your device."
90+ echo " the one above whose 'PATH' column is '${POSSIBLE} ',"
91+ echo " as long as the 'SIZE' roughly matches your device."
92+ if [[ ${POSSIBLE} =~ " " ]] then
93+ echo
94+ echo " If this is your device we suggest removing the space from the PATH name,"
95+ echo " which may require changing the disk's label."
96+ fi
6997 else
70- echo " one of the entries below whose 'TYPE' column is 'part'."
98+ echo " one of the entries above whose 'TYPE' column is 'part'."
7199 echo " Pick the entry whose 'SIZE' most closely matches your device."
72100 echo " Your storage device is in the directory in the 'PATH' column."
73101 fi
74-
75- outputData " ${HEADER} " " ${ENTRIES} "
102+ echo
76103
77104else
78- # We get here if the disk isn't connected or isn't mounted.
79- # Light versions of Pi OS don't have an automounter so we need to use lsblk.
80- W_ " WARNING: Unable to determine where your storage device is.\n "
81-
82- echo " All the disks the Pi sees are shown below: "
83-
84- outputData " ${HEADER} " " ${ENTRIES} "
105+ # We get here if the "other" disk isn't connected or isn't mounted.
106+ MSG= " \nWARNING: Unable to determine where your storage device is "
107+ if [[ ${NUM_DISKS} -eq 1 ]] ; then
108+ W_ " ${MSG} - only 1 disk found: "
109+ indent --spaces " $( outputData " ${HEADER} " " ${ENTRIES} " ) "
110+ exit 1
111+ fi
85112
113+ W_ " ${MSG} ."
114+ echo
86115 echo " Look at the line whose 'TYPE' column is 'part' and whose 'SIZE' column is"
87116 echo " roughly the size of your device."
88117 echo " If there is something in the 'PATH' column, your device is likely in that directory."
0 commit comments