Determine disk type for clearpart on kickstarts

Latest response

I would like to develop logic in my kickstart to determine whether or not there is an sda or nvme0n1 drive and then clearpart that disk. So far, I have tried the solutions here:

https://www.redhat.com/archives/kickstart-list/2012-October/msg00014.html
https://gist.github.com/oogali/7629503#file-root-disk-selection-ks

But, they fail saying the drive cannot be located (it is an sda drive on my test machine). I have also tried just using the command "clearpart --drives=sda,nvme0n1 --Linux" and it fails saying it could not locate the drive.
Any assistance is appreciated. I am using RHEL 6.9, Satellite 5.7.

Responses

Roy,

I would have taken the approach used in your first link to generate a kickstart snippet in %pre and the include it in the later part of the kickstart.

Which part of this logic didn't work in your scenario?

The error I get is "Specified nonexistent disk in clearpart command."

The relevant portion of the kickstart is here:

# Kickstart config file generated by Red Hat Satellite Config Management
# Profile Label : ROYTEST
# Date Created  : 2017-06-12 15:44:24.284791

install
text
network --bootproto dhcp 
url --url http://XXXXXXXXXXXXXXXXXXXXX/ks/dist/ks-rhel-x86_64-workstation-6-6.8
lang en_US
keyboard us
zerombr
bootloader --location mbr
timezone America/New_York
auth --enablesssd --enablesssdauth --enablemkhomedir --updateall
rootpw --iscrypted XXXXXXXXXXXXXXXXXXXX
selinux --permissive
reboot
firewall --disabled
repo --name=Workstation --baseurl=http://XXXXXXXXXXXXXXXXXXXXXXXXXXX/ks/dist/ks-rhel-x86_64-workstation-6-6.8/Workstation
graphical
repo --name=rhn-tools-rhel-x86_64-workstation-6 --baseurl=http://XXXXXXXXXXXXXXXXXXXXXXXXXXXXX/ks/dist/child/rhn-tools-rhel-x86_64-workstation-6/ks-rhel-x86_64-workstation-6-6.8
repo --name=rhel-x86_64-workstation-optional-6 --baseurl=http://XXXXXXXXXXXXXXXX/ks/dist/child/rhel-x86_64-workstation-optional-6/ks-rhel-x86_64-workstation-6-6.8
%include /tmp/part-include

---------------MORE KICKSTART STUFF-----------------

%pre
# pre section
#----- partitioning logic below--------------
# pick the first drive that is not removable and is over MINSIZE
DIR="/sys/block"

# minimum size of hard drive needed specified in GIGABYTES
MINSIZE=60

ROOTDRIVE=""

# /sys/block/*/size is in 512 byte chunks

for DEV in sda nvme0n1; do
  if [ -d $DIR/$DEV ]; then
    REMOVABLE=`cat $DIR/$DEV/removable`
    if (( $REMOVABLE == 0 )); then
      echo $DEV
      SIZE=`cat $DIR/$DEV/size`
      GB=$(($SIZE/2**21))
      if [ $GB -gt $MINSIZE ]; then
        echo "$(($SIZE/2**21))"
        if [ -z $ROOTDRIVE ]; then
          ROOTDRIVE=$DEV
        fi
      fi
    fi
  fi
done

echo "ROOTDRIVE=$ROOTDRIVE"

# drives smaller than 240GB use fixed-size partions
# drives larger than 240GB use percentage-based partition sizes

if [ $GB -lt 240 ]; then

# drives smaller than 240GB
cat << EOF > /tmp/part-include
zerombr
clearpart --linux --drives=$ROOTDRIVE --initlabel
#clearpart --linux --drives=$ROOTDRIVE
bootloader --location=mbr --driveorder=$ROOTDRIVE
part /boot --fstype=ext3 --size=200 
part pv.01 --size=1000 --grow 
part swap --size=4000
volgroup myvg pv.01 
logvol / --vgname=myvg --name=rootvol --size=1000 --grow 
EOF

else
# drives 240GB and larger

