1 #!/sbin/sh
   2 #
   3 # CDDL HEADER START
   4 #
   5 # The contents of this file are subject to the terms of the
   6 # Common Development and Distribution License (the "License").
   7 # You may not use this file except in compliance with the License.
   8 #
   9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10 # or http://www.opensolaris.org/os/licensing.
  11 # See the License for the specific language governing permissions
  12 # and limitations under the License.
  13 #
  14 # When distributing Covered Code, include this CDDL HEADER in each
  15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16 # If applicable, add the following below this CDDL HEADER, with the
  17 # fields enclosed by brackets "[]" replaced with your own identifying
  18 # information: Portions Copyright [yyyy] [name of copyright owner]
  19 #
  20 # CDDL HEADER END
  21 #
  22 #
  23 # Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
  24 # Copyright 2017 Nexenta Systems, Inc. All rights reserved.
  25 #
  26 
  27 . /lib/svc/share/smf_include.sh
  28 . /lib/svc/share/fs_include.sh
  29 
  30 #
  31 # mksavedir
  32 # Make sure that $DUMPADM_SAVDIR is set and exists.
  33 #
  34 mksavedir ()
  35 {
  36         [ -z "$DUMPADM_SAVDIR" ] && DUMPADM_SAVDIR=/var/crash
  37         [ -d "$DUMPADM_SAVDIR" ] || /usr/bin/mkdir -m 0700 -p $DUMPADM_SAVDIR
  38 }
  39 
  40 #
  41 # We haven't run savecore on a dump device yet
  42 #
  43 savedev=none
  44 
  45 #
  46 # If we previously crashed early in boot before dumpadm was used to configure
  47 # an alternate dump device, then the dump is in the primary swap partition,
  48 # which was configured as the dump device by the first swapadd early in boot.
  49 # Thus before we run dumpadm to configure the dump device, we first run
  50 # savecore to check the swap partition for a dump;  this is run in the
  51 # foreground to reduce the chances of overwriting the dump.
  52 #
  53 # This does not apply for zfs root systems that use a zvol for dump;
  54 # for such systems the dedicated dump device is appointed during startup
  55 # of the filesystem/usr:default instance before any swap is added.
  56 # Therefore we must check that the dump device is a swap device here -
  57 # if not then we'll run savecore here in the foreground and prevent
  58 # our dependent services coming online until we're done.
  59 #
  60 
  61 rootiszfs=0
  62 alreadydedicated=0
  63 
  64 readmnttab / </etc/mnttab
  65 if [ "$fstype" = zfs ] ; then
  66         rootiszfs=1
  67         if [ -x /usr/sbin/dumpadm ]; then
  68                 if /usr/sbin/dumpadm 2>/dev/null | grep "Dump device:" | \
  69                     grep '(dedicated)' > /dev/null 2>&1; then
  70                         alreadydedicated=1
  71                 fi
  72         fi
  73 fi
  74 
  75 if [ -x /usr/bin/savecore -a \
  76     \( ! $rootiszfs -eq 1 -o $alreadydedicated -eq 0 \) ]; then
  77         [ -r /etc/dumpadm.conf ] && . /etc/dumpadm.conf
  78 
  79         if [ "x$DUMPADM_ENABLE" != xno ] && mksavedir; then
  80                 /usr/bin/savecore $DUMPADM_SAVDIR
  81                 shift $#
  82                 set -- `/usr/sbin/dumpadm 2>/dev/null | /usr/bin/grep 'device:'`
  83                 savedev=${3:-none}
  84         else
  85                 #
  86                 # dumpadm -n is in effect, but we can still run savecore
  87                 # to raise an event with initial panic detail extracted
  88                 # from the dump header.
  89                 #
  90                 /usr/bin/savecore -c
  91         fi
  92 fi
  93 
  94 if [ ! -x /usr/bin/savecore ]; then
  95         echo "WARNING: /usr/bin/savecore is missing or not executable" >& 2
  96 fi
  97 
  98 #
  99 # Now run dumpadm to configure the dump device based on the settings
 100 # previously saved by dumpadm.  See dumpadm(1m) for instructions on
 101 # how to modify the dump settings.
 102 #
 103 if [ -x /usr/sbin/dumpadm ]; then
 104         /usr/sbin/dumpadm -u || $SMF_EXIT_ERR_CONFIG
 105 else
 106         echo "WARNING: /usr/sbin/dumpadm is missing or not executable" >& 2
 107         exit $SMF_EXIT_ERR_CONFIG
 108 fi
 109 
 110 if [ -r /etc/dumpadm.conf ]; then
 111         . /etc/dumpadm.conf
 112 else
 113         echo "WARNING: /etc/dumpadm.conf is missing or unreadable" >& 2
 114         exit $SMF_EXIT_ERR_CONFIG
 115 fi
 116 
 117 #
 118 # If the savecore executable is absent then we're done
 119 #
 120 if [ ! -x /usr/bin/savecore ]; then
 121         exit $SMF_EXIT_ERR_CONFIG
 122 fi
 123 
 124 #
 125 # Now that dumpadm has reconfigured /dev/dump, we need to run savecore again
 126 # because the dump device may have changed.  If the earlier savecore had
 127 # saved the dump, savecore will just exit immediately.
 128 #
 129 
 130 isswap=0
 131 swapchanged=0
 132 if /usr/sbin/swap -l 2>/dev/null | grep "^${DUMPADM_DEVICE} " \
 133             >/dev/null 2>&1; then
 134         isswap=1
 135         if [ "x$savedev" != "x$DUMPADM_DEVICE" ]; then
 136                 swapchanged=1
 137         fi
 138 fi
 139 
 140 if [ "x$DUMPADM_ENABLE" != xno ]; then
 141         if [ $isswap -eq 1 ]; then
 142                 #
 143                 # If the dump device is part of swap, we only need to run
 144                 # savecore a second time if the device is different from the
 145                 # swap device on which we initially ran savecore.
 146                 #
 147                 if [ $swapchanged -eq 1 ]; then
 148                     mksavedir && /usr/bin/savecore $DUMPADM_SAVDIR &
 149                 fi
 150         else
 151                 #
 152                 # The dump device couldn't have been dedicated before we
 153                 # ran dumpadm, so we must execute savecore again.
 154                 #
 155                 mksavedir && /usr/bin/savecore $DUMPADM_SAVDIR &
 156         fi
 157 else
 158                 #
 159                 # savecore not enabled.  Check whether a valid dump is
 160                 # present on the device and raise an event to signal that,
 161                 # but avoid sending a duplicate event from the savecore -c
 162                 # earlier.
 163                 #
 164                 if [ $isswap -eq 0 -o $swapchanged -eq 1 ]; then
 165                         /usr/bin/savecore -c
 166                 fi
 167 fi
 168 
 169 exit $SMF_EXIT_OK