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