Hi,

First version of a script-shell to save and restore a CKRM
configuration.

Save this script into ckrmsave and create a symbolic link
named ckrmrestore.

Usage: ckrmsave/ckrmrestore [<options>]
ckrmsave    to save the current configuration.
ckrmrestore to restore the last saved configuration.
options      description
-v           verbose
-h           display this help


Comments :
Use the /ckrm directory by default. This can be changed with the
CKRM_SAVE_RESTORE_DIR variable from your environment.

ckrmsave :
Archive the previous saved configuration if present in a tar
file.

ckrmrestore :
Simplified version to restore the last saved configuration after
a reboot : just overwrite existing files.


A valid scenario :

./ckrminit                                // mount rcfs on /rcfs
./ckrmtask -c c1 c2                // create taskclass c1 and c2

./ckrmtask -s c1 res=mem,max_limit=90                  // set c1

./ckrmrules -c r1 cmd=bash,class=c1          // create a rule r1

./ckrmsocket -c s1                      // create socketclass s1
./ckrmsocket -m 0.0.0.0:513 s1          // move a socket into s1

./ckrmrules -c r2 cmd=telnet,class=s1        // create a rule r2
./ckrmsave

< umount /rcfs, reboot, ...>

./ckrminit        // the Rule Base CE is working with /rcfs only
./ckrmrestore


All commands are made for linux-2.6.12-rc3 with Gerrit's patches.


Enjoy,
Patrick

+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+    Patrick Le Dot
 mailto: [EMAIL PROTECTED]@bull.net         Centre UNIX de BULL S.A.
 Phone : +33 4 76 29 73 20               1, Rue de Provence        B.P. 208
 Fax   : +33 4 76 29 76 00               38130 ECHIROLLES Cedex     FRANCE
 http://www.bull.com

===================================================================
#!/bin/bash
#
# Save and restore a ckrm configuration :
# taskclass, socketclass and rules.

RCFS=rcfs

# User's preference else directory by default
#
ROOT=${CKRM_SAVE_RESTORE_DIR:-/ckrm}
RCFS_MOUNT=${CKRM_RCFS_MOUNT_POINT:-/$RCFS}
BIN=/bin

# Names of the save/restore commands
CKRMSAVE=ckrmsave
CKRMRESTORE=ckrmrestore

# Internals goodies
#
RBCE_DIR=$RCFS_MOUNT/ce
RULE_DIR=$RCFS_MOUNT/ce/rules
TASKCLASS_DIR=$RCFS_MOUNT/taskclass
SOCKETCLASS_DIR=$RCFS_MOUNT/socketclass

CONFIG=config
MEMBERS=members
SHARES=shares
IPV4="ipv4="
TARGET=target
VERBOSE=

# Directory and files used to save/restore a config
#
CKRMADMIN=ckrm_admin
DEST=$ROOT/$CKRMADMIN
FILE1=$DEST/taskclass.config
FILE2=$DEST/taskclass.classes+shares
FILE3=$DEST/socketclass.classes+shares
FILE_RULE=$DEST/rules

# Empty class
#
NO_DATA_TO_DISPLAY="No data to display"

# To restore a rule in another mount point
#
MOUNT_POINT_MASK="MOUNT_POINT_MASK"

# End of class mark
#
KEY_WORD="END_OF_CLASS"

# Archive file id suffix
#
SUFFIX=$(date +%m%d-%H%M%S)


Usage(){
echo "Usage: $(basename $0) [<options>]
$CKRMSAVE    to save the current configuration.
$CKRMRESTORE to restore the last saved configuration.
options      description
-v           verbose
-h           display this help
"
}

