3 # Copyright © 2008-2009 Andres Salomon <dilinger@collabora.co.uk>
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.
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.
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.
28 # @img - fs image to attach loop device to
29 # @offset - if the image is partitioned, the offset to attach at
31 # sets $LOOP_DEV with the path of the newly attached loop device
37 LOOP_DEV=$(losetup -f)
39 if [ "$offset" != "" ]; then
40 losetup -o "$offset" "$LOOP_DEV" "$img"
42 losetup "$LOOP_DEV" "$img"
46 # @dev - the loop device to detach
52 # @img - fs image to mount
53 # @type - fs type to mount
54 # @offset - if the image is partitioned, the offset to mount at
56 # sets $MOUNT_POINT with the path of the newly created (and mounted) dir
63 if [ "$offset" != "" ]; then
64 offset=",offset=$offset"
71 mount "$img" "$MOUNT_POINT" -o loop$offset -t "$type"
74 # @mntpt - directory to umount and delete
81 # @img - image name to create
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))
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"
100 # @mntpt - path to mounted root directory
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
110 cat >${mntpt}/boot/grub/menu.lst<<EOF
113 color cyan/blue white/blue
117 kernel /vmlinuz root=LABEL=${IMG_LABEL} ro
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
132 # @img - image to create a filesystem on
134 # @root_dir - root directory to populate the fs with
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)
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"
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"
161 echo "Usage: $0 [<options>] <root directory> <img>" 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
176 [ -d ./configs/${CONFIG_TYPE} ] || {
177 echo "Error: can't find directory './configs/${CONFIG_TYPE}/'!" 1>&2
193 if [ "$#" = "1" ]; then
201 if [ "$ROOT_DIR" = "" ]; then
203 echo "*** No root directory specified!" 1>&2
206 if [ "$IMG_NAME" = "" ]; then
208 echo "*** No image name specified!" 1>&2
211 if [ ! -d "$ROOT_DIR" ]; then
213 echo "*** Unable to find root directory!" 1>&2
217 check_for_cmds losetup parted mke2fs tune2fs grub || exit 1
219 # create image's /etc/fstab
220 if [ ! -f ./configs/${CONFIG_TYPE}/fstab-ext3 ]; then
221 echo "*** Unable to find fstab-ext3!" 1>&2
224 sed 's/[[:space:]]#.*//' ./configs/${CONFIG_TYPE}/fstab-ext3 > ${ROOT_DIR}/etc/fstab
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
232 create_bootable_img ${IMG_NAME} ${ROOT_SIZE}
233 mk_ext3_fs ${IMG_NAME} ${IMG_LABEL} ${ROOT_DIR}
235 #mount ${IMG_NAME}.ext3 $MOUNT_POINT -o loop,offset=$OS_PART1_BEGIN -t ext3
236 #cp -r "$ROOT_DIR"/* $MOUNT_POINT