[b495a61] | 1 | #!/bin/bash |
---|
| 2 | |
---|
| 3 | #/** |
---|
| 4 | #@file oglivecli |
---|
| 5 | #@brief Command line tool to manage ogLive clients. |
---|
[240a4dc] | 6 | #@usage oglivecli Command [Options ...] |
---|
[1952845] | 7 | #@param Command: |
---|
| 8 | #@param help show this help |
---|
| 9 | #@param version show script version |
---|
| 10 | #@param config [Parameter] show configuration parameters |
---|
| 11 | #@param check check system consistency |
---|
| 12 | #@param convert convert old ogclient to new default ogLive client |
---|
| 13 | #@param list list installed ogLive clients |
---|
| 14 | #@param show all show JSON information about all installed ogLive clients |
---|
| 15 | #@param show default show JSON information about ogLive client marked as default |
---|
| 16 | #@param show Index|Dir show JSON information about an installed ogLive client |
---|
| 17 | #@param search Index|Dir show corresponding index or directory |
---|
| 18 | #@param download show a menu to download an ogLive ISO image from the OpenGnsys website |
---|
| 19 | #@param download Iso download an specific ogLive ISO image from the OpenGnsys website |
---|
| 20 | #@param install Iso install a new ogLive client from a downloaded ISO image |
---|
| 21 | #@param uninstall Iso remove ISO image and uninstall its ogLive client |
---|
| 22 | #@param uninstall Index|Dir uninstall an ogLive client |
---|
| 23 | #@param get-default get index value for default ogLive client |
---|
| 24 | #@param set-default Index set default ogLive client |
---|
| 25 | #@param rebuild rebuild a lost configuration file |
---|
| 26 | #@param assign Iso Index assign an ISO file to a JSON entry |
---|
| 27 | #@param Options: |
---|
| 28 | #@param Index a number, starting by 0 |
---|
| 29 | #@param Dir directory (relative to installation directory) |
---|
| 30 | #@param Iso ISO file name (relative to download URL or download directory) |
---|
[240a4dc] | 31 | #@warning This script needs "jq" command. |
---|
[b495a61] | 32 | #@version 1.1.0 - Initial version. |
---|
| 33 | #@author Ramón M. Gómez - ETSII Univ. Sevilla |
---|
| 34 | #@date 2016-12-05 |
---|
[54d7ca27] | 35 | #@version 1.1.1b - Use reduced directory names. |
---|
[240a4dc] | 36 | #@author Ramón M. Gómez - ETSII Univ. Sevilla |
---|
| 37 | #@date 2020-01-17 |
---|
[b495a61] | 38 | #*/ ## |
---|
| 39 | |
---|
| 40 | |
---|
[a30baf2] | 41 | # Global constants definition. |
---|
[72d19da] | 42 | PROG=$(basename "$(realpath "$0")") # Program name. |
---|
| 43 | OPENGNSYS=/opt/opengnsys # OpenGnsys main directory. |
---|
| 44 | DOWNLOADDIR=$OPENGNSYS/lib # Directory to store ogLive images. |
---|
| 45 | DOWNLOADURL="https://opengnsys.es/trac/downloads" # Download URL. |
---|
| 46 | TFTPDIR=$OPENGNSYS/tftpboot # TFTP directory. |
---|
| 47 | DEFOGLIVE="ogLive" # Default ogLive directory. |
---|
| 48 | MINREL=20190601 # Mininum ogLive compatibility release. |
---|
| 49 | INFOFILE=$OPENGNSYS/etc/ogliveinfo.json # Configuration file. |
---|
[a30baf2] | 50 | |
---|
| 51 | |
---|
[240a4dc] | 52 | # Global and secondary functions. |
---|
[b495a61] | 53 | |
---|
[240a4dc] | 54 | source $OPENGNSYS/lib/ogfunctions.sh || exit 1 |
---|
[3554392] | 55 | |
---|
[b495a61] | 56 | # Create/edit JSON file about installed ogLive clients. |
---|
| 57 | function addToJson() { |
---|
[3dd397f] | 58 | local i DATA OGLIVEDIST="$1" OGLIVEKRNL="$2" OGLIVEARCH="$3" OGLIVEREV="$4" |
---|
[d1776403] | 59 | local OGLIVEDIR=$(basename $5 2>/dev/null) OGLIVEISO=$(basename $6 2>/dev/null) |
---|
[b495a61] | 60 | # JSON data for installed ogLive. |
---|
[3554392] | 61 | DATA=$(cat << EOT | jq . |
---|
[d244d7f] | 62 | {"distribution":"$OGLIVEDIST","kernel":"$OGLIVEKRNL","architecture":"$OGLIVEARCH","revision":"$OGLIVEREV","directory":"$OGLIVEDIR","iso":"$OGLIVEISO"} |
---|
[b495a61] | 63 | EOT |
---|
| 64 | ) |
---|
| 65 | # Check JSON file consistency. |
---|
[d063c01] | 66 | if [ "$(jq -c keys $INFOFILE 2>/dev/null)" == '["default","oglive"]' ]; then |
---|
[b495a61] | 67 | # Check if ogLive is defined into JSON file. |
---|
[3554392] | 68 | n=$(jq ".oglive | length" $INFOFILE) |
---|
[b495a61] | 69 | for ((i=0; i<n; i++)); do |
---|
[d244d7f] | 70 | [ "$(jq ".check=$DATA | .check==.oglive[$i]" $INFOFILE)" == "true" ] && INDEX=$i |
---|
[b495a61] | 71 | done |
---|
| 72 | # Check if it needs to insert data. |
---|
| 73 | if [ -z "$INDEX" ]; then |
---|
| 74 | INDEX=$n |
---|
[3554392] | 75 | jq ".oglive |= (. + [$DATA])" $INFOFILE | sponge $INFOFILE |
---|
[b495a61] | 76 | fi |
---|
[4be0673] | 77 | # Show JSON entry. |
---|
[3554392] | 78 | jq ".oglive[$INDEX]" $INFOFILE |
---|
[b495a61] | 79 | else |
---|
| 80 | # Create new JSON file. |
---|
[3554392] | 81 | cat << EOT | jq . | tee $INFOFILE |
---|
[b495a61] | 82 | {"oglive":[$DATA],"default":0} |
---|
| 83 | EOT |
---|
| 84 | fi |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | # Command functions. |
---|
| 88 | |
---|
| 89 | # Convert default ogclient to a new ogLive format. |
---|
| 90 | function convert() { |
---|
| 91 | local OGCLIENT=ogclient OLDINFOFILE=$OPENGNSYS/doc/veroglive.txt |
---|
| 92 | local OGLIVEKRNL OGLIVEDIR OGLIVEISO |
---|
| 93 | [ $# -ne 0 ] && raiseError usage |
---|
| 94 | [ ! -w $(dirname $INFOFILE) ] && raiseError access "Configuration file." |
---|
[3554392] | 95 | [ -n "$(stat -c "%N" $TFTPDIR/ogclient | awk '$3~/'$DEFOGLIVE'/ {print}')" ] && raiseError access "ogLive is already converted." |
---|
[b495a61] | 96 | pushd $TFTPDIR >/dev/null || raiseError access "Installation directory." |
---|
[1952845] | 97 | [ ! -f $OGCLIENT/ogvmlinuz ] && raiseError notfound "\"ogclient\"." |
---|
[b495a61] | 98 | # Add entry to JSON file using ogclient kernel version. |
---|
| 99 | OGLIVEKRNL=$(file -bkr $OGCLIENT/ogvmlinuz | awk '/Linux/ {for(i=1;i<=NF;i++) if($i~/version/) {v=$(i+1);sub(/-.*/,"",v);print v}}') |
---|
| 100 | OGLIVEDIR=$DEFOGLIVE-$OGLIVEKRNL |
---|
[b17e87e] | 101 | [ -r $OLDINFOFILE ] && OGLIVEISO="$(head -1 $OLDINFOFILE)" |
---|
| 102 | addToJson "$(echo $OGLIVEISO|cut -f2 -d-)" "$OGLIVEKRNL" "i386" "${OGLIVEISO##*-}" "$OGLIVEDIR" "$OGLIVEISO.iso" |
---|
[b495a61] | 103 | # Rename directory, link to default and clean old files. |
---|
| 104 | mv -v $OGCLIENT $OGLIVEDIR |
---|
| 105 | ln -vfs $OGLIVEDIR $DEFOGLIVE |
---|
[d1776403] | 106 | rm -f $OGCLIENT |
---|
[b495a61] | 107 | ln -vfs $DEFOGLIVE $OGCLIENT |
---|
| 108 | mv -v $OGCLIENT.old $OGLIVEDIR.old 2>/dev/null |
---|
| 109 | rm -fv {ogvmlinuz,oginitrd.img}{,.sum} $OLDINFOFILE |
---|
| 110 | popd >/dev/null |
---|
[d55c26d] | 111 | # Delete old config file. |
---|
| 112 | rm -f $OLDINFOFILE |
---|
[b495a61] | 113 | } |
---|
| 114 | |
---|
| 115 | # Show script configuration parameters. |
---|
| 116 | function config() { |
---|
[1952845] | 117 | local DATA |
---|
| 118 | DATA=$(cat << EOT |
---|
| 119 | [ |
---|
| 120 | { "param": "config-file", "description": "Configuration file", "value": "$INFOFILE" }, |
---|
| 121 | { "param": "download-url", "description": "ogLive download URL", "value": "$DOWNLOADURL" }, |
---|
| 122 | { "param": "download-dir", "description": "ogLive download directory", "value": "$DOWNLOADDIR" }, |
---|
| 123 | { "param": "install-dir", "description": "ogLive installation directory", "value": "$TFTPDIR" }, |
---|
| 124 | { "param": "default-name", "description": "Default ogLive name", "value": "$DEFOGLIVE" }, |
---|
| 125 | { "param": "min-release", "description": "Mainimum compatibility release", "value": "r$MINREL" } |
---|
| 126 | ]" |
---|
| 127 | EOT |
---|
| 128 | ) |
---|
[b495a61] | 129 | case $# in |
---|
| 130 | 0) # Show all parameters. |
---|
[1952845] | 131 | echo $DATA | jq -r '.[] | .description + " (" + .param+ ")," + .value' | column -ts, |
---|
[b495a61] | 132 | ;; |
---|
| 133 | 1) # Show specified parameter. |
---|
[1952845] | 134 | DATA=$(echo $DATA | jq -r ".[] | select(.param==\"$1\").value") |
---|
| 135 | [ "$DATA" ] || raiseError notfound "\"$1\"." |
---|
| 136 | echo "$DATA" |
---|
[b495a61] | 137 | ;; |
---|
[3dd397f] | 138 | *) # Usage error. |
---|
[b495a61] | 139 | raiseError usage |
---|
| 140 | ;; |
---|
| 141 | esac |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | # Check consistency, showing configuration problems. |
---|
| 145 | function check() { |
---|
| 146 | local ERR=0 AUX INST DEF |
---|
| 147 | [ $# -ne 0 ] && raiseError usage |
---|
| 148 | # Check for old system that needs conversion. |
---|
| 149 | if [ -z "$(stat -c "%N" $TFTPDIR/ogclient | awk '$3~/'$DEFOGLIVE'/ {print}')" ]; then |
---|
| 150 | echo "This server uses old ogclient, please run \"$PROG convert\" to update." |
---|
| 151 | let ERR++ |
---|
[82ed0eb] | 152 | [ ! -f $INFOFILE ] && return $ERR |
---|
[b495a61] | 153 | fi |
---|
| 154 | # Check for other problems. |
---|
[d1776403] | 155 | [ ! -f $INFOFILE ] && echo "Configuration file does not exists: $INFOFILE" && let ERR++ |
---|
| 156 | [ -f $INFOFILE -a "$(jq -c keys $INFOFILE 2>/dev/null)" != "[\"default\",\"oglive\"]" ] && echo "Format error in configuration file: $INFOFILE" && let ERR++ |
---|
[b495a61] | 157 | [ ! -e $TFTPDIR ] && echo "TFTP directory does not exist: $TFTPDIR." && let ERR++ |
---|
[f60723c] | 158 | # Check for installed ogLive clients. |
---|
[b495a61] | 159 | INST=( $(find $TFTPDIR/ -type d -name "$DEFOGLIVE-*" -a ! -name "*.old" -printf "%f\n" | sort) ) |
---|
| 160 | [[ ${#INST[@]} -eq 0 ]] && echo "No ogLive clients are installed." && let ERR++ |
---|
[3554392] | 161 | DEF=( $(jq -r .oglive[].directory $INFOFILE 2>/dev/null | sort) ) |
---|
[b495a61] | 162 | # Compare installed and defined ogLive clients. |
---|
| 163 | AUX=$(comm -23 <(printf "%s\n" ${INST[*]}) <(printf "%s\n" ${DEF[*]})) |
---|
| 164 | [ -n "$AUX" ] && echo "Some ogLive are installed but not defined: ${AUX//$'\n'/, }" && let ERR++ |
---|
| 165 | AUX=$(comm -13 <(printf "%s\n" ${INST[*]}) <(printf "%s\n" ${DEF[*]})) |
---|
| 166 | [ -n "$AUX" ] && echo "Some ogLive are defined but not installed: ${AUX//$'\n'/, }" && let ERR++ |
---|
| 167 | # Compare downloaded and defined ISO images. |
---|
| 168 | INST=( $(find $DOWNLOADDIR/ -type f -name "$DEFOGLIVE-*.iso" -printf "%f\n" | sort) ) |
---|
[3554392] | 169 | DEF=( $(jq -r .oglive[].iso $INFOFILE 2>/dev/null | sort) ) |
---|
[b495a61] | 170 | AUX=$(comm -23 <(printf "%s\n" ${INST[*]}) <(printf "%s\n" ${DEF[*]})) |
---|
[3554392] | 171 | [ -n "$AUX" ] && echo "Some ISOs are downloaded but not defined: ${AUX//$'\n'/, }" && let ERR++ |
---|
[b495a61] | 172 | AUX=$(comm -13 <(printf "%s\n" ${INST[*]}) <(printf "%s\n" ${DEF[*]})) |
---|
[3554392] | 173 | [ -n "$AUX" ] && echo "Some ISOs are defined but not downloaded: ${AUX//$'\n'/, }" && let ERR++ |
---|
[f60723c] | 174 | # Check for new ISO files downloaded after installation. |
---|
| 175 | AUX=$(jq -r '.oglive[] as $og | $og.iso + ":" + $og.directory' $INFOFILE 2>/dev/null | \ |
---|
| 176 | while IFS=":" read -r DEF INST; do |
---|
| 177 | [ $DOWNLOADDIR/$DEF -nt $TFTPDIR/$INST ] && echo "$DEF" |
---|
| 178 | done) |
---|
| 179 | [ -n "$AUX" ] && echo "Some ISOs are downloaded after installation: ${AUX//$'\n'/, }" && let ERR++ |
---|
[0b856b4d] | 180 | AUX=$(jq -r '.oglive[] as $og | if ($og.revision[1:9] | tonumber) < '$MINREL' then $og.directory else "" end' $INFOFILE 2>/dev/null) |
---|
[72d19da] | 181 | [ -n "$AUX" ] && echo "Some installed ogLive aren't fully compatible: ${AUX//$'\n'/, }" && let ERR++ |
---|
[bd4a17f] | 182 | DEF=$(jq -r ".oglive[$(getdefault)].directory" $INFOFILE 2>/dev/null) |
---|
| 183 | INST=$(stat -c "%N" $TFTPDIR/$DEFOGLIVE | cut -f4 -d\') |
---|
| 184 | [ "$DEF" != "$INST" ] && echo "Default ogLive is not linked to right directory: $DEF <> $INST" && let ERR++ |
---|
[b495a61] | 185 | # Print result. |
---|
| 186 | [ $ERR -eq 0 ] && echo "OK!" || echo "Problems detected: $ERR" |
---|
| 187 | return $ERR |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | # List installed ogLive clients. |
---|
| 191 | function list() { |
---|
| 192 | [ $# -ne 0 ] && raiseError usage |
---|
| 193 | [ ! -r $INFOFILE ] && raiseError access "Configuration file." |
---|
| 194 | # List all defined indexes, directories and check if missing. |
---|
[3554392] | 195 | jq -r .oglive[].directory $INFOFILE | nl -v 0 | \ |
---|
[d244d7f] | 196 | awk '{system("echo -n "$0"; test -d '$TFTPDIR'/"$2" || echo -n \" (missing)\"; echo")}' | column -t |
---|
[b495a61] | 197 | } |
---|
| 198 | |
---|
[3dd397f] | 199 | # Show information about an installed ogLive client. |
---|
[b495a61] | 200 | function show() { |
---|
| 201 | local INDEX |
---|
| 202 | [ $# -ne 1 ] && raiseError usage |
---|
| 203 | [ ! -r $INFOFILE ] && raiseError access "Configuration file." |
---|
| 204 | # Show JSON entries. |
---|
| 205 | case "$1" in |
---|
| 206 | default) # Default index. |
---|
[3554392] | 207 | INDEX="[$(jq -r .default $INFOFILE)]" ;; |
---|
[b495a61] | 208 | all) # All intries. |
---|
| 209 | ;; |
---|
| 210 | [0-9]*) # Index. |
---|
[3554392] | 211 | INDEX="[$1]" ;; |
---|
[b495a61] | 212 | *) # Directory. |
---|
[3554392] | 213 | INDEX="[$(search "$1" 2>/dev/null)]" || raiseError notfound "Directory \"$1\"." |
---|
[b495a61] | 214 | ;; |
---|
| 215 | esac |
---|
[3554392] | 216 | jq ".oglive$INDEX" $INFOFILE || raiseError notfound "Index \"$1\"." |
---|
[b495a61] | 217 | } |
---|
| 218 | |
---|
| 219 | # Show index or directory corresponding to searching parameter. |
---|
| 220 | function search() { |
---|
| 221 | [ $# -ne 1 ] && raiseError usage |
---|
| 222 | [ ! -r $INFOFILE ] && raiseError access "Configuration file." |
---|
| 223 | # Show corresponding index or directory. |
---|
[1952845] | 224 | list | awk -v d="$1" '{if ($2==d) print $1; if ($1==d) print $2}' | grep . || raiseError notfound "Index/Directory \"$1\"." |
---|
[b495a61] | 225 | } |
---|
| 226 | |
---|
| 227 | # Show a menu to select and download an ogLive ISO image from the OpenGnsys website. |
---|
| 228 | function download() { |
---|
[1952845] | 229 | local OGLIVE NISOS i HTTPCODE TARGETFILE |
---|
[72d19da] | 230 | local ISOREL |
---|
[b495a61] | 231 | [ $# -gt 1 ] && raiseError usage |
---|
[1952845] | 232 | [ ! -d $DOWNLOADDIR ] && raiseError notfound "Download directory." |
---|
| 233 | [ ! -w $DOWNLOADDIR ] && raiseError access "Download directory." |
---|
[b495a61] | 234 | # Check parameter. |
---|
| 235 | if [ -n "$1" ]; then |
---|
| 236 | # ogLive to download. |
---|
| 237 | OGLIVEFILE="$1" |
---|
| 238 | else |
---|
| 239 | # Show download menu. |
---|
[a30baf2] | 240 | OGLIVE=( $(curl --silent $DOWNLOADURL | grep "$DEFOGLIVE.*iso") ) |
---|
[b495a61] | 241 | NISOS=${#OGLIVE[@]} |
---|
[d063c01] | 242 | echo "Available downloads (+ = installed, * = full compatibility):" |
---|
[b495a61] | 243 | for i in $(seq 1 $NISOS); do |
---|
[d063c01] | 244 | [ -e $DOWNLOADDIR/${OGLIVE[i-1]} ] && OGLIVE[i-1]="(+) ${OGLIVE[i-1]}" |
---|
[9c2b75c] | 245 | ISOREL=${OGLIVE[i-1]##*-r}; ISOREL=${ISOREL%%.*} |
---|
[d063c01] | 246 | [ $ISOREL -ge $MINREL ] && OGLIVE[i-1]="(*) ${OGLIVE[i-1]}" |
---|
[b495a61] | 247 | done |
---|
[d063c01] | 248 | select opt in "${OGLIVE[@]}"; do |
---|
[2411498] | 249 | [ -n "$opt" ] && OGLIVEFILE=${opt##* } && break |
---|
[b495a61] | 250 | done |
---|
| 251 | fi |
---|
| 252 | # Get download size. |
---|
[1952845] | 253 | HTTPCODE=$(curl --head --silent $DOWNLOADURL/$OGLIVEFILE | awk '/HTTP\// {print $2}') |
---|
| 254 | [ "$HTTPCODE" != "200" ] && raiseError download "\"$OGLIVEFILE\"." |
---|
[b495a61] | 255 | # Download ogLive. |
---|
| 256 | TARGETFILE=$DOWNLOADDIR/$OGLIVEFILE |
---|
[1952845] | 257 | curl $DOWNLOADURL/$OGLIVEFILE -o $TARGETFILE || raiseError download "\"$OGLIVEFILE\"." |
---|
[b495a61] | 258 | } |
---|
| 259 | |
---|
| 260 | # Install an ogLive client from a previously downloaded ISO image. |
---|
| 261 | function install() { |
---|
[715ff08] | 262 | local OGLIVEFILE OGLIVEDIST OGLIVEREV OGLIVEKRNL OGLIVEDIR OGINITRD OGSQFS OGCLIENT=ogclient |
---|
[0e3e167] | 263 | local COMPRESS SAMBAPASS TMPDIR RSYNCSERV RSYNCCLNT |
---|
[b495a61] | 264 | [ $# -ne 1 ] && raiseError usage |
---|
[d43640a] | 265 | OGLIVEFILE=$(realpath $DOWNLOADDIR/$1) |
---|
| 266 | # Only 1 file in pathname expansion. |
---|
| 267 | [ $(echo $OGLIVEFILE | wc -w) -gt 1 ] && raiseError usage |
---|
[b495a61] | 268 | [ ! -f $OGLIVEFILE ] && raiseError notfound "Downloaded file: \"$1\"." |
---|
| 269 | [ ! -r $OGLIVEFILE ] && raiseError access "Downloaded file: \"$1\"." |
---|
| 270 | [ ! -w $(dirname $INFOFILE) ] && raiseError access "Configuration directory." |
---|
| 271 | [ ! -w $TFTPDIR ] && raiseError access "Installation directory." |
---|
| 272 | [ -z "$(file -b $OGLIVEFILE | grep "ISO.*ogClient")" ] && raiseError access "File is not an ogLive ISO image." |
---|
[54d7ca27] | 273 | # Working directory: |
---|
| 274 | # 64-bit: ogLive-KernelVersion-rCodeRelease |
---|
| 275 | # 32-bit: ogLive-KernelVersion-i386-rCodeRelease |
---|
[b495a61] | 276 | OGLIVEDIST="$(echo $OGLIVEFILE|cut -f2 -d-)" |
---|
[54d7ca27] | 277 | OGLIVEREV="${OGLIVEFILE##*-}"; OGLIVEREV="${OGLIVEREV%%.*}" |
---|
[b495a61] | 278 | OGLIVEKRNL="$(echo $OGLIVEFILE|cut -f3- -d-)"; OGLIVEKRNL="${OGLIVEKRNL%-$OGLIVEREV.*}" |
---|
[d244d7f] | 279 | OGLIVEARCH="$(echo $OGLIVEFILE|awk -F- '{print $(NF-1)}')" |
---|
| 280 | case "$OGLIVEARCH" in |
---|
[a30baf2] | 281 | i386|amd64) # Get architecture. |
---|
[d244d7f] | 282 | OGLIVEKRNL="${OGLIVEKRNL%-$OGLIVEARCH}" ;; |
---|
[a30baf2] | 283 | *) # 32-bit by default. |
---|
[d244d7f] | 284 | OGLIVEARCH="i386" ;; |
---|
| 285 | esac |
---|
[54d7ca27] | 286 | OGLIVEDIR="$TFTPDIR/$DEFOGLIVE-${OGLIVEKRNL%%-*}-$OGLIVEARCH-$OGLIVEREV" |
---|
| 287 | OGLIVEDIR="${OGLIVEDIR/amd64-/}" |
---|
[b495a61] | 288 | # Get current or default Samba key. |
---|
| 289 | OGINITRD=$OGLIVEDIR/oginitrd.img |
---|
| 290 | [ ! -r $OGINITRD ] && OGINITRD=$TFTPDIR/$DEFOGLIVE/oginitrd.img |
---|
| 291 | if [ -r $OGINITRD ]; then |
---|
[15d8a19] | 292 | COMPRESS=$(file -b "$OGINITRD" | awk '{print tolower($1);}') |
---|
[0e3e167] | 293 | SAMBAPASS=$($COMPRESS -dc $OGINITRD | \ |
---|
| 294 | cpio -i --to-stdout scripts/ogfunctions 2>&1 | \ |
---|
| 295 | sed -n '/^[ ].*OPTIONS=/s/.*pass=\(\w*\).*/\1/p') |
---|
[b495a61] | 296 | fi |
---|
| 297 | # Make ogLive backup. |
---|
| 298 | rm -fr ${OGLIVEDIR}.old |
---|
| 299 | mv -fv $OGLIVEDIR ${OGLIVEDIR}.old 2>/dev/null |
---|
[4be0673] | 300 | # Mount ogLive ISO image, update its files and unmount it. |
---|
[b495a61] | 301 | TMPDIR=/tmp/${OGLIVEFILE%.iso} |
---|
| 302 | mkdir -p $OGLIVEDIR $TMPDIR |
---|
| 303 | trap "umount $TMPDIR; rm -fr $TMPDIR" 1 2 3 6 9 15 |
---|
| 304 | mount -o loop,ro $OGLIVEFILE $TMPDIR |
---|
[82ed0eb] | 305 | cp -va $TMPDIR/ogclient/* $OGLIVEDIR || raiseError access "Cannot copy files to ogLive directory." |
---|
[b495a61] | 306 | umount $TMPDIR |
---|
[4be0673] | 307 | # Link to default directory if it's the first ogLive. |
---|
| 308 | if [ ! -f $INFOFILE ]; then |
---|
| 309 | rm -f $TFTPDIR/$DEFOGLIVE $TFTPDIR/$OGCLIENT |
---|
| 310 | ln -vfs $(basename $OGLIVEDIR) $TFTPDIR/$DEFOGLIVE |
---|
| 311 | ln -vfs $DEFOGLIVE $TFTPDIR/$OGCLIENT |
---|
| 312 | fi |
---|
[b495a61] | 313 | # Recover or ask for a new Samba access key. |
---|
| 314 | if [ -n "$SAMBAPASS" ]; then |
---|
[a30baf2] | 315 | echo -ne "$SAMBAPASS\n$SAMBAPASS\n" | $OPENGNSYS/bin/setsmbpass "$(basename $OGLIVEDIR)" |
---|
[b495a61] | 316 | else |
---|
[a30baf2] | 317 | $OPENGNSYS/bin/setsmbpass "$(basename $OGLIVEDIR)" |
---|
[b495a61] | 318 | fi |
---|
| 319 | # Set permissions. |
---|
| 320 | find -L $OGLIVEDIR -type d -exec chmod 755 {} \; |
---|
| 321 | find -L $OGLIVEDIR -type f -exec chmod 644 {} \; |
---|
| 322 | chown -R :opengnsys $OGLIVEDIR |
---|
| 323 | # Mount SquashFS and check Rsync version. |
---|
| 324 | OGSQFS=$OGLIVEDIR/ogclient.sqfs |
---|
| 325 | mount -o loop,ro $OGSQFS $TMPDIR |
---|
| 326 | # If Rsync server version > client version, link to compiled file. |
---|
| 327 | RSYNCSERV=$(rsync --version 2>/dev/null | awk '/protocol/ {print $6}') |
---|
| 328 | RSYNCCLNT=$(chroot $TMPDIR /usr/bin/rsync --version 2>/dev/null | awk '/protocol/ {print $6}') |
---|
| 329 | if [ -z "$RSYNCSERV" -o ${RSYNCSERV:-0} -gt ${RSYNCCLNT:-1} ]; then |
---|
| 330 | [ -e $OPENGNSYS/client/bin/rsync-$RSYNCSERV ] && mv -f $OPENGNSYS/client/bin/rsync-$RSYNCSERV $OPENGNSYS/client/bin/rsync |
---|
| 331 | else |
---|
[3dd397f] | 332 | # Else, rename compiled file using Rsync protocol number. |
---|
[b495a61] | 333 | [ -e $OPENGNSYS/client/bin/rsync ] && mv -f $OPENGNSYS/client/bin/rsync $OPENGNSYS/client/bin/rsync-$($OPENGNSYS/client/bin/rsync --version 2>/dev/null | awk '/protocol/ {print $6}') |
---|
| 334 | fi |
---|
| 335 | # Unmount SquashFS. |
---|
| 336 | umount $TMPDIR |
---|
| 337 | rmdir $TMPDIR |
---|
| 338 | # Update JSON file. |
---|
[d244d7f] | 339 | addToJson "$OGLIVEDIST" "$OGLIVEKRNL" "$OGLIVEARCH" "$OGLIVEREV" "$OGLIVEDIR" "$OGLIVEFILE" |
---|
[b495a61] | 340 | } |
---|
| 341 | |
---|
| 342 | # Uninstall an ogLive client. |
---|
| 343 | function uninstall() { |
---|
| 344 | local ISO DIR INDEX DEFINDEX |
---|
| 345 | [ $# -ne 1 ] && raiseError usage |
---|
| 346 | [ ! -r $INFOFILE ] && raiseError access "Configuration file." |
---|
| 347 | [ ! -w $TFTPDIR ] && raiseError access "Installation directory." |
---|
| 348 | # Get index and directory for the entry. |
---|
| 349 | case "$1" in |
---|
| 350 | */*) # Error (access to other directory). |
---|
| 351 | raiseError access "Cannot access outside installation directory." |
---|
| 352 | ;; |
---|
[d063c01] | 353 | *.iso) # ISO file. |
---|
[b495a61] | 354 | ISO="$1" |
---|
| 355 | # Working directory (ogLive-Distribution-KernelVersion-CodeRevision). |
---|
| 356 | DIR="$(echo $ISO|cut -f1,3 -d-)-${ISO##*-}"; DIR=${DIR%.*} |
---|
| 357 | INDEX=$(search $DIR 2>/dev/null) |
---|
| 358 | ;; |
---|
| 359 | [0-9]*) # Index. |
---|
| 360 | INDEX=$1; DIR=$(search $INDEX 2>/dev/null) |
---|
| 361 | ;; |
---|
| 362 | *) # Directory. |
---|
| 363 | DIR="$1"; INDEX=$(search $DIR 2>/dev/null) |
---|
| 364 | ;; |
---|
| 365 | esac |
---|
| 366 | DEFINDEX=$(getdefault) |
---|
| 367 | [[ $INDEX = $DEFINDEX ]] && raiseError access "Cannot uninstall default ogLive." |
---|
| 368 | # Remove files and delete index entry. |
---|
[a79df90a] | 369 | rm -vfr ${ISO:+$DOWNLOADDIR/$ISO} ${DIR:+$TFTPDIR/$DIR} ### Remove $TFTPDIR/$DIR.old ? |
---|
[3554392] | 370 | if [ -n "$INDEX" ]; then |
---|
| 371 | jq "del(.oglive[$INDEX])" $INFOFILE | sponge $INFOFILE |
---|
| 372 | # Decrement default index if needed (removed < default). |
---|
[a30baf2] | 373 | [[ $INDEX < $DEFINDEX ]] && jq ".default=$((DEFINDEX-1))" $INFOFILE | sponge $INFOFILE |
---|
[3554392] | 374 | fi |
---|
[b495a61] | 375 | } |
---|
| 376 | |
---|
| 377 | # Get default ogLive index. |
---|
| 378 | function getdefault() { |
---|
| 379 | [ $# -ne 0 ] && raiseError usage |
---|
| 380 | [ ! -r $INFOFILE ] && raiseError access "Configuration file." |
---|
| 381 | # Read default parameter. |
---|
[3554392] | 382 | jq -r .default $INFOFILE || raiseError notfound "Undefined default index." |
---|
[b495a61] | 383 | } |
---|
| 384 | |
---|
| 385 | # Set default ogLive index. |
---|
| 386 | function setdefault() { |
---|
| 387 | local INDEX OGLIVEDIR |
---|
| 388 | [ $# -ne 1 ] && raiseError usage |
---|
| 389 | [ ! -w $INFOFILE ] && raiseError access "Configuration file." |
---|
| 390 | INDEX=$1 |
---|
| 391 | # Check if index entry exists. |
---|
[3554392] | 392 | jq ".oglive[$INDEX]" $INFOFILE || raiseError notfound "Index \"$INDEX\"." |
---|
[b495a61] | 393 | # Get ogLive directory. |
---|
[3554392] | 394 | OGLIVEDIR=$(jq -r ".oglive[$INDEX].directory" $INFOFILE) || raiseError notfound "Directory for index \"$INDEX\"." |
---|
[b495a61] | 395 | # Update default parameter. |
---|
[3554392] | 396 | jq ".default=$INDEX" $INFOFILE | sponge $INFOFILE |
---|
[b495a61] | 397 | # Link to default directory. |
---|
| 398 | rm -f $TFTPDIR/$DEFOGLIVE |
---|
| 399 | ln -vfs $(basename $OGLIVEDIR) $TFTPDIR/$DEFOGLIVE |
---|
| 400 | } |
---|
| 401 | |
---|
[d1776403] | 402 | # Rebuild a lost configuration file. |
---|
| 403 | function rebuild() { |
---|
[3dd397f] | 404 | local i INST NF DEF |
---|
[d1776403] | 405 | [ $# -ne 0 ] && raiseError usage |
---|
| 406 | [ -f $INFOFILE ] && raiseError access "Configuration file exists." |
---|
| 407 | INST=$(find $TFTPDIR/ -type d -name "$DEFOGLIVE-*" -a ! -name "*.old" -printf "%f\n" | sort) |
---|
| 408 | for i in $INST; do |
---|
| 409 | NF=$(echo $i | awk -F- '{print NF-1}') |
---|
| 410 | case $NF in |
---|
| 411 | 1) addToJson "" "$(echo $i|cut -f2 -d-)" "i386" "" "$i" "" ;; |
---|
[3dd397f] | 412 | 2) eval addToJson $(echo $i | awk -F- '{printf "\"\" %s amd64 %s %s \"\"",$2,$3,$0}') ;; |
---|
| 413 | 3) eval addToJson $(echo $i | awk -F- '{if ($3=="i386") printf "\"\" %s %s %s %s \"\"",$2,$3,$4,$0; else printf "%s %s i386 %s %s \"\"",$2,$3,$4,$0}') ;; |
---|
| 414 | 4) eval addToJson $(echo $i | awk -F- '{printf "%s %s %s %s %s \"\"",$2,$3,$4,$5,$0}') ;; |
---|
[d1776403] | 415 | esac |
---|
| 416 | # Check for is default oglive. |
---|
| 417 | [ -n "$(stat -c "%N" $TFTPDIR/$DEFOGLIVE | awk '$3~/'$i'/ {print}')" ] && DEF="$i" |
---|
| 418 | done |
---|
| 419 | # Set default ogLive. |
---|
| 420 | [ -n "$DEF" ] && setdefault $(search $DEF) |
---|
| 421 | } |
---|
| 422 | |
---|
[b495a61] | 423 | # Assign an ISO file to a JSON entry. |
---|
| 424 | function assign() { |
---|
| 425 | local ISOFILE DIR |
---|
| 426 | [ $# -ne 2 ] && raiseError usage |
---|
| 427 | [ ! -w $INFOFILE ] && raiseError access "Configuration file." |
---|
[3dd397f] | 428 | # Check if ISO file and index directory exist. |
---|
[b495a61] | 429 | ISOFILE=$DOWNLOADFILE/$1 |
---|
| 430 | [ ! -f $DOWNLOADDIR/$ISOFILE ] && raiseError notfound "ISO file \"$1\"." |
---|
| 431 | DIR=$(search $2 2>/dev/null) |
---|
| 432 | [ ! -d $TFTPDIR/$DIR ] && raiseError notfound "Directory for index \"$2\"." |
---|
| 433 | # Assign ISO file to JSON entry. |
---|
[3554392] | 434 | jq ".oglive[$2].iso=\"$1\"" $INFOFILE | sponge $INFOFILE && jq ".oglive[$2]" $INFOFILE |
---|
[b495a61] | 435 | } |
---|
| 436 | |
---|
| 437 | |
---|
| 438 | # Main progrram. |
---|
| 439 | |
---|
| 440 | # Access control. |
---|
[b8498a1] | 441 | [ -r $OPENGNSYS/www/controlacceso.php ] && ACCESS="web" |
---|
| 442 | [ "$USER" = "root" ] && ACCESS="root" |
---|
| 443 | [ -z "$ACCESS" ] && raiseError access "Need to be root." |
---|
[b495a61] | 444 | # Check dependencies. |
---|
[3554392] | 445 | which sponge &>/dev/null || raiseError notfound "Need to install \"moreutils\"." |
---|
[b495a61] | 446 | # Commands control. |
---|
[b8498a1] | 447 | shopt -s extglob |
---|
| 448 | case "$ACCESS" in |
---|
[240a4dc] | 449 | root) CMDS='+(help|version|convert|config|check|list|show|search|download|install|uninstall|get-default|set-default|rebuild|assign)' ;; |
---|
[b8498a1] | 450 | web) CMDS='+(list|show|search|get-default)' ;; |
---|
| 451 | esac |
---|
[b495a61] | 452 | case "$1" in |
---|
[b8498a1] | 453 | $CMDS) COMMAND="${1/-/}"; shift; $COMMAND "$@" ;; |
---|
| 454 | *) raiseError usage ;; |
---|
[b495a61] | 455 | esac |
---|
| 456 | |
---|
| 457 | exit $? |
---|
| 458 | |
---|