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.
28 PREREQ_CMDS="losetup dd parted mke2fs tune2fs grub"
33 for cmd in $PREREQ_CMDS; do
34 which $cmd >/dev/null || {
35 echo "Missing required command '$cmd'!" 1>&2
39 if [ "$err" = 1 ]; then
44 # @img - fs image to attach loop device to
45 # @offset - if the image is partitioned, the offset to attach at
47 # sets $LOOP_DEV with the path of the newly attached loop device
53 LOOP_DEV=$(losetup -f)
55 if [ "$offset" != "" ]; then
56 losetup -o "$offset" "$LOOP_DEV" "$img"
58 losetup "$LOOP_DEV" "$img"
62 # @dev - the loop device to detach
68 # @img - fs image to mount
69 # @type - fs type to mount
70 # @offset - if the image is partitioned, the offset to mount at
72 # sets $MOUNT_POINT with the path of the newly created (and mounted) dir
79 if [ "$offset" != "" ]; then
80 offset=",offset=$offset"
87 mount "$img" "$MOUNT_POINT" -o loop$offset -t "$type"
90 # @mntpt - directory to umount and delete
97 # @img - image name to create
104 # first, create a sparse image
105 minus_size=$(($size * 6 / 100))
106 size=$(($size - $minus_size))
107 dd if=/dev/zero of=$img bs=1M count=1 seek=$(($size - 1))
109 # fill in the partition table
110 parted -s "$img" "mklabel msdos"
111 parted -s "$img" "mkpart primary ext2 0 -1"
112 parted -s "$img" "set 1 boot on"
115 # @mntpt - path to mounted root directory
116 # @fstype - the root filesystem type (in a form that 'mount' understands)
122 cat >${mntpt}/etc/fstab<<EOF
123 LABEL=OLPCRoot / $fstype defaults,noatime 1 1
124 devpts /dev/pts devpts gid=5,mode=620 0 0
125 tmpfs /dev/shm tmpfs defaults,size=15% 0 0
126 proc /proc proc defaults 0 0
127 sysfs /sys sysfs defaults 0 0
133 # @mntpt - path to mounted root directory
139 mkdir -p ${mntpt}/boot/grub
140 cp /usr/lib/grub/i386-pc/stage[12] ${mntpt}/boot/grub
141 cp /usr/lib/grub/i386-pc/e2fs_stage1_5 ${mntpt}/boot/grub
143 cat >${mntpt}/boot/grub/menu.lst<<EOF
146 color cyan/blue white/blue
150 kernel /bzImage root=LABEL=${IMG_LABEL} rootfstype=ext3 ro
154 # grub-install is pretty broken, so we do this manually
155 geom=`parted -s "$img" "unit chs" "print" | sed -ne 's/geometry: \([0-9]\+\),\([0-9]\+\),\([0-9]\+\)/:\1 \2 \3:/p' | cut -d: -f2`
156 grub --device-map=/dev/null <<EOF
165 # @img - image to create a filesystem on
167 # @root_dir - root directory to populate the fs with
174 # parted 1.8 added --machine for easier parsing; however,
175 # debian still has 1.7 in unstable.
176 partition_start=$(parted -s "$img" "unit B" "print" | sed -ne 's/^ 1[[:space:]]\+//p' | cut -dB -f1)
178 # create the filesystem
179 attach_loop "$img" "$partition_start"
180 mke2fs -q -L "$label" -m0 -j "$LOOP_DEV"
181 tune2fs -c0 -i0 "$LOOP_DEV"
182 detach_loop "$LOOP_DEV"
184 # populate the filesystem
185 mk_mount "$img" "ext3" "$partition_start"
186 cp -ra "$root_dir"/* "$MOUNT_POINT" || true
187 mk_fstab "$MOUNT_POINT" "ext3"
188 grub_install "$img" "$MOUNT_POINT"
189 rm_mount "$MOUNT_POINT"
194 mkfs.jffs2 -n -e128KiB -r "$1" -o "${IMG_NAME}.pre"
195 sumtool -n -p -e 128KiB -i "${IMG_NAME}.pre" -o "${IMG_NAME}.jffs2"
196 rm -f "${IMG_NAME}.pre"
197 $CRCIMG < ${IMG_NAME}.jffs2 > ${IMG_NAME}.jffs2.crc
204 echo "Usage: $0 [<options>] <root directory>" 1>&2
207 echo " -l <label> Image label" 1>&2
208 echo " -o <img> Image (base) filename" 1>&2
209 echo " -s <size> Root filesystem size (in MB)" 1>&2
232 if [ "$#" != "1" ]; then
233 echo "Unknown option $1" 1>&2
243 if [ "$(id -u)" != "0" ]; then
245 echo "*** Error: must be run as root!" 1>&2
250 if [ "$ROOT_DIR" = "" ]; then
252 echo "*** No root directory specified!" 1>&2
255 if [ ! -d "$ROOT_DIR" ]; then
257 echo "*** Unable to find root directory!" 1>&2
261 mk_bootable_img "${IMG_NAME}.ext3.img" "$ROOT_SIZE"
262 mk_ext3_fs "${IMG_NAME}.ext3.img" "$IMG_LABEL" "$ROOT_DIR"
264 #mount ${IMG_NAME}.ext3 $MOUNT_POINT -o loop,offset=$OS_PART1_BEGIN -t ext3
265 #cp -r "$ROOT_DIR"/* $MOUNT_POINT
266 #create_jffs2_img $MOUNT_POINT