]> spindle.queued.net Git - xodist/blob - mkubifs.sh
debxo-sugar: update packages for squeeze
[xodist] / mkubifs.sh
1 #!/bin/bash -e
2 #
3 # Copyright © 2008-2010  Andres Salomon <dilinger@debian.org>
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 . ./functions.sh
20
21 CONFIG_TYPE=generic
22
23 usage()
24 {
25         echo "" 1>&2
26         echo "Usage: $0 [<options>] <root directory> <img>" 1>&2
27         echo "" 1>&2
28         echo "Options:" 1>&2
29         echo "  --config-type <config>    directory name in configs/ to use" 1>&2
30         echo "  --help                    display this help screen" 1>&2
31         echo "" 1>&2
32         exit 1
33 }
34
35 while test $# != 0
36 do
37         case $1 in
38         --config-type)
39                 CONFIG_TYPE=$2
40                 [ -d ./configs/${CONFIG_TYPE} ] || {
41                         echo "Error: can't find directory './configs/${CONFIG_TYPE}/'!" 1>&2
42                         exit 2
43                 }
44                 shift
45                 ;;
46         --help|-h)
47                 usage
48                 ;;
49         *)
50                 if [ "$#" != "2" ]; then
51                         echo "Unknown option $1" 1>&2
52                         usage
53                 else
54                         ROOT_DIR=$1
55                         IMG_NAME=$2
56                         shift
57                 fi
58                 ;;
59         esac
60         shift
61 done
62
63 if [ "${IMG_NAME}" == "${IMG_NAME/.img/.dat}" ]; then
64         DAT_NAME=${IMG_NAME}.dat
65 else
66         DAT_NAME=${IMG_NAME/.img/.dat}
67 fi
68
69 if [ ! -d "${ROOT_DIR}" ]; then
70         echo "" 1>&2
71         echo "*** Unable to find root directory!" 1>&2
72         usage
73 fi
74
75 check_for_cmds mkfs.jffs2 sumtool mkfs.ubifs ubinize || exit 1
76
77 # create image's /etc/fstab
78 if [ ! -f ./configs/${CONFIG_TYPE}/fstab-ubifs ]; then
79         echo "*** Unable to find fstab-ubifs!" 1>&2
80         exit 1
81 fi
82 sed 's/[[:space:]]#.*//' ./configs/${CONFIG_TYPE}/fstab-ubifs > ${ROOT_DIR}/etc/fstab
83
84 # TODO: this needs to go into an OFW package; here it's a hack
85 # create image's /boot/olpc.fth
86 if [ -f ./configs/${CONFIG_TYPE}/olpc.fth-ubifs ]; then
87         cp ./configs/${CONFIG_TYPE}/olpc.fth-ubifs ${ROOT_DIR}/boot/olpc.fth
88 fi
89
90 create_ubifs()
91 {
92         root_dir="$1"
93         out="$2"
94
95         cat >ubinize.cfg<<EOF
96 # Section header
97 [ubifs]
98 # Volume mode (other option is static)
99 mode=ubi
100 # Source image
101 image=${out}.pre
102 # Volume ID in UBI image
103 vol_id=0
104 # Volume size
105 vol_size=800000KiB
106 # Allow for dynamic resize
107 vol_type=dynamic
108 # Volume name
109 vol_name=root
110 # Autoresize volume at first mount
111 vol_flags=autoresize
112 EOF
113
114         mkfs.ubifs -m 2048 -e 124KiB -c 8000 -r ${root_dir} -o ${out}.pre
115         ubinize -o ${out} -m 2048 -p 128KiB -s 2048 ubinize.cfg
116         rm -f ${out}.pre ubinize.cfg
117 }
118
119 create_jffs2()
120 {
121         root_dir="$1"
122         out="$2"
123
124         mkfs.jffs2 -n -e128KiB -r ${root_dir} -o ${out}.pre
125         sumtool -n -p -e 128KiB -i ${out}.pre -o ${out}
126         rm -f ${out}.pre
127 }
128
129 partition_map()
130 {
131         cat >${IMG_NAME}<<EOF
132 data:  ${DAT_NAME}
133 erase-all
134 EOF
135
136         # partition size map
137         printf "partitions:" >> ${IMG_NAME}
138         sed -n -e 's/^mtd://p' -e 's/^ubi://p' configs/${CONFIG_TYPE}/fstab-ubifs | \
139                         while read name mntpt fstype extra; do
140                 size=$(echo $extra | sed -ne 's/.*[[:space:]]\+#[[:space:]]\+//p')
141                 if [ "${size}" = "" ]; then
142                         size="-1"
143                 else
144                         # size in fstab is in MB; convert to 128KiB chunks (in hex)
145                         # MB * (1024/128)
146                         size=$((size * 8))
147                         size=`printf "%x\n" $size`
148                 fi
149                 printf "  $name $size" >> ${IMG_NAME}
150         done
151         printf "\n" >> ${IMG_NAME}
152
153         # individual partitions
154         sed -n -e 's/^mtd://p' -e 's/^ubi://p' configs/${CONFIG_TYPE}/fstab-ubifs | \
155                         while read name mntpt fstype extra; do
156                 cat >>${IMG_NAME}<<EOF
157 set-partition: ${name}
158 mark-pending: 0
159 EOF
160                 # sha256 summing for data
161                 eblocks=$((`stat --printf "%s\n" _${name}.img` / (128*1024)))
162                 for b in $(seq 0 $(($eblocks - 1))); do
163                         sha=$(dd status=noxfer bs=128KiB skip=$b count=1 if=_${name}.img 2>/dev/null \
164                                         | sha256sum | cut -d\  -f1)
165                         echo "eblock: `printf '%x' $b` sha256 $sha" >> ${IMG_NAME}
166                 done
167                 cat >>${IMG_NAME}<<EOF
168 cleanmarkers
169 mark-complete: 0
170 EOF
171         done
172 }
173
174 # move separate partitions out of the way
175 sed -n -e 's/^mtd://p' -e 's/^ubi://p' configs/${CONFIG_TYPE}/fstab-ubifs | \
176                 while read name mntpt fstype extra; do
177         if [ "$mntpt" = "/" ]; then
178                 continue
179         fi
180
181         mv ${ROOT_DIR}/${mntpt} "_${name}"
182         mkdir ${ROOT_DIR}/${mntpt}
183 done
184
185 # create partitions
186 sed -n -e 's/^mtd://p' -e 's/^ubi://p' configs/${CONFIG_TYPE}/fstab-ubifs | \
187                 while read name mntpt fstype extra; do
188         if [ "$mntpt" = "/" ]; then
189                 create_${fstype} ${ROOT_DIR} "_${name}.img"
190
191         else
192                 create_${fstype} "_${name}" "_${name}.img"
193         fi
194 done
195
196 # move separate partitions back into the chroot
197 sed -n -e 's/^mtd://p' -e 's/^ubi://p' configs/${CONFIG_TYPE}/fstab-ubifs | \
198                 while read name mntpt fstype extra; do
199         if [ "$mntpt" = "/" ]; then
200                 continue
201         fi
202
203         rmdir ${ROOT_DIR}/${mntpt}
204         mv "_${name}" ${ROOT_DIR}/${mntpt}
205 done
206
207 # partition map is used by OFW for partition layout and sha256 checksums
208 partition_map
209
210 # concat partitions, finish up
211 rm -f ${DAT_NAME}
212 sed -n -e 's/^mtd://p' -e 's/^ubi://p' configs/${CONFIG_TYPE}/fstab-ubifs | \
213                 while read name mntpt fstype extra; do
214         cat "_${name}.img" >> ${DAT_NAME}
215         rm -f "_${name}.img"
216 done
217
218 exit 0