2 # (c) copyright 2010 by Thomas Tanner <tanner@gmx.de>
4 # create a bootable Maemo 5 root on /home or some mounted MMC partition
5 # either symlink or copy all files on NAND
6 # copy files relocated to /home
7 # ignore or copy files from /opt
8 # symlink or mount old home, or setup new home
9 # for installation in /home move /home/opt to /home/opt.orig and update /opt symlink
11 # $dest/nand - mount point for UBIFS or mount binds
12 # for chroot mounts in $dest are: dev home media proc srv sys syspart tmp var/run
13 mountpoints="cdrom dev floppy home initrd media mnt nand proc sys syspart tmp"
15 mode=link # link or copy content
16 withopt=no # copy or ignore files in /opt
17 homedir=keep # link/mount old home (keep) or create new home (new)
18 bootable=yes # bootable installation
24 test "$#" -eq 0 && echo "$0 destination [source]" && exit 1
25 dest="$1" # rootfs destination (absolute path or device)
27 src= # default source (absolute path)
28 test -n "$2" && src="$2" # or alternative rootfs copy
29 case "$src" in */);; *) src="$src/" ;; esac # make sure src has trailing slash
37 echo destination is partition
38 test -d /media/moebian && (echo cannot create mountdir; exit 1)
39 echo -n formatting $dest as ext3. are you sure [y/N]:
40 read go && test $go != y && echo aborting && exit 1
42 mkdir -p /media/moebian
43 mount $dest /media/moebian
48 if test $bootable = yes; then
49 device=`cat /proc/mounts | grep " $dest " | awk '{print $1}'`
51 /dev/mmcblk0*) card='${INT_CARD}'; cardname='eMMC' ;;
52 /dev/mmcblk1*) card='${EXT_CARD}'; cardname='SD' ;;
54 echo invalid destination
58 partition=`echo $device | sed s%/dev/mmcblk.%%`
60 echo installing bootable rootfs in $dest on $device = $card
62 echo installing chroot in $dest
64 echo mode: $mode, copy /opt: $withopt, $homedir /home
67 # create toplevel dirs and nand mount point
68 mountpoints="cdrom floppy home initrd media mnt nand proc srv sys syspart tmp"
70 copydirs="bin boot etc dev lib sbin root var"
71 checklinkdirs="bin boot etc lib sbin root usr var"
74 mkdir -p mnt/initfs # bootmenu nand mount point (required for boot!)
76 if test $dest = /home && test -L /opt; then
77 if test -d opt.nand; then
78 echo "opt for NAND does already exist. not updating"
81 ln -sf $dest/opt.nand /opt
82 echo "/home/opt moved to /home/opt.nand and symlink updated"
86 if test $mode = link; then
88 copydirs="$copydirs usr/local usr/share/man"
89 # excluded shared dirs
91 dontlink="usr/bin usr/games usr/include usr/lib usr/libexec usr/sbin \
92 usr/share usr/share/applications/*/ usr/X11R6"
95 for d in $dontlink; do
96 test -L "$d" && test ! -L "$src$d" && rm $d # remove existing links
99 elif test $mode = copy; then
100 copydirs="$copydirs $content"
102 echo "error: unknown mode $mode" && exit 1
105 for d in $copydirs; do
107 echo "skipping existing directory ${src}$d"
110 echo copying ${src}$d
114 modules=/lib/modules/2.6.28maemo-omap1
115 if test $bootable = yes && test -d $modules -a ! -d $dest$modules; then
116 echo copying maemo kernel modules
117 cp -a /lib/modules/2.6.28maemo-omap1 $dest/lib/modules
119 if test $withopt = yes; then
121 cp -a ${src}home/opt/* $dest/opt
123 mkdir -p $dest/opt # create empty opt
126 symlink () { # args: subdir path relpath
127 # recursive symlinking: create or update
128 $debug sub $1 $2 $topdir$2 $3
131 IFS="%" && for f in `/bin/ls -d * .[^.]* 2>/dev/null | tr '\n' '%'`; do
132 #IFS=$'\n' && for f in `/bin/ls -d * .[^.]* 2>/dev/null`; do
133 if test -L "$f"; then # update symlinks
134 # target exists and is not symlink: ignore
135 if test ! -e "$dest$2/$f" -o -L "$dest$2/$f"; then
138 /home/*) # copy relocated files
139 mkdir -p "$dest$2/$f"
141 cp -a $target "$dest$2/$f/.."
145 $debug al "$2/$f" $target
146 # if it links to rootfs make it a rel. symlink
147 test -e "$3$target" && target="$3$target"
150 $debug rl "$2/$f" $target
151 # just keep the rel. symlink
155 ln -s "$target" "$dest$2/$f"
157 echo "warning: $dest$2/$f exists but is not a symlink. ignoring"
159 elif test -d "$f"; then # a subdirectory
161 if test -L "$dest$2/$f" -o ! -e "$dest$2/$f"; then
162 # target is a symlink or doesn't exist -> update symlink
163 if test -L "$2/$f"; then
164 # symlink on NAND, was relocated
165 mkdir -p "$dest$2/$f"
167 cp -a $2/$f/* "$dest$2/$f"
170 ln -s "$3$2/$f" "$dest$2/$f"
173 if test -d "$dest$2/$f"; then
174 # symlink contents if is an existing directory in root
175 symlink "$f" "$2/$f" "../$3"
177 echo "warning: $dest$2/$f exists but is not a file. ignoring"
182 # update symlink is it not a existing file
183 test -f "$dest$2/$f" || ln -sf "$3$2/$f" "$dest$2/$f"
189 if test $mode = link; then
190 # TODO: find files that were moved from NAND to /home and copy or symlink them
191 for d in $content; do
192 (symlink "$src$d" "/$d" ../nand)
194 elif test $mode = copy; then
195 # process /home and /opt symlinks
196 echo -n checking for external symlinks...
198 find $checklinkdirs -type l > $tmp
199 echo processing `wc -l $tmp | awk '{print $1}'` links
204 test $withopt = yes || rm $l # delete /opt links
206 /home/*) # copy files relocated from NAND to /home
208 test -d $f && echo copying $f
209 cp -a $f $l # copy original file/dir
217 if test $homedir = keep; then
218 if test $dest = /home && test ! -d $dest/home/user; then
219 echo symlinking existing home
220 ln -s /user $dest/home/user
222 elif test $homedir = new; then
223 echo creating new home
224 rm -rf $dest/home/user
225 mkdir -p $dest/home/user
226 (cd $src/etc/skel && cp -a . $dest/home/user)
227 chown -R user:users $dest/home/user
229 echo "error: unknown homedir mode $mode" && exit 1
231 if test $bootable = yes; then
232 stat=$(dpkg-query -W -f='${Status}' bootmenu 2>&1) || stat=none
233 if test "$stat" != "install ok installed"; then
234 echo installing bootmenu
235 deb=/var/tmp/bootmenu.deb
236 wget -O$deb "http://www.daimi.au.dk/~cvm/bootmenu_1.6_armel.deb" || \
237 (echo 'download failed'; exit 1)
238 dpkg -i $deb || (echo 'installation failed'; exit 1)
240 echo bootmenu successfully installed
242 echo bootmenu is already installed
244 bootitem=/etc/bootmenu.d/moebian.item
245 cat > $bootitem <<EOF
246 ITEM_NAME="Moebian ($cardname $partition)"
249 ITEM_MODULES="mbcache jbd ext3"
251 ITEM_FSOPTIONS="noatime,ro"
253 echo bootmenu item created
256 echo copying configuration
257 cp -a $here/etc $dest
258 mkdir -p $dest/var/lib/apt/lists/partial
260 $here/mountroot $dest
261 inroot="chroot $dest"
262 $inroot apt-get update
263 if test $bootable = yes; then
264 $inroot apt-get install -y --force-yes openssh-server
266 $here/umountroot $dest
268 echo installation finished successfully
271 if test $rawdest = yes; then
272 echo removing temporary mount point
273 umount $dest && rm -rf $dest