[BACK]Return to fw_install.sh CVS log [TXT][DIR] Up to [local] / openbsd / fw_update

Annotation of openbsd/fw_update/fw_install.sh, Revision 1.24

1.1       afresh1     1: #!/bin/ksh
                      2: set -e
                      3:
                      4: scan_dmesg() {
                      5:        # no bsort for now
                      6:        sed -n "$1" /var/run/dmesg.boot
                      7: }
                      8:
1.24    ! afresh1     9: # Fake up some things from install.sub that we don't need to actually do
1.22      afresh1    10: prefetcharea_fs_list() {
                     11:     echo "/mnt/tmp"
                     12: }
                     13: reset_watchdog() {
                     14: }
                     15:
1.8       afresh1    16: # tmpdir, do_as, unpriv, and unpriv2 are from install.sub
                     17:
                     18: # Create a temporary directory based on the supplied directory name prefix.
                     19: tmpdir() {
                     20:        local _i=1 _dir
                     21:
                     22:        until _dir="${1?}.$_i.$RANDOM" && mkdir -- "$_dir" 2>/dev/null; do
                     23:                ((++_i < 10000)) || return 1
                     24:        done
                     25:        echo "$_dir"
                     26: }
1.6       afresh1    27:
                     28: # Run a command ($2+) as unprivileged user ($1).
                     29: # Take extra care that after "cmd" no "user" processes exist.
                     30: #
                     31: # Optionally:
                     32: #      - create "file" and chown it to "user"
                     33: #      - after "cmd", chown "file" back to root
                     34: #
                     35: # Usage: do_as user [-f file] cmd
                     36: do_as() {
                     37:        (( $# >= 2 )) || return
                     38:
                     39:        local _file _rc _user=$1
                     40:        shift
                     41:
                     42:        if [[ $1 == -f ]]; then
                     43:                _file=$2
                     44:                shift 2
                     45:        fi
                     46:
                     47:        if [[ -n $_file ]]; then
                     48:                >$_file
                     49:                chown "$_user" "$_file"
                     50:        fi
                     51:
                     52:        doas -u "$_user" "$@"
                     53:        _rc=$?
                     54:
                     55:        while doas -u "$_user" kill -9 -1 2>/dev/null; do
                     56:                echo "Processes still running for user $_user after: $@"
                     57:                sleep 1
                     58:        done
                     59:
                     60:        [[ -n $_file ]] && chown root "$_file"
                     61:
                     62:        return $_rc
                     63: }
                     64:
                     65: unpriv() {
                     66:        do_as _sndio "$@"
                     67: }
                     68:
                     69: unpriv2() {
                     70:        do_as _file "$@"
                     71: }
                     72:
1.20      afresh1    73: # "fail" needs to be replaced with the "ask_yn" loop like in the installer.
1.8       afresh1    74: _issue=
                     75: fail() {
                     76:        echo $_issue >&2
                     77:        exit 1
                     78: }
                     79:
1.7       afresh1    80: VNAME=$(sysctl -n kern.osrelease)
                     81: VERSION="${VNAME%.*}${VNAME#*.}"
                     82: FWDIR="$VNAME"
1.1       afresh1    83:
1.14      afresh1    84: # TODO: We need the firmware for the system we just installed
                     85: #       not the one we booted from.  For example:
                     86: #       * booting from a snapshot bsd.rd that thinks it is the 7.0 release
                     87: #         will install the firmware from the 7.0 directory instead of
                     88: #         from the snapshots dir.
                     89: #       If they're using sysupgrade, then the installer kernel will be correct.
                     90: #       If we're doing this in the installer we can check what they picked
                     91: #       for downloading sets and use that value.
                     92: #       Otherwise, the fw_update after first boot will fix it up for us.
                     93:
1.7       afresh1    94: HTTP_FWDIR=$FWDIR
                     95: set -- $(scan_dmesg "/^OpenBSD $VNAME\([^ ]*\).*$/s//\1/p")
                     96: [[ $1 == -!(stable) ]] && HTTP_FWDIR=snapshots
                     97:
                     98: FWURL=http://firmware.openbsd.org/firmware/${HTTP_FWDIR}
                     99: FWPUB_KEY=${DESTDIR}/etc/signify/openbsd-${VERSION}-fw.pub
1.20      afresh1   100: FWPATTERNS="file:${0%/*}/firmware_patterns"
1.1       afresh1   101:
1.20      afresh1   102: # TODO: support srclocal installation of firmware somehow
1.11      afresh1   103: fw_update() {
1.24    ! afresh1   104:        local _src=$1 _tmpfs_list _tmpfs _tmpsrc \
        !           105:         _t=Get _cfile="/tmp/SHA256" _pkgdir=/mnt/var/db/pkg \
        !           106:         _f _r _remove _i _installed
        !           107:     local _srclocal=false _unpriv=unpriv
1.15      afresh1   108:        local _d _drivers=$(
1.11      afresh1   109:                last=''
1.24    ! afresh1   110:                $_unpriv ftp -D "Detecting" -Vmo- $FWPATTERNS |
1.15      afresh1   111:                while read _d _m; do
1.11      afresh1   112:                        grep=grep
1.15      afresh1   113:                        [ "$last" = "$_d" ] && continue
                    114:                        [ "$_m" ] || _m="^$_d[0-9][0-9]* at "
                    115:                        [ "$_m" = "${_m#^}" ] && grep=fgrep
                    116:                        $grep -q "$_m" /var/run/dmesg.boot || continue
                    117:                        echo $_d
                    118:                        last=$_d
1.11      afresh1   119:                done
                    120:        )
                    121:
                    122:        if [ -z "$_drivers" ]; then
1.22      afresh1   123:                echo "No devices found which need firmware files to be downloaded."
1.11      afresh1   124:                return
                    125:        fi
1.1       afresh1   126:
1.22      afresh1   127:        ! _tmpfs_list=$(prefetcharea_fs_list) &&
                    128:                echo "Cannot determine prefetch area" >&2 && return
                    129:
                    130:        for _tmpfs in $_tmpfs_list; do
                    131:                # Try to clean up from previous runs, assuming
                    132:                # the _tmpfs selection yields the same mount
                    133:                # point.
                    134:                for _tmpsrc in $_tmpfs/firmware.+([0-9]).+([0-9]); do
                    135:                        [[ -d $_tmpsrc ]] && rm -r $_tmpsrc
                    136:                done
                    137:
                    138:                # Create a download directory for the firmware and
                    139:                # check that the _sndio user can read files from
                    140:                # it. Otherwise cleanup and skip the filesystem.
                    141:                if _tmpsrc=$(tmpdir "$_tmpfs/firmware"); then
                    142:                        (
                    143:                        >$_tmpsrc/t &&
                    144:                        $_unpriv cat $_tmpsrc/t
                    145:                        ) >/dev/null 2>&1 && break ||
                    146:                                rm -r $_tmpsrc
                    147:                fi
                    148:        done
1.1       afresh1   149:
1.11      afresh1   150:        [[ ! -d $_tmpsrc ]] &&
1.22      afresh1   151:                echo "Cannot create prefetch area" >&2 && return 1
1.1       afresh1   152:
1.16      afresh1   153:        # Cleanup from previous runs.
                    154:        rm -f $_cfile $_cfile.sig
                    155:
1.21      afresh1   156:        _t=Get/Verify
1.17      afresh1   157:
1.11      afresh1   158:        ! $_unpriv ftp -D "$_t" -Vmo - "$_src/SHA256.sig" >"$_cfile.sig" &&
1.22      afresh1   159:            echo "Cannot fetch SHA256.sig" >&2 && return 1
1.1       afresh1   160:
1.11      afresh1   161:        # Verify signature file with public keys.
1.24    ! afresh1   162:        ! $_unpriv -f "$_cfile" \
1.11      afresh1   163:            signify -Vep $FWPUB_KEY -x "$_cfile.sig" -m "$_cfile" &&
1.22      afresh1   164:            echo "Signature check of SHA256.sig failed" >&2 && return 1
1.11      afresh1   165:
1.15      afresh1   166:        for _d in $_drivers; do
1.22      afresh1   167:                $UU && reset_watchdog
1.15      afresh1   168:                _f=$( sed -n "s/.*(\($_d-firmware-.*\.tgz\)).*/\1/p" "$_cfile" )
1.19      afresh1   169:                _installed=$(
1.22      afresh1   170:                for fw in "${_pkgdir}/$_d-firmware"*; do
1.19      afresh1   171:                        [ -e "$fw" ] || continue
                    172:                        echo ${fw##*/}
                    173:                done
1.21      afresh1   174:                )
1.15      afresh1   175:
                    176:                for _i in $_installed; do
                    177:                        if [ "$_f" = "$_i.tgz" ]; then
1.24    ! afresh1   178:                                echo "$_i already installed"
1.11      afresh1   179:                                continue 2
1.1       afresh1   180:                        fi
                    181:                done
                    182:
1.11      afresh1   183:                rm -f /tmp/h /tmp/fail
                    184:
                    185:                # Fetch firmware file and create a checksum by piping through
                    186:                # sha256. Create a flag file in case ftp failed. Firmware
                    187:                # from net is written to the prefetch area.
                    188:                ( $_unpriv ftp -D "$_t" -Vmo - "$_src/$_f" || >/tmp/fail ) |
                    189:                ( $_srclocal && unpriv2 sha256 -b >/tmp/h ||
                    190:                    unpriv2 -f /tmp/h sha256 -bph /tmp/h >"$_tmpsrc/$_f" )
                    191:
                    192:                # Handle failed transfer.
                    193:                if [[ -f /tmp/fail ]]; then
                    194:                        rm -f "$_tmpsrc/$_f"
1.22      afresh1   195:                        echo "Fetching of $_f failed!" >&2
                    196:                        continue
1.11      afresh1   197:                fi
                    198:
                    199:                # Verify firmware by comparing its checksum with SHA256.
1.22      afresh1   200:                if ! fgrep -qx "SHA256 ($_f) = $(</tmp/h)" "$_cfile"; then
1.11      afresh1   201:                        [[ -d "$_tmpsrc" ]] && rm -rf "$_tmpsrc"
1.22      afresh1   202:                        echo "Checksum test for $_f failed." >&2
                    203:                        continue
1.11      afresh1   204:                fi
                    205:
                    206:                # TODO: Check hash for files before deleting
1.22      afresh1   207:                if [ "$_installed" ] && [ -e "${_pkgdir}/$_installed/+CONTENTS" ]; then
1.18      afresh1   208:                        echo "Uninstalling $_installed"
1.22      afresh1   209:                        cwd=${_pkgdir}/$_installed
1.11      afresh1   210:
                    211:                        set -A _remove -- "${cwd}/+CONTENTS" "${cwd}"
                    212:
                    213:                        while read c g; do
                    214:                                case $c in
1.22      afresh1   215:                                @cwd) cwd="/mnt/$g"
1.11      afresh1   216:                                  ;;
                    217:                                @*) continue
                    218:                                  ;;
                    219:                                *)  set -A _remove -- "$cwd/$c" "${_remove[@]}"
                    220:                                  ;;
                    221:                                esac
1.22      afresh1   222:                        done < "${_pkgdir}/$_installed/+CONTENTS"
1.11      afresh1   223:
1.21      afresh1   224:                        # We specifically rm -f here because not removing files/dirs
                    225:                        # is probably not worth failing over.
1.11      afresh1   226:                        for _r in "${_remove[@]}" ; do
                    227:                                if [ -d "$_r" ]; then
                    228:                                        # Try hard not to actually remove recursively
                    229:                                        # without rmdir on the install media.
                    230:                                        [ "$_r/*" = $( echo "$_r"/* ) ] && rm -rf "$_r"
                    231:                                else
                    232:                                        rm -f "$_r"
                    233:                                fi
                    234:                        done
                    235:                fi
                    236:
1.21      afresh1   237:                # TODO: Should we mark these so real fw_update can -Drepair?
1.23      afresh1   238:                $_unpriv ftp -D "Install" -Vmo- "file:$_tmpsrc/$_f" |
                    239:             tar -s ",^\+,${_pkgdir}/${_f%.tgz}/+," \
                    240:                 -s ",^firmware,mnt/etc/firmware," \
                    241:                 -C / -zxphf - \
                    242:                 "+*" "firmware/*"
                    243:
1.22      afresh1   244:                ed -s "${_pkgdir}/${_f%.tgz}/+CONTENTS" <<EOL
1.1       afresh1   245: /^@comment pkgpath/ -1a
                    246: @option manual-installation
                    247: @option firmware
                    248: @comment install-script
                    249: .
                    250: w
                    251: EOL
1.11      afresh1   252:        done
                    253: }
1.1       afresh1   254:
1.24    ! afresh1   255: fw_update "$FWURL"

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>