lshw学习——简单介绍

embedded/2024/12/21 21:46:49/

文章目录

    • 简介
    • 核心结构
    • 扫描设备原理
      • scan_abi
      • scan_burner
      • scan_cdrom
      • scan_cpufreq
      • scan_cpuid
      • scan_cpuinfo
      • scan_device_tree
      • scan_disk
      • scan_display
      • scan_dmi
      • scan_fat
      • scan_fb
      • scan_graphics
      • scan_ide
      • scan_ideraid
      • scan_input
      • scan_isapnp
      • scan_lvm
      • scan_memory
      • scan_mmc
      • scan_mounts
      • scan_network
      • scan_nvme
      • scan_parisc
      • scan_partitions
      • scan_pci、scan_pci_legacy
      • scan_pcmcialegacy
      • scan_pcmcia
      • scan_pnp
      • scan_s390_devices
      • scan_scsi
      • scan_smp
      • scan_sound
      • scan_spd
      • scan_sysfs
      • scan_usb
      • scan_vio
      • scan_virtio
      • scan_volume
    • 总结
    • 参考

简介

lshw: HardWare LiSter for Linux 一般用来查看linux设备的硬件信息,包括内存、cpu、主板等信息。

$ lshw --help
Hardware Lister (lshw) - 
usage: lshw [-format] [-options ...]lshw -version-version        print program version ()format can be-html           output hardware tree as HTML-xml            output hardware tree as XML-json           output hardware tree as a JSON object-short          output hardware paths-businfo        output bus informationoptions can be-class CLASS    only show a certain class of hardware-C CLASS        same as '-class CLASS'-c CLASS        same as '-class CLASS'-disable TEST   disable a test (like pci, isapnp, cpuid, etc. )-enable TEST    enable a test (like pci, isapnp, cpuid, etc. )-quiet          don't display status-sanitize       sanitize output (remove sensitive information like serial numbers, etc.)-numeric        output numeric IDs (for PCI, USB, etc.)-notime         exclude volatile attributes (timestamps) from output

代码:https://ezix.org/src/pkg/lshw.git

核心结构

class hwNode
{public:hwNode(const string & id,hw::hwClass c = hw::generic,const string & vendor = "",const string & product = "",const string & version = "");hwNode(const hwNode & o);~hwNode();hwNode & operator =(const hwNode & o);string getId() const;void setHandle(const string & handle);string getHandle() const;bool enabled() const;bool disabled() const;void enable();void disable();bool claimed() const;void claim(bool claimchildren=false);void unclaim();hw::hwClass getClass() const;const char * getClassName() const;void setClass(hw::hwClass c);string getDescription() const;void setDescription(const string & description);string getVendor() const;void setVendor(const string & vendor);string getSubVendor() const;void setSubVendor(const string & subvendor);string getProduct() const;void setProduct(const string & product);string getSubProduct() const;void setSubProduct(const string & subproduct);string getVersion() const;void setVersion(const string & version);string getDate() const;void setDate(const string &);string getSerial() const;void setSerial(const string & serial);unsigned long long getStart() const;void setStart(unsigned long long start);unsigned long long getSize() const;void setSize(unsigned long long size);unsigned long long getCapacity() const;void setCapacity(unsigned long long capacity);unsigned long long getClock() const;void setClock(unsigned long long clock);unsigned int getWidth() const;void setWidth(unsigned int width);string getSlot() const;void setSlot(const string & slot);string getModalias() const;void setModalias(const string & modalias);unsigned int countChildren(hw::hwClass c = hw::generic) const;hwNode * getChild(unsigned int);hwNode * getChildByPhysId(long);hwNode * getChildByPhysId(const string &);hwNode * getChild(const string & id);hwNode * findChildByHandle(const string & handle);hwNode * findChildByLogicalName(const string & handle);hwNode * findChildByBusInfo(const string & businfo);hwNode * findChildByResource(const hw::resource &);hwNode * findChild(bool(*matchfunction)(const hwNode &));hwNode * addChild(const hwNode & node);bool isBus() const{return countChildren()>0;}bool isCapable(const string & feature) const;void addCapability(const string & feature, const string & description = "");void describeCapability(const string & feature, const string & description);string getCapabilities() const;vector<string> getCapabilitiesList() const;string getCapabilityDescription(const string & feature) const;void attractHandle(const string & handle);void setConfig(const string & key, const string & value);void setConfig(const string & key, unsigned long long value);string getConfig(const string & key) const;vector<string> getConfigKeys() const;vector<string> getConfigValues(const string & separator = "") const;vector<string> getLogicalNames() const;string getLogicalName() const;void setLogicalName(const string &);string getDev() const;void setDev(const string &);string getBusInfo() const;void setBusInfo(const string &);string getPhysId() const;void setPhysId(long);void setPhysId(unsigned, unsigned);void setPhysId(unsigned, unsigned, unsigned);void setPhysId(const string &);void assignPhysIds();void addResource(const hw::resource &);bool usesResource(const hw::resource &) const;vector<string> getResources(const string & separator = "") const;void addHint(const string &, const hw::value &);hw::value getHint(const string &) const;vector<string> getHints() const;void merge(const hwNode & node);void fixInconsistencies();string asXML(unsigned level = 0);string asJSON(unsigned level = 0);string asString();bool dump(const string & filename, bool recurse = true);private:void setId(const string & id);bool attractsHandle(const string & handle) const;bool attractsNode(const hwNode & node) const;struct hwNode_i * This;
};

