Detect if a block device is a local disk or a removable usb disk - linux

There's any way to detect if a block device, like /dev/sda or /dev/sdc, is related to a local disk (scsi or sata I mean) or to a removable USB disk?
I'm writing a shell script that have to detect ONLY local disks block devices, excluding any removable disks.
Thanks!

You can use udev, the Linux device manager.
Querying for each block device will show several informations about it, including the bus, which you can use in order to discern if the device is a removable USB one.
This is the script:
for device in /sys/block/sd*; do
device_info="$(udevadm info --query=property --path=$device)"
device_name=$(echo "$device_info" | perl -ne 'print "$1" if /^DEVNAME=(.*)/')
device_bus=$(echo "$device_info" | perl -ne 'print "$1" if /^ID_BUS=(.*)/')
echo "Device $device_name bus: $device_bus"
done
and this is a sample result:
Device /dev/sda bus: ata
Device /dev/sdb bus: ata
Device /dev/sdc bus: usb

Use lshw:
lshw -class disk -class storage
and look for the bus info string.

Related

Linux command to display the number of hard drive slots your machine has

I'm currently working on a Linux machine and I want to know if there is a command that tells you the number of hard drive slots the actual machine has regardless of whether there are hard drives installed or not. I know reading the machine's manual provides that information but is there any way to get this information by command line?
I've tried lshw and dmidecode commands but they do not provide information on slots. This specific machine has 6 slots for hard drives to be installed with only 3 currently occupied. It does not have hardware raid either so I cannot use megacli.
Any help would be appreciated.
What about this command:
smartctl --scan
Output:
/dev/sda -d scsi # /dev/sda, SCSI device
/dev/sdb -d scsi # /dev/sdb, SCSI device
/dev/sdc -d scsi # /dev/sdc, SCSI device
/dev/sdd -d scsi # /dev/sdd, SCSI device
/dev/sde -d scsi # /dev/sde, SCSI device
/dev/sdf -d scsi # /dev/sdf, SCSI device
/dev/bus/0 -d megaraid,8 # /dev/bus/0 [megaraid_disk_08], SCSI device
/dev/bus/0 -d megaraid,9 # /dev/bus/0 [megaraid_disk_09], SCSI device
/dev/bus/0 -d megaraid,10 # /dev/bus/0 [megaraid_disk_10], SCSI device
/dev/bus/0 -d megaraid,11 # /dev/bus/0 [megaraid_disk_11], SCSI device
/dev/bus/0 -d megaraid,12 # /dev/bus/0 [megaraid_disk_12], SCSI device
/dev/bus/0 -d megaraid,13 # /dev/bus/0 [megaraid_disk_13], SCSI device
lsblk should list all block devices. If you want only physical disks you can use lsblk -d.
Example:
lsblk -o name,serial
Output:
NAME SERIAL
sda S2U5J1VZ500792
├─sda1
└─sda9
sdb W3APDFP8
├─sdb1
└─sdb9
Can it be done by lspci or lsscsi command. It can tell you the PCI or SCSI slots available and used.

Mount USB device on router - OpenWrt

