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
Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.