扫描设备原理

大部分都是通过读取/proc/sys/dev虚拟文件系统下面的文件内容获取系统运行状态和设备信息。
下面就依次分析lshw源码中core文件夹下面提供的函数:

scan_abi

/proc/sys/abi,含义参考https://www.kernel.org/doc/Documentation/admin-guide/sysctl/abi.rst

scan_burner

ioctl执行scsi命令

scan_cdrom

核心代码:

ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
ioctl(fd, CDROM_GET_CAPABILITY);
ioctl(fd, CDROM_DRIVE_STATUS, 0)

scan_cpufreq

/sys/devices/system/cpu/cpu%d/cpufreq/,下面有cpu频率信息

ls /sys/devices/system/cpu/cpu0/cpufreq/
affected_cpus     cpuinfo_min_freq                          energy_performance_preference  scaling_cur_freq  scaling_max_freq
base_frequency    cpuinfo_transition_latency                related_cpus                   scaling_driver    scaling_min_freq
cpuinfo_max_freq  energy_performance_available_preferences  scaling_available_governors    scaling_governor  scaling_setspeed

scan_cpuid

scan_cpuinfo

/proc/cpuinfo,从该文件获取cpu信息。

scan_device_tree

/proc/device-tree,从该路径下获取信息

scan_disk

核心代码:

long size = 0;
unsigned long long bytes = 0;
int sectsize = 0;
int physsectsize = 0;
int fd = open(disk_path, O_RDONLY | O_NONBLOCK);
ioctl(fd, BLKPBSZGET, &physsectsize)
ioctl(fd, BLKSSZGET, &sectsize)
ioctl(fd, BLKGETSIZE64, &bytes)
(ioctl(fd, BLKGETSIZE, &size)

scan_display

scan_dmi

/sys/firmware/dmi/tables,扫描该路径下面信息。

$ ls /sys/firmware/dmi/tables
DMI  smbios_entry_point

还会尝试结合/dev/mem/sys/firmware/efi/systab/proc/efi/systab的内容分析。

scan_fat

读取裸设备块数据,随后解析FAT文件系统格式。

scan_fb

/proc/devices中获取fb设备号,随后获取到framebuffer设备文件描述符fb,核心代码:

#define FBIOGET_VSCREENINFO     0x4600
#define FBIOGET_FSCREENINFO     0x4602
ioctl(fd[i], FBIOGET_FSCREENINFO, &fbi);
ioctl(fd[i], FBIOGET_VSCREENINFO, &fbconfig);

scan_graphics

/sys/class/graphics,扫描该路径下面的设备信息。

ls /sys/class/graphics/fb0/
bits_per_pixel  bl_curve  cursor  device  modes  pan    rotate  stride     uevent
blank           console   dev     mode    name   power  state   subsystem  virtual_size

scan_ide

/proc/ide,扫描该路径下设备的信息。

scan_ideraid

扫描ideraid设备,核心还是通过ioctl来直接获取设备信息。

scan_input

/sys/class/input,扫描该路径下面设备信息。

$ ls /sys/class/input/
event0  event2  event4  input0  input3  input5  js0   mouse0  mouse2
event1  event3  event5  input1  input4  input6  mice  mouse1

scan_isapnp

scan_lvm

通过读取裸设备文件,通过解析LVM2 structures获取lvm信息。

scan_memory

核心代码

pagesize = sysconf(_SC_PAGESIZE);
physpages = sysconf(_SC_PHYS_PAGES);
stat("/proc/kcore", &buf)

读取/sys/bus/memory/sys/devices/system/memory/

$ ls /sys/bus/memory/
devices  drivers  drivers_autoprobe  drivers_probe  uevent
$ ls /sys/devices/system/memory/
auto_online_blocks  memory1   memory13  memory3   memory35  memory39  memory42  memory46  memory7  probe
block_size_bytes    memory10  memory14  memory32  memory36  memory4   memory43  memory47  memory8  soft_offline_page
hard_offline_page   memory11  memory15  memory33  memory37  memory40  memory44  memory5   memory9  uevent
memory0             memory12  memory2   memory34  memory38  memory41  memory45  memory6   power

scan_mmc

/sys/class/mmc_host/

scan_mounts

/proc/mounts读取挂载信息。

scan_network

通过扫描/proc/net/dev/sys/class/net获取网络设备,之后通过ioctl获取设备信息。

scan_nvme

扫描/sys/class/nvme获取nvme设备,之后再扫描磁盘。

scan_parisc

/sys/devices/parisc

scan_partitions

扫描磁盘分区

scan_pci、scan_pci_legacy

扫描/proc/bus/pci/sys/bus/pci获取pci设备,随后读取设备裸数据来获取信息。

scan_pcmcialegacy

scan_pcmcia

scan_pnp

/sys/bus/pnp

scan_s390_devices

scan_scsi

扫描scsi设备,/proc/scsi/sys/class/scsi_*

scan_smp

/sys/devices/system/cpu/online

scan_sound

/sys/class/sound

scan_spd

/proc/sys/dev/sensors

scan_sysfs

scan_usb

/proc/bus/usb/devices/sys/kernel/debug/usb/devices

scan_vio

/sys/bus/vio

scan_virtio

/sys/bus/virtio

scan_volume

总结

lshw源码用到一下几点

  1. linux内核文件系统
  2. 系统接口如ioctlstat
  3. 操作系统概念,比如文件系统结构、网络、总线
  4. 工程上抽象出统一的设备结构
    以上只是对lshw的初步分析,进一步的理解还是需要直接阅读代码。

参考

Hardware Lister (lshw)
linux内核文件系统:proc、tmpfs、devfs、sysfs简要介绍
linux下 /proc 和 /sys 详解
Linux kernel简介
proc/sys目录介绍
Documentation for /proc/sys/abi/
The Linux Kernel documentation


http://www.ppmy.cn/embedded/147633.html

相关文章

双剑合璧:转录组+宏基因组揭示微生物与宿主互作机制

微生物、宿主以及环境三者之间编织着一张错综复杂的生命网络&#xff0c;这张网的解析需要跨学科、多层面的信息协同。近日&#xff0c;《Lancet eBioMedicine》期刊上的一篇研究&#xff0c;采用多组学分析的独特视角&#xff0c;将皮肤微生物组、转录组等数据融会贯通&#x…

docker 安装 mongo 命令

## 拉取 MongoDB 镜像docker pull mongo:latest## 挂载文件目录&#xff1a;mkdir -p /usr/local/mongo/configmkdir -p /usr/local/mongo/datamkdir -p /usr/local/mongo/logstouch /usr/local/mongo/config/mongod.confchmod 777 /usr/local/mongo## 配置文件##vim …

网络视频监控平台/安防监控/视频综合管理Liveweb视频汇聚平台解决方案

一、当前现状分析 当前视频资源面临以下问题&#xff1a; 1&#xff09;不同单位在视频平台建设中以所属领域为单位&#xff0c;设备品牌众多&#xff0c;存在的标准不一&#xff0c;各系统之间也没有统一标准&#xff1b; 2&#xff09;各单位视频平台建设分散、统筹性差&am…

基于字节大模型的论文翻译(含免费源码)

基于字节大模型的论文翻译 源代码&#xff1a; &#x1f44f; star ✨ https://github.com/boots-coder/LLM-application 展示 项目简介 本项目是一个基于大语言模型&#xff08;Large Language Model, LLM&#xff09;的论文阅读与翻译辅助工具。它通过用户界面&#xff08…

【Mars3d】设置backgroundImage、map.scene.skyBox、backgroundImage来回切换

相关链接&#xff1a; http://mars3d.cn/editor-vue.html?keyex_1_2_1&idmap/other/backgroundImg 实现代码&#xff1a; export function show1() {map.setOptions({scene: {backgroundType: "image",backgroundImage: "url(//data.mars3d.cn/img/busin…

计算机视觉学习证书为智能化工作做贡献

在当今数字化与智能化浪潮汹涌澎湃的时代&#xff0c;计算机视觉技术作为人工智能领域的关键分支&#xff0c;正以前所未有的速度改变着我们的生活与工作方式。工信部计算机视觉证书&#xff0c;便是在这一背景下应运而生的权威认证&#xff0c;为广大技术爱好者与从业者开启了…

oracle常用语句

目录 一.连接数据库SQL*Plus 连接本地连接远程连接使用操作系统认证 二.管理数据库启动数据库关闭数据库 三.数据字典和视图视图&#xff1a;1.数据库基础信息视图V$DATABASEV$INSTANCEV$VERSION 2.用户和权限管理视图DBA_USERSDBA_ROLE_PRIVSDBA_SYS_PRIVSDBA_TAB_PRIVS 3.表空…

Unbuntu下怎么生成SSL自签证书?

环境&#xff1a; WSL2 Unbuntu 22.04 问题描述&#xff1a; Unbuntu下怎么生成SSL自签证书&#xff1f; 解决方案&#xff1a; 生成自签名SSL证书可以使用OpenSSL工具&#xff0c;这是一个广泛使用的命令行工具&#xff0c;用于创建和管理SSL/TLS证书。以下是生成自签名…