CentOS 上的 Kickstart 文件的 %pre 部分中有哪些命令可用?

Goi*_*Off 8 linux centos kickstart

环境:CentOS 5.5 和 6.4

我要求在安装前分析硬件,以确保我们的客户不会在不合标准的服务器硬件上安装我们的软件。例如,检查内存、磁盘空间、CPU、网卡……那么,我的 ks.cfg 文件中的 %pre 部分似乎是执行此操作的最佳位置???但是,我无法获得像 free to work 这样的命令...。我想找出 %pre 部分中可用的命令,这是在安装开始前执行硬件分析的正确位置吗???。 . 如果 ks.cfg 的 %pre 部分不是执行此操作的好地方,那么在哪里?这是我迄今为止尝试过的,但没有输出:

ks.cfg:

%pre
  (echo "Analyzing Hardware...") >/dev/tty1
  free >/dev/tty1
  free_txt=`free -o`
  (echo "$free_txt") >/dev/tty1
%end
Run Code Online (Sandbox Code Playgroud)

在安装的第一部分,我在屏幕上看到“分析硬件...”,但之后什么都没有.....

wwo*_*ods 13

%prekickstart的部分在安装程序环境中运行。

以下是 RHEL6.5 安装程序环境中可用的有用命令列表:

  • 外壳工具: arch awk basename bash cat chattr chgrp chmod chown chroot clear clock consoletype cp cut date df dmesg du echo egrep env expr false fgrep find getopt grep head hwclock id kill killall killall5 less ln ls lsattr mkdir mknod mktemp mv pidof ps pwd readlink rm rmdir sed sh shred sleep sort split sync tac tail tee top touch true tty uname uniq wc which xargs
  • 编辑和寻呼机: less more vi
  • 哈希实用程序: md5sum sha1sum sha256sum
  • 压缩和存档: gzip bzip2 cpio dd tar rpm
  • fsck/mkfs的/ etc。为了ext2 ext3 ext4 xfs btrfs msdos vfat
  • 其他文件系统内容: mkswap swapon swapoff dmraid dmsetup mdadm mdmon dump restore mt lvm lvs vgs pvs ...
  • 网络实用程序: arp arping curl dhclient dhclient-script ftp ifconfig hostname ip ipcalc mtr nc ping rcp rdate rlogin telnet nslookup ntpdate route rsh rsync ssh ssh-keygen sshd scp sftp wget
  • 硬件信息: biosdevname blkdeactivate blkid blockdev dmidecode lshal lspci lsscsi sginfo smartctl
  • 磁盘实用程序: eject dump restore hdparm smartctl losetup kpartx parted fdisk sfdisk
  • 控制台处理/对话框: chvt consolehelper openvt whiptail zenity
  • 记录: logger rsyslogd syslogd
  • python
  • 还有更多!

如果您运行手动安装,您可以切换到 VT2 ( CtrlAltF2)上的终端并四处寻找安装程序环境中可用的所有内容。compgen -c | sort -u是列出每个可用命令的简单方法,并且可以在/sys和 中找到许多系统信息/proc

(是的,在%pre脚本运行后重新解析 kickstart ,因此您%pre可以编辑 kickstart 和/或生成新的 kickstart 片段以用于%include.)


Goi*_*Off 2

经过更多挖掘后,我发现大量系统信息/proc可供在执行%pre中的部分时查看ks.cfg。检查 dmidecode 和 /proc 中的文件以获取您需要的所有信息。这对我有用:

