Skip to content

Instantly share code, notes, and snippets.

@click0
Last active January 16, 2025 15:55
Show Gist options
  • Save click0/597ebcb4bc2788beaaabbaf5a6dba84e to your computer and use it in GitHub Desktop.
Save click0/597ebcb4bc2788beaaabbaf5a6dba84e to your computer and use it in GitHub Desktop.

Revisions

  1. click0 revised this gist Jan 16, 2025. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -4,4 +4,6 @@ reboot -r для этого необязателен, а вот для чего
    md формируется в RAM (в моём случае было 128M RAM), reboot -r, на md стартует кастомный /etc/rc, который качает по сети на md новый образ, льет его во флеш и делает полный ребут
    Рабочий код для этого http://www.grosbein.net/freebsd/mips/upgrade/
    скрипт upgrade подготавливает md
    скрипт rc.upgrade это и есть кастомный /etc/rc на md
    скрипт rc.upgrade это и есть кастомный /etc/rc на md

    https://lists.freebsd.org/pipermail/freebsd-mips/2016-February/004431.html
  2. click0 created this gist Jan 16, 2025.
    7 changes: 7 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    reboot -r для этого необязателен, а вот для чего он обязателен, у меня тоже есть тесткейс
    то есть был
    Фряха в виде nanobsd на 8-мегабайтном флеше с загрузчиком U-Boot, перепрошивка без консоли по сети
    md формируется в RAM (в моём случае было 128M RAM), reboot -r, на md стартует кастомный /etc/rc, который качает по сети на md новый образ, льет его во флеш и делает полный ребут
    Рабочий код для этого http://www.grosbein.net/freebsd/mips/upgrade/
    скрипт upgrade подготавливает md
    скрипт rc.upgrade это и есть кастомный /etc/rc на md
    21 changes: 21 additions & 0 deletions rc.upgrade
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    #!/bin/sh

    flashit=/bin/flashit
    if [ -x $flashit ]; then
    echo ''
    echo 'REPROGRAMMING FLASH'
    echo 'DO NOT REBOOT OR POWER OFF THE DEVICE'
    if $flashit; then
    echo 'FLASH REPROGRAMMING FINISHED SUCCESSFULLY'
    else
    echo 'FAILED'
    sleep 3
    fi
    else
    echo "FLASH REPROGRAMMING ABORTED: $flashit not found"
    sleep 3
    fi
    echo ''

    # quick reboot
    kill -INT 1
    91 changes: 91 additions & 0 deletions upgrade
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    #!/bin/sh -e

    PATH=/bin:/sbin:/usr/bin:/usr/sbin

    mp="/tmp/upgrade.$$"
    image='newimage'
    cfg='/etc/upgrade.conf'
    progs='/bin/dd /bin/kill /bin/sh /bin/sleep /libexec/ld-elf.so.1 /sbin/init'

    roundup() { echo $(( (($1+256)/512+1)*512 )); }

    load_files() {
    local bdir f

    for f
    do
    bdir=${f%/*}
    mkdir -p $mp$bdir
    cp -p $f $mp$bdir/
    done
    }

    #
    # Main
    #

    [ -r $cfg ] && . $cfg || { echo $0: cannot read $cfg; exit 1; }
    export FTP_USER FTP_PASSWORD HTTP_AUTH

    echo "Verifying $url"
    size=$(fetch -s "$url")

    if [ -z "$size" -o "$size" = Unknown ]; then
    echo Failed >&2
    exit 1
    fi

    echo Size: $size bytes
    size=$(roundup $size)

    depends=$(ldd -f '%p\n' $progs 2>/dev/null | sort -u)
    for f in $progs $depends
    do
    size=$(( $size + $(roundup `stat -f '%z' $f`) ))
    done
    size=$(($size*6/5))

    echo Allocating $(($size/1024))K for memory disk
    if ! md=$(mdconfig -at malloc -o reserve -s ${size}b); then
    echo Failed to allocate $size bytes >&2
    exit 1
    fi

    md=/dev/$md
    mkdir -p $mp; [ -d $mp ] || exit 1

    newfs -O1 -b 4096 -f 512 -m 0 -o space $md >/dev/null
    mount -o async $md $mp
    cd $mp
    mkdir dev etc
    load_files $progs $depends
    cp -p /etc/rc.upgrade etc/rc
    # silence init(8) moaning "login_getclass: unknown class"
    cp -p /etc/login.conf.db etc

    bsize=$(geom map list | awk '/Stripesize:/ {print $2; exit}')
    iseek=''; off=''; skip=0; stage=1
    {
    echo '#!/bin/sh -e'
    for part in $layout
    do
    size=$(sysctl kern.geom.conftxt | awk '$3=="map/'$part'" {print $4}')
    blocks=$(($size/$bsize))
    [ -z "$off" ] && off=$(sysctl kern.geom.conftxt |\
    awk -vb=$bsize '$3=="map/'$part'" {printf "%d", $9/b}')
    printf 'echo STAGE %s: WRITING BLOCKS %s-%s\n' \
    $stage $(($skip+$off)) $(($skip+$blocks+$off-1))
    printf 'dd if=/%s of=/dev/map/%s ibs=%s obs=%s conv=osync count=%s %s\n' \
    "$image" "$part" $bsize $bsize $blocks $iseek
    skip=$(($skip+$blocks))
    iseek="iseek=$skip"
    stage=$(($stage+1))
    done
    } > bin/flashit
    chmod +x bin/flashit

    echo Downloading $url
    fetch -vo $image $url
    kenv vfs.root.mountfrom=ufs:$md
    echo Closing network connections and unmounting flash...
    reboot -r