-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmkext3.sh
More file actions
executable file
·312 lines (266 loc) · 7.96 KB
/
Copy pathmkext3.sh
File metadata and controls
executable file
·312 lines (266 loc) · 7.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/bin/bash -e
#
# Copyright © 2008-2009 Andres Salomon <dilinger@collabora.co.uk>
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
IMG_NAME=""
ROOT_DIR=""
SKIP_GRUB="n"
. ./functions.sh
CONFIG_TYPE=generic
# @img - fs image to attach loop device to
# @offset - if the image is partitioned, the offset to attach at
#
# sets $LOOP_DEV with the path of the newly attached loop device
attach_loop()
{
img="$1"
offset="$2"
LOOP_DEV=$(losetup -f)
if [ "$offset" != "" ]; then
losetup -o "$offset" "$LOOP_DEV" "$img"
else
losetup "$LOOP_DEV" "$img"
fi
}
# @dev - the loop device to detach
detach_loop()
{
losetup -d "$1"
}
# @img - image name to create
create_bootable_img()
{
img="$1"
# get the total size by summing all the partition sizes (listed in fstab comments)
partition_sizes=`grep '^LABEL=' configs/${CONFIG_TYPE}/fstab-ext3 | \
sed 's/.*#[[:space:]]\+\([0-9]\+\)$/\1/' | xargs echo | tr ' ' +`
size=$(($partition_sizes))
# there's some partition overhead; pad the image so that parted doesn't whine
overhead=`echo | awk "{ printf(\"%d\n\", 0.014 * $size); }"`
size=$((size + overhead))
# first, create a sparse image
minus_size=$(($size * 6 / 100))
size=$(($size - $minus_size))
dd if=/dev/zero of=$img bs=1M count=1 seek=$size
# fill in the partition table
parted -s "$img" "mklabel msdos"
prior=0
grep '^LABEL=' configs/${CONFIG_TYPE}/fstab-ext3 | \
sed 's/.*#[[:space:]]\+\([0-9]\+\)$/\1/' | while read s; do
end=$((prior + s))
parted -s "$img" "mkpart primary ext2 $prior $end"
prior=$end
done
parted -s "$img" "set 1 boot on"
}
# @img - image name
# @mntpt - path to mounted root directory
grub_install()
{
img="$1"
mntpt="$2"
mkdir -p ${mntpt}/boot/grub
cp /usr/lib/grub/i386-pc/stage[12] ${mntpt}/boot/grub
cp /usr/lib/grub/i386-pc/e2fs_stage1_5 ${mntpt}/boot/grub
cat >${mntpt}/boot/grub/menu.lst<<EOF
default 0
timeout 5
color cyan/blue white/blue
EOF
installer_added=0
label=`sed -ne 's/^LABEL=\(.\+\)[[:space:]]\+\/[[:space:]]\+.*/\1/p' configs/${CONFIG_TYPE}/fstab-ext3`
prefix=
grep -q ' ${mntpt}/boot ' /proc/mounts && prefix=/boot
for kern in ${mntpt}/boot/vmlinuz-*; do
v=$(basename $kern | sed 's/vmlinuz-//')
if [ "${v}" = '*' ]; then
echo "*** Error: no kernel images found in /boot!" 1>&2
exit 1
fi
cat >>${mntpt}/boot/grub/menu.lst<<EOF
title Debian GNU/Linux, kernel ${v}
root (hd0,0)
kernel ${prefix}/vmlinuz-${v} root=LABEL=${label} ro
initrd ${prefix}/initrd.img-${v}
boot
EOF
if [ "$installer_added" = "0" ]; then
installer_added=1
cat >>${mntpt}/boot/grub/menu.lst<<EOF
title Debian GNU/Linux Installer
root (hd0,0)
kernel ${prefix}/vmlinuz-${v} root=LABEL=${label} ro installer
initrd ${prefix}/initrd.img-${v}
boot
EOF
fi
done
# grub-install is pretty broken, so we do this manually
geom=`parted -s "$img" "unit chs" "print" | sed -ne 's/geometry: \([0-9]\+\),\([0-9]\+\),\([0-9]\+\)/:\1 \2 \3:/p' | cut -d: -f2`
grub --batch --device-map=/dev/null <<EOF
device (hd0) $img
geometry (hd0) $geom
root (hd0,0)
setup (hd0)
EOF
}
# @img - image to create a filesystem on
# @root_dir - root directory to populate the fs with
mk_ext3_fs()
{
img="$1"
root_dir="$2"
# create root mount point
mount_point_root=$(mktemp)
rm -f $mount_point_root
mkdir $mount_point_root
i=1
sed -ne 's/^LABEL=//p' configs/${CONFIG_TYPE}/fstab-ext3 | \
while read name mntpt fstype extra; do
partition_start=$(parted -m -s "$img" "unit B" "print" | grep "^$i" | cut -d: -f2 | cut -dB -f1)
partition_size=$(parted -m -s "$img" "unit B" "print" | grep "^$i" | cut -d: -f4 | cut -dB -f1)
bs=1024
# create the filesystems/swap
attach_loop "$img" "$partition_start"
if [ "$fstype" = "ext3" ]; then
mke2fs -q -b $bs -L "$name" -m0 -j "$LOOP_DEV" $((partition_size / bs))
tune2fs -c0 -i0 "$LOOP_DEV" # XXX: this is from OLPC days; do we still want this?
elif [ "$fstype" = "ext2" ]; then
mke2fs -q -b $bs -L "$name" -m0 "$LOOP_DEV" $((partition_size / bs))
tune2fs -c0 -i0 "$LOOP_DEV"
elif [ "$fstype" = "swap" ]; then
mkswap -L "$name" "$LOOP_DEV" $((partition_size / bs))
fi
detach_loop "$LOOP_DEV"
# mount the root partition if it's found
if [ "$mntpt" = "/" ]; then
mount "$img" "${mount_point_root}" -o loop,offset=$partition_start -t $fstype
fi
i=$((i + 1))
done
# mount the rest of the partitions (working around /boot coming before /)
sed -ne 's/^LABEL=//p' configs/${CONFIG_TYPE}/fstab-ext3 | \
while read name mntpt fstype extra; do
# / is already mounted
if [ "$mntpt" = "/" ]; then
continue
fi
# if $mntpt doesn't start with '/', don't mount it
if [ "${mntpt##/}" = "$mntpt" ]; then
continue
fi
# parted 1.8 added --machine for easier parsing; however,
# debian still has 1.7 in unstable.
partition_start=$(parted -s "$img" "unit B" "print" | sed -ne "s/^ $i[[:space:]]\+//p" | cut -dB -f1)
[ -d "${mount_point_root}${mntpt}" ] || mkdir -p "${mount_point_root}${mntpt}"
mount "$img" "${mount_point_root}${mntpt}" -o loop,offset=$partition_start -t $fstype
done
# populate the filesystem
cp -ra "$root_dir"/* "$mount_point_root" || true
if [ "$SKIP_GRUB" != "y" ]; then
grub_install "$img" "$mount_point_root"
fi
# umount the filesystem
sed -ne 's/^LABEL=//p' configs/${CONFIG_TYPE}/fstab-ext3 | \
while read name mntpt fstype extra; do
# don't unmount / yet
if [ "$mntpt" = "/" ]; then
continue
fi
# if $mntpt doesn't start with '/', it's not mounted
if [ "${mntpt##/}" = "$mntpt" ]; then
continue
fi
umount "${mount_point_root}${mntpt}"
done
umount "${mount_point_root}"
rmdir "${mount_point_root}"
}
usage()
{
echo "" 1>&2
echo "Usage: $0 [<options>] <root directory> <img>" 1>&2
echo "" 1>&2
echo "Options:" 1>&2
echo " --config-type <config> directory name in configs/ to use" 1>&2
echo " --help display this help screen" 1>&2
echo " --skip-grub don't install GRUB on image" 1>&2
echo "" 1>&2
exit 1
}
while test $# != 0
do
case $1 in
--config-type)
CONFIG_TYPE=$2
[ -d ./configs/${CONFIG_TYPE} ] || {
echo "Error: can't find directory './configs/${CONFIG_TYPE}/'!" 1>&2
exit 2
}
shift
;;
--skip-grub)
SKIP_GRUB="y"
;;
--help|-h)
usage
;;
*)
ROOT_DIR="$1"
shift
if [ "$#" = "1" ]; then
IMG_NAME="$1"
fi
;;
esac
shift
done
if [ "$ROOT_DIR" = "" ]; then
echo "" 1>&2
echo "*** No root directory specified!" 1>&2
usage
fi
if [ "$IMG_NAME" = "" ]; then
echo "" 1>&2
echo "*** No image name specified!" 1>&2
usage
fi
if [ ! -d "$ROOT_DIR" ]; then
echo "" 1>&2
echo "*** Unable to find root directory!" 1>&2
usage
fi
check_for_cmds losetup parted mke2fs tune2fs || exit 1
if [ "$SKIP_GRUB" != "y" ]; then
check_for_cmds grub || exit 1
fi
# create image's /etc/fstab
if [ ! -f ./configs/${CONFIG_TYPE}/fstab-ext3 ]; then
echo "*** Unable to find fstab-ext3!" 1>&2
exit 1
fi
sed 's/[[:space:]]#.*//' ./configs/${CONFIG_TYPE}/fstab-ext3 > ${ROOT_DIR}/etc/fstab
# TODO: this needs to go into an OFW package; here it's a hack
# create image's /boot/olpc.fth
if [ -f ./configs/${CONFIG_TYPE}/olpc.fth-ext3 ]; then
cp ./configs/${CONFIG_TYPE}/olpc.fth-ext3 ${ROOT_DIR}/boot/olpc.fth
fi
create_bootable_img ${IMG_NAME}
mk_ext3_fs ${IMG_NAME} ${ROOT_DIR}
#mount ${IMG_NAME}.ext3 $MOUNT_POINT -o loop,offset=$OS_PART1_BEGIN -t ext3
#cp -r "$ROOT_DIR"/* $MOUNT_POINT
#umount $MOUNT_POINT
exit 0