1

So I have this assignment that is suppose to be a automated script that is run on a aws instance, the script is suppose to essentially update the kernel to the newest stable release. The script is also suppose to take some parameters in ..

Installs current linux stable kernel source code into given Subdir
    or ~/src/linux-stable by default.  p01 takes a number of options,
    as described below.
    -g         git clone source from kernel.org instead of wget or curl
    -v         Version of new stable kernel but does not install it.
    -h         Help should display options with examples.
    -R         Reboot after download and install.
    -D Subdir  Subdir is the fullpath of downloaded stable kernel source

So , the issue im running into is with the -D parameter. I can run my script just fine using any of the other parameters, but when it comes to actually directing the script to use a specific subdirectory i am just lost. The thing is, my teacher could run this script a few ways ...

script.sh -g -D /some/Directory

or

script.sh -D /some/Directory -g

So my problem is that I cant rely on counting arguments and hoping that the -D parameter is passed at the end. Can someone guide me so that I can maybe look for the -D parameter and then maybe taking the next input after that as the subdirectory ? or maybe there is a better way ?

#!/bin/bash

for arg in "$@"
do
    case $arg in
        -g) Git=true ;;
        -v) Version=true ;;
        -h) Help=true ;;
        -R) Reboot=true ;;
        -D) Dir=true ;;
        *) echo "Unknown"
        exit 1 ;;
    esac
done

sudo yum update -y -q && sudo yum install -y -q jq wget perl ncurses-devel make gcc bc \
bison flex elfutils-libelf-devel openssl-devel  

sudo yum groupinstall -q -y “Development Tools”

InstalledKernel=$(uname -r | cut -d. -f-2)
TargetDir=${Arg_TargetDir:-"$HOME/src/linux-stable"}

mkdir $HOME/src
mkdir $TargetDir
#sudo rm /etc/dracut.conf.d/xen.conf

V=$(curl -s https://www.kernel.org/releases.json | \
    jq '.latest_stable.version' -r)

M="$(echo $(curl -s https://www.kernel.org/releases.json | \
    jq '.latest_stable.version' -r)  \
    | cut -d. -f1)"

if [[ $InstalledKernel == $V ]]; then
    echo "linux-stable already installed"
   # exit 1
fi 

if [[ $Help ]]; then
    echo "p01 - install current linux stable kernel
    Installs current linux stable kernel source code into given Subdir
    or ~/src/linux-stable by default.  p01 takes a number of options,
    as described below.
    -g         git clone source from kernel.org instead of wget or curl
    -v         Version of new stable kernel but does not install it.
    -h         Help should display options with examples.
    -R         Reboot after download and install.
    -D Subdir  Subdir is the fullpath of downloaded stable kernel source
    "
    exit 0
fi

if [[ $Version ]]; then
    echo $(curl -s https://www.kernel.org/releases.json | \
    jq '.latest_stable.version' -r)  
    exit 0
fi

if [[ $Dir ]]; then
    echo $TargetDir
fi

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

gpg2 --locate-keys "torvalds@kernel.org" "gregkh@kernel.org"
echo -e "1\nq" | gpg --command-fd 0 --search-keys "joel@debian.org"
for trusted in "torvalds@kernel.org" "gregkh@kernel.org" "joel@debian.org"; do
    echo -e "5\ny\n" | gpg --command-fd 0 --expert --edit-key $trusted trust
done

if [[ $( ccache --version | grep -c 3.7.8 ) -lt 1 ]]; then 
    cd $HOME/src
    wget "https://github.com/ccache/ccache/releases/download/v3.7.8/ccache-3.7.8.tar.xz"
    wget "https://github.com/ccache/ccache/releases/download/v3.7.8/ccache-3.7.8.tar.xz.asc"

    if [[ $(gpg --verify ccache-3.7.8.tar.xz.asc ccache-3.7.8.tar.xz 2>&1 \
        | grep -c "Good signature .* \[ultimate\]") -lt 1 ]]; then
        echo "Error: invalid source"
        exit 1
    else
        echo "Source verified"
    fi

    tar xvf ccache-3.7.8.tar
    cd $HOME/src/ccache-3.7.8
    ./configure
    make -j$(nproc)
    sudo make -j$(nproc) install
fi



if [[ $Git ]]; then
    #echo "git"
    git clone --depth 1 --single-branch --branch v$V \
        "https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git" \
        $TargetDir || exit 1
    cd $TargetDir

    if [[ $(git tag -v v$V 2>&1 \
        | grep -c "Good signature .* \[ultimate\]") -lt 1 ]]; then
        echo "Error: invalid source"
        exit 1
    else
        echo "Source verified"
    fi

else 
    #echo "wget"
    cd $TargetDir
    wget "https://cdn.kernel.org/pub/linux/kernel/v${M}.x/linux-${V}.tar.xz"
    wget "https://cdn.kernel.org/pub/linux/kernel/v${M}.x/linux-${V}.tar.sign"
    xz -d -v linux-${V}.tar.xz

    if [[ $(gpg --verify linux-$V.tar.sign linux-$V.tar 2>&1 \
        | grep -c "Good signature .* \[ultimate\]") -lt 1 ]]; then
        echo "Error: invalid source"
        exit 1
    else
        echo "Source verified"
    fi
    tar xvf linux-${V}.tar
fi

sudo rm /etc/dracut.conf.d/xen.conf
cd ${TargetDir}
cd linux-${V}
sudo cp /boot/config-`uname -r` .config 
yes '' | ccache make -j$(nproc) localmodconfig
ccache make -j$(nproc) 
ccache make -j$(nproc) modules
sudo make -j$(nproc) modules_install
sudo make -j$(nproc) install

if [[ $Reboot ]]; then
    sudo reboot 
fi

0 Answers0