Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Using basic shell tools to get mount (fs) stats
View unanswered posts
View posts from last 24 hours

Goto page Previous  1, 2  
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Zucca
Moderator
Moderator


Joined: 14 Jun 2007
Posts: 3683
Location: Rasi, Finland

PostPosted: Thu Aug 01, 2024 10:01 am    Post subject: Reply with quote

Goverp wrote:
(though anyone who includes a newline character in a mount point deserves all the problems they get). In a system with such a weird mountpoint, I expect parsing "df" output for statistics will be the least of their problems!
I couldn't agree more.
I think it's a design flaw to allow newline character in any file name. (In fact I wish Linux kernel would disallow newlines in filenames.)

Sometimes I've designed my code to simply emit an error and exit when it detects possible newline character where there shouldn't be one. While it's a bit against the philosophy of "a program should not crash because of invalid input". But at least it falls gracefully and emits an error of why it exited.

In this case where we are parsing the output of df, it think it's very much justified to error out if the process encounters less columns on a line than it expects.
The code I've written, so far, still does not expect fixed width columns, so it can cope with busybox df too.
_________________
..: Zucca :..

My gentoo installs:
init=/sbin/openrc-init
-systemd -logind -elogind seatd

Quote:
I am NaN! I am a man!
Back to top
View user's profile Send private message
sabayonino
Veteran
Veteran


Joined: 03 Jan 2012
Posts: 1035

PostPosted: Thu Aug 01, 2024 10:41 am    Post subject: Reply with quote

@zucca : look at "lsblk" comand and -o option

Quote:

Available output columns:
ALIGNMENT alignment offset
ID-LINK the shortest udev /dev/disk/by-id link name
ID udev ID (based on ID-LINK)
DISC-ALN discard alignment offset
DAX dax-capable device
DISC-GRAN discard granularity
DISK-SEQ disk sequence number
DISC-MAX discard max bytes
DISC-ZERO discard zeroes data
FSAVAIL filesystem size available
FSROOTS mounted filesystem roots
FSSIZE filesystem size
FSTYPE filesystem type
FSUSED filesystem size used
FSUSE% filesystem use percentage
FSVER filesystem version
GROUP group name
HCTL Host:Channel:Target:Lun for SCSI
HOTPLUG removable or hotplug device (usb, pcmcia, ...)
KNAME internal kernel device name
LABEL filesystem LABEL
LOG-SEC logical sector size
MAJ:MIN major:minor device number
MIN-IO minimum I/O size
MODE device node permissions
MODEL device identifier
MQ device queues
NAME device name
OPT-IO optimal I/O size
OWNER user name
PARTFLAGS partition flags
PARTLABEL partition LABEL
PARTN partition number as read from the partition table
PARTTYPE partition type code or UUID
PARTTYPENAME partition type name
PARTUUID partition UUID
PATH path to the device node
PHY-SEC physical sector size
PKNAME internal parent kernel device name
PTTYPE partition table type
PTUUID partition table identifier (usually UUID)
RA read-ahead of the device
RAND adds randomness
REV device revision
RM removable device
RO read-only device
ROTA rotational device
RQ-SIZE request queue size
SCHED I/O scheduler name
SERIAL disk serial number
SIZE size of the device
START partition start offset
STATE state of the device
SUBSYSTEMS de-duplicated chain of subsystems
MOUNTPOINT where the device is mounted
MOUNTPOINTS all locations where device is mounted
TRAN device transport type
TYPE device type
UUID filesystem UUID
VENDOR device vendor
WSAME write same max bytes
WWN unique storage identifier
ZONED zone model
ZONE-SZ zone size
ZONE-WGRAN zone write granularity
ZONE-APP zone append max bytes
ZONE-NR number of zones
ZONE-OMAX maximum number of open zones
ZONE-AMAX maximum number of active zones




Remove the blank lines using sed
Code:
lsblk -n -o MOUNTPOINT | sed '/^$/d'
/boot/efi
/mnt/bind-root
/mnt/bind-home
/home/my/fs_snapshots
/home/my/Var
/home/my//Document
/run/media/my/Backup-1
/run/media/my/Backup-4