%pre --log=/tmp/ks_pre.log
  #!/bin/sh
  #----------------------------------------------
  # echos message to console screen and a log file
  #----------------------------------------------
  echo_screen_n_log() {
    msg=$1
    # Send to console screen
    (echo "$msg") >/dev/tty1
    # Send to log
    echo "$msg"
  }

  echo_screen_n_log ""
  echo_screen_n_log "Analyzing Hardware..."
  echo_screen_n_log ""

  #----------------------------------------------
  # System Memory
  #----------------------------------------------
  IFS=$'\n'
  mem_info=(`dmidecode --type memory`)
  unset IFS

  sys_mem_sizes=""
  sys_mem_banks=""
  sys_tot_mem=0
  cntr=0
  bank_cntr=0
  for i in "${mem_info[@]}"
  do
    # echo_screen_n_log "i: $i"
    # Maximum system memory that can be placed on the motherboard
    REG_EX="Maximum Capacity: (.*)$"
    if [[ $i =~ $REG_EX ]]
    then
      sys_mem_max=${BASH_REMATCH[1]} 
    fi
    # How many memory slots are on the motherboard
    REG_EX="Number Of Devices: (.*)$"
    if [[ $i =~ $REG_EX ]]
    then
      sys_mem_slots=${BASH_REMATCH[1]} 
    fi
    REG_EX="^[[:space:]]+Size: (.*)$"
    if [[ $i =~ $REG_EX ]]
    then
      sys_mem_sizes[cntr]=${BASH_REMATCH[1]}
      cntr=$(( $cntr + 1 ))
    fi
    REG_EX="^[[:space:]]+Bank Locator: (.*)$"
    if [[ $i =~ $REG_EX ]]
    then
      sys_mem_banks[bank_cntr]=${BASH_REMATCH[1]}
      bank_cntr=$(( $bank_cntr + 1 ))
    fi   
  done
  cntr=$(( $cntr - 1 ))
  echo_screen_n_log "Max system memory: $sys_mem_max"
  echo_screen_n_log "Total system slots: $sys_mem_slots"
  i=0
  while [ $i -le $cntr ]
  do
    echo_screen_n_log "Memory Bank Location ${sys_mem_banks[$i]} : ${sys_mem_sizes[$i]}"
    REG_EX="No Module Installed$"
    if [[ ! ${sys_mem_sizes[$i]} =~ $REG_EX ]]
    then
      REG_EX="^([0-9]+) [A-Z][A-Z]$"
      if [[ ${sys_mem_sizes[$i]} =~ $REG_EX ]]
      then
    sys_tot_mem=$(( $sys_tot_mem + ${BASH_REMATCH[1]} ))
      fi
    fi
    i=$(( $i + 1 ))
  done
  echo_screen_n_log "System Total Memory: $sys_tot_mem MB"

  #--------------------------------------------
  # Get Disk size information
  #--------------------------------------------
  IFS=$'\n'
  disk_info=(`cat /proc/partitions`)
  unset IFS

  total_disk_space=0
  type=""
  # Grab from minor column starting with 0 ending in 3 letters (drive node) 
  REG_EX="0\s+([0-9]+) [a-z][a-z][a-z]$"
  for i in "${disk_info[@]}"
  do
    # echo_screen_n_log "i: $i"
    if [[ $i =~ $REG_EX ]]
    then
      total_disk_space=${BASH_REMATCH[1]}
      total_disk_space=$(( $total_disk_space * 1024 ))
      type="GB"
      div_num=1000000000
      if [ "$total_disk_space" -lt $div_num ]
      then
        type="MB"
        div_num=1000000
      fi
      total_disk_space=$(( $total_disk_space / $div_num ))
    fi
  done
  echo_screen_n_log "Disk Space: $total_disk_space $type"

  #-----------------------------------------------------
  # Get CPU model name
  #-----------------------------------------------------
  cpu_grep=`grep 'model name' /proc/cpuinfo`
  cpu_model_nm="Not Found!"
  REG_EX="^.*: (.*)$"
  if [[ $cpu_grep =~ $REG_EX ]]
  then
    cpu_model_nm=${BASH_REMATCH[1]}
  fi
  echo_screen_n_log "CPU Model: $cpu_model_nm"

  #-------------------------------------------------------
  # Get number of physical CPUs
  #-------------------------------------------------------
  IFS=$'\n'
  cpu_count=(`grep "physical id" /proc/cpuinfo`)
  unset IFS

  last_cpu_id=""
  total_cpu_cnt=0
  # Add up all cores of the CPU to get total MIPS
  total_cpus=0
  REG_EX="^physical id\s+: ([0-9]+)$"
  for i in "${cpu_count[@]}"
  do
    # echo_screen_n_log "i: $i"
    if [[ $i =~ $REG_EX ]]
    then
  cpu_id=${BASH_REMATCH[1]}
      if [ ! "$last_cpu_id" = "$cpu_id" ]
      then
    total_cpu_cnt=$(( $total_cpu_cnt + 1 ))
    last_cpu_id=$cpu_id
  fi
    fi
  done
  echo_screen_n_log "System physical CPUs: $total_cpu_cnt"

  #-------------------------------------------------------
  # Get number of CPU cores
  #-------------------------------------------------------
  IFS=$'\n'
  cpu_cores=(`grep -m 1 "cpu cores" /proc/cpuinfo`)
  unset IFS

  total_cpu_cores=0
  REG_EX="^cpu cores\s+: ([0-9]+)$"
  for i in "${cpu_cores[@]}"
  do
    # echo_screen_n_log "i: $i"
    if [[ $i =~ $REG_EX ]]
    then
  total_cpu_cores=${BASH_REMATCH[1]}
    fi
  done
  echo_screen_n_log "CPU cores: $total_cpu_cores"

  #-------------------------------------------------------
  # CPU MHz
  #-------------------------------------------------------
  IFS=$'\n'
  dmi_cpu_MHz=(`dmidecode --string processor-frequency`)
  unset IFS

  cpu_MHz=0
  REG_EX="^[0-9]+ "
  for i in "${dmi_cpu_MHz[@]}"
  do
    # echo_screen_n_log "i: $i"
    if [[ $i =~ $REG_EX ]]
    then
  cpu_MHz=${BASH_REMATCH[1]}
    fi
  done
  echo_screen_n_log "CPU MHz: ${dmi_cpu_MHz:0:1}.${dmi_cpu_MHz:1:$(( ${#dmi_cpu_MHz} - 1 ))}"

  #-------------------------------------------------------
  # Get CPU bogomips (Millions of instructions per second)
  #-------------------------------------------------------
  IFS=$'\n'
  cpu_mips=(`grep "bogomips" /proc/cpuinfo`)
  unset IFS

  # Add up all cores of the CPU to get total MIPS
  total_mips=0
  REG_EX="\s([0-9]+)\..*$"
  for i in "${cpu_mips[@]}"
  do
    # echo_screen_n_log "i: $i"
    if [[ $i =~ $REG_EX ]]
    then
  cpu_bogomips=${BASH_REMATCH[1]}
      total_mips=$(( $total_mips + $cpu_bogomips ))
    fi
  done
  echo_screen_n_log "Total CPU MIPS (Millions of instructions per second) : $total_mips"

  echo_screen_n_log ""
  (echo -n "Press <enter> to continue..") >/dev/tty1
  read text
