[Gambas-user] Disk devices info and management

Brian G brian at westwoodsvcs.com
Wed Dec 30 20:31:59 CET 2020


BruceS, 

The information you are looking for can be got from the 

/sys/block directory 
ls /sys/block will return a list of devices 

ls /sys/block/sda gets you 
alignment_offset capability device events events_poll_msecs hidden inflight mq queue removable sda1 size stat trace 
bdi dev discard_alignment events_async ext_range holders integrity power range ro sda2 slaves subsystem uevent 

Notice it lists the sda1 and sda2 partitions etc 

cat stat gets you 
2752110 675832 28981442 3612713 152716 5416705 79021648 4189622 0 3484324 7008448 115123 0 1122950384 7017 
this list is left to write 
The stat file consists of a single line of text containing 11 decimal
values separated by whitespace.  The fields are summarized in the
following table, and described in more detail below.

Name            units         description
----            -----         -----------
read I/Os       requests      number of read I/Os processed
read merges     requests      number of read I/Os merged with in-queue I/O
read sectors    sectors       number of sectors read
read ticks      milliseconds  total wait time for read requests
write I/Os      requests      number of write I/Os processed
write merges    requests      number of write I/Os merged with in-queue I/O
write sectors   sectors       number of sectors written
write ticks     milliseconds  total wait time for write requests
in_flight       requests      number of I/Os currently in flight
io_ticks        milliseconds  total time this block device has been active
time_in_queue   milliseconds  total wait time for all requests
discard I/Os    requests      number of discard I/Os processed
discard merges  requests      number of discard I/Os merged with in-queue I/O
discard sectors sectors       number of sectors discarded
discard ticks   milliseconds  total wait time for discard requests 
notice it lists all sorts of great information 

cat /sys/block/sda/ size 
1953525168 

ls /proc/mounts 
Gets you all the system mounts 

More info here [ https://www.kernel.org/doc/Documentation/block/stat.txt | https://www.kernel.org/doc/Documentation/block/stat.txt ] 

Here is a script that gets data with just gambas I did a while ago, 
Dont know if it helps to do it in just gambas way 

#!/usr/bin/gbs3 
Public Sub Main() 
'Linux 
dim statmap As String[] = [ 
"&1 = Read I / Os requests number Of Read I / Os processed", 
"&1 = Read merges requests number Of Read I / Os merged With In -queue I / O", 
"&1 = Read sectors sectors number Of sectors Read", 
"&1 = Read ticks milliseconds total Wait Time For Read requests", 
"&1 = Write I / Os requests number Of Write I / Os processed", 
"&1 = Write merges requests number Of Write I / Os merged With In -queue I / O", 
"&1 = Write sectors sectors number Of sectors written", 
"&1 = Write ticks milliseconds total Wait Time For Write requests", 
"&1 = in_flight requests number Of I / Os currently In flight", 
"&1 = io_ticks milliseconds total Time this block device has been active", 
"&1 = time_in_queue milliseconds total Wait Time For all requests", 
"&1 = discard I / Os requests number Of discard I / Os processed", 
"&1 = discard merges requests number Of discard I / Os merged With In -queue I / O", 
"&1 = discard sectors sectors number Of sectors discarded", 
"&1 = discard ticks milliseconds total Wait Time For discard requests" 
] 

dim i as integer 
Dim disks As String[] = Dir("/sys/block","s*") ' you can filter by whatever you like 
Dim Partitions As String[] = Dir("/sys/block/sda", "sda*") ' again I only filter for sda etc 
Dim stats As String[] = Split(file.load("/sys/block/sda/stat"), " \n", "", True, True) 
Dim dSize As String = file.load("/sys/block/sda/size") 
Dim sda1Stats As String[] = Split(file.load("/sys/block/sda/sda1/stat"), " \n", "", True, True) 
Dim sda1dSize As String = file.load("/sys/block/sda/sda1/size") 
' get mount points 
Dim mounts As String[] = Split(file.load("/proc/mounts"), "\n", "", True, True) 
for i = 0 to mounts.max 'lets extract the most interesting fields 
mounts[i] = split(mounts[i]," \n","",true,true).extract(0,3).join("\t") 
next 
'mount info also retrun the filesystem type 
'/dev/disk/by-uuid lists all the devices as links to the physical device such as sda1 etc 
'these are all available by-id by-label by-partlabel by-partuuid by-path by-uuid 
Dim uuids As String[] = Dir("/dev/disk/by-uuid") ' each is a link to the device 
Dim labels As String[] = Dir("/dev/disk/by-label") ' each is a link to the device 

listing(disks, "Disks") 
listing(Partitions, "DiskPartitions") 
listing(stats, "Stats for sda", "*", statmap) 
Listing(sda1stats, "Stats for sda1", "*", statmap) 
Print "Sizeof sda :";; Dsize;; 
Print " sda1 :";; sda1dsize 
listing(mounts, "Mounted Devices", "/dev*") ' change /dev to UUID if your system uses that 

For i As Integer = 0 To labels.Max 
Dim st As Stat = Stat("/dev/disk/by-label" &/ labels[i]) 
labels[i] = labels[i] & "->" & File.Name(st.link) 
Next 
listing(labels, "Label of disks") 

Print "done" 

Quit 0 
Catch 
Print error.text & "\n" & error.where 
End 

Sub listing(source As String[], title As String, Optional filter As String = "*", map As String[] = []) 
Print title 
For i As Integer = 0 To source.max 
Dim s As String = source[i] 
If s Like filter Then 
If map.count > 0 Then 
Print Space(10); Subst(map[i], Left(s & Space(10), 10)) 
Else 
Print Space(10); s 
Endif 
Endif 
Next 
Print 
End 


Cheers 
Brian G 


From: "Bruce Steers" <bsteers4 at gmail.com> 
To: "Gambas mailing list" <user at lists.gambas-basic.org> 
Sent: Tuesday, December 29, 2020 9:06:12 AM 
Subject: Re: [Gambas-user] Disk devices info and management 

oh noo, not in terminal !! :( (shakes fist in the air) 

Well i guess the terminal side could use shell lsblk and mount, BSD will be a different case, i have it running now on a VM so will see what output conversion i can manage. 
your component gets an awful lot more info compared to mine. 

My initial thoughts were to look into liblsblk and libmount somehow integrate that way but i'm on a bit of a learning curve when it comes to C. 


On Tue, 29 Dec 2020 at 16:08, Fabien Bodard < [ mailto:gambas.fr at gmail.com | gambas.fr at gmail.com ] > wrote: 



My goal was to improve the gambas dialogs.. because fining my usb key content is a job. 

But my component need to be finished and having a non dbus part in case of terminal using. 

----[ [ http://gambaswiki.org/wiki/doc/netiquette | http://gambaswiki.org/wiki/doc/netiquette ] ]---- 





----[ http://gambaswiki.org/wiki/doc/netiquette ]---- 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20201230/5a272415/attachment-0001.htm>


More information about the User mailing list