#!/bin/bash # functions need to be placed in alphabetical order dev_name_to_underscores() { echo ${1} | sed 's/\//_/g' } device_name () { local name if ! name="$(device_info name "$1")"; then name="$(printf %s "${1##*/}" | \ sed 's,!,/,g')" fi echo "/dev/$name" } get_drive_config_file() { MY_PATH=$(dirname $0) DRIVE_CONFIG_FILE="${MY_PATH}/drive_purpose.cfg" echo ${DRIVE_CONFIG_FILE} } # Returns 1 with echo 'Unknown' if soc*/soc_id is unavailable. # Returns 0 with 'XXYY:ZZZZ" if soc*/soc_id is available from sysfs. # For "jep106:XXYY:ZZZZ" where XX is identity code, # YY is continuation code and ZZZZ is the SOC ID. get_soc_id() { SOC_ID_FILE=$(ls -d /sys/bus/soc/devices/soc* 2>/dev/null) if [ -z "$SOC_ID_FILE" ]; then echo "Unknown" return 0 fi SOC_ID=$(cat /sys/bus/soc/devices/soc*/soc_id | grep jep | head -n 1) echo "${SOC_ID#*:}" } # Returns 0 if the system has Hopper GPU # Returns 1 if the system hasn't Hopper GPU # hopper list is from https://devicehunt.com/view/type/pci/vendor/10DE has_hopper_gpu() { pci_device=$(lspci -n 2> /dev/null) hopper_list=("2302" "2313" "2321" "2322" "2324" "2330" "2331" "2336" \ "2337" "2339" "233a" "233d" "2342" "2343" "2345") for i in "${hopper_list[@]}"; do if echo "$pci_device"| grep -q "10de:$i"; then return 0 fi done return 1 } # Returns 0 if the system has Grace CPU # Returns 1 if the system hasn't Grace CPU is_grace_cpu() { if ! SOC_ID=$(get_soc_id); then return 1 fi if [ "$SOC_ID" = "036b:0241" ]; then return 0 fi return 1 } is_lvm() { grep -qs ^LVM- "$1/dm/uuid" } is_sataraid () { grep -qs ^DMRAID- "$1/dm/uuid" } is_sataraid_partition () { # dmraid partitions are always slaved to another dm device for slave in "$1"/slaves/dm-*; do if [ -e "$slave" ]; then return 0 fi done return 1 } # This function taken from the list_devices script provided by the ubiquity # package list_devices() { TYPE=${1} case $TYPE in maybe-floppy) TYPE=floppy ;; cd|disk|partition|floppy|maybe-usb-floppy|usb-partition|mmc-partition) ;; *) return ;; esac if [ ! -d /sys/block ]; then return fi if type udevadm >/dev/null 2>&1; then device_info () { udevadm info -q "$1" -p "$2" 2>/dev/null } elif type udevinfo >/dev/null 2>&1; then device_info () { udevinfo -q "$1" -p "$2" 2>/dev/null } else return fi if type dmraid >/dev/null 2>&1; then raiddevs="$(dmraid -r -c || true)" else raiddevs= fi if type pvs >/dev/null 2>&1; then lvm_pvs="$(pvs -S pv_in_use=1 -o pv_name --no-headings || true)" else lvm_pvs= fi syspaths= scan_partition=false case $TYPE in partition) for x in /sys/block/*/*[0-9]; do [ -d "$x" ] || continue name="$(device_name "$x")" if part_of_lvmvg "$name"; then continue fi syspaths="${syspaths:+$syspaths }$x" done for x in /sys/block/dm-*; do [ -d "$x" ] || continue if is_lvm "$x"; then : # Keep LVM logical volumes elif is_sataraid "$x" && is_sataraid_partition "$x"; then : # Keep dmraid partitions else continue # Skip unknown entries fi syspaths="${syspaths:+$syspaths }$x" done TYPE=disk # Also allow misdetected USB devices scan_partition=: ;; usb-partition|mmc-partition) for x in /sys/block/*/*; do [ -d "$x" ] || continue syspaths="${syspaths:+$syspaths }$x" done ;; *) for x in /sys/block/*; do [ -d "$x" ] || continue case $x in /sys/block/dm-*) if is_sataraid "$x" && is_sataraid_partition "$x"; then continue fi if is_lvm "$x"; then continue fi ;; *) name="$(device_name "$x")" if part_of_sataraid "$name"; then continue fi if part_of_lvmvg "$name"; then continue fi ;; esac syspaths="${syspaths:+$syspaths }$x" done ;; esac for x in $syspaths; do devpath="${x#/sys}" match=false case $TYPE in floppy) # TODO ugly special case for non-IDE floppies case $devpath in /block/fd[0-9]*) match=: ;; esac ;; esac if ! $match && [ "$TYPE" = cd ]; then if device_info env "$devpath" | grep -q '^ID_CDROM='; then match=: fi fi if ! $match; then if device_info env "$devpath" | grep -q "^ID_TYPE=$TYPE"; then match=: fi fi if ! $match && [ "$TYPE" = disk ]; then case $devpath in /block/cciss\!*|/block/ida\!*|/block/rd\!*|/block/mmcblk*|/block/vd[a-z]*|/block/xvd[a-z]*) match=: ;; /block/nvme*) # Skip NVME controllers if [[ ! "$devpath" =~ nvme[0-9]+c[0-9]+ ]]; then match=: fi ;; /block/dm-*) # for now, we only understand dmraid and LVM if is_sataraid "/sys$devpath"; then match=: fi if is_lvm "/sys$devpath"; then match=: fi ;; esac fi # Some USB sticks and CD drives are misdetected as floppy # This allows to scan for those if ! $match && ( $scan_partition || [ "$TYPE" = maybe-usb-floppy ] ); then if device_info env "$devpath" | grep -q '^ID_BUS=usb' && \ device_info env "$devpath" | grep -q '^ID_TYPE=floppy'; then match=: fi fi # Disk partitions, but only on USB drives if ! $match && [ "$TYPE" = usb-partition ]; then if device_info env "$devpath" | grep -q '^ID_BUS=usb' && \ device_info env "$devpath" | grep -q '^ID_TYPE=disk'; then match=: fi fi # Disk partitions, but only on (non-USB) MMC devices if ! $match && [ "$TYPE" = mmc-partition ]; then if device_info env "$devpath" | grep -E -q '^ID_PATH=(platform-mmc|platform-orion-ehci|platform-mxsdhci|platform-omap)'; then match=: fi fi if $match && ! device_info env "$devpath" | grep -E -q '^ID_PART_ENTRY_TYPE=0x(5|f|85)$'; then device_name "/sys$devpath" fi done } part_of_lvmvg () { local pvdev for pvdev in $lvm_pvs; do if [ "$pvdev" = "$1" ]; then return 0 fi done return 1 } part_of_sataraid () { local raiddev for raiddev in $raiddevs; do if [ "$(readlink -f "$raiddev")" = "$1" ]; then return 0 fi done return 1 } prepend_dev() { if echo ${1} | grep -q '^/dev/'; then echo "${1}" return fi echo "/dev/${1}" }