or parse the Output in Json format using "-J" option
Code:
lsblk -J -n -A -o MOUNTPOINT,FSSIZE | sed '/^$/d'


see
Code:
lsblk -h

_________________
LRS i586 on G.Drive
LRS x86-64 EFI on MEGA


Last edited by sabayonino on Thu Aug 01, 2024 11:06 am; edited 1 time in total
Back to top
View user's profile Send private message
Zucca
Moderator
Moderator


Joined: 14 Jun 2007
Posts: 3683
Location: Rasi, Finland

PostPosted: Thu Aug 01, 2024 11:00 am    Post subject: Reply with quote

@sabayonino: Yes.
I've considered it. So far it's the best option, when it comes to "basic shell tools", but lsblk isn't in the list of busybox's commands. blkid comes close, but it can't fetch free space information from mounted filesystems.
That said... I should have used lsblk from the beginning.
_________________
..: Zucca :..

My gentoo installs:
init=/sbin/openrc-init
-systemd -logind -elogind seatd

Quote:
I am NaN! I am a man!
Back to top
View user's profile Send private message
Goverp
Advocate
Advocate


Joined: 07 Mar 2007
Posts: 2170

PostPosted: Thu Aug 01, 2024 5:43 pm    Post subject: Reply with quote

Goverp wrote:
RumpletonBongworth wrote:
...
EDIT: As an aside, be careful where writing parameter expansions of the form ${word1#$word2}. The value of word2 will be treated as a pattern in turn, which matters if globbing metacharacters are present. Wherever the intent is to have said value be treated as a literal string, the expansion may be double-quoted e.g. ${word1#"$word2"}.

Ah, but if you read the code carefully, you'll notice that the expansion is exactly what I want.

Rumpleton, my apologies you were right. I hadn't realised the problem if "$word2" looks like a shell pattern, such as "[1937]", in which case that parameter expansion deletes the last character if it is '1', '9', '3' or '7'. I wonder how many broken scripts I have?
_________________
Greybeard
Back to top
View user's profile Send private message
Goverp
Advocate
Advocate


Joined: 07 Mar 2007
Posts: 2170

PostPosted: Thu Aug 01, 2024 6:03 pm    Post subject: Re: Using basic shell tools to get mount (fs) stats Reply with quote

Back to square 1:

Zucca wrote:
...
If I loosen then requirement a bit I could have a very small C-program to get all the same info as df, but separated with tabs or (even better) NUL. Another way would be to use python for that, but then it would require more stuff since python is interpreted language.
...


How about
Code:
#!/bin/sh

doit() {
        read -r foo
        while read -r devpath type size used free perc mount
        do
                printf '%s\000%s\000%s\000%s\000%s\000%s\000%s\000\n' "$devpath" "$type" "$size" "$used" "$free" "$perc" "$mount"
        done
}

df -PTk | doit

This works in both dash and busybox shell, producing the body of the df report, delimited by nulls, and a new line after the last item; other delimiters are available :-)
One problem, bash's df gets thrown by /run/<foo>/doc, a mount point of which I care nothing, but it's unreadable, so you get a spurious output line.

Here's the output on my terminal, complete with nulls, not sure what it will do to the forum
Code:
df: /run/user/1005/doc: Permission denied
df: /run/user/1000/doc: Operation not permitted
nonedevtmpfs102400102400%/dev
/dev/nvme0n1p3f2fs2673838085959730020778650822%/
tmpfstmpfs6555500153265539680%/run
shmtmpfs16388748177516162112321%/dev/shm
efivarfsefivarfs128348928%/sys/firmware/efi/efivars
/dev/md127p2ext498317712428835928864480145231%/home
/dev/md127p1ext414307087610721357293880%/var/tmp
/dev/md127p1ext414307087610721357293880%/tmp
/dev/nvme0n1p3f2fs2673838085959730020778650822%/home/distfiles
/dev/nvme0n1p3f2fs2673838085959730020778650822%/home/binpkgs
tmpfstmpfs3277748032777480%/run/user/0
/dev/loop0squashfs79232792320100%/var/db/repos/gentoo
tmpfstmpfs32777482032777280%/run/user/1005
tmpfstmpfs32777482432777240%/run/user/1000

_________________
Greybeard
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum