Skip to content

Instantly share code, notes, and snippets.

@davidbeauchamp
Created December 20, 2015 16:36
Show Gist options
  • Save davidbeauchamp/f92324c349b783b223da to your computer and use it in GitHub Desktop.
Save davidbeauchamp/f92324c349b783b223da to your computer and use it in GitHub Desktop.

Revisions

  1. davidbeauchamp created this gist Dec 20, 2015.
    152 changes: 152 additions & 0 deletions emrk-reinstall-custom.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,152 @@
    #!/bin/sh
    #
    # emrk-reinstall: EdgeOS installer
    #
    # Maintainer: Daniil Baturin <daniil at baturin dot org>
    #
    # Copyright (C) 2013 SO3Group
    #
    # This program is free software; you can redistribute it and/or modify
    # it under the terms of the GNU General Public License version 2 as
    # published by the Free Software Foundation.
    #
    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    # GNU General Public License for more details.
    #
    # You should have received a copy of the GNU General Public License
    # along with this program. If not, see <http://www.gnu.org/licenses/>.
    #
    #

    # It's time to reinstall EdgeOS!
    # EdgeOS won't reinstall itself!

    DEV=/dev/sdc
    BOOT=/dev/sdc1
    ROOT=/dev/sdc2
    BOOT_MNT_DIR=/root/mnt/boot
    ROOT_MNT_DIR=/root/mnt/root
    TMP_DIR=$ROOT_MNT_DIR/tmp
    W_DIR=w

    # Release tarball file names
    KERNEL_ORIG=vmlinux.tmp
    KERNEL_ORIG_MD5=vmlinux.tmp.md5
    SQUASHFS_ORIG=squashfs.tmp
    SQUASHFS_MD5_ORIG=squashfs.tmp.md5
    VERSION_ORIG=version.tmp

    # Target file names
    KERNEL=vmlinux.64
    KERNEL_MD5=vmlinux.64.md5
    SQUASHFS=squashfs.img
    SQUASHFS_MD5=squashfs.img.md5
    VERSION=version

    PARTED=/sbin/parted
    YESNO=/bin/yesno

    ## Scary disclaimer
    ##echo "WARNING: This script will reinstall EdgeOS from scratch"
    ##echo "If you have any usable data on your router storage,"
    ##echo "it will be irrecoverably destroyed!"
    ##echo "Do you want to continue?"
    ##$YESNO

    ##if [ $? == 1 ]; then
    ## exit 0
    ##fi

    # Umount USB stick filesystems that could be mounted at boot time
    if mount | grep $BOOT > /dev/null; then
    echo "Unmounting boot partition"
    umount $BOOT
    fi

    if mount | grep $ROOT > /dev/null; then
    echo "Unmounting root partition"
    umount $ROOT
    fi

    ## Repartition

    # Remove everything
    echo "Re-creating partition table"
    $PARTED --script $DEV mktable msdos

    # Boot
    echo "Creating boot partition"
    $PARTED --script $DEV mkpart primary fat32 1 150MB
    echo "Formatting boot partition"
    mkfs.vfat $BOOT

    # Root
    echo "Creating root partition"
    $PARTED --script $DEV mkpart primary ext3 150MB 1900MB
    echo "Formatting root partition"
    mkfs.ext3 -q $ROOT

    ## Mount partitions
    echo "Mounting boot parition"
    mount -t vfat $BOOT $BOOT_MNT_DIR

    echo "Mounting root partition"
    mount -t ext3 $ROOT $ROOT_MNT_DIR

    ## Download image

    ##while true; do
    ## read -p "Enter EdgeOS image url: " URL
    ## curl -o $ROOT_MNT_DIR/tmp/edgeos.tar $URL
    ## if [ $? == 0 ]; then
    ## break
    ## else
    ## echo "Could not download EdgeOS image, try again!"
    ## fi
    ##done
    mkdir $ROOT_MNT_DIR/tmp
    cp edgeos.tar $ROOT_MNT_DIR/tmp/edgeos.tar


    ## Reinstall

    # Unpack image
    echo "Unpacking EdgeOS release image"
    tar xf $TMP_DIR/edgeos.tar -C $TMP_DIR

    # The kernel
    echo "Verifying EdgeOS kernel"
    if [ `md5sum $TMP_DIR/$KERNEL_ORIG | awk -F ' ' '{print $1}'` != `cat $TMP_DIR/$KERNEL_ORIG_MD5` ]; then
    echo "Kernel from your image is corrupted! Check your image and start over."
    exit 1
    fi

    echo "Copying EdgeOS kernel to boot partition"
    cp $TMP_DIR/$KERNEL_ORIG $BOOT_MNT_DIR/$KERNEL
    cp $TMP_DIR/$KERNEL_ORIG_MD5 $BOOT_MNT_DIR/$KERNEL_MD5

    # The image
    echo "Verifying EdgeOS system image"
    if [ `md5sum $TMP_DIR/$SQUASHFS_ORIG | awk -F ' ' '{print $1}'` != `cat $TMP_DIR/$SQUASHFS_MD5_ORIG` ]; then
    echo "System image from your image is corrupted! Check your image and start over."
    exit 1
    fi

    echo "Copying EdgeOS system image to root partition"
    mv $TMP_DIR/$SQUASHFS_ORIG $ROOT_MNT_DIR/$SQUASHFS
    mv $TMP_DIR/$SQUASHFS_MD5_ORIG $ROOT_MNT_DIR/$SQUASHFS_MD5

    echo "Copying version file to the root partition"
    mv $TMP_DIR/$VERSION_ORIG $ROOT_MNT_DIR/$VERSION

    # Writable data dir
    echo "Creating EdgeOS writable data directory"
    mkdir $ROOT_MNT_DIR/$W_DIR

    ## Cleanup
    echo "Cleaning up"
    rm -rf $TMP_DIR

    echo "Installation finished"