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.
27 # @img - fs image to attach loop device to
28 # @offset - if the image is partitioned, the offset to attach at
30 # sets $LOOP_DEV with the path of the newly attached loop device
36 LOOP_DEV=$(losetup -f)
38 if [ "$offset" != "" ]; then
39 losetup -o "$offset" "$LOOP_DEV" "$img"
41 losetup "$LOOP_DEV" "$img"
45 # @dev - the loop device to detach
51 # @img - fs image to mount
52 # @type - fs type to mount
53 # @offset - if the image is partitioned, the offset to mount at
55 # sets $MOUNT_POINT with the path of the newly created (and mounted) dir
62 if [ "$offset" != "" ]; then
63 offset=",offset=$offset"
70 mount "$img" "$MOUNT_POINT" -o loop$offset -t "$type"
73 # @mntpt - directory to umount and delete
80 # @img - image name to create
87 # first, create a sparse image
88 minus_size=$(($size * 6 / 100))
89 size=$(($size - $minus_size))
90 dd if=/dev/zero of=$img bs=1M count=1 seek=$(($size - 1))
92 # fill in the partition table
93 parted -s "$img" "mklabel msdos"
94 parted -s "$img" "mkpart primary ext2 0 -1"
95 parted -s "$img" "set 1 boot on"
99 # @mntpt - path to mounted root directory
105 mkdir -p ${mntpt}/boot/grub
106 cp /usr/lib/grub/i386-pc/stage[12] ${mntpt}/boot/grub
107 cp /usr/lib/grub/i386-pc/e2fs_stage1_5 ${mntpt}/boot/grub
109 cat >${mntpt}/boot/grub/menu.lst<<EOF
112 color cyan/blue white/blue
116 kernel /bzImage root=LABEL=${IMG_LABEL} rootfstype=ext3 ro
120 # grub-install is pretty broken, so we do this manually
121 geom=`parted -s "$img" "unit chs" "print" | sed -ne 's/geometry: \([0-9]\+\),\([0-9]\+\),\([0-9]\+\)/:\1 \2 \3:/p' | cut -d: -f2`
122 grub --device-map=/dev/null <<EOF
131 # @img - image to create a filesystem on
133 # @root_dir - root directory to populate the fs with
140 # parted 1.8 added --machine for easier parsing; however,
141 # debian still has 1.7 in unstable.
142 partition_start=$(parted -s "$img" "unit B" "print" | sed -ne 's/^ 1[[:space:]]\+//p' | cut -dB -f1)
144 # create the filesystem
145 attach_loop "$img" "$partition_start"
146 mke2fs -q -L "$label" -m0 -j "$LOOP_DEV"
147 tune2fs -c0 -i0 "$LOOP_DEV"
148 detach_loop "$LOOP_DEV"
150 # populate the filesystem
151 mk_mount "$img" "ext3" "$partition_start"
152 cp -ra "$root_dir"/* "$MOUNT_POINT" || true
153 create_fstab "$MOUNT_POINT" "ext3"
154 grub_install "$img" "$MOUNT_POINT"
155 rm_mount "$MOUNT_POINT"
161 echo "Usage: $0 [<options>] <root directory>" 1>&2
164 echo " -l <label> Image label" 1>&2
165 echo " -o <img> Image (base) filename" 1>&2
166 echo " -s <size> Root filesystem size (in MB)" 1>&2
171 check_for_cmds losetup dd parted mke2fs tune2fs grub || exit 1
189 if [ "$#" != "1" ]; then
190 echo "Unknown option $1" 1>&2
200 if [ "$(id -u)" != "0" ]; then
202 echo "*** Error: must be run as root!" 1>&2
207 if [ "$ROOT_DIR" = "" ]; then
209 echo "*** No root directory specified!" 1>&2
212 if [ ! -d "$ROOT_DIR" ]; then
214 echo "*** Unable to find root directory!" 1>&2
218 mk_bootable_img "${IMG_NAME}.ext3.img" "$ROOT_SIZE"
219 mk_ext3_fs "${IMG_NAME}.ext3.img" "$IMG_LABEL" "$ROOT_DIR"
221 #mount ${IMG_NAME}.ext3 $MOUNT_POINT -o loop,offset=$OS_PART1_BEGIN -t ext3
222 #cp -r "$ROOT_DIR"/* $MOUNT_POINT