%end
Run Code Online (Sandbox Code Playgroud)

我只需要添加标准来确定我们安装的基本系统应该是什么样子,我就完成了......

使用更多信息更新了此内容...您还可以在 %pre 部分中对磁盘信息执行以下操作:

IFS=$'\n'
parted_txt=(`parted -l`)
unset IFS

for i in "${parted_txt[@]}"
do
#    (echo "i: \"$i\"") >/dev/tty1
  REG_EX="^Model: (.*)$"
  if [[ $i =~ $REG_EX ]]
  then
    disk_model=${BASH_REMATCH[1]}
#      (echo "Disk Model: \"$disk_model\"") >/dev/tty1
  fi

  REG_EX="^Disk (.*): ([0-9]+).[0-9]([A-Z][A-Z])$"
  if [[ $i =~ $REG_EX ]]
  then
    disk_device=${BASH_REMATCH[1]}
    disk_capacity=${BASH_REMATCH[2]}
    disk_capacity_type=${BASH_REMATCH[3]}
    (echo "Device: \"$disk_device\"  \"$disk_capacity\"  $disk_capacity_type") >/dev/tty1
    IFS=$'\n'
    disk_txt=(`udevadm info --query=all --name=$disk_device`)
    unset IFS
     is_USB_drive=0
    for j in "${disk_txt[@]}"
    do
      #(echo "j: \"$j\"") >/dev/tty1
   REG_EX="^ID_BUS=usb$"
   if [[ $j =~ $REG_EX ]]
       then
     # USB keys are not to be included in total disk space
       #       (echo "$disk_device is a USB drive!") >/dev/tty1
        is_USB_drive=1
   fi
    done
    if [ "$is_USB_drive" = "0" ]
    then
   total_capacity=$(( $total_capacity + $disk_capacity ))
    fi
  fi
done

(echo "Disk Model: $disk_model") >/dev/tty1
(echo "Disk $disk_device Capacity: $total_capacity $disk_capacity_type") >/dev/tty1
Run Code Online (Sandbox Code Playgroud)