Guest User

Untitled

a guest
Nov 24th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. log_name="filer"
  4. log_prio="user.notice"
  5.  
  6. flog () {
  7. logger -p "$log_prio" -t "$log_name" "$1"
  8. }
  9.  
  10. boottime=$(sysctl -n kern.boottime | awk '{print $4}' | tr -dc 0-9)
  11. now=$(date +%s)
  12. delta=$(($now-$boottime))
  13.  
  14. # This is to prevent the script to be executed at boot. If the two
  15. # machines are powered on at the same time there is a risk that the
  16. # CARP status switches rapidly in a few seconds, for example:
  17. # bge0: link state changed to DOWN
  18. # bge0: promiscuous mode enabled
  19. # carp: demoted by 240 to 240 (interface down)
  20. # carp: VHID 54@bge0: INIT -> BACKUP
  21. # carp: demoted by -240 to 0 (interface up)
  22. # bge0: link state changed to UP
  23. # carp: VHID 54@bge0: BACKUP -> MASTER (master down)
  24. # carp: VHID 54@bge0: MASTER -> BACKUP (more frequent advertisement received)
  25. # Also, don't do failover if there is a /root/scripts/NO_FAILOVER file,
  26. # in case we have to reboot the MASTER for some maintenance
  27. # (freebsd-update, updates, etc) or ...
  28. if [ $delta -lt 180 -o -f /root/scripts/NO_FAILOVER ] ; then
  29. flog "Failover script skipped (delta: $delta)"
  30. exit
  31. else
  32. flog "Running failover script (delta: $delta)"
  33. fi
  34.  
  35. case "$1" in
  36. MASTER)
  37. if [ -f /root/scripts/NO_FAILOVER_MASTER ] ; then
  38. flog "No failover: /root/scripts/NO_FAILOVER_MASTER exists"
  39. exit
  40. fi
  41. # This is run when the machine WAS a SLAVE and becomes the new
  42. # MASTER. Basically we do the following:
  43. # 1) Shutdown the replication interface so that we are sure that
  44. # no data is written on the iSCSI disks
  45. # 2) Change the advskew of the CARP interface, so that the OLD
  46. # MASTER never returns (and adapt rc.conf)
  47. # 3) Shutdown the CAM Target Layer / iSCSI target daemon. At this
  48. # stage the disks are *unlocked*
  49. # 4) Import the shared pool (data). As we turned off the
  50. # replication interface an import -f should be safe (and needed
  51. # as the old MASTER may have been restarted roughly: powerloss,
  52. # etc)
  53. # 5) Start the NFS services (and adapt rc.conf)
  54. flog "Shutting down the replication interface"
  55. ifconfig bge1 down
  56. sysrc ifconfig_bge1="down"
  57.  
  58. # Backup is the new master
  59. flog "Adapting advskew on the main interface"
  60. sysrc ifconfig_bge0_alias0="inet vhid 54 advskew 10 pass xxx alias 192.168.10.15/32"
  61. ifconfig bge0 vhid 54 advskew 10
  62.  
  63. flog "Shutting down the CTLD daemon"
  64. sysrc ctld_enable="NO"
  65. service ctld stop
  66. while pgrep -u root ctld > /dev/null ; do
  67. sleep 0.01
  68. done
  69.  
  70. flog "Import data zpool"
  71. zpool import -f -o cachefile=none data 2> /dev/null
  72.  
  73. flog "Enable NFS services in rc.conf"
  74.  
  75. if [ ! -f /etc/exports ] ; then
  76. echo "V4: /data -sec=sys" > /etc/exports
  77. fi
  78.  
  79. sysrc mountd_enable="YES"
  80. sysrc mountd_flags="-r -S -h 192.168.10.15"
  81. sysrc nfs_server_enable="YES"
  82. sysrc nfs_server_flags="-t -h 192.168.10.15"
  83. sysrc nfsuserd_enable="YES"
  84. sysrc nfsuserd_flags="-domain prod.lan"
  85. sysrc nfsv4_server_enable="YES"
  86.  
  87. flog "Start NFS services (nfsuserd, nfsd)"
  88. service nfsuserd start
  89. service nfsd start
  90. ;;
  91. BACKUP)
  92. if [ -f /root/scripts/NO_FAILOVER_SLAVE ] ; then
  93. flog "No failover: /root/scripts/NO_FAILOVER_SLAVE exists"
  94. exit
  95. fi
  96. # This is run when the machine WAS a MASTER and becomes a SLAVE.
  97. # It's too risky to be done automatically as we risk to corrupt the
  98. # zpool, it's the task of the sysadmin to do that manually.
  99. flog "Stop NFS services (nfsuserd, nfsd)"
  100. service nfsuserd stop
  101. service nfsd stop
  102. service mountd stop
  103.  
  104. flog "Disable NFS services in rc.conf"
  105. sysrc mountd_enable="NO"
  106. sysrc nfs_server_enable="NO"
  107. sysrc nfsuserd_enable="NO"
  108. sysrc nfsv4_server_enable="NO"
  109. ;;
  110. esac
  111.  
  112. #####
  113.  
  114. filer2% pwd
  115. /usr/local/etc/devd
  116. filer2% cat filer_failover.conf
  117. notify 0 {
  118. match "system" "CARP";
  119. match "subsystem" "54@bge0";
  120. match "type" "(MASTER|BACKUP)";
  121. action "/root/scripts/filer_failover.sh $type";
  122. };
Add Comment
Please, Sign In to add comment