ckrm_save() {
#
# Archive the previous saved configuration if present
#
if [ -d $DEST ]
then
  echo "Archive the previous copy in $ROOT/$CKRMADMIN.$SUFFIX.tar"
  cd $ROOT
  tar cf $CKRMADMIN.$SUFFIX.tar ./$CKRMADMIN
  rm -rf ./$CKRMADMIN
else
  # first call
  echo -ne "Use the $ROOT directory to save/restore the configuration ([y]/n) ? 
\c"
  read REP
  if [ "$REP" = n ]
  then
    echo "Please set the CKRM_SAVE_RESTORE_DIR variable in your environment."
    exit 0
  fi
fi
mkdir $VERBOSE -p $DEST

if [ -d $TASKCLASS_DIR ]
then
  # Create a copy of the taskclass config file.
  #
  # Kernel oops on write with 2.6.12+ckrm
  # temporary WA : do not create the file
  #  cat $TASKCLASS_DIR/$CONFIG > $FILE1

  # Create a file to save all classes and shares
  # Iterative walk of the tree (find cmd) and relative path
  # to restore in another directory
  # Add a special delimiter after each class
  #
  cd $TASKCLASS_DIR
  (for ITEM in $(find . -type d)
  do
    # remove . and ./
    DIR=$(echo $ITEM | cut -c 3-)
    if [ "$DIR" != "" -a -d "$DIR" ]
    then
      # save the name and the shares file of the class
      echo $DIR
      cat  $DIR/$SHARES
      echo $KEY_WORD
    fi
  done) > $FILE2
  if [ "$VERBOSE" ]
  then
    echo saved in the $FILE2 file :
    cat $FILE2 | grep -v $KEY_WORD
  fi
fi

if [ -d $SOCKETCLASS_DIR ]
then
  # Create a file to save all socket classes
  #
  cd $SOCKETCLASS_DIR
  (for DIR in $($BIN/ls)
  do
    if [ -d $DIR ]
    then
      # save name and members of the socket class
      echo $DIR
      cat  $DIR/$MEMBERS
#     TODO LATER ?
#     cat  $DIR/$CONFIG
#     cat  $DIR/$SHARES
      echo $KEY_WORD
    fi
  done) > $FILE3
  if [ "$VERBOSE" ]
  then
    echo saved in the $FILE3 file :
    cat $FILE3 | grep -v $KEY_WORD
  fi
fi

if [ -d $RBCE_DIR ]
then
  # Create a file to save all rules.
  # The first line is the rbce state
  #
  (cd $RBCE_DIR
  cat rbce_state

  cd $RULE_DIR
  for RULE in $($BIN/ls)
  do
    echo $RULE
    # mask the mount point to restore the rule in another
    # environment.
    cat  $RULE | sed "s/\\$RCFS_MOUNT/$MOUNT_POINT_MASK/"
  done) > $FILE_RULE
  if [ "$VERBOSE" ]
  then
    echo saved in the $FILE_RULE file :
    cat $FILE_RULE
  fi
fi
}
# End of ckrm_save


