H1_under_Qemu: create_SD_image.sh

File create_SD_image.sh, 2.3 KB (added by stappers, 3 years ago)

create SD image for QEMU

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