Get hard drive model name with bash - bash

I want to get the various model names of my hard drives using bash.
I can do it if there's just one, using hwinfo like this:
hwinfo --ide|grep Model|sed -ne '/Model/s/.*Model: "\([^"]*\)".*/\1/p'
But this obviously fails when there's more than one. One regular hwinfo output when there are several hard drives is this:
[faidoc#Delorean ~]$ hwinfo --ide
11: IDE 200.0: 10600 Disk
[Created at block.245]
Unique ID: 3OOL.XFCtBh10jZ2
Parent ID: qnJ_.3_X41NtKT36
SysFS ID: /class/block/sda
SysFS BusID: 2:0:0:0
SysFS Device Link: /devices/pci0000:00/0000:00:0d.0/ata3/host2/target2:0:0/2:0:0:0
Hardware Class: disk
Model: "VBOX HARDDISK"
Vendor: "VBOX"
Device: "HARDDISK"
Revision: "1.0"
Serial ID: "VBfa9b1456-03d78f51"
Driver: "ahci", "sd"
Driver Modules: "ahci"
Device File: /dev/sda
Device Files: /dev/sda, /dev/disk/by-id/ata-VBOX_HARDDISK_VBfa9b1456-03d78f51
Device Number: block 8:0-8:15
BIOS id: 0x80
Geometry (Logical): CHS 1305/255/63
Size: 20971520 sectors a 512 bytes
Capacity: 10 GB (10737418240 bytes)
Config Status: cfg=new, avail=yes, need=no, active=unknown
Attached to: #10 (SATA controller)
12: IDE 300.0: 10600 Disk
[Created at block.245]
Unique ID: WZeP.0xN7VsONW+D
Parent ID: qnJ_.3_X41NtKT36
SysFS ID: /class/block/sdb
SysFS BusID: 3:0:0:0
SysFS Device Link: /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0
Hardware Class: disk
Model: "VBOX HARDDISK"
Vendor: "VBOX"
Device: "HARDDISK"
Revision: "1.0"
Serial ID: "VB350f9911-48221ae2"
Driver: "ahci", "sd"
Driver Modules: "ahci"
Device File: /dev/sdb
Device Files: /dev/sdb, /dev/disk/by-id/ata-VBOX_HARDDISK_VB350f9911-48221ae2
Device Number: block 8:16-8:31
BIOS id: 0x81
Geometry (Logical): CHS 2349/255/63
Size: 37748736 sectors a 512 bytes
Capacity: 18 GB (19327352832 bytes)
Config Status: cfg=new, avail=yes, need=no, active=unknown
Attached to: #10 (SATA controller)
Every drive begans with for example "11:" or "12:" so if I could get one at a time that will be the solution.
Any ideas?
Thanks

You can get the info with:
hdparm -i /dev/sda | grep -i model
or, if you want just the model name:
hdparm -i /dev/sda | perl -n -e 'print "$1\n" if (m/model=(.+?),/i);'

If you know which one you want a very easy way would be grep -A8 -E '^11:'
hwinfo --ide|grep -A8 -E '^11:'|grep Model|sed -ne '/Model/s/.*Model: "\([^"]*\)".*/\1/p'
The -A flag on grep grabs that many lines "After" the match as well as the line with the match.
There's also -B for "Before" and -C for "Context"

here's a quick and dirty awk statement that may help:
hwinfo --ide | awk '{ if($2=="IDE"){ide=$3} if($1=="Model:"){print "IDE " ide $0} }'
basically searches for the pattern "IDE" in the second word of each line.
if it finds that, it stores the third word of the line in the variable named "ide".
then it searches the first word of each line for "Model:".
if found, it prints the IDE it stored earlier and the whole line containing the model name.
so you end up with the name and IDE location in your output:
IDE 200.0: Model: "VBOX HARDDISK"
IDE 300.0: Model: "VBOX HARDDISK"
and it should work no matter how many disks are attached.

Related

kernel not able to run /init in initramfs in linux android 10 kernel version 4.9

I have created a custom initramfs using the below command in my custom initramfs directory:
find . | cpio --quiet -H newc -o | gzip -9 -n > ../diaginitrd.img
Then I built a custom bootimage with the above cpio using the command
mkbootimg --kernel --ramdisk
Defconfig variables are as below:
CONFIG_BLK_DEV_INITRD=y
CONFIG_RD_LZMA=y
CONFIG_RD_BZIP2=y
I have not provided CONFIG_INITRAMFS_SOURCE variable in defconfig.
Kernel command line given is as:
Kernel command line: console=ttyMSM0,115200n8 androidboot.console=ttyMSM0 androidboot.configfs=true loop.max_part=7 androidboot.usbcontroller=a600000.dwc3 root=/dev/dm-0 rdinit=/init user_debug=31
On build when i unpack my bootimage i can see my initramfs .
On unpacking the initramfs i can see the content of my initamfs.
When i boot the my device with this bootimage i get below logs:
[ 17.850396] sde: sde1 sde2 sde3 sde4 sde5 sde6 sde7 sde8 sde9 sde10 sde11 sde12 sde13 sde14 sde15 sde16 sde17 sde18 sde19 sde20 sde21 sde22 sde23 sde24 sde25 sde26 sde27 sde28 sde29 sde30 sde31 sde32 sde33 sde34 sde35 sde36 sde37 sde38 sde39 sde40 sde41 sde42 sde43 sde44 sde45 sde46 sde47 sde48 sde49 sde50 sde51 sde52 sde53 sde54 sde55 sde56
[ 17.853364] sd 0:0:0:7: [sdh] Write Protect is off
[ 17.854114] sdf: sdf1 sdf2 sdf3 sdf4 sdf5
[ 17.854573] sd 0:0:0:7: [sdh] Optimal transfer size 8192 bytes
[ 17.861410] sdg: sdg1 sdg2 sdg3 sdg4 sdg5 sdg6 sdg7 sdg8
[ 17.870417] sdh: sdh1 sdh2 sdh3 sdh4 sdh5 sdh6 sdh7 sdh8 sdh9 sdh10 sdh11 sdh12 sdh13 sdh14 sdh15 sdh16 sdh17 sdh18 sdh19 sdh20
[ 17.967128] sda: sda1 sda2 sda3 sda4 sda5 sda6 sda7 sda8 sda9 sda10
[ 17.995848] Freeing unused kernel memory: 7616K
[ 18.002632] Initramfs ---> Failed to execute /init (error -2)
[ 18.009114] Kernel panic - not syncing: Requested init /init failed (error -2).
[ 18.016482] CPU: 6 PID: 1 Comm: swapper/0 Not tainted 4.9.206+ #10
[ 18.022707] Hardware name: Qualcomm Technologies, Inc. sda845 v2.1 MTP (DT)
[ 18.029718] Call trace:
[ 18.032195] [<0000000011338cd0>] dump_backtrace+0x0/0x260
[ 18.037635] [<0000000085c2bec8>] show_stack+0x20/0x28
[ 18.042724] [<00000000d2899c10>] dump_stack+0xbc/0xf8
[ 18.047815] [<00000000bd9287e7>] panic+0x204/0x3f8
[ 18.052652] [<00000000b85fea6d>] kernel_init+0xa4/0x10c
I am not sure why kernel is not able to read my /init file
Note: my /init is a shell script of type
init: POSIX shell script, ASCII text executable.
All the binaries in initramfs are statically linked.
I found the answer:
It was issue of sh and all other binaries.
They were not cross compiled for my system architecture.
Now after cross-compiling the script can work.
Thanks "Ian Abbot" for pointing out the issue

cgroups blkio subsystem is not counting the block write byte count properly for conatiner applications

I am working on the linux kernel base 3.14 version and i have enabled the cgroup and blkio subsystem on it for checking the write byte count of the block device from the container and host applications.
But, I have problems in getting the written bytes from the cgroup blkio throttling function for the container application.
It works for the main hierarchy (e.g. /sys/fs/cgroup/blkio/blkio.throttle.io_service_bytes) , but not for the deeper ones (e.g. /sys/fs/cgroup/blkio/lxc/web (container name is web))
I created a small test script (checkWrite), which will simply enter ther cgroup it is started in (pwd) and will create 1M.
#!/bin/bash
SIZE=1M
DST="/home/root"
#check if we are in the /sys/fs/cgroup/ dir
if [ ! -e ./tasks ]; then
echo "Error, this script must be started in a cgroup blkio directory"
echo "Start in or below /sys/fs/cgroup/blkio !"
exit -1
fi
echo "Using the cgroup: ${PWD##*/cgroup}"
# add myself to cgroup
echo $$ > tasks
mygroup=`cat /proc/$$/cgroup | grep blkio`
echo "we're now in bklio cgroup: ${mygroup}"
# call sync to let kernel store data
sync
sleep 1
# fetch current writen bytes count for eMMC
before=$(cat blkio.throttle.io_service_bytes | grep "179:24 Write")
echo "before writing: ${before}"
echo "writing ${SIZE} random data to ${DST}/DELME ..."
dd if=/dev/urandom of=${DST}/DELME bs=${SIZE} count=1
sync
sleep 2
# fetch current writen bytes count for eMMC
after=$(cat blkio.throttle.io_service_bytes | grep "179:24 Write")
echo "after writing: ${after}"
written=$((${after##* }-${before##* }))
written=$((written/1024))
echo "written = ${after##* }B - ${before##* }B = ${written}kB"
rm -rf ${DST}/DELME
The output is;
/sys/fs/cgroup/blkio# ~/checkWrite
Using the cgroup: /blkio
we're now in bklio cgroup: 3:blkio:/ <- this task is in this blkio chgroup now
before writing: 179:24 Write 200701952 <- from blkio.throttle.io_service_bytes
writing 1M random data to /var/opt/bosch/dynweb/DELME ...
1+0 records in
1+0 records out
after writing: 179:24 Write 201906176
written = 201906176B - 200701952B = **1176kB** **<- fairly ok**
/sys/fs/cgroup/blkio/lxc/web# ~/checkWrite
Using the cgroup: /blkio/system.slice
we're now in bklio cgroup: 3:blkio:/system.slice
before writing: 179:24 Write 26064896
writing 1M random data to /var/opt/bosch/dynweb/DELME ...
1+0 records in
1+0 records out
after writing: 179:24 Write 26130432
written = 26130432B - 26064896B = **64kB** **<- much too less**
Do I misunderstand the handling?
If it is not working, then how to monitor/watch/read the block device write from the container applications.

Returned lines overlap when using ncat pipe in a shell script

I am using /bin/sh to write a shell script that fetches data from a telnet call via ncat, like so:
echo 'transport info' | ncat hostname 9993
When I do this from a command line the output looks like this:
500 connection info:
protocol version: 1.3
model: HyperDeck Studio
208 transport info:
status: record
speed: 0
slot id: 1
clip id: none
display timecode: 00:28:01:27
timecode: 00:00:00:00
video format: 1080i5994
loop: false
But when I do it in a shell script /bin/sh it looks like this:
loop: falseat: 1080i599443:15
Here is my sample script:
#!/bin/sh
FOO="$( echo "transport info" | ncat -C hostname 9993 )"
echo $FOO
Anyone know why this happens?
#!/bin/sh
echo "transport info" | ncat -C hostname 9993 | dos2unix > /tmp/test.txt
cat /tmp/test.txt
Output:
500 connection info:
protocol version: 1.3
model: HyperDeck Studio
208 transport info:
status: preview
speed: 0
slot id: none
clip id: none
display timecode: 01:10:01:01
timecode: 00:00:00:00
video format: 1080i5994
loop: false

BASH/shell script, blkid fails with newlines in drive labels

I have a BASH/shell script, running on Linux, but it sometimes has a problem getting the drive labels, using blkid.. blkid gets the drive labels nad UUIDs of the given drive.
Example of blkid output:
# blkid /dev/sda1
/dev/sda1: LABEL="Home" UUID="f1e5e82b-1c75-4fd7-8841-6ad766152dcf" TYPE="ext2"
The problem occurs when the drive label has a newline character... I have included the relevant function. The problem seems to be with the eval command.. Can someone help me?
blkid_name_generator() {
#Gather blkid output and filter out required device
if [ ! -f /tmp/blkid.txt ]; then
blkid -c /dev/null > /tmp/blkid.txt
fi
blkid_cleaner &
LABEL=""
LABEL1=""
eval $( cat /tmp/blkid.txt | grep "$#:" | cut --delimiter=" " -f 2- | sed -e 's/ /;/g')
LABEL1=${LABEL//;/ } # akita beta4 fix: remove newlines below
LABEL=`echo $LABEL1 | tr -d '\n'` # akita beta5 fix, remove newlines from drive label
#Generate drive label
[ "$LABEL" = "" ] && ONEDRVLABEL="$#\n \n " || ONEDRVLABEL="${LABEL}\n($#)\n "
echo "${LABEL}" and "${ONEDRVLABEL}"
}
blkid_name_generator /dev/sda1
Here's my bash snippet:
while read curline; do
if [[ ${curline:0:1} == "/" ]]; then
[[ $prevline ]] && echo "$prevline"
prevline="$curline"
else
prevline+="$curline"
fi
done < $INPUTFILE
[[ $prevline ]] && echo "$prevline"
Here's my test file:
/dev/sda1: LABEL="Boot" UUID="fdc5e51d-3da6-4edf-bf07-6397b1765797" TYPE="ext2"
/dev/sda2: LABEL="Root" UUID="f883df24-1b93-46fb-8990-840774c380c4" TYPE="ext4"
/dev/sda3: LABEL="Usr" UUID="ba759de3-e0e4-4603-a324-f11dc25fa784" TYPE="reiserfs"
/dev/sdb1: LABEL="Persis
tents" UUID="50b81ef0-a38b-4677-b9d9-9548b29ce2bb" TYPE="ext4"
/dev/sdc1: LABEL="Tempo
raries" UUID="a649c535-1a8a-4b2b-b0a0-4afbdc60a3bc" TYPE="reiserfs"
/dev/sdd1: LABEL="Usr
Portage" UUID="a228148e-6405-4bbe-990f-df6eaebb1b1d" TYPE="reiserfs"
/dev/sda1: LABEL="Boot" UUID="fdc5e51d-3da6-4edf-bf07-6397b1765797" TYPE="ext2"
/dev/sda2: LABEL="Root" UUID="f883df24-1b93-46fb-8990-840774c380c4" TYPE="ext4"
Here's my test file after processed by the bash snippet:
/dev/sda1: LABEL="Boot" UUID="fdc5e51d-3da6-4edf-bf07-6397b1765797" TYPE="ext2"
/dev/sda2: LABEL="Root" UUID="f883df24-1b93-46fb-8990-840774c380c4" TYPE="ext4"
/dev/sda3: LABEL="Usr" UUID="ba759de3-e0e4-4603-a324-f11dc25fa784" TYPE="reiserfs"
/dev/sdb1: LABEL="Persistents" UUID="50b81ef0-a38b-4677-b9d9-9548b29ce2bb" TYPE="ext4"
/dev/sdc1: LABEL="Temporaries" UUID="a649c535-1a8a-4b2b-b0a0-4afbdc60a3bc" TYPE="reiserfs"
/dev/sdd1: LABEL="UsrPortage" UUID="a228148e-6405-4bbe-990f-df6eaebb1b1d" TYPE="reiserfs"
/dev/sda1: LABEL="Boot" UUID="fdc5e51d-3da6-4edf-bf07-6397b1765797" TYPE="ext2"
/dev/sda2: LABEL="Root" UUID="f883df24-1b93-46fb-8990-840774c380c4" TYPE="ext4"
Hope this helps!
I don't see exactly what your script is intending to do (apart from the missing bits).
Here's what I came up with:
blkid | perl -ne 'print "$1\n" if m/LABEL="(.*?)"/o'
It will show this (on my system):
test^J123
WIN7VIRT
TEMP
Note how the newline in the label is depicted as ^J ? Viewing through a hex encoder shows that it is indeed a two-character combination 0x5e 0x4a
Hope you can get some further with that.
PS: 'proof' that the newline is in fact correctly in the volume label
tune2fs /dev/sdc1 -l
tune2fs 1.41.14 (22-Dec-2010)
Filesystem volume name: test
123
Last mounted on: /media/9868d90c-aede-4e7a-b105-d9312f8b17ab
Filesystem UUID: 9868d90c-aede-4e7a-b105-d9312f8b17ab
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
...
and on block level:
dd if=/dev/sdc1 count=10 | xxd | grep 123
0000470: b105 d931 2f8b 17ab 7465 7374 0a31 3233 ...1/...test.123
Thanks to this question... I could find a solution for another problem. Example (sda2 = ntfs partition):
blkid /dev/sda2
Segmentation Fault (core dump)
Hard to find troubleshooting guide for this issue (ubuntu based OS or any other)
The reason behind is a malformed LABEL in NTFS partition.
(of course, mal-formation we cant see, as newlines ;-)
Solution: Clear LABEL with GParted; then rewrite it or set a new LABEL.

Determine values of several system variables in the terminal in a Mac

I'm on a Mac. In the terminal, how would you figure out each of the following values?
Word size (64 bit vs. 32 bit)
L1/L2 cache size
Determine how much memory is being used (like df, but for RAM)
Thanks! I know you can find these in Activity Monitor, System Profiler etc. but I am trying to boost my knowledge of the terminal, and UNIX.
System Profiler is a GUI wrapper around /usr/sbin/system_profiler.
mress:10008 Z$ system_profiler -listDataTypes
Available Datatypes:
SPHardwareDataType
SPNetworkDataType
SPSoftwareDataType
SPParallelATADataType
SPAudioDataType
SPBluetoothDataType
SPCardReaderDataType
SPDiagnosticsDataType
SPDiscBurningDataType
SPEthernetDataType
SPFibreChannelDataType
SPFireWireDataType
SPDisplaysDataType
SPHardwareRAIDDataType
SPMemoryDataType
SPPCIDataType
SPParallelSCSIDataType
SPPowerDataType
SPPrintersDataType
SPSASDataType
SPSerialATADataType
SPUSBDataType
SPAirPortDataType
SPFirewallDataType
SPNetworkLocationDataType
SPModemDataType
SPNetworkVolumeDataType
SPWWANDataType
SPApplicationsDataType
SPDeveloperToolsDataType
SPExtensionsDataType
SPFontsDataType
SPFrameworksDataType
SPLogsDataType
SPManagedClientDataType
SPPrefPaneDataType
SPStartupItemDataType
SPSyncServicesDataType
SPUniversalAccessDataType
mress:10009 Z$ system_profiler SPHardwareDataType
Hardware:
Hardware Overview:
Model Name: iMac
Model Identifier: iMac10,1
Processor Name: Intel Core 2 Duo
Processor Speed: 3.33 GHz
Number Of Processors: 1
Total Number Of Cores: 2
L2 Cache: 6 MB
Memory: 16 GB
Bus Speed: 1.33 GHz
Boot ROM Version: IM101.00CC.B00
SMC Version (system): 1.52f9
Serial Number (system): QP0241DXB9S
Hardware UUID: 01C6B9E9-B0CB-5249-8AC7-069A3E44A188
You can also get some useful information from /usr/sbin/sysctl (try sysctl -a).
mress:10014 Z$ sudo sysctl -a | grep cache
Password:
hw.cachelinesize = 64
hw.l1icachesize = 32768
hw.l1dcachesize = 32768
hw.l2cachesize = 6291456
kern.flush_cache_on_write: 0
vfs.generic.nfs.client.access_cache_timeout: 60
vfs.generic.nfs.server.reqcache_size: 64
net.inet.ip.rtmaxcache: 128
net.inet6.ip6.rtmaxcache: 128
hw.cacheconfig: 2 1 2 0 0 0 0 0 0 0
hw.cachesize: 17179869184 32768 6291456 0 0 0 0 0 0 0
hw.cachelinesize: 64
hw.l1icachesize: 32768
hw.l1dcachesize: 32768
hw.l2cachesize: 6291456
machdep.cpu.cache.linesize: 64
machdep.cpu.cache.L2_associativity: 8
machdep.cpu.cache.size: 6144

Resources