lshw学习——简单介绍

news2025/10/28 3:58:23

文章目录

    • 简介
    • 核心结构
    • 扫描设备原理
      • 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 information

options 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.coloradmin.cn/o/2263536.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C# opencvsharp 流程化-脚本化-(2)ROI

ROI ROI也是经常需要使用的方法。特别是在图像编辑中。ROI又称感兴趣的区域&#xff0c;但是图像是矩阵是矩形的&#xff0c;感兴趣的是乱八七糟的&#xff0c;所以还有一个Mask需要了解一下的。 public class RoiStep : IImageProcessingStep{public ImageProcessingStepType…

wazuh-modules-sca-scan

sca模块主函数wm_sca_main -> wm_sca_start 检查policy文件中的每一个项目wm_sca_check_policy static int wm_sca_check_policy(const cJSON * const policy, const cJSON * const checks, OSHash *global_check_list) {if(!policy) {return 1;}const cJSON * const id c…

uniapp 自定义图标03

插入工程&#xff0c;修改名称文件内容 编译运行

在Windows本地用网页查看编辑服务器上的 jupyter notebook

​ Motivation: jupyter notebook 可以存中间变量&#xff0c;方便我调整代码&#xff0c;但是怎么用服务器的GPU并在网页上查看编辑呢&#xff1f; 参考 https://zhuanlan.zhihu.com/p/440080687 服务端(Ubuntu)&#xff1a; 激活环境 source activate my_env安装notebook …

【YOLO 项目实战】(11)YOLO8 数据集与模型训练

欢迎关注『youcans动手学模型』系列 本专栏内容和资源同步到 GitHub/youcans 【YOLO 项目实战】&#xff08;1&#xff09;YOLO5 环境配置与检测 【YOLO 项目实战】&#xff08;10&#xff09;YOLO8 环境配置与推理检测 【YOLO 项目实战】&#xff08;11&#xff09;YOLO8 数据…

Ubuntu22.04上安装esp-idf

一、安装准备# 建议使用Ubuntu 20.04 或 Ubuntu 22.04 操作系统 为了在 Ubuntu 22.04 中使用 esp-idf&#xff0c;需要安装一些依赖包 sudo apt-get install git wget flex bison gperf python3\python3-pip python3-venv cmake ninja-build ccache\libffi-dev libssl-dev dfu…

nginx-虚拟主机配置笔记

目录 nginx的安装可以查看nginx安装https://blog.csdn.net/m0_68472908/article/details/144609023?spm1001.2014.3001.5501 一、 基于域名 二、 基于IP 三、 基于端口 nginx的安装可以查看nginx安装https://blog.csdn.net/m0_68472908/article/details/144609023?spm100…

AlipayHK支付宝HK接入-商户收款(PHP)

一打开支付宝国际版 二、点开商户服务 三、下载源码

Soul Android端稳定性背后的那些事

前言&#xff1a;移动应用的稳定性对于用户体验和产品商业价值都有着至关重要的作用。应用崩溃会导致关键业务中断、用户留存率下降、品牌口碑变差、生命周期价值下降等影响&#xff0c;甚至会导致用户流失。因此&#xff0c;稳定性是APP质量构建体系中最基本和最关键的一环。当…

深度学习模型 DeepSeek-VL2 及其消费级显卡需求分析

DeepSeek-VL2 是由 DeepSeek 团队开发的一款先进的视觉语言模型&#xff0c;采用了混合专家&#xff08;MoE&#xff09;架构&#xff0c;旨在提升多模态理解能力。该模型包括三个版本&#xff1a;DeepSeek-VL2-Tiny、DeepSeek-VL2-Small 和 DeepSeek-VL2。每个版本具有不同的模…

首批|云轴科技ZStack成为开放智算产业联盟首批会员单位

近日 &#xff0c;在Linux基金会AI & Data及中国开源软件推进联盟的指导之下&#xff0c;开放智算产业联盟成立大会在北京成功召开。在大会上&#xff0c;联盟首次公布了组织架构并颁发了首批会员单位证书。凭借ZStack AIOS平台智塔和在智算领域的技术创新&#xff0c;云轴…

word实现两栏格式公式居中,编号右对齐

1、确定分栏的宽度 选定一段文字 点击分栏&#xff1a;如本文的宽度为22.08字符 2、将公式设置为 两端对齐&#xff0c;首行无缩进。 将光标放在 公式前面 点击 格式-->段落-->制表位 在“制表位位置”输入-->11.04字符&#xff08;22.08/211.04字符&#xff09;&…

go语言zero框架中config读取不到.env文件问题排查与解决方案

在Go语言中&#xff0c;如果你使用.env文件来存储环境变量&#xff0c;通常会用到一些第三方库&#xff0c;例如github.com/joho/godotenv&#xff0c;它可以帮助我们从.env文件中读取环境变量。然而&#xff0c;在使用godotenv时&#xff0c;可能会遇到一些问题&#xff0c;导…

修改vscode设置的原理

转载请标明出处&#xff1a;小帆的帆的专栏 修改vscode设置 首先需要理解的是&#xff0c;vscode的系统设置和插件设置都是通过settings.json文件管理的。 vscode中有三个Settings&#xff0c;三个Settings分别对应三个settings.json文件 Default Settings&#xff1a;默认…

Qt之修改窗口标题、图标以及自定义标题栏(九)

Qt开发 系列文章 - titles-icons-titlebars&#xff08;九&#xff09; 目录 前言 一、修改标题 二、添加图标 三、更换标题栏 1.效果演示 2.创建标题栏类 3.定义相关函数 4.使用标题栏类 总结 前言 在我们利用Qt设计软件时&#xff0c;经常需要修改窗口标题、更改软…

环境变量的知识

目录 1. 环境变量的概念 2. 命令行参数 2.1 2.2 创建 code.c 文件 2.3 对比 ./code 执行和直接 code 执行 2.4 怎么可以不带 ./ 2.4.1 把我们的二进制文件拷贝到 usr/bin 路径下&#xff0c;也不用带 ./ 了 2.4.2 把我们自己的路径添加到环境变量里 3. 认识PATH 3.…

【时时三省】(C语言基础)通讯录1

山不在高&#xff0c;有仙则名。水不在深&#xff0c;有龙则灵。 ----CSDN 时时三省 通讯录 1 .通讯录中能够存放1000个人的信息 每个人的信息&#xff1a; 名字年龄性别电话地址 2、增加人的信息 3、删除指定人的信息 4、修改指定人的信息 5&#xff0c;查找指定人的信…

Vulhub:Redis[漏洞复现]

4-unacc(Redis未授权代码执行) 启动漏洞环境 docker-compose up -d 阅读vulhub给出的漏洞文档 cat README.zh-cn.md # Redis 4.x/5.x 主从复制导致的命令执行 Redis是著名的开源Key-Value数据库&#xff0c;其具备在沙箱中执行Lua脚本的能力。 Redis未授权访问在4.x/5.0.5以…

【PGCCC】Postgresql Varlena 结构

前言 postgresql 会有一些变长的数据类型&#xff0c;存储都是采用 varlena 格式的&#xff08;除了 cstring 类型&#xff09;&#xff0c;通过语句 SELECT typname FROM pg_type WHERE typlen -1就可以看到所有采用 varlena 格式的数据类型&#xff0c;比如常见的 text &am…

Ubuntu搭建ES8集群+加密通讯+https访问

目录 写在前面 一、前期准备 1. 创建用户和用户组 2. 修改limits.conf文件 3. 关闭操作系统swap功能 4. 调整mmap上限 二、安装ES 1.下载ES 2.配置集群间安全访问证书密钥 3.配置elasticsearch.yml 4.修改jvm.options 5.启动ES服务 6.修改密码 7.启用外部ht…