ckrm_restore() {
#
# Simplified version to restore a configuration after a reboot.
# Calling ckrm_restore on the current configuration overwrite
# existing files.
#
if [ ! -d $DEST ]
then
  echo "No saved configuration found in $ROOT..."
  echo "Please check the CKRM_SAVE_RESTORE_DIR variable in your environment."
  exit 0
fi

# Task and socket classes should be restored (created) before the rules.
#
if [ -d $TASKCLASS_DIR ]
then
  cd $TASKCLASS_DIR
  #
  # taskclass/config file
  # paranoid sanity check
  #
  if [ ! -f $CONFIG ]
  then
    echo "Something bad : the $TASKCLASS_DIR/$CONFIG file does not exist - 
abort"
    exit 2
  fi
  if [ -s $FILE1 ]
  then
    # overwrite the current file
    while read LINE
    do
    echo "$LINE" | cat > $CONFIG
    done < $FILE1
    if [ "$VERBOSE" ]
    then
      echo the $CONFIG file has been restored with :
      cat $CONFIG
    fi
  fi

  # taskclass/classes and shares files
  #
  if [ -s $FILE2 ]
  then
    (while read CLASS_NAME
    do
      if [ ! -d $CLASS_NAME ]
      then
        mkdir $VERBOSE $CLASS_NAME
      fi
      while read LINE
      do
        if [ "$LINE" != $KEY_WORD ]
        then
          echo "$LINE" | cat > $CLASS_NAME/$SHARES
        else
          break
        fi
      done
      if [ "$VERBOSE" ]
      then
        echo the class $CLASS_NAME has been restored with :
        cat $CLASS_NAME/$SHARES
      fi
    done) < $FILE2
  fi
else
  echo "Cannot restore : $TASKCLASS_DIR not initialized."
fi

if [ -d $SOCKETCLASS_DIR ]
then
  cd $SOCKETCLASS_DIR
  #
  # socketclass/classes
  #
  if [ -s $FILE3 ]
  then
    (while read CLASS_NAME
    do
      if [ ! -d $CLASS_NAME ]
      then
        mkdir $VERBOSE $CLASS_NAME
      fi
      while read LINE
      do
        # socketclass/members file only
        if [ "$LINE" != $KEY_WORD ]
        then
          if [ "$LINE" != "$NO_DATA_TO_DISPLAY" ]
          then
            echo "$IPV4$LINE" | cat > $CLASS_NAME/$TARGET
          fi
        else
          break
        fi
      done
      if [ "$VERBOSE" ]
      then
        echo the socketclass $CLASS_NAME has been restored with :
        cat $CLASS_NAME/$MEMBERS
      fi
    done) < $FILE3
  fi
else
  if [ -s $FILE3 ]
  then
    echo "Cannot restore : $SOCKETCLASS_DIR not initialized."
  fi
fi

if [ -d $RBCE_DIR ]
then
  #
  # Restore the rbce state and rules
  #
  if [ -s $FILE_RULE ]
  then
    (cd $RBCE_DIR
    read STATE
    echo "$STATE" | cat > rbce_state

    cd $RULE_DIR
    while read RULE_NAME
    do
      read RULE
      # restore the rule with the current mount point
      RULE_TO_RESTORE=$(echo "$RULE" | sed "s/$MOUNT_POINT_MASK/\\$RCFS_MOUNT/")
      if [ "$VERBOSE" ]
      then
        echo restore the rule $RULE_NAME with
        echo $RULE_TO_RESTORE
      fi
      echo "$RULE_TO_RESTORE" | cat > $RULE_NAME
    done) < $FILE_RULE
  fi
else
  if [ -s $FILE_RULE ]
  then
    echo "Cannot restore : $RBCE_DIR not initialized."
  fi
fi
}
# End of ckrm_restore


# main
#
NAME=$(basename $0)

if [ "$($BIN/ls $RCFS_MOUNT)" = "" ]
then
  echo "Sorry, cannot save/restore : CKRM is not initialized."
  exit 0
fi

# Simplified version : no option yet
#
while getopts "h?-:v" option_name
do
  case $option_name in
  h|'?')
     Usage
     exit 0
     ;;
  -) # --help
     if [ $OPTARG = help ]
     then
       Usage
       exit 0
     fi
     ;;
  v) # verbose
     VERBOSE=-v
  esac
done

case $NAME in
  ckrmsave)     ckrm_save ;;
  ckrmrestore)  ckrm_restore ;;
  *)            echo "$0 : unknown command name"
                exit 0 ;;
esac

echo "$0 : done."



-------------------------------------------------------
This SF.Net email is sponsored by Yahoo.
Introducing Yahoo! Search Developer Network - Create apps using Yahoo!
Search APIs Find out how you can build Yahoo! directly into your own
Applications - visit http://developer.yahoo.net/?fr=offad-ysdn-ostg-q22005
_______________________________________________
ckrm-tech mailing list
https://lists.sourceforge.net/lists/listinfo/ckrm-tech

Reply via email to