Advertisement
rockdrilla

/etc/init.d/cgconfig [rev.1]

Nov 10th, 2013
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides:            cgconfig
  4. # Required-Start:      $local_fs $syslog
  5. # Required-Stop:       $local_fs $syslog
  6. # Should-Start:        
  7. # Should-Stop:        
  8. # Short-Description:   create and setup control group filesystem(s)
  9. # Description:         Create and setup control group filesystem(s)
  10. ### END INIT INFO
  11.  
  12. NAME=cgconfig
  13. CONFIG=/etc/cgconfig.conf
  14. CONF_D=/etc/cgroups.d
  15. CONF_RULES=/etc/cgrules.conf
  16. DEFAULTS=/etc/default/${NAME}
  17. LOCKFILE=/run/lock/${NAME}.lock
  18. CGPARSER_BIN=/usr/sbin/cgconfigparser
  19. CGROOT=/sys/fs/cgroup
  20.  
  21. . /lib/lsb/init-functions
  22.  
  23. test -x ${CGPARSER_BIN} || {
  24.     log_failure_msg "${NAME}: missing executable ${CGPARSER_BIN}"
  25.     return 1
  26. }
  27.  
  28. CREATE_DEFAULT=yes
  29. test -s ${DEFAULTS} && . ${DEFAULTS}
  30.  
  31. test -n "${CGROUP_LOGLEVEL}" && export CGROUP_LOGLEVEL
  32.  
  33. create_default_groups() {
  34.     defaultcgroup=
  35.  
  36.     test -s ${CONF_RULES} && {
  37.         L=$(grep -m1 '^\*[[:space:]]\+' ${CONF_RULES})
  38.         T=$(mktemp)
  39.         echo "$L" > $T
  40.         read user ctrl defaultcgroup < $T
  41.         rm $T
  42.         test -n "${defaultcgroup}" -a "${defaultcgroup}" = "*" && {
  43.             log_warning_msg "${CONF_RULES} incorrect; overriding it"
  44.             defaultcgroup=
  45.         }
  46.     }
  47.  
  48.     test -z "${defaultcgroup}" && {
  49.         defaultcgroup=default/
  50.     }
  51.  
  52.     # find all mounted subsystems and create comma-separated list
  53.     # of controllers.
  54.     controllers=$(lssubsys 2>/dev/null | tr '\n' ',' | sed -e 's/.$//')
  55.  
  56.     # create the default group, ignore errors when the default group
  57.     # already exists.
  58.     cgcreate -f 664 -d 775 -g ${controllers}:${defaultcgroup} 2>/dev/null
  59.  
  60.     # special rule for cpusets
  61.     echo ${controllers} | grep -q -w cpuset && {
  62.         cpus=$(cgget -nv -r cpuset.cpus /)
  63.         cgset -r cpuset.cpus=${cpus} ${defaultcgroup}
  64.         mems=$(cgget -nv -r cpuset.mems /)
  65.         cgset -r cpuset.mems=${mems} ${defaultcgroup}
  66.     }
  67.  
  68.     # classify everything to default cgroup. Ignore errors, some processes
  69.     # may exit after ps is run and before cgclassify moves them.
  70.     cgclassify -g ${controllers}:${defaultcgroup} $(ps --no-headers -eL o tid) \
  71.     2>/dev/null || :
  72. }
  73.  
  74. start() {
  75.     test -f ${LOCKFILE} && {
  76.         log_warning_msg "${NAME}: already running"
  77.         return 0
  78.     }
  79.  
  80.     test -s ${CONFIG} || {
  81.         log_failure_msg "${NAME}: not configured"
  82.         return 6
  83.     }
  84.  
  85.     mountpoint -q ${CGROOT} || {
  86.         mount -t tmpfs cgroup_root ${CGROOT} -o mode=0755 || {
  87.             log_failure_msg "${NAME}: failed to mount cgroup_root"
  88.             return 1
  89.         }
  90.     }
  91.  
  92.     ${CGPARSER_BIN} -l ${CONFIG} || {
  93.         log_failure_msg "${NAME}: failed to parse ${CONFIG}"
  94.         return 1
  95.     }
  96.  
  97.     test ${CREATE_DEFAULT} = "yes" && {
  98.         create_default_groups || {
  99.             log_failure_msg "${NAME}: failed to set up default groups"
  100.             return 1
  101.         }
  102.     }
  103.  
  104.     umask 0077
  105.     touch ${LOCKFILE} || {
  106.         log_failure_msg "${NAME}: failed to create lock file"
  107.         return 1
  108.     }
  109.  
  110.     log_success_msg "${NAME}: starting"
  111.     return 0
  112. }
  113.  
  114. stop() {
  115. #   cgclear -l ${CONFIG} || {
  116.     cgclear || {
  117.         log_failure_msg "${NAME}: failed to clear cgroups"
  118.         return 1
  119.     }
  120.  
  121.     mountpoint -q ${CGROOT} && {
  122.         umount ${CGROOT} || {
  123.             log_failure_msg "${NAME}: failed to unmount cgroup_root"
  124.             return 1
  125.         }
  126.     }
  127.  
  128.     rm -f ${LOCKFILE}
  129.  
  130.     log_success_msg "${NAME}: stopping"
  131.     return 0
  132. }
  133.  
  134. restart() {
  135.     stop && sleep 1
  136.     start
  137. }
  138.  
  139. status() {
  140.     test -f ${LOCKFILE} && {
  141.         log_success_msg "${NAME}: running"
  142.     } || {
  143.         log_failure_msg "${NAME}: stopped"
  144.         return 3
  145.     }
  146.     return 0
  147. }
  148.  
  149. usage() {
  150.     echo "Usage: $0 {start|stop|status|restart|reload|force-reload}" >&2
  151.     exit 2
  152. }
  153.  
  154. RETVAL=0
  155. case "$1" in
  156. stop)
  157.     stop
  158.     RETVAL=$?
  159. ;;
  160. start)
  161.     start
  162.     RETVAL=$?
  163. ;;
  164. restart|reload)
  165.     restart
  166.     RETVAL=$?
  167. ;;
  168. force-reload)
  169.     rm -f ${LOCKFILE}
  170.     restart
  171.     RETVAL=$?
  172. ;;
  173. status)
  174.     status
  175.     RETVAL=$?
  176. ;;
  177. *)
  178.     usage
  179. ;;
  180. esac
  181.  
  182. exit ${RETVAL}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement