paul@0 | 1 | #!/bin/sh |
paul@0 | 2 | |
paul@0 | 3 | # Copyright (C) 2012 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 4 | # |
paul@0 | 5 | # This program is free software; you can redistribute it and/or modify it under |
paul@0 | 6 | # the terms of the GNU General Public License as published by the Free Software |
paul@0 | 7 | # Foundation; either version 3 of the License, or (at your option) any later |
paul@0 | 8 | # version. |
paul@0 | 9 | # |
paul@0 | 10 | # This program is distributed in the hope that it will be useful, but WITHOUT |
paul@0 | 11 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@0 | 12 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@0 | 13 | # details. |
paul@0 | 14 | # |
paul@0 | 15 | # You should have received a copy of the GNU General Public License along with |
paul@0 | 16 | # this program. If not, see <http://www.gnu.org/licenses/>. |
paul@0 | 17 | |
paul@0 | 18 | PROGNAME=`basename $0` |
paul@0 | 19 | |
paul@0 | 20 | if [ ! "$1" ] || [ "$1" = '--help' ]; then |
paul@0 | 21 | cat 1>&2 <<EOF |
paul@0 | 22 | Usage: $PROGNAME <root filesystem path> <device path> --mkfs | --update |
paul@0 | 23 | |
paul@0 | 24 | Example: $PROGNAME rootfs /dev/sdb1 --mkfs |
paul@0 | 25 | |
paul@0 | 26 | This program copies the contents of the specified root filesystem directory |
paul@0 | 27 | onto the indicated device. |
paul@0 | 28 | |
paul@0 | 29 | If --mkfs is specified, a new filesystem is created on the device, erasing any |
paul@0 | 30 | existing filesystem. Then, the copying of the root filesystem is performed. |
paul@0 | 31 | |
paul@0 | 32 | If --update is specified, any existing filesystem is preserved on the device. |
paul@0 | 33 | |
paul@0 | 34 | Be very careful using this program. Please verify first that the specified |
paul@0 | 35 | device really does refer to a medium that can be overwritten. If in doubt, use |
paul@0 | 36 | the dmesg tool to check the kernel log, and use the fdisk tool to list any |
paul@0 | 37 | partitions on the medium and the devices these correspond to. |
paul@0 | 38 | EOF |
paul@0 | 39 | exit 1 |
paul@0 | 40 | fi |
paul@0 | 41 | |
paul@0 | 42 | ROOTDIR=$1 |
paul@0 | 43 | DEVICE=$2 |
paul@0 | 44 | OPTION=$3 |
paul@0 | 45 | |
paul@0 | 46 | if [ ! "$DEVICE" ]; then |
paul@0 | 47 | echo "$PROGNAME: Need a device to store the system image." 1>&2 |
paul@0 | 48 | exit 1 |
paul@0 | 49 | fi |
paul@0 | 50 | |
paul@0 | 51 | if [ "$OPTION" = '--mkfs' ]; then |
paul@0 | 52 | /sbin/mkfs.ext2 "$DEVICE" |
paul@0 | 53 | elif [ "$OPTION" = '--update' ]; then |
paul@0 | 54 | echo "$PROGNAME: Not erasing the media using mkfs." 1>&2 |
paul@0 | 55 | else |
paul@0 | 56 | echo "$PROGNAME: Please specify --mkfs or --update." 1>&2 |
paul@0 | 57 | exit 1 |
paul@0 | 58 | fi |
paul@0 | 59 | |
paul@0 | 60 | MEDIADIR="/media/`basename $DEVICE`" |
paul@0 | 61 | |
paul@0 | 62 | if [ ! -e "$MEDIADIR" ]; then |
paul@0 | 63 | mkdir "$MEDIADIR" |
paul@0 | 64 | fi |
paul@0 | 65 | |
paul@0 | 66 | echo "$PROGNAME: Mounting $DEVICE at $MEDIADIR..." 1>&2 |
paul@0 | 67 | mount "$DEVICE" "$MEDIADIR" |
paul@0 | 68 | |
paul@0 | 69 | echo "$PROGNAME: Copying system from $ROOTDIR..." 1>&2 |
paul@0 | 70 | cp -a $ROOTDIR/* "$MEDIADIR/" |
paul@0 | 71 | |
paul@0 | 72 | echo "$PROGNAME: Unmounting $MEDIADIR..." 1>&2 |
paul@0 | 73 | umount "$MEDIADIR" |