| 1 | #!/bin/bash |
|---|
| 2 | # |
|---|
| 3 | # Create SD disk image for hackable 1 |
|---|
| 4 | # |
|---|
| 5 | # GPL v2 copyright Geert Stappers 2009 |
|---|
| 6 | # |
|---|
| 7 | # files to use: |
|---|
| 8 | FAT1=${FAT1:=h1-fat_partition.tgz} |
|---|
| 9 | OTHER1=${OTHER1:=h1-ext2_partition-rev2.tar.gz} |
|---|
| 10 | |
|---|
| 11 | # SD image to create |
|---|
| 12 | SDIMG=h1-sd.img |
|---|
| 13 | |
|---|
| 14 | # log destination |
|---|
| 15 | BUCKET=${BUCKET:=/dev/null} |
|---|
| 16 | |
|---|
| 17 | # check available |
|---|
| 18 | if [ ! -r ${FAT1} -o ! -r ${OTHER1} ] ; then |
|---|
| 19 | echo 1>&2 File ${FAT1} or file ${OTHER1} not readable \(not available\), |
|---|
| 20 | echo 1>&2 Build or download those files. |
|---|
| 21 | exit 1 |
|---|
| 22 | fi |
|---|
| 23 | |
|---|
| 24 | if [ -d /tmp/mntpnth1p1 ] ; then |
|---|
| 25 | echo 1>&2 Mount point /tmp/mntpnth1p1 allready exists |
|---|
| 26 | exit 2 |
|---|
| 27 | fi |
|---|
| 28 | |
|---|
| 29 | if [ -d /tmp/mntpnth1p2 ] ; then |
|---|
| 30 | echo 1>&2 Mount point /tmp/mntpnth1p2 allready exists |
|---|
| 31 | exit 3 |
|---|
| 32 | fi |
|---|
| 33 | |
|---|
| 34 | if [ `id -u` -ne 0 ] ; then |
|---|
| 35 | echo 1>&2 You should be root to run this script |
|---|
| 36 | echo 1>&2 '( it is because `losetup` and `mount` require root privilege)' |
|---|
| 37 | exit 4 |
|---|
| 38 | fi |
|---|
| 39 | |
|---|
| 40 | LPDV=$( losetup --find ) |
|---|
| 41 | if [ -z "$LPDV" ] ; then |
|---|
| 42 | echo 1>&2 No loopdevice found |
|---|
| 43 | exit 5 |
|---|
| 44 | fi |
|---|
| 45 | |
|---|
| 46 | losetup $LPDV $SDIMG |
|---|
| 47 | if [ $? ] ; then |
|---|
| 48 | echo Information from \'losetup\': $(losetup --all | grep $LPDV) >> $BUCKET |
|---|
| 49 | else |
|---|
| 50 | echo 1>&2 "losetup $LPDV $SDIMG failed ..." |
|---|
| 51 | exit 6 |
|---|
| 52 | fi |
|---|
| 53 | |
|---|
| 54 | # All tests done that could spoil the actual actions |
|---|
| 55 | |
|---|
| 56 | echo -n Creating an one gigabyte file ... |
|---|
| 57 | dd if=/dev/zero of=${SDIMG} bs=1024k count=1024 2>> $BUCKET |
|---|
| 58 | echo ' done.' |
|---|
| 59 | |
|---|
| 60 | echo -n Partitioning ... |
|---|
| 61 | fdisk $LPDV << MY_PARTED >> $BUCKET 2>&1 |
|---|
| 62 | n |
|---|
| 63 | p |
|---|
| 64 | 1 |
|---|
| 65 | |
|---|
| 66 | +8M |
|---|
| 67 | n |
|---|
| 68 | p |
|---|
| 69 | 2 |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | w |
|---|
| 73 | MY_PARTED |
|---|
| 74 | echo ' done.' |
|---|
| 75 | echo Partion table >> $BUCKET |
|---|
| 76 | fdisk -l $LPDV >> $BUCKET |
|---|
| 77 | |
|---|
| 78 | kpartx -av $LPDV >> $BUCKET |
|---|
| 79 | LP=$( basename $LPDV ) |
|---|
| 80 | |
|---|
| 81 | mkfs -t ext2 /dev/mapper/${LP}p1 >> $BUCKET 2>&1 |
|---|
| 82 | mkfs -t ext2 /dev/mapper/${LP}p2 >> $BUCKET 2>&1 |
|---|
| 83 | |
|---|
| 84 | mkdir /tmp/mntpnth1p1 |
|---|
| 85 | mkdir /tmp/mntpnth1p2 |
|---|
| 86 | |
|---|
| 87 | mount /dev/mapper/${LP}p1 /tmp/mntpnth1p1 |
|---|
| 88 | mount /dev/mapper/${LP}p2 /tmp/mntpnth1p2 |
|---|
| 89 | |
|---|
| 90 | echo The timestamp warning from \'tar\' can be safely ignored >> $BUCKET |
|---|
| 91 | tar xzf ${FAT1} -C /tmp/mntpnth1p1 >> $BUCKET 2>&1 |
|---|
| 92 | |
|---|
| 93 | echo -n Extracting a lot of files ..... |
|---|
| 94 | tar xzf ${OTHER1} -C /tmp/mntpnth1p2 |
|---|
| 95 | echo ' done.' |
|---|
| 96 | |
|---|
| 97 | echo -n Unmounting ... |
|---|
| 98 | umount /tmp/mntpnth1p1 |
|---|
| 99 | umount /tmp/mntpnth1p2 |
|---|
| 100 | echo ' done.' |
|---|
| 101 | |
|---|
| 102 | echo -n Running \'fsck\' ... |
|---|
| 103 | fsck -t ext2 -y /dev/mapper/${LP}p2 >> $BUCKET 2>&1 |
|---|
| 104 | echo ' done.' |
|---|
| 105 | |
|---|
| 106 | echo -n Removal mount points ... |
|---|
| 107 | rmdir /tmp/mntpnth1p1 |
|---|
| 108 | rmdir /tmp/mntpnth1p2 |
|---|
| 109 | echo ' done.' |
|---|
| 110 | |
|---|
| 111 | echo -n Final cleanup ... |
|---|
| 112 | kpartx -d $LPDV |
|---|
| 113 | losetup -d $LPDV |
|---|
| 114 | echo ' done.' |
|---|
| 115 | # last line |
|---|