I am writing an application that works like Neofetch when a -w
option is passed. It shows some of the the system information like memory, swap, cpu, battery usages, hostname, local ip, kernel version etc.
I am wondering how to get the "Host" like in Neofetch. For example:
-` sourav@archlinux-arm
.o+` --------------------
`ooo/ OS: Arch Linux armv7l
`+oooo: Host: Raspberry Pi 3 Model B Rev 1.2
`+oooooo: Kernel: 4.19.108-1-ARCH
-+oooooo+: Uptime: 10 mins
`/:-:++oooo+: Packages: 804 (pacman)
`/++++/+++++++: Shell: bash 5.0.16
`/++++++++++++++: Resolution: 1366x768
`/+++ooooooooooooo/` DE: Xfce
./ooosssso++osssssso+` WM: Xfwm4
.oossssso-````/ossssss+` WM Theme: XFCE_Colour_Lite_Pink
-osssssso. :ssssssso. Theme: XFCE_Colour_Lite_Pink [GTK2], X
:osssssss/ osssso+++. Icons: Papirus [GTK2], Tela-orange [GT
/ossssssss/ +ssssooo/- Terminal: tilix
`/ossssso+/:- -:/+osssso+- CPU: BCM2835 (4) @ 1.350GHz
`+sso+:-` `.-/+oso: Memory: 333MiB / 901MiB
`++:. `-/+/
.` `/
I get an information like this.
On my laptop:
-` sourav@archlinux
.o+` ----------------
`ooo/ OS: Arch Linux x86_64
`+oooo: Host: Inspiron 5567
`+oooooo: Kernel: 5.5.10-arch1-1
-+oooooo+: Uptime: 3 hours
`/:-:++oooo+: Packages: 1163 (pacman)
`/++++/+++++++: Shell: bash 5.0.16
`/++++++++++++++: Resolution: 1920x1080
`/+++ooooooooooooo/` DE: Xfce
./ooosssso++osssssso+` WM: Xfwm4
.oossssso-````/ossssss+` WM Theme: XFCE_Colour_Lite_Ruby
-osssssso. :ssssssso. Theme: XFCE_Colour_Lite_Purple [GTK2
:osssssss/ osssso+++. Icons: Papirus [GTK2/3]
/ossssssss/ +ssssooo/- Terminal: tilix
`/ossssso+/:- -:/+osssso+- CPU: Intel i3-6006U (4) @ 2.000GHz
`+sso+:-` `.-/+oso: GPU: Intel Skylake GT2 [HD Graphics
`++:. `-/+/ Memory: 2814MiB / 3755MiB
.` `/
My question is related to this question, but it doesn't answer my question because my raspberry pi can't run dmidecode
, (no /sys/devices/virtual/dmi/
either), no lshw
installed. Also, the /etc/hostname
are not the computers' model name, instead they are just archlinux-arm and archlinux. The uname -a
or cat /proc/version
doesn't have the 'Rapsberry Pi' string on the raspberry pi.
Is there a way to get the hardware name like neofetch without using any dependency which should also run on most hardware?
Best Answer
There is no a portable, reliable, generic method to retrieve hardware model name in Linux. Let me describe 2 different cases: ARM-based Raspberry Pi with Raspbian installed and MIPS-based TP-LINK router with OpenWRT installed.
Raspberry Pi has an ARM CPU and ARM devices commonly use device-tree to describe hardware and Wikipedia article even mentions that it's mandatory since 2012. Device-tree structure is exposed to userspace and can be used for retrieving a model name by
cat
ing/proc/device-tree/model
where/proc/device-tree
itself is a symlink to/sys/firmware/devicetree/base
like that (notice that there is no newline at the end of the device-tree files so we create a helper function calledcatn
that cats the file and adds a newline):or by manually dumping
/sys/firmware/fdt
flattened device-tree blob with dtc:If an official Raspberry Pi Linux fork is in use model is also written to /proc/cpuinfo:
Also notice that the full name of the board -
Raspberry Pi 3 Model B Rev 1.2
is constructed by low-level firmware and that you will not find a full string like that anywhere in the Linux kernel code:model
is a standard device-tree property described in the DTSpec.Other architectures such as RISC-V also use device tree to describe hardware but I don't have any RISC-V board to check.
There is no /proc/device-tree, no /sys/firmware/devicetree/base and no /sys/firmware/fdt on my TP-LINK router - it means that it either doesn't come with device-tree at all or that some appropriate Linux kernel config options have been disabled and device-tree is not exposed to the userspace. The former is, however, more likely as there is /tmp/sysinfo instead:
These values are generated by ar71xx.sh script which is rather long but you can see that
name
is assigned in line 1313:based on
TL-WDR4900 v2
which is in turn taken frommachine
field from /proc/cpuinfo:and is then assigned to
AR71XX_BOARD_NAME
and written to/tmp/sysinfo/board_name
by the end of the script.The full value of
machine
field in /proc/cpuinfo on this router is:But Neofetch is not looking for /tmp/sysinfo/board_name, it's looking for /tmp/sysinfo/model. It's not taken from /proc/cpuinfo but read from the
firmware
flash partition:Model is assigned in line 321:
Of course it's hard to imagine that a generic program such as Neofetch would have so much knowledge about each firmware, its flash layout etc. I could however imagine a MIPS-based implementation that wouldn't support device-tree and wouldn't provide any useful hardware model information in /tmp/sysinfo and anywhere else and in such cases /proc/cpuinfo could be used as a last resort to get any information about hardware.