[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.31

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

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