Log In
New Account
  
Home My Page Project Cloud Code Snippets Project Openings Moebian
Summary Tracker Lists Tasks Docs News SCM Files Wiki
1 #!/bin/sh
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
20 debug=:
21 #debug=echo
22 #set -x
24 test "$#" -eq 0 && echo "$0 destination [source]" && exit 1
25 dest="$1" # rootfs destination (absolute path or device)
26 #dest=`pwd`
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 
30 here=`dirname $0`
31 here=`realpath $here`
33 rawdest=no
34 case $dest in 
35 /dev/mmcblk[01]p?)
36     rawdest=yes
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
41     mkfs.ext3 $dest
42     mkdir -p /media/moebian
43     mount $dest /media/moebian
44     dest=/media/moebian
45     ;;
46 esac
48 if test $bootable = yes; then
49     device=`cat /proc/mounts | grep " $dest " | awk '{print $1}'`
50     case $device in 
51         /dev/mmcblk0*) card='${INT_CARD}'; cardname='eMMC' ;;
52         /dev/mmcblk1*) card='${EXT_CARD}'; cardname='SD' ;;
53         *)
54             echo invalid destination
55             exit 1
56             ;;
57     esac
58     partition=`echo $device | sed s%/dev/mmcblk.%%`
59     card=$card$partition
60     echo installing bootable rootfs in $dest on $device = $card
61 else
62     echo installing chroot in $dest
63 fi
64 echo mode: $mode, copy /opt: $withopt, $homedir /home
66 cd "$dest"
67 # create toplevel dirs and nand mount point
68 mountpoints="cdrom floppy home initrd media mnt nand proc srv sys syspart tmp"
69 content="usr"
70 copydirs="bin boot etc dev lib sbin root var"
71 checklinkdirs="bin boot etc lib sbin root usr var"
73 mkdir -p $mountpoints
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"
79     else
80         mv opt opt.nand
81         ln -sf $dest/opt.nand /opt
82         echo "/home/opt moved to /home/opt.nand and symlink updated"
83     fi
84 fi
86 if test $mode = link; then
87     mkdir -p $content
88     copydirs="$copydirs usr/local usr/share/man"
89     # excluded shared dirs
90     cd "$src"
91     dontlink="usr/bin usr/games usr/include usr/lib usr/libexec usr/sbin \
92     usr/share usr/share/applications/*/ usr/X11R6"
93     echo $dontlink
94     cd "$dest"
95     for d in $dontlink; do
96         test -L "$d" && test ! -L "$src$d" && rm $d # remove existing links
97         mkdir -p $d
98     done
99 elif test $mode = copy; then
100     copydirs="$copydirs $content"
101 else
102     echo "error: unknown mode $mode" && exit 1
103 fi
105 for d in $copydirs; do
106     if test -d $d; then
107         echo "skipping existing directory ${src}$d"
108         continue
109     fi
110     echo copying ${src}$d
111     mkdir -p $d
112     cp -a ${src}$d $d/..
113 done
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
118 fi
119 if test $withopt = yes; then
120     echo copying /opt
121     cp -a ${src}home/opt/* $dest/opt
122 else
123     mkdir -p $dest/opt # create empty opt
124 fi
126 symlink () { # args: subdir path relpath
127     # recursive symlinking: create or update
128     $debug sub $1 $2 $topdir$2 $3
129     echo symlinking "$2"
130     cd "$1"
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
136                 target=`readlink $f`
137                 case "$target" in
138                 /home/*) # copy relocated files
139                         mkdir -p "$dest$2/$f"
140                     echo copying $target
141                         cp -a $target "$dest$2/$f/.."
142                     continue
143                         ;;
144                 /*) 
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"
148                     ;;
149                 *)
150                     $debug rl "$2/$f" $target
151                     # just keep the rel. symlink
152                     ;;
153                 esac
154                 rm -f "$dest$2/$f"
155                 ln -s "$target" "$dest$2/$f"
156             else
157                 echo "warning: $dest$2/$f exists but is not a symlink. ignoring"
158             fi
159         elif test -d "$f"; then # a subdirectory
160             $debug sd "$2/$f"
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"
166                     echo copying "$2/$f"
167                         cp -a $2/$f/* "$dest$2/$f"
168                 else
169                         rm -f "$dest$2/$f"
170                     ln -s "$3$2/$f" "$dest$2/$f"
171                 fi
172             else
173                 if test -d "$dest$2/$f"; then
174                     # symlink contents if is an existing directory in root
175                     symlink "$f" "$2/$f" "../$3"
176                 else
177                     echo "warning: $dest$2/$f exists but is not a file. ignoring"
178                 fi
179             fi
180         else
181             $debug fi "$2/$f"
182             # update symlink is it not a existing file
183             test -f "$dest$2/$f" || ln -sf "$3$2/$f" "$dest$2/$f"
184         fi
185     done
186     cd ..
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)
193     done
194 elif test $mode = copy; then
195     # process /home and /opt symlinks
196     echo -n checking for external symlinks...
197     tmp=`mktemp`
198     find $checklinkdirs -type l > $tmp
199     echo processing `wc -l $tmp | awk '{print $1}'` links
200     while read l; do
201         f=`readlink $l`
202         case "$f" in
203         /opt/*)
204             test $withopt = yes || rm $l # delete /opt links
205             ;;
206         /home/*) # copy files relocated from NAND to /home
207             rm $l
208             test -d $f && echo copying $f
209             cp -a $f $l # copy original file/dir
210             ;;
211         *) ;;
212         esac
213     done < $tmp
214     rm $tmp
215 fi
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
221     fi
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
228 else
229     echo "error: unknown homedir mode $mode" && exit 1
230 fi
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)
239         rm $deb
240         echo bootmenu successfully installed
241     else
242         echo bootmenu is already installed
243     fi
244     bootitem=/etc/bootmenu.d/moebian.item
245     cat > $bootitem <<EOF
246 ITEM_NAME="Moebian ($cardname $partition)"
247 ITEM_ID="moebian"
248 ITEM_DEVICE="$card"
249 ITEM_MODULES="mbcache jbd ext3"
250 ITEM_FSTYPE="ext3"
251 ITEM_FSOPTIONS="noatime,ro"
252 EOF
253     echo bootmenu item created
254 fi
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
265 fi
266 $here/umountroot $dest
268 echo installation finished successfully
269 df -h $dest
271 if test $rawdest = yes; then
272     echo removing temporary mount point
273     umount $dest && rm -rf $dest
274 fi
276 exit 0

Terms of Use    Privacy Policy    Contribution Guidelines    Feedback

Powered By GForge Collaborative Development Environment