Advertisement
Guest User

Untitled

a guest
Jan 27th, 2016
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.98 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. CACHEPARAMS="512 1 writethrough default 0"
  4. CACHEDEVICES="o=/dev/mapper/storage c=/dev/mapper/suse-cache"
  5. MAPPER="storagecached"
  6.  
  7. if [ "$(id -u)" != "0" ] ; then
  8.   echo "Must be run as root - exiting"
  9.   exit
  10. fi
  11.  
  12. if [ "$1" == "-u" ] ; then
  13. {
  14.   for i in $CACHEDEVICES ; do
  15.     if [ "`echo $i | grep \"^c=\"`" != "" ] ; then
  16.       __CACHEDEV=${i:2}
  17.     elif [ "`echo $i | grep \"^o=\"`" != "" ] ; then
  18.       __ORIGINALDEV=${i:2}
  19.     fi
  20.   done
  21.  
  22.   echo \^ $MAPPER \| $__CACHEDEV \$
  23.  
  24.   dmsetup suspend $MAPPER
  25.   dmsetup remove $MAPPER
  26.   dmsetup remove `basename $__CACHEDEV`-blocks
  27.   dmsetup remove `basename $__CACHEDEV`-metadata
  28.  
  29.   unset __ORIGINALDEV
  30.   unset __CACHEDEV
  31. }
  32.  
  33. else
  34.  
  35. {
  36.   for i in $CACHEDEVICES ; do
  37.     if [ "`echo $i | grep \"^c=\"`" != "" ] ; then
  38.       __CACHEDEV=${i:2}
  39.     elif [ "`echo $i | grep \"^o=\"`" != "" ] ; then
  40.       __ORIGINALDEV=${i:2}
  41.     fi
  42.   done
  43.  
  44.   if [ "$__CACHEDEV" == "" ] ; then
  45.     echo -e "${C_LIGHT_RED}Devices list invalid${C_OFF}" ; exit ; fi
  46.    
  47.   if [ "$__ORIGINALDEV" == "" ] ; then
  48.     echo -e "${C_LIGHT_RED}Devices list invalid${C_OFF}" ; exit ; fi
  49.  
  50.   if [ -e "$__CACHEDEV" ] ; then echo -n
  51.   else
  52.     echo -e "${C_LIGHT_RED}Devices list invalid${C_OFF}" ; exit ; fi
  53.    
  54.   if [ -e "$__ORIGINALDEV" ] ; then echo -n
  55.   else
  56.     echo -e "${C_LIGHT_RED}Devices list invalid${C_OFF}" ; exit ; fi
  57.    
  58.   __CACHEDEVSIZE="`blockdev --getsize64 \"$__CACHEDEV\"`"
  59.  
  60.   __CACHEMETASIZE="$(((4194304 + (16 * $__CACHEDEVSIZE / 262144))/512))"
  61.   if [ "$__CACHEMETASIZE" == ""$(((4194303 + (16 * $__CACHEDEVSIZE / 262144))/512))"" ] ; then
  62.     __CACHEMETASIZE="$(($__CACHEMETASIZE + 1))" ; fi
  63.    
  64.   __CACHEBLOCKSSIZE="$((($__CACHEDEVSIZE/512) - $__CACHEMETASIZE))"
  65.  
  66.   __ORIGINALDEVSIZE="`blockdev --getsz $__ORIGINALDEV`"
  67.  
  68.   echo \^ $__CACHEDEV \| $__CACHEMETASIZE \| $__CACHEBLOCKSSIZE \| $MAPPER \| $__ORIGINALDEVSIZE \| $__ORIGINALDEV \| $CACHEPARAMS \$
  69.  
  70.   echo dmsetup create `basename $__CACHEDEV`-metadata --table "0 $__CACHEMETASIZE linear /dev/mapper/suse-cache 0"
  71.   echo dmsetup create `basename $__CACHEDEV`-blocks --table "0 $__CACHEBLOCKSSIZE linear /dev/mapper/suse-cache $__CACHEMETASIZE"
  72.   echo dmsetup create $MAPPER --table "0 $__ORIGINALDEVSIZE cache /dev/mapper/`basename $__CACHEDEV`-metadata /dev/mapper/`basename $__CACHEDEV`-blocks $__ORIGINALDEV $CACHEPARAMS"
  73.   echo dmsetup resume $MAPPER
  74.  
  75.  
  76.   dmsetup create `basename $__CACHEDEV`-metadata --table "0 $__CACHEMETASIZE linear /dev/mapper/suse-cache 0"
  77.   dmsetup create `basename $__CACHEDEV`-blocks --table "0 $__CACHEBLOCKSSIZE linear /dev/mapper/suse-cache $__CACHEMETASIZE"
  78.   dmsetup create $MAPPER --table "0 $__ORIGINALDEVSIZE cache /dev/mapper/`basename $__CACHEDEV`-metadata /dev/mapper/`basename $__CACHEDEV`-blocks $__ORIGINALDEV $CACHEPARAMS"
  79.   dmsetup resume $MAPPER
  80.  
  81.   unset __ORIGINALDEV
  82.   unset __ORIGINALDEVSIZE
  83.   unset __CACHEDEV
  84.   unset __CACHEDEVSIZE
  85.   unset __CACHEMETASIZE
  86.   unset __CACHEBLOCKSSIZE
  87. }
  88. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement