3 # Copyright © 2008 Andres Salomon <dilinger@queued.net>
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.
26 # @img - fs image to attach loop device to
27 # @offset - if the image is partitioned, the offset to attach at
29 # sets $LOOP_DEV with the path of the newly attached loop device
35 LOOP_DEV=$(losetup -f)
37 if [ "$offset" != "" ]; then
38 losetup -o "$offset" "$LOOP_DEV" "$img"
40 losetup "$LOOP_DEV" "$img"
44 # @dev - the loop device to detach
50 # @img - fs image to mount
51 # @type - fs type to mount
52 # @offset - if the image is partitioned, the offset to mount at
54 # sets $MOUNT_POINT with the path of the newly created (and mounted) dir
61 if [ "$offset" != "" ]; then
62 offset=",offset=$offset"
69 mount "$img" "$MOUNT_POINT" -o loop$offset -t "$type"
72 # @mntpt - directory to umount and delete
79 # @img - image name to create
86 # first, create a sparse image
87 minus_size=$(($size * 6 / 100))
88 size=$(($size - $minus_size))
89 dd if=/dev/zero of=$img bs=1M count=1 seek=$(($size - 1))
91 # fill in the partition table
92 parted -s "$img" "mklabel msdos"
93 parted -s "$img" "mkpart primary ext2 0 -1"
94 parted -s "$img" "set 1 boot on"
98 # @mntpt - path to mounted root directory
104 mkdir -p ${mntpt}/boot/grub
105 cp /usr/lib/grub/i386-pc/stage[12] ${mntpt}/boot/grub
106 cp /usr/lib/grub/i386-pc/e2fs_stage1_5 ${mntpt}/boot/grub
108 cat >${mntpt}/boot/grub/menu.lst<<EOF
111 color cyan/blue white/blue
115 kernel /vmlinuz root=LABEL=${IMG_LABEL} ro
119 # grub-install is pretty broken, so we do this manually
120 geom=`parted -s "$img" "unit chs" "print" | sed -ne 's/geometry: \([0-9]\+\),\([0-9]\+\),\([0-9]\+\)/:\1 \2 \3:/p' | cut -d: -f2`
121 grub --device-map=/dev/null <<EOF
130 # @img - image to create a filesystem on
132 # @root_dir - root directory to populate the fs with
139 # parted 1.8 added --machine for easier parsing; however,
140 # debian still has 1.7 in unstable.
141 partition_start=$(parted -s "$img" "unit B" "print" | sed -ne 's/^ 1[[:space:]]\+//p' | cut -dB -f1)
143 # create the filesystem
144 attach_loop "$img" "$partition_start"
145 mke2fs -q -L "$label" -m0 -j "$LOOP_DEV"
146 tune2fs -c0 -i0 "$LOOP_DEV"
147 detach_loop "$LOOP_DEV"
149 # populate the filesystem
150 mk_mount "$img" "ext3" "$partition_start"
151 cp -ra "$root_dir"/* "$MOUNT_POINT" || true
152 create_fstab "$MOUNT_POINT" "ext3"
153 grub_install "$img" "$MOUNT_POINT"
154 rm_mount "$MOUNT_POINT"
160 echo "Usage: $0 [<options>] <root directory> <img>" 1>&2
163 echo " -l <label> Image label" 1>&2
164 echo " -s <size> Root filesystem size (in MB)" 1>&2
183 if [ "$#" = "1" ]; then
191 if [ "$ROOT_DIR" = "" ]; then
193 echo "*** No root directory specified!" 1>&2
196 if [ "$IMG_NAME" = "" ]; then
198 echo "*** No image name specified!" 1>&2
201 if [ ! -d "$ROOT_DIR" ]; then
203 echo "*** Unable to find root directory!" 1>&2
207 check_for_cmds losetup parted mke2fs tune2fs grub || exit 1
208 create_fstab ${ROOT_DIR} ext3
209 create_ofwboot ${ROOT_DIR} ext3
211 create_bootable_img ${IMG_NAME} ${ROOT_SIZE}
212 mk_ext3_fs ${IMG_NAME} ${IMG_LABEL} ${ROOT_DIR}
214 #mount ${IMG_NAME}.ext3 $MOUNT_POINT -o loop,offset=$OS_PART1_BEGIN -t ext3
215 #cp -r "$ROOT_DIR"/* $MOUNT_POINT