Apocalypse_Cow

RH Linux Clevis/Tang setup script

Apr 29th, 2022 (edited)
828
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.02 KB | None | 0 0
  1. #!/bin/bash
  2. #####
  3. # This script configures Clevis/Tang binding for network bound disk encryption.
  4. # When properly configured, a LUKS encrypted drive will be decrypted on boot via key exchange with a Tang server.
  5. # The Tang server should be defined below in place of tang.example.com.
  6.  
  7. #####
  8. # Verify there is an encrypted drive. (Because otherwise what's the point?)
  9. verify_encryption(){
  10. # On a UEFI system there should be a boot/efi partition.  The first 3 letters of the block device indicate if it is NVME or SD drive.
  11. DTYPE=`lsblk -fs|egrep -w boot/efi|awk '{print $1}'|cut -c 1,2,3`
  12.  
  13. # Find a LUKS encrypted partition on the drive containing the boot/efi partition.
  14. # Of course this assumes the efi partition is not on a separate drive.
  15. CDEVICE=`lsblk --fs|egrep $DTYPE|grep LUKS|awk '{print $1}'|tr -cd "[:alnum:]"`
  16.  
  17. if [ -z "$CDEVICE" ]
  18.    then
  19.       echo "Found no encrypted device."
  20.       exit 1
  21.    else
  22.       set_requires
  23. fi
  24. }
  25.  
  26. #####
  27. # RH7 & RH8 have different package requirements for clevis/tang setup.
  28. # This module sets up the list for each version.
  29. set_requires(){
  30. if grep Ootpa /etc/redhat-release &>/dev/null
  31.    then
  32.        #RHEL8
  33.        requires_array=("clevis"
  34.        "clevis-dracut"
  35.        "clevis-luks"
  36.        "clevis-systemd"
  37.        "ipcalc")
  38.    elif grep Maipo /etc/redhat-release &>/dev/null
  39.    then
  40.       #RHEL7
  41.       requires_array=("clevis"
  42.       "clevis-dracut"
  43.       "clevis-luks"
  44.       "dnf"
  45.       "initscripts")
  46.    else
  47.    echo "Unexpected RHEL version.  Exiting."
  48.    exit 1
  49. fi
  50. }
  51.  
  52. #####
  53. # Use the list of required packages based on the RHEL release major version;
  54. # See if we have them installed.  Install them if we don't.
  55. check_requires(){
  56. if  [ $LOOP -lt 2 ]
  57.    then
  58.       do
  59.          for PKG in "${requires_array[@]}"
  60.             do
  61.                 if ! rpm -qa|grep "$PKG" &>/dev/null
  62.                    then
  63.                       echo "Installing required package $PKG."
  64.                       dnf install -y $PKG
  65.                       ((LOOP++))
  66.                       check_requires
  67.                 fi
  68.             done
  69.     else
  70.         echo "Too many attempts to install packages.  Exiting."
  71.         exit 1
  72. fi
  73. }
  74.  
  75. #####
  76. # Make sure our DNS IP address is assigned to some interface on this box.
  77. # Also determine values for various network parameters needed to build a static_ip.conf file.
  78. find_iface(){
  79. DNSIP=`host $HOSTNAME|awk '{print $4}'`
  80. MATCH=`ip -4 -o address|awk '{print $2,$4}'|egrep -v lo|grep $DNSIP`
  81.  
  82. if [ ! -z "$MATCH" ]
  83.     then
  84.         # Public IP
  85.         MYIP=$DNSIP
  86.         # Interface
  87.         MYIF=`echo $MATCH|awk '{print $1}'`
  88.         # Subnet Mask
  89.         MYSN=`echo $MATH|awk '{print $2}'`
  90.         # Netmask
  91.         MYNM=`ipcalc -m $MYSN|awk -F= '{print $2}'`
  92.         # Gateway
  93.         MYGW=`ip route show dev $MYIF|grep default|awk '{print $3}'`
  94.         # MTU
  95.         MYMTU=ip address show $MYIF|head -1|awk '{print $5}'`
  96.         # Nameserver
  97.         MYNS=`cat /etc/resolv.conf|grep nameserver|sed -s/ /=/'|tr '\n' ' '`
  98.         MYNS=${MYNS%%}
  99.         echo Verified DNS for IF:$MYIF IP:$MYIP NM:$MYNM GW:$MYGW NS:$MYNS
  100.         setup_conf
  101.     else
  102.         echo "Found IP Address Mismatch."
  103.         echo "Please fix."
  104.         exit 1
  105. fi
  106.  
  107. #####
  108. # Provided that none of the values we found in find_iface are blank, build the static_ip.conf file.
  109. setup_conf(){
  110. if [ -z "$MYIP" ] || [ -z "$MYIF" ] || [ -z "$MYNM" ] || [ -z "$MYGW"] || [ -z "$MYMTU" ] || [ -z "$MYNS" ]
  111.     then
  112.         echo "One or more parameters are empty."
  113.         echo "Exiting."
  114.         exit 1
  115.     else
  116.         echo "Building static_ip.conf file."
  117.         echo "kernel_cmdline=\"ip=${MYIP}::${MYGW}:${MYNM}::${MYIF}:none:${MYMTU}: ${MYNS}\"" > /etc/dracut.conf.d/static_ip.conf
  118.         echo "Starting Clevis Setup"
  119.         LOOP=0
  120.         setup_clevis
  121. fi
  122. }
  123.  
  124. #####
  125. # Now actually try to bind to the Tang server.
  126. setup_clevis(){
  127. if grep Ootpa /etc/redhat-release &>/dev/null
  128.     then
  129.         if [ $LOOP -lt 2 ]
  130.             then
  131.                 clevis luks bind -y -d /dev/$CDEVICE tang '{"url":"http://tang.example.com"}'
  132.                 ecode=$?
  133.                     if [ $ecode -eq 0 ]
  134.                         then
  135.                         echo "Success."
  136.                         LOOP=0
  137.                         rebuild_initrd
  138.                     else
  139.                         echo "Fail."
  140.                         ((LOOP++))
  141.                         setup_clevis
  142.                 fi
  143.             else
  144.                 echo ""
  145.                 echo "Error in module setup_clevis for RH8."
  146.                 echo "Too many attempts to bind to Tang."
  147.                 echo "Exiting."
  148.                 exit 1
  149.         fi
  150. elif grep Maipo /etc/redhat-release &>/dev/null
  151.     then
  152.         if [ $LOOP -lt 2 ]
  153.             then
  154.                 clevis luks bind -f -d /dev/$CDEVICE tang '{"url":"http://tang.example.com"}'
  155.                 ecode=$?
  156.                     if [ $ecode -eq 0 ]
  157.                         then
  158.                         echo "Success."
  159.                         LOOP=0
  160.                         rebuild_initrd
  161.                     else
  162.                         echo "Fail."
  163.                         ((LOOP++))
  164.                         setup_clevis
  165.                 fi
  166.             else
  167.                 echo ""
  168.                 echo "Error in module setup_clevis for RH7."
  169.                 echo "Too many attempts to bind to Tang."
  170.                 echo "Exiting."
  171.                 exit 1
  172.         fi
  173. else
  174.     echo ""
  175.     echo "Error in module setup_clevis."
  176.     echo "Unexpected RHEL version.  Exiting."
  177.     exit 1
  178. fi
  179. }
  180.  
  181. #####
  182. # rebuild initial ramdisk
  183. rebuild_initrd(){
  184. echo "Rebuilding initial ramdisk."
  185. dracut -fv --regenerate-all
  186. }
  187.  
  188. #####
  189. # Set LOOP to 0 and launch modules in order.
  190. LOOP=0
  191. verify_encryption
  192. set_requires
  193. check_requires
  194. find_iface
  195.  
  196. # Once you've pulled out the pin, Mr. Grenade is no longer your friend.
Add Comment
Please, Sign In to add comment