]> spindle.queued.net Git - xodist/blob - mkext3.sh
strip common stuff out of mkext3.sh
[xodist] / mkext3.sh
1 #!/bin/sh -e
2 #
3 # Copyright © 2008  Andres Salomon <dilinger@queued.net>
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 IMG_NAME="DebOLPC"
20 ROOT_SIZE="512"
21 IMG_LABEL="DebOLPC"
22 ROOT_DIR=""
23 CRCIMG="./crcimg.pl"
24
25 . functions.sh
26
27 # @img - fs image to attach loop device to
28 # @offset - if the image is partitioned, the offset to attach at
29 #
30 # sets $LOOP_DEV with the path of the newly attached loop device
31 attach_loop()
32 {
33         img="$1"
34         offset="$2"
35
36         LOOP_DEV=$(losetup -f)
37
38         if [ "$offset" != "" ]; then
39                 losetup -o "$offset" "$LOOP_DEV" "$img"
40         else
41                 losetup "$LOOP_DEV" "$img"
42         fi
43 }
44
45 # @dev - the loop device to detach
46 detach_loop()
47 {
48         losetup -d "$1"
49 }
50
51 # @img - fs image to mount
52 # @type - fs type to mount
53 # @offset - if the image is partitioned, the offset to mount at
54 #
55 # sets $MOUNT_POINT with the path of the newly created (and mounted) dir
56 mk_mount()
57 {
58         img="$1"
59         type="$2"
60         offset="$3"
61
62         if [ "$offset" != "" ]; then
63                 offset=",offset=$offset"
64         fi
65
66         MOUNT_POINT=$(mktemp)
67         rm -f $MOUNT_POINT
68         mkdir $MOUNT_POINT
69
70         mount "$img" "$MOUNT_POINT" -o loop$offset -t "$type"
71 }
72
73 # @mntpt - directory to umount and delete
74 rm_mount()
75 {
76         umount "$1"
77         rmdir "$1"
78 }
79
80 # @img - image name to create
81 # @size - image size
82 mk_bootable_img()
83 {
84         img="$1"
85         size="$2"
86
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))
91
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"
96 }
97
98 # @img - image name
99 # @mntpt - path to mounted root directory
100 grub_install()
101 {
102         img="$1"
103         mntpt="$2"
104
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
108
109         cat >${mntpt}/boot/grub/menu.lst<<EOF
110 default 0
111 timeout 5
112 color cyan/blue white/blue
113
114 title           $IMG_LABEL
115 root            (hd0,0)
116 kernel          /bzImage root=LABEL=${IMG_LABEL} rootfstype=ext3 ro
117 boot
118 EOF
119
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
123 device (hd0) $img
124 geometry (hd0) $geom
125 root (hd0,0)
126 setup (hd0)
127 EOF
128
129 }
130
131 # @img - image to create a filesystem on
132 # @label - fs label
133 # @root_dir - root directory to populate the fs with
134 mk_ext3_fs()
135 {
136         img="$1"
137         label="$2"
138         root_dir="$3"
139
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)
143
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"
149
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"
156 }
157
158 usage()
159 {
160         echo "" 1>&2
161         echo "Usage: $0 [<options>] <root directory>" 1>&2
162         echo "" 1>&2
163         echo "Options:" 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
167         echo "" 1>&2
168         exit 1
169 }
170
171 check_for_cmds losetup dd parted mke2fs tune2fs grub || exit 1
172
173 while test $# != 0
174 do
175         case $1 in
176         -l)
177                 IMG_LABEL=$2
178                 shift
179                 ;;
180         -o)
181                 IMG_NAME=$2
182                 shift
183                 ;;
184         -s)
185                 ROOT_SIZE=$2
186                 shift
187                 ;;
188         *)
189                 if [ "$#" != "1" ]; then
190                         echo "Unknown option $1" 1>&2
191                         usage
192                 else
193                         ROOT_DIR="$1"
194                 fi
195                 ;;
196         esac
197         shift
198 done
199
200 if [ "$(id -u)" != "0" ]; then
201         echo "" 1>&2
202         echo "*** Error: must be run as root!" 1>&2
203         echo "" 1>&2
204         exit 2
205 fi
206
207 if [ "$ROOT_DIR" = "" ]; then
208         echo "" 1>&2
209         echo "*** No root directory specified!" 1>&2
210         usage
211 fi
212 if [ ! -d "$ROOT_DIR" ]; then
213         echo "" 1>&2
214         echo "*** Unable to find root directory!" 1>&2
215         usage
216 fi
217
218 mk_bootable_img "${IMG_NAME}.ext3.img" "$ROOT_SIZE"
219 mk_ext3_fs "${IMG_NAME}.ext3.img" "$IMG_LABEL" "$ROOT_DIR"
220
221 #mount ${IMG_NAME}.ext3 $MOUNT_POINT -o loop,offset=$OS_PART1_BEGIN -t ext3
222 #cp -r "$ROOT_DIR"/* $MOUNT_POINT
223 #umount $MOUNT_POINT