Portál AbcLinuxu, 30. dubna 2025 14:32
#!/bin/bash echo "hdt.sh v0.1 - bash script displays the temperature of hd/nvme disks" echo " " echo "--help show usage of script" echo " " get_disk_temperature() { local disk_path=$1 local temperature=$(smartctl -a "$disk_path" 2>/dev/null | awk '/Temperature_Celsius/ { print $10 }') if [[ -n $temperature ]]; then echo "$temperature°C" else echo "Disk temperature is not available." fi } display_usage() { echo "Usage: $0 [disk_name]" echo "disk_name (optional): Name of the disk to display the temperature." echo "If not provided, temperatures for all eligible disks will be displayed." echo "Package smartmontools is mandatory." } main() { if [[ $1 == "--help" ]]; then display_usage exit 0 fi if [[ -n $1 ]]; then local disk_path="/dev/$1" if [[ -e $disk_path ]]; then get_disk_temperature "$disk_path" else echo "Error: Disk $1 does not exist." exit 1 fi else local disks_output=$(lsblk -d -n -o NAME) for disk in ${disks_output[@]}; do if [[ $disk == sd* || $disk == nvme* ]]; then local disk_path="/dev/$disk" echo "Disk temperature for $disk: $(get_disk_temperature "$disk_path")" fi done fi } main "$@"
Bez roota to nepôjde. Spustil som strace
s rootom a bez. Podľa toho čo píše strace tak to spadne na prístupových právachk danému zariadeniu sdX.
openat(AT_FDCWD, "/dev/sda", O_RDONLY|O_NONBLOCK) = 3
ioctl(3, SG_IO, {interface_id='S', dxfer_direction=SG_DXFER_FROM_DEV, cmd_len=6,
cmdp="\x12\x00\x00\x00\x24\x00", mx_sb_len=32, iovec_count=0, dxfer_len=36, tim
eout=60000, flags=0, dxferp="\x00\x00\x05\x02\x5b\x00\x00\x02\x41\x54\x41\x20\x20\x20\x20\x20\x57\x44\x43\x20\x57\x44\x35\x30\x30\x33\x41\x5a\x45\x58\x2d\x30"..., status=0, masked_status=0, msg_status=0, sb_len_wr=0, sbp="", host_status=0, driver_status=0, resid=0, duration=0, info=0}) = 0
ioctl(3, SG_IO, {interface_id='S', dxfer_direction=SG_DXFER_FROM_DEV, cmd_len=16, cmdp="\x85\x08\x0e\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\xec\x00", mx_sb_len=32, iovec_count=0, dxfer_len=512, timeout=60000, flags=0, dxferp="\x7a\x42\xff\x3f\x37\xc8\x10\x00\x00\x00\x00\x00\x3f\x00\x00\x00\x00\x00\x00\x00\x20\x20\x20\x20\x57\x20\x2d\x44\x43\x57\x36\x43"..., status=0, masked_status=0, msg_status=0, sb_len_wr=0, sbp="", host_status=0, driver_status=0, resid=0, duration=0, info=0}) = 0
openat(AT_FDCWD, "/dev/sda", O_RDONLY|O_NONBLOCK) = -1 EACCES (Permission denied)
write(1, "Smartctl open device: /dev/sda f"..., 57) = 57
exit_group(2) = ?
+++ exited with 2 +++
CAP_FOWNER,CAP_RAWIO
. Lenže tieto povolenia sú všeobecne a zatial neviem o tom, že existuje ako vyriešiť riadené prístupu. Na bezpečný prístup k daným funkciam by musela byť aplikácia prepísaná. Čo asi nebude a zrejme bude nutné doprogramovať nejaký výstup cez filesystém sysfs
.
Napríklad teploty, napätia a frekvenicie sú v /sys/class/hwmon/
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.