]> spindle.queued.net Git - xodist/blob - mkext3.sh
mkext3/mkjffs2: rework fstab/olpc.fth file generation during image creation
[xodist] / mkext3.sh
1 #!/bin/bash -e
2 #
3 # Copyright © 2008-2009  Andres Salomon <dilinger@collabora.co.uk>
4 #
5 # This file is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 ROOT_SIZE="1998"
20 IMG_LABEL="DebXO"
21 IMG_NAME=""
22 ROOT_DIR=""
23
24 . ./functions.sh
25
26 CONFIG_TYPE=generic
27
28 # @img - fs image to attach loop device to
29 # @offset - if the image is partitioned, the offset to attach at
30 #
31 # sets $LOOP_DEV with the path of the newly attached loop device
32 attach_loop()
33 {
34         img="$1"
35         offset="$2"
36
37         LOOP_DEV=$(losetup -f)
38
39         if [ "$offset" != "" ]; then
40                 losetup -o "$offset" "$LOOP_DEV" "$img"
41         else
42                 losetup "$LOOP_DEV" "$img"
43         fi
44 }
45
46 # @dev - the loop device to detach
47 detach_loop()
48 {
49         losetup -d "$1"
50 }
51
52 # @img - fs image to mount
53 # @type - fs type to mount
54 # @offset - if the image is partitioned, the offset to mount at
55 #
56 # sets $MOUNT_POINT with the path of the newly created (and mounted) dir
57 mk_mount()
58 {
59         img="$1"
60         type="$2"
61         offset="$3"
62
63         if [ "$offset" != "" ]; then
64                 offset=",offset=$offset"
65         fi
66
67         MOUNT_POINT=$(mktemp)
68         rm -f $MOUNT_POINT
69         mkdir $MOUNT_POINT
70
71         mount "$img" "$MOUNT_POINT" -o loop$offset -t "$type"
72 }
73
74 # @mntpt - directory to umount and delete
75 rm_mount()
76 {
77         umount "$1"
78         rmdir "$1"
79 }
80
81 # @img - image name to create
82 # @size - image size
83 create_bootable_img()
84 {
85         img="$1"
86         size="$2"
87
88         # first, create a sparse image
89         minus_size=$(($size * 6 / 100))
90         size=$(($size - $minus_size))
91         dd if=/dev/zero of=$img bs=1M count=1 seek=$(($size - 1))
92
93         # fill in the partition table
94         parted -s "$img" "mklabel msdos"
95         parted -s "$img" "mkpart primary ext2 0 -1"
96         parted -s "$img" "set 1 boot on"
97 }
98
99 # @img - image name
100 # @mntpt - path to mounted root directory
101 grub_install()
102 {
103         img="$1"
104         mntpt="$2"
105
106         mkdir -p ${mntpt}/boot/grub
107         cp /usr/lib/grub/i386-pc/stage[12] ${mntpt}/boot/grub
108         cp /usr/lib/grub/i386-pc/e2fs_stage1_5 ${mntpt}/boot/grub
109
110         cat >${mntpt}/boot/grub/menu.lst<<EOF
111 default 0
112 timeout 5
113 color cyan/blue white/blue
114
115 title           $IMG_LABEL
116 root            (hd0,0)
117 kernel          /vmlinuz root=LABEL=${IMG_LABEL} ro
118 boot
119 EOF
120
121         # grub-install is pretty broken, so we do this manually
122         geom=`parted -s "$img" "unit chs" "print" | sed -ne 's/geometry: \([0-9]\+\),\([0-9]\+\),\([0-9]\+\)/:\1 \2 \3:/p' | cut -d: -f2`
123         grub --device-map=/dev/null <<EOF
124 device (hd0) $img
125 geometry (hd0) $geom
126 root (hd0,0)
127 setup (hd0)
128 EOF
129
130 }
131
132 # @img - image to create a filesystem on
133 # @label - fs label
134 # @root_dir - root directory to populate the fs with
135 mk_ext3_fs()
136 {
137         img="$1"
138         label="$2"
139         root_dir="$3"
140
141         # parted 1.8 added --machine for easier parsing; however,
142         # debian still has 1.7 in unstable.
143         partition_start=$(parted -s "$img" "unit B" "print" | sed -ne 's/^ 1[[:space:]]\+//p' | cut -dB -f1)
144
145         # create the filesystem
146         attach_loop "$img" "$partition_start"
147         mke2fs -q -L "$label" -m0 -j "$LOOP_DEV"
148         tune2fs -c0 -i0 "$LOOP_DEV"
149         detach_loop "$LOOP_DEV"
150
151         # populate the filesystem
152         mk_mount "$img" "ext3" "$partition_start"
153         cp -ra "$root_dir"/* "$MOUNT_POINT" || true
154         grub_install "$img" "$MOUNT_POINT"
155         rm_mount "$MOUNT_POINT"
156 }
157
158 usage()
159 {
160         echo "" 1>&2
161         echo "Usage: $0 [<options>] <root directory> <img>" 1>&2
162         echo "" 1>&2
163         echo "Options:" 1>&2
164         echo "  -l <label>    Image label" 1>&2
165         echo "  -s <size>     Root filesystem size (in MB)" 1>&2
166         echo "  --config-type <config>    directory name in configs/ to use" 1>&2
167         echo "" 1>&2
168         exit 1
169 }
170
171 while test $# != 0
172 do
173         case $1 in
174         --config-type)
175                 CONFIG_TYPE=$2
176                 [ -d ./configs/${CONFIG_TYPE} ] || {
177                         echo "Error: can't find directory './configs/${CONFIG_TYPE}/'!" 1>&2
178                         exit 2
179                 }
180                 shift
181                 ;;
182         -l)
183                 IMG_LABEL=$2
184                 shift
185                 ;;
186         -s)
187                 ROOT_SIZE=$2
188                 shift
189                 ;;
190         *)
191                 ROOT_DIR="$1"
192                 shift
193                 if [ "$#" = "1" ]; then
194                         IMG_NAME="$1"
195                 fi
196                 ;;
197         esac
198         shift
199 done
200
201 if [ "$ROOT_DIR" = "" ]; then
202         echo "" 1>&2
203         echo "*** No root directory specified!" 1>&2
204         usage
205 fi
206 if [ "$IMG_NAME" = "" ]; then
207         echo "" 1>&2
208         echo "*** No image name specified!" 1>&2
209         usage
210 fi
211 if [ ! -d "$ROOT_DIR" ]; then
212         echo "" 1>&2
213         echo "*** Unable to find root directory!" 1>&2
214         usage
215 fi
216
217 check_for_cmds losetup parted mke2fs tune2fs grub || exit 1
218
219 # create image's /etc/fstab
220 if [ ! -f ./configs/${CONFIG_TYPE}/fstab-ext3 ]; then
221         echo "*** Unable to find fstab-ext3!" 1>&2
222         exit 1
223 fi
224 sed 's/[[:space:]]#.*//' ./configs/${CONFIG_TYPE}/fstab-ext3 > ${ROOT_DIR}/etc/fstab
225
226 # TODO: this needs to go into an OFW package; here it's a hack
227 # create image's /boot/olpc.fth
228 if [ -f ./configs/${CONFIG_TYPE}/olpc.fth-ext3 ]; then
229         cp ./configs/${CONFIG_TYPE}/olpc.fth-ext3 ${ROOT_DIR}/boot/olpc.fth
230 fi
231
232 create_bootable_img ${IMG_NAME} ${ROOT_SIZE}
233 mk_ext3_fs ${IMG_NAME} ${IMG_LABEL} ${ROOT_DIR}
234
235 #mount ${IMG_NAME}.ext3 $MOUNT_POINT -o loop,offset=$OS_PART1_BEGIN -t ext3
236 #cp -r "$ROOT_DIR"/* $MOUNT_POINT
237 #umount $MOUNT_POINT