ironic-inspector硬件信息收集

主机上报

ironic-inspector流程会在小系统里收集裸机的硬件信息,然后上报到ironic-conductor。
其中收集硬件信息主要使用hwinfo和lshw命令。Centos可以使用如下命令安装:

yum -y install lshw
yum -y install hwinfo

cpu核心数

function cpu_cores() {                                                                                                                                       
  hwinfo --cpu | grep -c "Hardware Class: cpu"
}

磁盘大小

function disk() {
  # XXX: This is returning only the first disk discovered, which is very likely to be insufficient on machines that present us with multiple disks
  # XXX: This is likely reporting in TB, but the units are not guaranteed. Maybe convert to bytes?
  lshw -C disk | grep size | awk -F'(' '{ print $2 }' | tr -d ')' | head -1
}

从上面的代码可以看出在存在多块硬盘的时候,上报的是第一块硬盘。
如果环境做过raid,看到的是raid之后的盘。
lshw -C disk看到的具体内容如下:

[root@64ef4b1c9859459da8f0387fc880b590 home]# lshw -C disk 
  *-disk                  
       description: SCSI Disk
       product: ServeRAID M5210
       vendor: IBM
       physical id: 2.0.0
       bus info: scsi@0:2.0.0
       logical name: /dev/sda
       version: 4.62
       serial: 00643eae284ef5a51f3023ed0bb00506
       size: 557GiB (598GB)
       capabilities: partitioned partitioned:dos
       configuration: ansiversion=5 logicalsectorsize=512 sectorsize=512 signature=0009c5fd
  *-cdrom
       description: DVD-RAM writer
       product: DVD-RW DU8A6SH
       vendor: PLDS
       physical id: 0.0.0
       bus info: scsi@1:0.0.0
       logical name: /dev/cdrom
       logical name: /dev/sr0
       version: DL64
       capabilities: removable audio cd-r cd-rw dvd dvd-r dvd-ram
       configuration: ansiversion=5 status=nodisc

内存大小

function ram() {
  # XXX: /proc may not be the best place to get this from, but hwinfo reports weird values (e.g. "1GB + 512MB" on a test VM of mine)
  _KB=$(grep MemTotal /proc/meminfo | awk '{ print $2 }')
  echo "$((_KB * 1024)) bytes"
}

这里内存信息的获取也是使用的/proc/meminfo。但是这一点与我们在
ironic node show $NODE_ID里看到的还是有差异,在社区文档上
有这么一段解释:

RAM information: total (total size in bytes), physical_mb
(physically installed memory size in MiB, optional).

Note The difference is that the latter includes the memory region reserved by
the kernel and is always slightly bigger. It also matches what the Nova
flavor would contain for this node and thus is used by the inspection 
process instead of total.

翻译过来就是我们最终看到的内存是kernel预留的 + /proc/meminfo看到的。
reserve_memory可以通过dmesg |grep reserve来查看,具体如下:

[    0.000000] Memory: 228249932k/236978176k available (6449k kernel code, 2680172k absent, 6048072k reserved, 4273k data, 1624k init)

raw disk

function raw_network() {
  hwinfo --network
}

raw network

function raw_network() {
  hwinfo --network
}

pxe mac

function pxe_mac() {
  local bootif_re='BOOTIF=([^ ]+)' _mac
  if [[ $(cat /proc/cmdline) =~ $bootif_re ]]; then
      # If we were booted using pxelinux and its config file has the
      # IPAPPEND 2 stanza under the entry we booted from, then pxelinux
      # will have appended a BOOTIF argument to the kernel parameters telling
      # us what MAC address we are booting with.  From that, we can derive the
      # boot interface with no problems.
      _mac="${BASH_REMATCH[1]//-/:}"
      _mac="${_mac#*:}"
  elif [[ -d /sys/firmware/efi ]] && which efibootmgr &>/dev/null; then
      # Likewise, if we booted via the network while running in UEFI mode, and
      # efibootmgr is installed, we can determine the MAC address of the nic we
      # booted from.  It would be good to have code that can also do this using
      # efivars or parsing the stuff under /sys/firmware/efi/efivars directly,
      # but that is a trickier thing to do.
      local -A boot_entries
      local bootent_re='^Boot([0-9]{4})'
      local efimac_re='MAC\(([0-9a-f]+)'
      local k v current_bootent
      while read line; do
          k="${line%% *}"
          v="${line#* }"
          if [[ $k = BootCurrent:* ]]; then
              current_bootent="${line##BootCurrent: }"
          elif [[ $k =~ $bootent_re ]]; then
              boot_entries["${BASH_REMATCH[1]}"]="$v"
          fi
      done < <(efibootmgr -v)

      if [[ ${boot_entries["$current_bootent"]} =~ $efimac_re ]]; then
          _mac=''
          for o in 0 2 4 6 8 10; do
              _mac+="${BASH_REMATCH[1]:$o:2}:"
          done
          _mac=${_mac%:}
      fi
  fi
  if [[ ! $_mac ]]; then
      # If none of the exact methods worked, fall back on the heuristic
      # method and just return the mac addresses of all the interfaces
      # that have a link.  Hopefully whatever consumes this info is smarter
      # than we are.
      local _info1 _info2 _dev
      _info1=$(hwinfo --network|grep -B2 "Link detected: yes"|grep -C1 "HW Address:")
      _info2=$(echo "${_info1}"|awk '/Device File: (vlan*|br*)/{for(x=NR-2;x<=NR+2;x++)d[x];}{a[NR]=$0}END{for(i=1;i<=NR;i++)if(!(i in d))print a[i]}')
      _dev=$(echo "${_info1}" | grep "Device File:"|awk -F':' {'print $2'}|tr -d ' ')
      _mac=$(echo "${_info2}" | grep "HW Address:"|awk -F'ss:' {'print $2'}|tr -d ' ')
  fi
  echo $_mac
  export HW_DISCOVERY_BOOT_IFACE="$_mac"
}

posted @ 2016-12-06 18:33  leesea  阅读(710)  评论(0编辑  收藏  举报