Changeset View
Changeset View
Standalone View
Standalone View
os/userland-base/files/base-update.in
- This file was added.
#!/bin/sh | |||||
# Copyright 2019 Kris Moore | |||||
# All rights reserved | |||||
# | |||||
# Redistribution and use in source and binary forms, with or without | |||||
# modification, are permitted providing that the following conditions | |||||
# are met: | |||||
# 1. Redistributions of source code must retain the above copyright | |||||
# notice, this list of conditions and the following disclaimer. | |||||
# 2. Redistributions in binary form must reproduce the above copyright | |||||
# notice, this list of conditions and the following disclaimer in the | |||||
# documentation and/or other materials provided with the distribution. | |||||
# | |||||
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |||||
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |||||
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |||||
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | |||||
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |||||
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |||||
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |||||
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |||||
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | |||||
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |||||
# POSSIBILITY OF SUCH DAMAGE. | |||||
PKG="pkg-static" | |||||
exit_err() | |||||
{ | |||||
echo "ERROR: $@" >&2 | |||||
exit 1 | |||||
} | |||||
get_userland_bootstrap_pkgs() | |||||
{ | |||||
# Get which world is in play | |||||
USERLANDPKGNAME=$(${PKG} query '%n' os/userland-base-bootstrap) | |||||
if [ -z "$USERLANDPKGNAME" ] ; then | |||||
exit_err "Could not determine userland-base-bootstrap package, is this using base packages?" | |||||
fi | |||||
echo $USERLANDPKGNAME | |||||
} | |||||
get_userland_pkgs() | |||||
{ | |||||
# Get which world is in play | |||||
USERLANDPKGNAME=$(${PKG} query '%n' os/userland) | |||||
if [ -z "$USERLANDPKGNAME" ] ; then | |||||
exit_err "Could not determine userland package, is this using base packages?" | |||||
fi | |||||
echo $USERLANDPKGNAME | |||||
} | |||||
do_userland_updates() | |||||
{ | |||||
check_userland_updates >/dev/null | |||||
case $? in | |||||
0) | |||||
echo "Your system is up to date!" | |||||
return | |||||
;; | |||||
1) | |||||
;; | |||||
*) | |||||
exit_err "Failed to check for userland updates" | |||||
;; | |||||
esac | |||||
# Check and do upgrade to bootstrap pkg first | |||||
${PKG} upgrade -y $(get_userland_bootstrap_pkgs) | |||||
if [ $? -ne 0 ] ; then | |||||
exit_err "Failed upgrading userland base bootstrap package" | |||||
fi | |||||
${PKG} upgrade -y $(get_userland_pkgs) | |||||
if [ $? -ne 0 ] ; then | |||||
exit_err "Failed upgrading userland" | |||||
fi | |||||
echo "Userland update complete!" | |||||
exit 0 | |||||
} | |||||
check_updates() | |||||
{ | |||||
${PKG} upgrade -n $@ | |||||
return $? | |||||
} | |||||
get_kernel_pkgs() | |||||
{ | |||||
# Get which kernel is booted | |||||
CURKERN=$(sysctl -n kern.bootfile) | |||||
if [ -z "$CURKERN" ] ; then | |||||
exit_err "Could not determine booted kernel via: kern.bootfile" | |||||
fi | |||||
KERNPKG=$(${PKG} which -q ${CURKERN}) | |||||
if [ -z "$KERNPKG" ] ; then | |||||
exit_err "Could not determine kernel package, is this using base packages?" | |||||
fi | |||||
KERNPKGNAME=$(${PKG} query '%n' ${KERNPKG}) | |||||
if [ -z "$KERNPKGNAME" ] ; then | |||||
exit_err "Could not determine kernel package, is this using base packages?" | |||||
fi | |||||
# Update kmod packages at same time | |||||
KERNKMODS=$(${PKG} query -g '%n' \*-kmod | tr -s '\n' ' ') | |||||
echo "$KERNPKGNAME $KERNKMODS" | |||||
} | |||||
check_userland_updates() | |||||
{ | |||||
check_updates $(get_userland_pkgs) $(get_userland_bootstrap_pkgs) | |||||
return $? | |||||
} | |||||
check_kernel_updates() | |||||
{ | |||||
check_updates $(get_kernel_pkgs) | |||||
return $? | |||||
} | |||||
do_kernel_updates() | |||||
{ | |||||
check_kernel_updates >/dev/null | |||||
case $? in | |||||
0) | |||||
return | |||||
;; | |||||
1) | |||||
;; | |||||
*) | |||||
exit_err "Failed to check for kernel updates" | |||||
;; | |||||
esac | |||||
${PKG} upgrade -y $(get_kernel_pkgs) | |||||
if [ $? -ne 0 ] ; then | |||||
exit_err "Failed updating kernel" | |||||
fi | |||||
echo "Kernel update complete!" | |||||
echo "Please reboot before running $0 again to continue the base update." | |||||
exit 0 | |||||
} | |||||
check_skip_file() | |||||
{ | |||||
case $1 in | |||||
/etc/login.conf.db) | |||||
return 1 | |||||
;; | |||||
/etc/pwd.db) | |||||
return 1 | |||||
;; | |||||
/etc/spwd.db) | |||||
return 1 | |||||
;; | |||||
esac | |||||
return 0 | |||||
} | |||||
# Look for pkgnew files and attempt to merge | |||||
do_etcmerge() | |||||
{ | |||||
for nfile in $(find /etc | grep '\.pkgnew$' | tr '\n' ' ') | |||||
do | |||||
ofile=$(echo $nfile | sed 's|.pkgnew||g') | |||||
# Stale file from a previous merge conflict | |||||
if [ ! -e "${ofile}" ] ; then | |||||
echo "Removing stale ${nfile}" | |||||
rm "${nfile}" | |||||
continue | |||||
fi | |||||
# Skip some files always | |||||
check_skip_file "${ofile}" | |||||
if [ $? -ne 0 ] ; then | |||||
rm "${nfile}" | |||||
continue | |||||
fi | |||||
# Yes, sometimes we've seen this | |||||
if [ "$(sha256 -q $nfile)" = "$(sha256 -q $ofile)" ] ; then | |||||
echo "Moving identical ${nfile} -> ${ofile}" | |||||
cp "${nfile}" "${ofile}" | |||||
if [ $? -ne 0 ] ; then | |||||
echo "ERROR: Failed to copy $nfile -> $ofile" | |||||
fi | |||||
rm "${nfile}" | |||||
continue | |||||
fi | |||||
# Need to merge the files, lets instruct the user on howto | |||||
echo "The File: ${ofile}" | |||||
echo "Needs manual updating. Do you wish to:" | |||||
echo "" | |||||
echo "m) Merge" | |||||
echo "d) Delete the new file" | |||||
echo "s) Skip (Default)" | |||||
echo -e ">\c" | |||||
read response | |||||
case $response in | |||||
m) | |||||
merge_file "$ofile" "$nfile" | |||||
check_db_gen "$ofile" | |||||
;; | |||||
d) | |||||
echo "Removing ${nfile}" | |||||
rm "${nfile}" | |||||
;; | |||||
*) | |||||
continue | |||||
;; | |||||
esac | |||||
done | |||||
} | |||||
check_db_gen() | |||||
{ | |||||
if [ "$1" = "/etc/login.conf" ] ; then | |||||
cap_mkdb /etc/login.conf | |||||
fi | |||||
if [ "$1" = "/etc/services" ] ; then | |||||
services_mkdb -q -o /var/db/services.db /etc/services | |||||
fi | |||||
if [ "$1" = "/etc/master.passwd" ] ; then | |||||
pwd_mkdb -p /etc/master.passwd | |||||
fi | |||||
if [ "$1" = "/etc/mail/aliases" ] ; then | |||||
newaliases | |||||
fi | |||||
} | |||||
merge_file() | |||||
{ | |||||
echo "Starting merge of $1" | |||||
echo "-----------------------" | |||||
echo "Old contents are on left, new contents on right." | |||||
echo "Enter 'l' select the left or 'r' to select the right side to merge" | |||||
echo "Use 'q' to quit" | |||||
cp -p ${2} ${1}.merged | |||||
sdiff -o "${1}.merged" --text --suppress-common-lines \ | |||||
--width=80 "${1}" "${2}" | |||||
while : | |||||
do | |||||
echo "Editing finshed. Do you wish to:" | |||||
echo "v) View merged file" | |||||
echo "i) Install merged file" | |||||
echo "s) Skip (Default)" | |||||
echo -e ">\c" | |||||
read response | |||||
case $response in | |||||
v|V) | |||||
${PAGER} ${1}.merged | |||||
;; | |||||
i|I) cp "${1}.merged" "${1}" | |||||
rm "${1}.merged" | |||||
rm "${2}" | |||||
break | |||||
;; | |||||
*) break | |||||
;; | |||||
esac | |||||
done | |||||
} | |||||
update_pkg() | |||||
{ | |||||
${PKG} upgrade -y ports-mgmt/pkg | |||||
} | |||||
usage() | |||||
{ | |||||
cat << EOF | |||||
$0 - Keep your base packages up to date | |||||
---------------------------------------------- | |||||
Arguments (Requires one) | |||||
check - Check if updates exist for kernel/userland | |||||
update - Perform updates to kernel/userland packages | |||||
merge - Merge updates to /etc files that did not automatically update | |||||
EOF | |||||
exit 1 | |||||
} | |||||
if [ -z "$1" ] ; then | |||||
usage | |||||
fi | |||||
case $1 in | |||||
check) | |||||
update_pkg | |||||
check_updates $(get_kernel_pkgs) $(get_userland_pkgs) $(get_userland_bootstrap_pkgs) | |||||
;; | |||||
update) | |||||
update_pkg | |||||
do_kernel_updates | |||||
do_userland_updates | |||||
;; | |||||
merge) | |||||
do_etcmerge | |||||
;; | |||||
*) | |||||
usage | |||||
;; | |||||
esac |