cat << EOF > /tmp/part-include
zerombr
clearpart --linux --drives=$ROOTDRIVE --initlabel
#clearpart --linux --drives=$ROOTDRIVE
bootloader --location=mbr --driveorder=$ROOTDRIVE
part /boot --fstype=ext3 --size=200 
part pv.01 --size=1000 --grow 
part swap --size=4000
volgroup myvg pv.01 
logvol / --vgname=myvg --name=rootvol --size=1000 --grow 
EOF

fi

%end

%post --nochroot
mkdir /mnt/sysimage/tmp/ks-tree-copy
if [ -d /oldtmp/ks-tree-shadow ]; then
cp -fa /oldtmp/ks-tree-shadow/* /mnt/sysimage/tmp/ks-tree-copy
elif [ -d /tmp/ks-tree-shadow ]; then
cp -fa /tmp/ks-tree-shadow/* /mnt/sysimage/tmp/ks-tree-copy
fi
cp /etc/resolv.conf /mnt/sysimage/etc/resolv.conf
cp -f /tmp/ks-pre.log* /mnt/sysimage/root/ || :

cp `awk '{ if ($1 ~ /%include/) {print $2}}' /tmp/ks.cfg` /tmp/ks.cfg /mnt/sysimage/root
%end

I got it working by adjusting the parameters for which it was searching for drives. Updated code:

for DEV in $DIR/sd? $DIR/nvme?; do
  DEV=`echo "$DEV" | awk -F'/' '{print $NF}'`
  if [ -d $DIR/$DEV ]; then
    REMOVABLE=`cat $DIR/$DEV/removable`
    if (( $REMOVABLE == 0 )); then
      echo $DEV
      SIZE=`cat $DIR/$DEV/size`
      GB=$(($SIZE/2**21))
      if [ $GB -gt $MINSIZE ]; then
        echo "$(($SIZE/2**21))"
        if [ -z $ROOTDRIVE ]; then
          ROOTDRIVE=$DEV
        fi
      fi
    fi
  fi
done

This is a great idea for identifying dynamically the boot disk device, be it NVME or SATA/SAS!

I've made some simplifying changes below, and also added a check if ROOTDRIVE is undefined. I'm assuming that NVME boot drives will be preferred over /dev/sd* drives, YMMV.
I introduced a MAXSIZE as well because we do not want to clobber any large data-disks! Also the partitioning details may be defined differently.

# The file /tmp/part-include is created below in the %pre section
%include /tmp/part-include

....

# Start of the %pre section with logging into /root/ks-pre.log
%pre --log=/root/ks-pre.log
#----- partitioning logic below--------------
# pick the first drive that is not removable and is over MINSIZE
DIR="/sys/block"
# minimum size of hard drive needed specified in GIGABYTES
MINSIZE=60
MAXSIZE=1100
ROOTDRIVE=""
# /sys/block/*/size is in 512 byte chunks
# The loop first checks NVME then SATA/SAS drives:
for d in $DIR/nvme* $DIR/sd*
do
  DEV=`basename "$d"`
  if [ -d $DIR/$DEV ]; then
    if [[ "`cat $DIR/$DEV/removable`" = "0" ]]
    then
      GB=$((`cat $DIR/$DEV/size`/2**21))
      echo "Disk device $DEV has size $GB GB"
      if [ $GB -gt $MINSIZE -a $GB -lt $MAXSIZE -a -z "$ROOTDRIVE" ]
      then
        ROOTDRIVE=$DEV
        echo "Select ROOTDRIVE=$ROOTDRIVE"
      fi
    fi
  fi
done

if [ -z "$ROOTDRIVE" ]
then
        echo "ERROR: ROOTDRIVE is undefined"
else
        echo "ROOTDRIVE=$ROOTDRIVE"
        cat << EOF > /tmp/part-include
zerombr
clearpart --drives=$ROOTDRIVE --all --initlabel
ignoredisk --only-use=$ROOTDRIVE
reqpart --add-boot
part swap --size 32768 --asprimary
part pv.01 --fstype xfs --size=1 --grow --asprimary
volgroup VolGroup00 pv.01
logvol / --fstype xfs --name=lv_root --vgname=VolGroup00 --size=32768
EOF
fi
%end