livecdfs-update.sh: Escape ampersands in STARTX sed expression
[catalyst.git] / targets / support / livecdfs-update.sh
1 #!/bin/bash
2
3 RUN_DEFAULT_FUNCS="no"
4
5 source /tmp/chroot-functions.sh
6
7 # Allow root logins to our CD by default
8 if [ -e /etc/sshd/sshd_config ]
9 then
10         sed -i 's:^#PermitRootLogin\ yes:PermitRootLogin\ yes:' \
11                 /etc/ssh/sshd_config
12 fi
13
14 # Turn off udev tarball
15 sed -i 's:RC_DEVICE_TARBALL="yes":RC_DEVICE_TARBALL="no":' /etc/conf.d/rc
16
17 # Turn off udev coldplugging
18 sed -i 's:RC_COLDPLUG="yes":RC_COLDPLUG="no":' /etc/conf.d/rc
19
20 # Clean up the time and set to UTC
21 rm -rf /etc/localtime
22 cp /usr/share/zoneinfo/UTC /etc/localtime
23
24 # Setup the hostname
25 if [ "${clst_livecd_type}" == "gentoo-gamecd" ]
26 then
27         echo 'HOSTNAME="gamecd"' > /etc/conf.d/hostname
28         echo "127.0.0.1 gamecd.gentoo gamecd localhost" > /etc/hosts
29 else
30         echo 'HOSTNAME="livecd"' > /etc/conf.d/hostname
31         echo "127.0.0.1 livecd.gentoo livecd localhost" > /etc/hosts
32 fi
33
34 # Since we're an official Gentoo release, we do things the official Gentoo way.
35 # As such, we override livecd/users.
36 case ${clst_livecd_type} in
37         gentoo-release-live*)
38                 user_comment="Gentoo default user"
39                 clst_livecd_users="gentoo"
40         ;;
41         gentoo-gamecd)
42                 user_comment="Gentoo GameCD default user"
43                 clst_livecd_users="gamecd"
44         ;;
45 esac
46
47 # Add any users
48 if [ -n "${clst_livecd_users}" ]
49 then
50         first_user=$(echo ${clst_livecd_users} | cut -d' ' -f1)
51         default_comment="Default LiveCD User"
52         [ -z "${user_comment}" ] && user_comment=${default_comment}
53
54         # Here we check to see if games exists for bug #125498
55         if [ "$(getent group games | cut -d: -f1)" != "games" ]
56         then
57                 echo "Adding games group"
58                 groupadd -g 35 games
59         fi
60         if [ "$(getent group plugdev | cut -d: -f1)" != "plugdev" ]
61         then
62                 echo "Adding plugdev group"
63                 groupadd plugdev
64         fi
65         for x in ${clst_livecd_users}
66         do
67                 useradd -G users,wheel,audio,plugdev,games,cdrom,disk,floppy,usb \
68                         -g 100 -c "${user_comment}" -m ${x}
69                 chown -R ${x}:users /home/${x}
70                 if [ -n "${clst_livecd_xdm}" -a -n "${clst_livecd_xsession}" ]
71                 then
72                         echo "[Desktop]" > /home/${x}/.dmrc
73                         echo "Session=${clst_livecd_xsession}" >> /home/${x}/.dmrc
74                         chown -R ${x}:users /home/${x}
75                 fi
76         done
77 fi
78
79 # Setup sudoers
80 if [ -f /etc/sudoers ]
81 then
82         sed -i '/NOPASSWD: ALL/ s/^# //' /etc/sudoers
83 fi
84
85 # Setup links for ethernet devices
86 cd /etc/init.d
87 ln -sf net.lo net.eth1
88 ln -sf net.lo net.eth2
89 ln -sf net.lo net.eth3
90 ln -sf net.lo net.eth4
91
92 # Add this for hwsetup/mkx86config
93 mkdir -p /etc/sysconfig
94
95 # Tweak the livecd fstab so that users know not to edit it
96 # http://bugs.gentoo.org/show_bug.cgi?id=60887
97 echo "####################################################" > /etc/fstab
98 echo "## ATTENTION: THIS IS THE FSTAB ON THE LIVECD     ##" >> /etc/fstab
99 echo "## PLEASE EDIT THE FSTAB at /mnt/gentoo/etc/fstab ##" >> /etc/fstab
100 echo "####################################################" >> /etc/fstab
101
102 # fstab tweaks
103 echo "tmpfs     /                                       tmpfs   defaults        0 0" >> /etc/fstab
104 echo "tmpfs     /lib/firmware                   tmpfs   defaults        0 0" >> /etc/fstab
105 echo "tmpfs     /usr/portage                    tmpfs   defaults        0 0" >> /etc/fstab
106 # If /usr/lib/X11/xkb/compiled then make it tmpfs
107 if [ -d /usr/lib/X11/xkb/compiled ]
108 then
109         echo "tmpfs     /usr/lib/X11/xkb/compiled       tmpfs   defaults        0 0" >> \
110                 /etc/fstab
111 fi
112
113 # Tweak the livecd make.conf so that users know not to edit it
114 # http://bugs.gentoo.org/show_bug.cgi?id=144647
115 mv /etc/portage/make.conf /etc/portage/make.conf.old
116 echo "####################################################" >> /etc/portage/make.conf
117 echo "## ATTENTION: THIS IS THE MAKE.CONF ON THE LIVECD ##" >> /etc/portage/make.conf
118 echo "## PLEASE EDIT /mnt/gentoo/etc/portage/make.conf INSTEAD  ##" >> /etc/portage/make.conf
119 echo "####################################################" >> /etc/portage/make.conf
120 cat /etc/portage/make.conf.old >> /etc/portage/make.conf
121
122 # devfs tweaks
123 [ -e /etc/devfsd.conf ] && sed -i '/dev-state/ s:^:#:' /etc/devfsd.conf
124
125 # Add some helpful aliases
126 echo "alias cp='cp -i'" >> /etc/profile
127 echo "alias mv='mv -i'" >> /etc/profile
128 echo "alias rm='rm -i'" >> /etc/profile
129 echo "alias ls='ls --color=auto'" >> /etc/profile
130 echo "alias ll='ls -l'" >> /etc/profile
131 echo "alias grep='grep --color=auto'" >> /etc/profile
132
133 # Make sure we have the latest pci,usb and hotplug ids.  Older versions of
134 # pciutils and usbutils used /sbin, where newer versions use /usr/sbin.
135 [ -x /sbin/update-pciids ] && /sbin/update-pciids
136 [ -x /sbin/update-usbids ] && /sbin/update-usbids
137 [ -x /usr/sbin/update-pciids ] && /usr/sbin/update-pciids
138 [ -x /usr/sbin/update-usbids ] && /usr/sbin/update-usbids
139 if [ -d /usr/share/hwdata ]
140 then
141         # If we have uncompressed pci and usb ids files, symlink them.
142         [ -f /usr/share/misc/pci.ids ] && [ -f /usr/share/hwdata/pci.ids ] && \
143                 rm -f /usr/share/hwdata/pci.ids && ln -s /usr/share/misc/pci.ids \
144                 /usr/share/hwdata/pci.ids
145         [ -f /usr/share/misc/usb.ids ] && [ -f /usr/share/hwdata/usb.ids ] && \
146                 rm -f /usr/share/hwdata/usb.ids && ln -s /usr/share/misc/usb.ids \
147                 /usr/share/hwdata/usb.ids
148         # If we have compressed pci and usb files, we download our own copies.
149         [ -f /usr/share/misc/pci.ids.gz ] && [ -f /usr/share/hwdata/pci.ids ] && \
150                 rm -f /usr/share/hwdata/pci.ids && wget -O /usr/share/hwdata/pci.ids \
151                 http://pciids.sourceforge.net/v2.2/pci.ids
152         [ -f /usr/share/misc/usb.ids.gz ] && [ -f /usr/share/hwdata/usb.ids ] && \
153                 rm -f /usr/share/hwdata/usb.ids && wget -O /usr/share/hwdata/usb.ids \
154                 http://www.linux-usb.org/usb.ids
155 fi
156
157 # Setup opengl in /etc (if configured)
158 [ -x /usr/sbin/openglify ] && /usr/sbin/openglify
159
160 # Setup configured display manager
161 if [ -n "${clst_livecd_xdm}" ]
162 then
163         sed -i \
164                 -e "s:^#\\?DISPLAYMANAGER=.\+$:DISPLAYMANAGER=\"${clst_livecd_xdm}\":" \
165                 /etc/rc.conf
166         sed -i \
167                 -e "s:^#\\?DISPLAYMANAGER=.\+$:DISPLAYMANAGER=\"${clst_livecd_xdm}\":" \
168                 /etc/conf.d/xdm
169 fi
170
171 # Setup configured default X Session
172 if [ -n "${clst_livecd_xsession}" ]
173 then
174         sed -i "s:#XSESSION=\"Gnome\":XSESSION=\"${clst_livecd_xsession}\":" \
175                 /etc/rc.conf
176 fi
177
178 # touch /etc/asound.state
179 touch /etc/asound.state
180
181 # Tweak the MOTD for Gentoo releases
182 case ${clst_livecd_type} in
183         gentoo-release-universal)
184                 cat /etc/generic.motd.txt /etc/universal.motd.txt \
185                         /etc/minimal.motd.txt > /etc/motd
186                 sed -i 's:^##GREETING:Welcome to the Gentoo Linux Universal Installation CD!:' /etc/motd
187         ;;
188         gentoo-release-minimal)
189                 cat /etc/generic.motd.txt /etc/minimal.motd.txt > /etc/motd
190                 sed -i 's:^##GREETING:Welcome to the Gentoo Linux Minimal Installation CD!:' /etc/motd
191         ;;
192         gentoo-release-live*)
193                 cat /etc/generic.motd.txt \
194                         /etc/minimal.motd.txt /etc/livecd.motd.txt > /etc/motd
195                 sed -i -e 's:^##GREETING:Welcome to the Gentoo Linux LiveCD!:' \
196                         -e "s:##DISPLAY_MANAGER:${clst_livecd_xdm}:" /etc/motd
197         ;;
198         gentoo-gamecd)
199                 cat /etc/generic.motd.txt /etc/gamecd.motd.txt > /etc/motd
200                 sed -i 's:^##GREETING:Welcome to the Gentoo Linux ##GAME_NAME GameCD!:' /etc/motd
201         ;;
202 esac
203
204 rm -f /etc/generic.motd.txt /etc/universal.motd.txt /etc/minimal.motd.txt /etc/livecd.motd.txt /etc/gamecd.motd.txt
205
206 # Setup splash (if called for)
207 if [ -n "${clst_livecd_splash_theme}" ]
208 then
209         if [ -d /etc/splash/${clst_livecd_splash_theme} ]
210         then
211                 sed -i \
212                         -e "s:# SPLASH_THEME=\"gentoo\":SPLASH_THEME=\"${clst_livecd_splash_theme}\":" \
213                         -e "/^# SPLASH_TTYS=/ s/^#//" \
214                         /etc/conf.d/splash
215                 rm -f /etc/splash/default
216                 ln -s /etc/splash/${clst_livecd_splash_theme} /etc/splash/default
217         else
218                 echo "Error, cannot setup splash theme ${clst_livecd_splash_theme}"
219                 exit 1
220         fi
221 fi
222
223 # Create firmware directory if it does not exist
224 [ ! -d /lib/firmware ] && mkdir -p /lib/firmware
225
226 # tar up the firmware so that it does not get clobbered by the livecd mounts
227 if [ -n "$(ls /lib/firmware)" ]
228 then
229         cd /lib/firmware
230         /bin/tar -I bzip2 -cpf /lib/firmware.tar.bz2 .
231         rm -rf /lib/firmware/*
232 fi
233
234 # Clear out locales
235 case ${clst_livecd_type} in
236         gentoo-release-minimal|gentoo-release-universal|gentoo-gamecd)
237                 rm -rf /usr/lib/locale/{a,b,c,d,e{l,n_{A,B,C,D,G,H,I,N,P,S,US.,Z},s,t,u},f,g,h,i,j,k,l,m,n,o,p,r,s,t,u,v,w,x,y,z}*
238         ;;
239 esac
240
241 # Post configuration
242 case ${clst_livecd_type} in
243         gentoo-gamecd )
244                 # We grab our configuration
245                 if [ -e /tmp/gamecd.conf ]
246                 then
247                         source /tmp/gamecd.conf || exit 1
248                         rm /tmp/gamecd.conf
249
250                         # Here we replace out game information into several files
251                         sed -i -e "s:##GAME_NAME:${GAME_NAME}:" /etc/motd
252
253                         # Here we setup our xinitrc
254                         echo "exec ${GAME_EXECUTABLE}" > /etc/X11/xinit/xinitrc
255                 fi
256
257                 # This is my hack to reduce tmpfs usage
258                 mkdir -p /usr/livecd/db/pkg/x11-base
259                 mv -f /var/db/pkg/x11-base/xorg* /usr/livecd/db/pkg/x11-base
260                 rm -rf /var/db
261
262                 touch /etc/startx
263                 ;;
264         gentoo-release-live*)
265                 # Setup Gnome theme
266                 if [ "${clst_livecd_xsession}" == "gnome" ]
267                 then
268                         gconftool-2 --direct \
269                                 --config-source xml:readwrite:/etc/gconf/gconf.xml.defaults \
270                                 --type string --set /desktop/gnome/interface/font_name "Sans 9"
271                 fi
272
273                 # Remove locking on screensaver
274                 gconftool-2 --direct \
275                         --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults -s \
276                         -t bool /apps/gnome-screensaver/lock_enabled false >/dev/null
277
278                 # Setup GDM
279                 if [ "${clst_livecd_xdm}" == "gdm" ]
280                 then
281                         if [ ! -e /etc/X11/gdm/gdm.conf ] && [ -e /usr/share/gdm/defaults.conf ]
282                         then
283                                 if [ -n "${clst_livecd_users}" ] && [ -n "${first_user}" ]
284                                 then
285                                         sedxtra="\nTimedLogin=${first_user}"
286                                 else
287                                         sedxtra=""
288                                 fi
289
290                                 cp -f /etc/X11/gdm/custom.conf /etc/X11/gdm/custom.conf.old
291
292                                 sed     -i \
293                                         -e "s:\(\[daemon\]\)$:\1\nTimedLoginEnable=true\nTimedLoginDelay=10${sedxtra}:" \
294                                         -e 's:\(\[greeter\]\)$:\1\nGraphicalTheme=gentoo-emergence:' \
295                                         /etc/X11/gdm/custom.conf
296                         else
297                                 cp -f /etc/X11/gdm/gdm.conf /etc/X11/gdm/gdm.conf.old
298                                 sed -i \
299                                         -e 's:TimedLoginEnable=false:TimedLoginEnable=true:' \
300                                         -e 's:TimedLoginDelay=30:TimedLoginDelay=10:' \
301                                         -e 's:AllowRemoteRoot=true:AllowRemoteRoot=false:' \
302                                         -e ':^#GraphicalTheme=: s:^#::' \
303                                         -e 's:^GraphicalTheme=.*$:GraphicalTheme=gentoo-emergence:' \
304                                         /etc/X11/gdm/gdm.conf
305
306                                 if [ -n "${clst_livecd_users}" ] && [ -n "${first_user}" ]
307                                 then
308                                         sed -i \
309                                                 -e "s:TimedLogin=:TimedLogin=${first_user}:" \
310                                                 /etc/X11/gdm/gdm.conf
311                                 fi
312                         fi
313                 fi
314
315                 # This gives us our list of system packages for the installer
316                 mkdir -p /usr/livecd
317                 ### XXX: Andrew says we don't need this anymore
318                 USE="-* $(cat /var/db/pkg/sys-libs/glibc*/USE)" emerge -eqp system | grep -e '^\[ebuild' | sed -e 's:^\[ebuild .\+\] ::' -e 's: .\+$::' > /usr/livecd/systempkgs.txt
319
320                 # This is my hack to reduce tmpfs usage
321                 cp -r /usr/portage/profiles /usr/livecd
322                 cp -r /usr/portage/eclass /usr/livecd
323                 rm -rf /usr/livecd/profiles/{co*,default-{1*,a*,b*,d*,h*,i*,m*,p*,s*,x*},g*,hardened-*,n*,x*}
324                 mv -f /etc/gconf /usr/livecd
325                 ln -sf /usr/livecd/gconf /etc/gconf
326                 mv -f /var/db /usr/livecd
327                 ln -sf /usr/livecd/db /var/db
328
329                 # Clear out lastlog
330                 rm -f /var/log/lastlog && touch /var/log/lastlog
331
332                 # Create our Handbook icon
333                 [ -e /docs/handbook/index.html ] && create_handbook_icon
334                 [ -n "${clst_livecd_overlay}" ] && [ -e ${clst_livecd_overlay}/docs/handbook/index.html ] && create_handbook_icon
335
336                 # Copy our icons into place and build home directories
337                 if [ -n "${clst_livecd_users}" ]
338                 then
339                         for username in ${clst_livecd_users}
340                         do
341                                 mkdir -p /home/${username}/Desktop
342                                 # Copy our Handbook icon
343                                 [ -e /usr/share/applications/gentoo-handbook.desktop ] && \
344                                         cp -f /usr/share/applications/gentoo-handbook.desktop \
345                                         /home/${username}/Desktop
346                                 # Copy our installer icons
347                                 if [ -e /usr/share/applications/installer-gtk.desktop ]
348                                 then
349                                         cp -f /usr/share/applications/installer-gtk.desktop \
350                                                 /home/${username}/Desktop
351                                         cp -f /usr/share/applications/installer-dialog.desktop \
352                                                 /home/${username}/Desktop
353                                         sed -i -e \
354                                                 's:Exec=installer-dialog:Exec=sudo installer-dialog:' \
355                                                 /home/${username}/Desktop/installer-dialog.desktop
356                                         sed -i -e 's:Exec=installer-gtk:Exec=installer:' \
357                                                 /home/${username}/Desktop/installer-gtk.desktop
358                                 fi
359                                 chown -R ${username}:100 /home/${username}
360                         done
361                 fi
362                 ;;
363         generic-livecd )
364                 # This is my hack to reduce tmpfs usage
365                 mkdir -p /usr/livecd
366
367                 if [ -d /etc/gconf ]
368                 then
369                         mv -f /etc/gconf /usr/livecd
370                         ln -sf /usr/livecd/gconf /etc/gconf
371                 fi
372
373                 if [ -e /usr/livecd/kernelpkgs.txt ]
374                 then
375                         rm -f /usr/livecd/kernelpkgs.txt
376                 fi
377
378                 touch /etc/startx
379                 ;;
380         * )
381                 if [ -e /usr/livecd/kernelpkgs.txt ]
382                 then
383                         rm -f /usr/livecd/kernelpkgs.txt
384                 fi
385                 ;;
386 esac
387
388 # We want the first user to be used when auto-starting X
389 if [ -e /etc/startx ]
390 then
391         sed -i \
392                 "s:##STARTX:source /etc/profile \&\& su - ${first_user} -c startx:" \
393                 /root/.bashrc
394 fi
395
396 if [ -e /lib/rcscripts/addons/udev-start.sh ]
397 then
398         sed -i "s:\t\[\[ -x /sbin/evms_activate:\t\[\[ -x \${CDBOOT} \]\] \&\& \[\[ -x /sbin/evms_activate:" /lib/rcscripts/addons/udev-start.sh
399 fi