Solução recomendada:
-
Copie o seguinte script python em um arquivo chamado
internal_block_device_resource
:#!/usr/bin/env python3 import os import re from glob import glob rootdir_pattern = re.compile('^.*?/devices') internal_devices = [] def device_state(name): """ Follow pmount policy to determine whether a device is removable or internal. """ with open('/sys/block/%s/device/block/%s/removable' % (name, name)) as f: if f.read(1) == '1': return path = rootdir_pattern.sub('', os.readlink('/sys/block/%s' % name)) hotplug_buses = ("usb", "ieee1394", "mmc", "pcmcia", "firewire") for bus in hotplug_buses: if os.path.exists('/sys/bus/%s' % bus): for device_bus in os.listdir('/sys/bus/%s/devices' % bus): device_link = rootdir_pattern.sub('', os.readlink( '/sys/bus/%s/devices/%s' % (bus, device_bus))) if re.search(device_link, path): return internal_devices.append(name) for path in glob('/sys/block/*/device'): name = re.sub('.*/(.*?)/device', '\g<1>', path) device_state(name) print(' '.join(internal_devices))
-
Certifique-se de que o script tenha permissão executável:
chmod +x internal_block_device_resource
-
Defina sua variável
DRIVES
bash desta maneira:DRIVES=$(./internal_block_device_resource)
$ echo $DRIVES sda
Versão inicial:
Você precisa filtrar dispositivos removíveis para poder usar este comando:
find /sys/block/*/device/block/*/removable -exec bash -c 'echo {} | perl -ne "$a=$_;s/^\/sys\/block\/(.*?)\/.*/$1/;print if (\'cat $a\' == "0")"' \;
Com um stick USB montado como /dev/sdb
, a saída é apenas sda
.