I am new to OpenWrt and Linux/Unix shell. Prior to this, was using dd-wrt for 2 years. Recently switched to OpenWrt and got the basic setup done(wifi/internet working). I need to install transmission my router. But, USB device won't mount.
The Details of the router are as follows:
Router Model:
Asus RT-N13U B1
Image flashed:
chaos_calmer 15.05.1 (openwrt-15.05.1-ramips-rt305x-rt-n13u-squashfs-sysupgrade.bin)
Usb Details:
Crusier Blade 16GB usb 2.0
Formatted to ext4 using GParted bootable cd
Drivers Installed:
kmod-usb-core(preinstalled)
kmod-usb-dwc2(preinstalled)
kmod-usb2
kmod-usb-storage
kmod-fs-ext4
kmod-scsi-core(preinstalled)
Here are permissions for dev/sda, /dev/sda1 and /mnt (set 777 manually):
~#ls -l /dev
brwxrwxrwx 1 root root 8, 0 Jan 17 21:56 sda
brwxrwxrwx 1 root root 8, 1 Jan 17 21:56 sda1
~#ls -l ..
drwxrwxrwx 2 root root 0 Jan 16 21:28 mnt
Error while mounting:
~# mount -t ext4 /dev/sda1 /mnt
mount: mounting /dev/sda1 on /mnt failed: No such file or directory
echo $?
255
sda1 is present inside dev and /mnt directory exists. Still it shows an error.
I have tried installing/reinstalling and then mounting, but for the same result. Also when I disconnect the USB sda and sda1 folders disappear and then reappear when USB is reconnected so I guess the device is being detected successfully.
What is it, that I am missing?
Edit:
Added edits as suggested in the comments.
dmesg output (The output is very big so linked it)
Edit 2:
Switched to LEDE 17. Is more stable and have had zero issues in the past week.
the critical part of the dmesg output is
[ 9.410000] mount_root: loading kmods from internal overlay
[ 9.940000] SCSI subsystem initialized
[ 9.960000] ehci_hcd: USB 2.0 'Enhanced' Host Controller (EHCI) Driver
[ 9.980000] ehci-platform:EHCI generic platform driver [ 9.990000] usb-storage 1-1:1.0: no of_node; not parsing pinctrl DT
[ 9.990000] usb-storage 1-1:1.0: USB Mass Storage device detected
[ 10.010000] scsi host0: usb-storage 1-1:1.0
[ 10.020000] usbcore: registered new interface driver usb-storage
[ 10.100000] block: attempting to load /tmp/jffs_cfg/upper/etc/config/fstab
[ 10.120000] block: extroot: not configured
[ 10.130000] mount_root: switching to jffs2 overlay
[ 10.180000] procd: - early -
[ 11.020000] scsi 0:0:0:0: Direct-Access SanDisk Cruzer Blade 1.27 PQ: 0 ANSI: 6
[ 11.030000] sd 0:0:0:0: no of_node; not parsing pinctrl DT
[ 11.050000] sd 0:0:0:0: [sda] 30529536 512-byte logical blocks: (15.6 GB/14.5 GiB)
[ 11.070000] sd 0:0:0:0: [sda] Write Protect is off
[ 11.070000] sd 0:0:0:0: [sda] Mode Sense: 43 00 00 00
[ 11.090000] sd 0:0:0:0: [sda] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
[ 11.120000] sda: sda1
[ 11.130000] sd 0:0:0:0: [sda] Attached SCSI removable disk
[ 11.550000] EXT4-fs (sda1): Cannot load crc32c driver
oh no, this seems to be a bug
Bug#819725: ext4 missing softdep on crc32c module
https://lists.debian.org/debian-kernel/2016/04/msg00013.html
the following workaround is not applicable because initramfs is a native file system at startup time (https://en.wikipedia.org/wiki/Initramfs) and has no influence when the system is fully up (rootfs is mounted):
Until this is fixed in the kernel package, you can work around it by
either:
Setting base-installer/initramfs-tools/driver-policy to "most" instead of "dep"
Setting base-config/late_command to a script that adds crc32c to /etc/initramfs-tools/modules
post the stack trace anyway, maybe there is another workaround
this is overcomplicated stuff...
here is maybe a solution https://forum.openwrt.org/viewtopic.php?id=69175
download kmod-lib-crc32c and kmod-crypto-crc32c
if this is not working maybe the easiest solution is to format the USB stack as VFAT and to wait for a new kernel...
this is not a permissions error. a permission error would return EPERM -> error code 1 Operation not permitted
it would be interesting to know what exit code the mount returns. The 'exit behavior is very different in the several mount version' i.e. mount(2) and mount(8)
for getting the return value, type in a shell the command
mount /dev/sda1 /mnt
then
echo $?
the number is the returned exit code of the mount (the 255 means 'exit status out of range' in this case '-1', http://www.tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF)
a listing of the mount(8) exit codes is i.e. in http://www.stackoverflow.com/questions/33167585/what-are-the-return-codes-values-of-linux-umount
http://www.becane.com/2014/09/02/understanding-exit-codes-and-how-to-use-them-in-a-bash-script
beside the return value mount(2) also sets an error code in errno (http://man7.org/linux/man-pages/man3/errno.3.html). printing errno in shell is a bit difficult it is easier to get a reference like http://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Errors/unix_system_errors.html and search for the error string, in this case No such file or directory
the string No such file or directory is system error ENOENT
As a system error ENOENT means A pathname was empty or had a nonexistent component. (http://man7.org/linux/man-pages/man2/mount.2.html)
try sudo mount -t ext4 /dev/sda1 /mnt because the stick is formatted in ext4
if this is not working dmesg output would be interesting
detach usb device, reattach usb device, type dmesg and see the output. in the output of dmesg you also see which drivers are loaded for the device
https://wiki.openwrt.org/doc/howto/usb.storage says that you need several more drivers (block-mount, kmod-scsi-core,...) because a usb stick (USB Mass Storage class) is also a SCSI and a block device...
(linux system error codes are in http://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Errors/unix_system_errors.html)
print a stack trace sudo strace -f mount -t ext4 -o default /dev/sda1 /mnt and post it

RS232 console communication - set baudrate to 1 MBaud

Within a bash script, I use the following:
$ stty -F /dev/ttyUSB0 921600 raw
$ echo -n "some test data" >/dev/ttyUSB0
and it works as expected.
Using a PL2303 USB to RS232 adapter:
$ lsusb
...
Bus 006 Device 010: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port
Bus 006 Device 011: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port
Now I tried to do the same with 1 MBd, but got an error:
$ stty -F /dev/ttyUSB0 1000000 raw
stty: /dev/ttyUSB0: unable to perform all requested operations
Also the same message when I try with 500 kBd. Trying 250 kBd the error message is different:
$ stty -F /dev/ttyUSB0 250000 raw
stty: invalid argument `250000'
Try `stty --help' for more information.
As seen here, it's a problem in the PL2303 linux driver.
I'm working on Kubuntu 12.04, 32 Bit. Unfortunally, I don't know how to fix that driver on my system (getting driver source, patch em, compile, install … hmm, maybe I learn a bit and give it a try - advice is welcome).
But maybe there is an updated driver avaliable which is easy to install?
Or does someone know an alternate USB to RS232 adapter which works at 1 MBd (hardware flowcontrol via rts/cts is needed, which works pretty well with the PL2303)?
After the realization that »Prolific and FTDI are competitors«, I bought Ftdi US232R-10 which is a FT232R based device and specified for 1 MBd transfer rate.
With this adapter I'd successfully tested communication at 1 MBd by transfering some GiB data without any error (including usage of Rts/Cts hardware flow control).
Configuring this device using stty like:
$ stty -F /dev/ttyUSB0 1000000 raw
works successfully.

bash, search for usb storage devices. output location

I am looking for a way to list any usb connected devices or removable storage media.
I will be using this list for a gtk boot media writer, so a user can easily write an iso to a usb.
This creates a perfect list of ALL partitions:
ls /dev | grep "[sh]d[a-z][1-9]"
How can I get a similar looking list that is only removable media?
On my system (Ubuntu 12.04), I can get a list of USB devices and partitions with
ls /dev/disk/by-path/*usb*
giving
/dev/disk/by-path/pci-0000:00:02.1-usb-0:1.1:1.0-scsi-0:0:0:0
/dev/disk/by-path/pci-0000:00:02.1-usb-0:1.1:1.0-scsi-0:0:0:0-part1
or partitions alone
ls /dev/disk/by-path/*usb*part*
These are symbolic links, pointing to the real device files, /dev/sdd and /dev/sdd1 for example.
I have tested this with a USB stick and an external USB hard disk only. I cannot say, whether or how this works with eSATA or Firewire disks.
Based on the answer of Olaf Dietsche, I end up with the following:
devs=`ls -al /dev/disk/by-path/*usb*part* 2>/dev/null | awk '{print($11)}'`; for dev in $devs; do dev=${dev##*\/}; echo -n "$dev ("; echo -n `mount | grep \`echo -E ${dev}\` | awk '{print($3)}'`; echo ")"; done
For me the above code is showing the usb devices and where they are mounted (between parentheses). It worked on Ubuntu 13.04 and 12.04.2, but I do not know if it will work on any other system.
Maybe you can use the output of lsusb command :
lsusb
Bus 001 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0db0:3870 Micro Star International
Bus 002 Device 003: ID 0000:0000
Bus 002 Device 004: ID 14cd:6116 Super Top M6116 SATA Bridge
To get the mounted path of usb storage use
mount|grep /media|awk '{print $3}'
explanation to command
mount will print all the mounted drives and grep will display only drives mounted in /media, (considering /media is default mount point ) this output is piped to awk which will print the mounted path of usb drive
bootiso BASH utility just do that when called with -l option. Here is the output of a slightly modified bash snippet:
/dev/sdd /dev/sde
Relevant snippet:
printUSBDevices() {
typeset -a usbDevices
typeset -a devices
getDeviceType() {
typeset deviceName=/sys/block/${1#/dev/}
typeset deviceType=$(udevadm info --query=property --path="$deviceName" | grep -Po 'ID_BUS=\K\w+')
echo "$deviceType"
}
mapfile -t devices < <(lsblk -o NAME,TYPE | grep --color=never -oP '^\K\w+(?=\s+disk$)')
for device in "${devices[#]}" ; do
if [ "$(getDeviceType "/dev/$device")" == "usb" ]; then
usbDevices+=("/dev/$device")
fi
done
echo "${usbDevices[#]}"
}
printUSBDevices

Get details of RAID configuration on Linux

How to get the details of RAID configuration in Linux ?
mdadm -D /dev/mdxx will give you detail of raid configuration.
cat /proc/mdstat will give detail about raid algorithm,level and chunk size etc .
This is real if this RAID is sofware....
In case of RAID hardware, you could type this command :
lspci -vv | grep -i raid
01:00.0 RAID bus controller: LSI Logic / Symbios Logic MegaRAID SAS 2208 [Thunderbolt] (rev 01)
Kernel driver in use: megaraid_sas
Kernel modules: megaraid_sas
If you're talking about a running array:
cat /proc/mdstat
If you're talking about the mdadm config file, it's usually in /etc or /etc/mdadm depending on the distribution you're running on. The following command should find it in any event:
find /etc -name '*mdadm*'
ETA: Also, I would strongly recommend that you carefully study the mdadm man page so that you are very familiar with that utility. Knowing that utility well will save your bacon at some point.
mdadm --detail /dev/md0
(or whatever /dev/mdXXX you are using)

Resources