#!/sbin/sh # # CDDL HEADER START # # The contents of this file are subject to the terms of the # Common Development and Distribution License, Version 1.0 only # (the "License"). You may not use this file except in compliance # with the License. # # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE # or http://www.opensolaris.org/os/licensing. # See the License for the specific language governing permissions # and limitations under the License. # # When distributing Covered Code, include this CDDL HEADER in each # file and include the License file at usr/src/OPENSOLARIS.LICENSE. # If applicable, add the following below this CDDL HEADER, with the # fields enclosed by brackets "[]" replaced with your own identifying # information: Portions Copyright [yyyy] [name of copyright owner] # # CDDL HEADER END # # # Copyright 2012 Nexenta Sysytems, Inc. All rights reserved. # Copyright 2005 Sun Microsystems, Inc. All rights reserved. # Use is subject to license terms. # PATH=/sbin:/usr/bin:/usr/sbin LC_ALL=C export PATH LC_ALL . /lib/svc/share/smf_include.sh . /lib/svc/share/fs_include.sh usage() { echo "usage: $0 [-r rootdir]" >&2 echo " See http://illumos.org/msg/SMF-8000-MY for more information on the use of this script." exit 2; } repositorydir=etc/svc repository=repository myroot=/ while getopts r: opt; do case "$opt" in r) myroot=$OPTARG if [ ! -d $myroot ]; then echo "$myroot: not a directory" >&2 exit 1 fi # validate directory and make sure it ends in '/'. case "$myroot" in //*) echo "$myroot: must begin with a single /" >&2 usage;; /) echo "$myroot: alternate root cannot be /" >&2 usage;; /*/) ;; # ends with / /*) myroot="$myroot/";; # add final / *) echo "$myroot: must be a full path" >&2 usage;; esac;; ?) usage;; esac done if [ $OPTIND -le $# ]; then # getopts(1) didn't slurp up everything. usage fi # # Note that the below test is carefully constructed to fail *open*; if # anything goes wrong, it will drive onward. # if [ -x /usr/bin/id -a -x /usr/bin/grep ] && /usr/bin/id 2>/dev/null | /usr/bin/grep -v '^[^=]*=0(' >/dev/null 2>&1; then echo "$0: may only be invoked by root" >&2 exit 2 fi echo >&2 " See http://illumos.org/msg/SMF-8000-MY for more information on the use of this script to restore backup copies of the smf(7) repository. If there are any problems which need human intervention, this script will give instructions and then exit back to your shell." if [ "$myroot" = "/" ]; then system="system" [ "`/sbin/zonename`" != global ] && system="zone" echo >&2 " Note that upon full completion of this script, the $system will be rebooted using reboot(8), which will interrupt any active services. " fi # check that the filesystem is as expected cd "$myroot" || exit 1 cd "$myroot$repositorydir" || exit 1 nouser=false rootro=false # check to make sure /usr is mounted if [ ! -x /usr/bin/pgrep ]; then nouser=true fi if [ ! -w "$myroot" ]; then rootro=true fi if [ "$nouser" = true -o "$rootro" = true ]; then if [ "$nouser" = true -a "$rootro" = true ]; then echo "The / filesystem is mounted read-only, and the /usr" >&2 echo "filesystem has not yet been mounted." >&2 elif [ "$nouser" = true ]; then echo "The /usr filesystem has not yet been mounted." >&2 else echo "The / filesystem is mounted read-only." >&2 fi echo >&2 " This must be rectified before $0 can continue. To properly mount / and /usr, run: /lib/svc/method/fs-root then /lib/svc/method/fs-usr After those have completed successfully, re-run: $0 $* to continue. " exit 1 fi # at this point, we know / is mounted read-write, and /usr is mounted. oldreps="` /bin/ls -1rt $repository-*-[0-9]*[0-9] | \ /bin/sed -e '/[^A-Za-z0-9_,.-]/d' -e 's/^'$repository'-//' `" if [ -z "$oldreps" ]; then cat >&2 <&2 <&2 echo "$checkresults" >&2 echo else cont=true fi else echo "$file: not readable" fi elif [ -n "$file" ]; then echo "$file: not found" fi done errors="$myroot"etc/svc/volatile/db_errors repo="$myroot$repositorydir/$repository.db" new="$repo"_old_"`date +%Y''%m''%d'_'%H''%M''%S`" steps= if [ "$myroot" = / ]; then steps="$steps svc.startd(8) and svc.configd(8) will be quiesced, if running." fi if [ -r $repo ]; then steps="$steps $repo -- renamed --> $new" fi if [ -r $errors ]; then steps="$steps $errors -- copied --> ${new}_errors" fi cat >&2 < $repo EOF if [ "$myroot" = / ]; then echo "and the system will be rebooted with reboot(8)." fi echo cont=false while [ $cont = false ]; do echo "Proceed [yes/no]? \c" read x || x=n case "$x" in [Yy]|[Yy][Ee][Ss]) cont=true;; [Nn]|[Nn][Oo]) echo; echo "Exiting..." exit 0; esac; done umask 077 # we want files to be root-readable only. startd_msg= if [ "$myroot" = / ]; then zone="`zonename`" startd="`pgrep -z "$zone" -f svc.startd`" echo echo "Quiescing svc.startd(8) and svc.configd(8): \c" if [ -n "$startd" ]; then pstop $startd startd_msg=\ "To start svc.start(8) running, do: /usr/bin/prun $startd" fi pkill -z "$zone" -f svc.configd sleep 1 # yes, this is hack echo "done." fi if [ -r "$repo" ]; then echo "$repo" echo " -- renamed --> $new" if mv $repo $new; then : else echo "Failed. $startd_msg" exit 1; fi fi if [ -r $errors ]; then echo "$errors" echo " -- copied --> ${new}_errors" if cp -p $errors ${new}_errors; then : else mv -f $new $repo echo "Failed. $startd_msg" exit 1; fi fi echo "$file" echo " -- copied --> $repo" if cp $file $repo.new.$$ && mv $repo.new.$$ $repo; then : else rm -f $repo.new.$$ ${new}_errors mv -f $new $repo echo "Failed. $startd_msg" exit 1; fi echo echo "The backup repository has been successfully restored." echo if [ "$myroot" = / ]; then echo "Rebooting in 5 seconds." sleep 5 reboot fi