comparison sbin/fjail @ 347:673505e96cea

Implement a "fjail freebsd-update": update "normal" jails and other directories where an OS is mounted
author Franz Glasner <hg@dom66.de>
date Mon, 05 Dec 2022 16:01:54 +0100
parents 3b2935985c73
children c559074302e0
comparison
equal deleted inserted replaced
346:3b2935985c73 347:673505e96cea
76 Copy a tree of ZFS datasets with "zfs send -R" and "zfs receive". 76 Copy a tree of ZFS datasets with "zfs send -R" and "zfs receive".
77 Note that the destination dataset must not exist already. 77 Note that the destination dataset must not exist already.
78 78
79 -r Copy the datasets with the -Lec options (aka "raw") 79 -r Copy the datasets with the -Lec options (aka "raw")
80 -u Do not automatically mount received datasets 80 -u Do not automatically mount received datasets
81
82 freebsd-update [OPTIONS] DIRECTORY OPERATIONS...
83
84 -c CURRENTLY-RUNNING Assume the systen given in CURRENTLY-RUNNING is
85 installed/running at given DIRECTORY
81 86
82 ENVIRONMENT: 87 ENVIRONMENT:
83 88
84 All environment variables that affect "zfs" are effective also. 89 All environment variables that affect "zfs" are effective also.
85 90
140 #: Check whether a FreeBSD version at a given location matches the userland 145 #: Check whether a FreeBSD version at a given location matches the userland
141 #: version of the host where the current process run. 146 #: version of the host where the current process run.
142 #: 147 #:
143 #: Args: 148 #: Args:
144 #: $1: the location where to check for 149 #: $1: the location where to check for
150 #: $2: an optional reference FreeBSD version to compare to (default is the
151 #: version of the host)
145 #: 152 #:
146 #: Returns: 153 #: Returns:
147 #: 0 if the userland versions match, 1 otherwise 154 #: 0 if the userland versions match, 1 otherwise
148 #: 155 #:
149 #: Exit: 156 #: Exit:
150 #: 1 on fatal errors (e.g. /bin/freebsd-version not found or errors) 157 #: 1 on fatal errors (e.g. /bin/freebsd-version not found or errors)
151 #: 158 #:
152 _has_same_userland_version() { 159 _has_same_userland_version() {
153 local directory 160 local directory ref_version
154 161
155 local _host_version _directory_version 162 local _directory_version
156 163
157 directory="$1" 164 directory="$1"
158 165 ref_version="${2:-}"
159 _host_version=$(/bin/freebsd-version -u) || exit 1 166
167 if [ -z "${ref_version}" ]; then
168 ref_version=$(/bin/freebsd-version -u) || exit 1
169 fi
160 _directory_version=$(chroot "${directory}" /bin/freebsd-version -u) || exit 1 170 _directory_version=$(chroot "${directory}" /bin/freebsd-version -u) || exit 1
161 if [ "${_host_version%%-*}" = "${_directory_version%%-*}" ]; then 171 if [ "${ref_version%%-*}" = "${_directory_version%%-*}" ]; then
162 return 0 172 return 0
163 fi 173 fi
164 return 1 174 return 1
165 } 175 }
166 176
593 chmod 0555 "${_mp}/var/empty" || { echo "WARNING: Cannot chmod on var/empty" >&2; } 603 chmod 0555 "${_mp}/var/empty" || { echo "WARNING: Cannot chmod on var/empty" >&2; }
594 chflags schg "${_mp}/var/empty" || { echo "WARNING: Cannot chflags on var/empty" >&2; } 604 chflags schg "${_mp}/var/empty" || { echo "WARNING: Cannot chflags on var/empty" >&2; }
595 # Reset the read-only status of the mountpoint as it was before 605 # Reset the read-only status of the mountpoint as it was before
596 if [ "${_vestatus}" = "on" ]; then 606 if [ "${_vestatus}" = "on" ]; then
597 zfs set readonly=on ${_veds} 1> /dev/null || { echo "ERROR: cannot reactivate readonly-status of ${_mp}/var/empty" >&2; return 1; } 607 zfs set readonly=on ${_veds} 1> /dev/null || { echo "ERROR: cannot reactivate readonly-status of ${_mp}/var/empty" >&2; return 1; }
608 fi
609 }
610
611
612 #:
613 #: Implement the "freebsd-update" command
614 #:
615 command_freebsd_update() {
616 local directory operations
617
618 local opt_currently_running
619
620 opt_currently_running=""
621 while getopts "c:" _opt ; do
622 case ${_opt} in
623 c)
624 opt_currently_running="$OPTARG"
625 ;;
626 \?|:)
627 return 2;
628 ;;
629 esac
630 done
631 shift $((OPTIND-1))
632 OPTIND=1
633
634 directory="${1-}"
635
636 [ -z "${directory}" ] && { echo "ERROR: no directory given" 1>&2; return 2; }
637 [ -d "${directory}" ] || { echo "ERROR: directory \`${directory}' does not exist" 1>&2; return 1; }
638
639 shift
640 operations="$@"
641
642 if _has_same_userland_version "${directory}" "${opt_currently_running}" ; then
643 if [ -n "${opt_currently_running}" ]; then
644 freebsd-update -b "${directory}" --currently-running "${opt_currently_running}" ${operations}
645 else
646 freebsd-update -b "${directory}" ${operations}
647 fi
648 else
649 echo "ERROR: Userland version mismatch" 1>&2
650 return 1
598 fi 651 fi
599 } 652 }
600 653
601 654
602 # 655 #
657 command_hostid "$@" 710 command_hostid "$@"
658 ;; 711 ;;
659 copy) 712 copy)
660 command_copy "$@" 713 command_copy "$@"
661 ;; 714 ;;
715 freebsd-update)
716 command_freebsd_update "$@"
717 ;;
662 *) 718 *)
663 echo "ERROR: unknown command \`${command}'" >&2 719 echo "ERROR: unknown command \`${command}'" >&2
664 exit 2 720 exit 2
665 ;; 721 ;;
666 esac 722 esac