PageRenderTime 46ms CodeModel.GetById 18ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/packages/tools/installer/scripts/installer

http://github.com/OpenELEC/OpenELEC.tv
Shell | 654 lines | 432 code | 118 blank | 104 comment | 39 complexity | 7de8d5e6137cee149608bd870d408514 MD5 | raw file
  1#!/bin/sh
  2
  3################################################################################
  4#      This file is part of OpenELEC - http://www.openelec.tv
  5#      Copyright (C) 2009-2014 Stephan Raue (stephan@openelec.tv)
  6#
  7#  OpenELEC is free software: you can redistribute it and/or modify
  8#  it under the terms of the GNU General Public License as published by
  9#  the Free Software Foundation, either version 2 of the License, or
 10#  (at your option) any later version.
 11#
 12#  OpenELEC is distributed in the hope that it will be useful,
 13#  but WITHOUT ANY WARRANTY; without even the implied warranty of
 14#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15#  GNU General Public License for more details.
 16#
 17#  You should have received a copy of the GNU General Public License
 18#  along with OpenELEC.  If not, see <http://www.gnu.org/licenses/>.
 19################################################################################
 20
 21# some DOCs:
 22
 23# list devices:
 24# cat /proc/partitions | sed -n "s/\ *[0-9][0-9]*\ *[0-9][0-9]*\ *[0-9][0-9]*\ \([a-z]*\)$/\1/p"
 25
 26# list all partitionnumbers from /dev/sda:
 27# parted -m /dev/sda print |grep -v ^/dev |grep -v ^BYT | cut -f1 -d ":"
 28
 29# list device data from /dev/sda:
 30# parted -m /dev/sda print |grep /dev/sda
 31
 32# list mounted partitions:
 33# mount |grep ^/dev
 34
 35# list modelnumber:
 36# parted -m /dev/sda print |grep /dev/sda | cut -f7 -d ":" | sed "s/;//"
 37# list size:
 38# parted -m /dev/sda print |grep /dev/sda | cut -f2 -d ":"
 39
 40# exclude mounted partitions
 41# for i in `cat /proc/mounts | grep ^/dev/ | cut -f1 -d " " | sed "s/[0-9]//"`; do TEST="$TEST `echo "| grep -v $i"`"; done
 42
 43# Interpret embedded "\Z" sequences in the dialog text by the following
 44# character, which tells dialog to set colors or video attributes: 0 through 7
 45# are the ANSI used in curses: black, red, green, yellow, blue, magenta, cyan
 46# and white respectively. Bold is set by 'b', reset by 'B'. Reverse is set
 47# by 'r', reset by 'R'. Underline is set by 'u', reset by 'U'. The settings are
 48# cumulative, e.g., "\Zb\Z1" makes the following text bold (perhaps bright)
 49# red. Restore normal settings with "\Zn".
 50
 51# disable Ctrl+C - can be very dangerous
 52trap '' 2
 53
 54[ -f /etc/installer.conf ] && . /etc/installer.conf || exit 0
 55
 56dbglg() {
 57  # Acts just like echo cmd, with automatic redirection
 58
 59  echo "" >> $LOGFILE
 60  echo "###################################################################" >> $LOGFILE
 61  echo "# $@" >> $LOGFILE
 62  echo "###################################################################" >> $LOGFILE
 63  echo "" >> $LOGFILE
 64}
 65
 66get_device_unmount() {
 67  # get all unmounted devices
 68  # usage: get_devices_unmount
 69  # uses:     -
 70  # provides: DEVICES
 71
 72  DEVICES=""
 73  DEVICES=$(parted -s -m -l 2>/dev/null | grep -E '^/dev/sd|^/dev/mmcblk|^/dev/nvme' | grep -E -v '.?rpmb|.?boot?' | cut -f1 -d ":")
 74
 75  for i in $(cat /proc/mounts | grep -E '^/dev/sd' | cut -f1 -d " " | sed "s/[0-9].*$//"); do
 76    DEVICES=$(echo $DEVICES |sed -e "s|$i||")
 77  done
 78
 79  for i in $(cat /proc/mounts | grep -E '^/dev/mmcblk' | cut -f1 -d " " | sed "s/p.*$//"); do
 80    DEVICES=$(echo $DEVICES |sed -e "s|$i||")
 81  done
 82
 83  for i in $(cat /proc/mounts | grep -E '^/dev/nvme' | cut -f1 -d " " | sed "s/p.*$//"); do
 84    DEVICES=$(echo $DEVICES |sed -e "s|$i||")
 85  done
 86}
 87
 88get_partition() {
 89  # get all partitions of a specifed device
 90  # usage:    get_partitions /dev/sda
 91  # uses:     -
 92  # provides: PARTITIONS
 93
 94  PARTITIONS=$(parted -s -m $1 print |grep -v ^/dev |grep -v BYT | cut -f1 -d ":")
 95}
 96
 97create_device_list() {
 98  # creates device list to use in menus
 99  # usage:    create_devices_list
100  # uses:     get_device_unmount
101  # provides: DEVICE_MODEL, DEVICE_SIZE, DEVICE_LIST, DEVICE_NAME,
102  #           DEVICES (get_device_unmount)
103
104  DEVICE_MODEL=""
105  DEVICE_SIZE=""
106  DEVICE_LIST=""
107  DEVICE_NAME=""
108
109  get_device_unmount
110
111  if [ "$DEVICES" = "" ]; then
112    msg_no_device
113  fi
114
115  for i in $DEVICES; do
116    DEVICE_MODEL=$(parted -s $i -m print | grep ^$i | cut -f7 -d ":" | sed "s/;//")
117    DEVICE_SIZE=$(parted -s $i -m print | grep ^$i | cut -f2 -d ":")
118    DEVICE_NAME=$(echo $DEVICE_MODEL ${DEVICE_SIZE} | sed 's/ /_/g')
119    DEVICE_LIST="$DEVICE_LIST $i $DEVICE_NAME"
120  done
121}
122
123create_partition_list() {
124  # get an overview of all partitions of a specified device
125  # usage:    create_partition_list /dev/sda
126  # uses:     get_partition
127  # provides: PARTITION_NUMBER, PARTITION_SIZE, PARTITION_FORMAT,
128  #           PARTITION_LIST, PARTITIONS (get_partition)
129
130  PARTITION_NUMBER=""
131  PARTITION_SIZE=""
132  PARTITION_FORMAT=""
133  PARTITION_LIST=""
134
135  get_partition $1
136
137  for partition in $PARTITIONS; do
138    PARTITION_NUMBER=$(parted -s -m $1 print |grep -v ^/dev |grep -v BYT | cut -f1 -d ":")
139    PARTITION_SIZE=$(parted -s -m $1 print |grep -v ^/dev |grep -v BYT | cut -f4 -d ":")
140    PARTITION_FORMAT=$(parted -s -m $1 print |grep -v ^/dev |grep -v BYT | cut -f5 -d ":")
141    PARTITION_LIST=" $PARTITION_LIST \n Partition $1$PARTITION_NUMBER Size: $PARTITION_SIZE Format: $PARTITION_FORMAT"
142  done
143}
144
145do_install_mbr() {
146  # show menu
147
148  MSG_TITLE="\Z4[ (RE)INSTALL MBR ]\Zn"
149  MSG_MENU="\n Please select where to install MBR.\n\n Please select a device:"
150  MSG_CANCEL="Back"
151
152  create_device_list
153
154  dialog --colors --backtitle "$BACKTITLE" --cancel-label "$MSG_CANCEL" \
155    --title "$MSG_TITLE" --menu "$MSG_MENU" 20 50 5 \
156    $DEVICE_LIST 2> $TMPDIR/device_for_install
157
158  # now we must do everything
159  case $? in
160    0)
161      INSTALL_DEVICE=$(cat "$TMPDIR/device_for_install")
162
163      # installing mbr
164      prompt_gpt
165      if [ "$GPT" = "1" ]; then
166        cat /usr/share/syslinux/gptmbr.bin > $INSTALL_DEVICE
167      else
168        cat /usr/share/syslinux/mbr.bin > $INSTALL_DEVICE
169      fi
170
171      msg_install_ready "Master Boot Record installed on $INSTALL_DEVICE"
172      ;;
173    1)
174      menu_main
175      ;;
176    255)
177      do_poweroff
178      ;;
179  esac
180}
181
182do_install_quick() {
183  # show menu
184  MSG_TITLE="\Z4[ QUICK INSTALL MENU ]\Zn"
185  MSG_MENU="\nUse the up/down arrows to select the correct device.\n\n Please select a device:"
186  MSG_CANCEL="Back"
187  DIALOG_OPTIONS="--defaultno"
188
189  create_device_list
190
191  dialog --colors --backtitle "$BACKTITLE" --cancel-label "$MSG_CANCEL" \
192    $DIALOG_OPTIONS --title "$MSG_TITLE" --menu "$MSG_MENU" 20 50 5 \
193    $DEVICE_LIST 2> $TMPDIR/device_for_install
194
195  # now we must do everything
196  case $? in
197    0)
198      INSTALL_DEVICE=$(cat "$TMPDIR/device_for_install")
199      INSTALL_DEVICE_FULL=$(echo $DEVICE_LIST | sed "s|.*$INSTALL_DEVICE \([^ ]*\).*|$INSTALL_DEVICE \1|")
200
201      case $INSTALL_DEVICE in
202        "/dev/mmcblk"*|"/dev/nvme"*)
203          PART1="p1"
204          PART2="p2"
205          ;;
206        *)
207          PART1="1"
208          PART2="2"
209          ;;
210      esac
211
212      prompt_gpt
213      prompt_backup_unpack
214
215      # check for confirmation (twice!)
216      MSG_TITLE="\Z1[ Confirmation before installing ]\Zn"
217      MSG_DETAIL="\nIf you continue the target disk will be wiped out:\n\n$INSTALL_DEVICE_FULL\n\n"
218      DIALOG_OPTIONS="--defaultno"
219      dialog --colors --backtitle "$BACKTITLE" --title "$MSG_TITLE" \
220        $DIALOG_OPTIONS --yesno "$MSG_DETAIL" 0 0
221      if [ $? -ne 0 ]; then
222        menu_main
223      fi
224
225      MSG_TITLE="\Z1[ Confirmation before installing ]\Zn"
226      MSG_DETAIL="\nThis is last chance to abort the installation!\n\nIf you continue the target disk will be wiped out:\n\n$INSTALL_DEVICE_FULL\n\n\n"
227      DIALOG_OPTIONS="--defaultno"
228      dialog --colors --backtitle "$BACKTITLE" --title "$MSG_TITLE" \
229        $DIALOG_OPTIONS --yesno "$MSG_DETAIL" 0 0
230      if [ $? -ne 0 ]; then
231        menu_main
232      fi
233
234      # remove all partitions
235      msg_progress_install "1" "get all partitions $INSTALL_DEVICE"
236      get_partition $INSTALL_DEVICE
237
238      msg_progress_install "5" "wiping disk $INSTALL_DEVICE"
239      dd if=/dev/zero of=$INSTALL_DEVICE bs=4096 count=1024
240
241      # create 2 new partitions (first $PARTSIZE_SYSTEM, second rest)
242      msg_progress_install "7" "creating label on $INSTALL_DEVICE"
243      if [ "$GPT" = "1" ]; then
244        parted -s $INSTALL_DEVICE mklabel gpt >> $LOGFILE 2>&1
245      else
246        parted -s $INSTALL_DEVICE mklabel msdos >> $LOGFILE 2>&1
247      fi
248
249      msg_progress_install "9" "writing Master Boot Record on $INSTALL_DEVICE"
250      if [ "$GPT" = "1" ]; then
251        cat /usr/share/syslinux/gptmbr.bin > $INSTALL_DEVICE
252      else
253        cat /usr/share/syslinux/mbr.bin > $INSTALL_DEVICE
254      fi
255
256      msg_progress_install "10" "creating partition on $INSTALL_DEVICE"
257      if [ "$UEFI" = "1" ]; then
258        parted -s $INSTALL_DEVICE unit cyl mkpart primary fat32 -- 0 $PARTSIZE_SYSTEM >> $LOGFILE 2>&1
259      else
260        parted -s $INSTALL_DEVICE unit cyl mkpart primary ext2 -- 0 $PARTSIZE_SYSTEM >> $LOGFILE 2>&1
261      fi
262
263      msg_progress_install "13" "creating partition on $INSTALL_DEVICE"
264      parted -s $INSTALL_DEVICE unit cyl mkpart primary ext2 -- $PARTSIZE_SYSTEM -2 >> $LOGFILE 2>&1
265
266      msg_progress_install "16" "setup bootflag on partition 1 of $INSTALL_DEVICE"
267      parted -s $INSTALL_DEVICE set 1 boot on >> $LOGFILE 2>&1
268      if [ "$GPT" = "1" ]; then
269        parted -s $INSTALL_DEVICE set 1 legacy_boot on >> $LOGFILE 2>&1
270      fi
271
272      msg_progress_install "20" "tell the kernel we have a new partitiontable on $INSTALL_DEVICE"
273      partprobe $INSTALL_DEVICE >> $LOGFILE 2>&1
274
275      # create filesystem
276      msg_progress_install "23" "creating filesystem on ${INSTALL_DEVICE}1"
277      if [ "$UEFI" = "1" ]; then
278        mkfs.vfat ${INSTALL_DEVICE}${PART1} >> $LOGFILE 2>&1
279      else
280        mke2fs -t ext4 -m 0 ${INSTALL_DEVICE}${PART1} >> $LOGFILE 2>&1
281      fi
282
283      msg_progress_install "25" "set uuid and disklabel $DISKLABEL_SYSTEM on ${INSTALL_DEVICE}${PART1}"
284      if [ "$UEFI" = "1" ]; then
285        dosfslabel ${INSTALL_DEVICE}${PART1}  $DISKLABEL_SYSTEM >> $LOGFILE 2>&1
286      else
287        tune2fs -U random -L $DISKLABEL_SYSTEM  ${INSTALL_DEVICE}${PART1} >> $LOGFILE 2>&1
288      fi
289
290      msg_progress_install "28" "creating filesystem on ${INSTALL_DEVICE}${PART2}"
291      mke2fs -t ext4 -m 0 ${INSTALL_DEVICE}${PART2} >> $LOGFILE 2>&1
292
293      msg_progress_install "30" "set uuid and disklabel $DISKLABEL_STORAGE on ${INSTALL_DEVICE}${PART2}"
294      tune2fs -U random -L $DISKLABEL_STORAGE  ${INSTALL_DEVICE}${PART2} >> $LOGFILE 2>&1
295
296      # mount system partition
297      msg_progress_install "35" "creating $TMPDIR/part1"
298      mkdir -p $TMPDIR/part1 >> $LOGFILE 2>&1
299
300      msg_progress_install "40" "mounting ${INSTALL_DEVICE}${PART1} to $TMPDIR/part1"
301      if [ "$UEFI" = "1" ]; then
302        mount -t vfat ${INSTALL_DEVICE}${PART1} $TMPDIR/part1 >> $LOGFILE 2>&1
303      else
304        mount -t ext4 ${INSTALL_DEVICE}${PART1} $TMPDIR/part1 >> $LOGFILE 2>&1
305      fi
306
307      # installing extlinux
308      msg_progress_install "50" "installing extlinux to $TMPDIR/part1"
309      if [ "$UEFI" = "1" ]; then
310        syslinux -i ${INSTALL_DEVICE}${PART1} >> $LOGFILE 2>&1
311      else
312        extlinux -i $TMPDIR/part1 >> $LOGFILE 2>&1
313      fi
314
315      # install system files
316      msg_progress_install "60" "installing Kernel"
317      cp /flash/KERNEL $TMPDIR/part1 >> $LOGFILE 2>&1
318
319      msg_progress_install "65" "installing System"
320      cp /flash/SYSTEM $TMPDIR/part1 >> $LOGFILE 2>&1
321      sync
322
323      # configuring bootloader
324      msg_progress_install "80" "setup bootloader with boot label = $DISKLABEL_SYSTEM and disk label = $DISKLABEL_STORAGE"
325      echo "DEFAULT linux" > $TMPDIR/part1/extlinux.conf
326      echo "PROMPT 0" >> $TMPDIR/part1/extlinux.conf
327      echo " " >> $TMPDIR/part1/extlinux.conf
328      echo "LABEL linux" >> $TMPDIR/part1/extlinux.conf
329      echo " KERNEL /KERNEL" >> $TMPDIR/part1/extlinux.conf
330      echo " APPEND boot=LABEL=$DISKLABEL_SYSTEM disk=LABEL=$DISKLABEL_STORAGE $EXTLINUX_PARAMETERS quiet" >> $TMPDIR/part1/extlinux.conf
331      # uefi boot / hybrid mode
332      if [ "$UEFI" = "1" ]; then
333        mv $TMPDIR/part1/extlinux.conf $TMPDIR/part1/syslinux.cfg
334        mkdir -p $TMPDIR/part1/EFI/BOOT
335        cp $TMPDIR/part1/syslinux.cfg $TMPDIR/part1/EFI/BOOT
336        cp /usr/share/syslinux/bootx64.efi $TMPDIR/part1/EFI/BOOT
337        cp /usr/share/syslinux/ldlinux.e64 $TMPDIR/part1/EFI/BOOT
338      fi
339      sync
340
341      # umount system partition, remove mountpoint
342      msg_progress_install "85" "unmount $TMPDIR/part1"
343      umount $TMPDIR/part1 >> $LOGFILE 2>&1
344
345      msg_progress_install "87" "remove $TMPDIR/part1"
346      rmdir $TMPDIR/part1 >> $LOGFILE 2>&1
347
348      if [ "$BACKUP_UNPACK" = "1" ]; then
349        # mount storage partition
350        msg_progress_install "89" "creating $TMPDIR/part2"
351        mkdir -p $TMPDIR/part2 >> $LOGFILE 2>&1
352
353        msg_progress_install "90" "mounting ${INSTALL_DEVICE}${PART2} to $TMPDIR/part2"
354        mount -t ext4 ${INSTALL_DEVICE}${PART2} $TMPDIR/part2 >> $LOGFILE 2>&1
355
356        msg_progress_install "92" "restoring backup"
357        [ -f /flash/backup.tar.bz2 ] && tar -xjf /flash/backup.tar.bz2 -C $TMPDIR/part2 >> $LOGFILE 2>&1
358        [ -f /flash/backup.zip ] && unzip -qq /flash/backup.zip -d $TMPDIR/part2 >> $LOGFILE 2>&1
359        sync
360
361        # umount system partition, remove mountpoint
362        msg_progress_install "97" "unmount $TMPDIR/part2"
363        umount $TMPDIR/part2 >> $LOGFILE 2>&1
364
365        msg_progress_install "100" "remove $TMPDIR/part2"
366        rmdir $TMPDIR/part2 >> $LOGFILE 2>&1
367      fi
368
369      # install complete
370      MSG_TITLE="\Z1[ OpenELEC Install Complete ]\Zn"
371      MSG_DETAIL="\nYou may now remove the install media and reboot\n"
372      dialog --colors --backtitle "$BACKTITLE" --title "$MSG_TITLE" --msgbox "$MSG_DETAIL" 6 52
373
374      menu_main
375      ;;
376    1)
377      menu_main
378      ;;
379    255)
380      do_poweroff
381      ;;
382  esac
383}
384
385do_install_custom() {
386  # show menu
387  MSG_TITLE="\Z4[ REPAIR/UPGRADE MENU ]\Zn"
388  MSG_MENU="\nUse the up/down arrows to select the correct partition where you want to overwrite KERNEL and SYSTEM files.\n\n Please select a partition:"
389  MSG_CANCEL="Back"
390  DIALOG_OPTIONS="--defaultno"
391
392  get_device_unmount
393
394  if [ "$DEVICES" = "" ]; then
395    msg_no_device
396  fi
397
398  PARTITION_LIST=""
399  for device in $DEVICES; do
400    get_partition $device
401    for partition in $PARTITIONS; do
402      LABEL=$(tune2fs -l $device$partition | awk 'BEGIN {FS=":"} /Filesystem volume name/ {gsub(/ /,"",$2); print $2}')
403      if [ "$LABEL" = "$DISKLABEL_SYSTEM" ]; then
404        DEVICE_MODEL=$(parted -s $device -m print | grep ^$device | cut -f7 -d ":" | sed "s/;//")
405        DEVICE_SIZE=$(parted -s $device -m print | grep ^$device | cut -f2 -d ":")
406        DEVICE_NAME=$(echo $DEVICE_MODEL ${DEVICE_SIZE} | sed 's/ /_/g')
407        PARTITION_LIST="$PARTITION_LIST $device$partition $DEVICE_NAME"
408      fi
409    done
410  done
411
412  if [ "$PARTITION_LIST" = "" ]; then
413    msg_no_device
414  fi
415
416  dialog --colors --backtitle "$BACKTITLE" --cancel-label "$MSG_CANCEL" \
417    $DIALOG_OPTIONS --title "$MSG_TITLE" --menu "$MSG_MENU" 20 50 5 \
418    $PARTITION_LIST 2> $TMPDIR/device_for_install
419
420  # now we must do everything
421  case $? in
422    0)
423      INSTALL_PARTITION=$(cat "$TMPDIR/device_for_install")
424      INSTALL_PARTITION_FULL=$(echo $PARTITION_LIST | sed "s|.*$INSTALL_PARTITION \([^ ]*\).*|$INSTALL_PARTITION \1|")
425
426      # check for confirmation (twice!)
427      MSG_TITLE="\Z1[ Confirmation before copying ]\Zn"
428      MSG_DETAIL="\nIf you continue the target partition will be\noverwritten with new KERNEL and SYSTEM files:\n\n$INSTALL_PARTITION_FULL\n\n"
429      DIALOG_OPTIONS="--defaultno"
430      dialog --colors --backtitle "$BACKTITLE" --title "$MSG_TITLE" \
431        $DIALOG_OPTIONS --yesno "$MSG_DETAIL" 0 0
432      if [ $? -ne 0 ]; then
433        menu_main
434      fi
435
436      MSG_TITLE="\Z1[ Confirmation before copying ]\Zn"
437      MSG_DETAIL="\nThis is last chance to abort the copying!\n\nIf you continue the target partition will be\noverwritten with new KERNEL and SYSTEM files:\n\n$INSTALL_PARTITION_FULL\n\n\n"
438      DIALOG_OPTIONS="--defaultno"
439      dialog --colors --backtitle "$BACKTITLE" --title "$MSG_TITLE" \
440        $DIALOG_OPTIONS --yesno "$MSG_DETAIL" 0 0
441      if [ $? -ne 0 ]; then
442        menu_main
443      fi
444
445      # mount system partition
446      msg_progress_install "5" "creating $TMPDIR/part1"
447      mkdir -p $TMPDIR/part1 >> $LOGFILE 2>&1
448
449      msg_progress_install "10" "mounting $INSTALL_PARTITION to $TMPDIR/part1"
450      mount -t ext4 $INSTALL_PARTITION $TMPDIR/part1 >> $LOGFILE 2>&1
451
452      # check for enough target space
453      msg_progress_install "15" "checking for space on $INSTALL_PARTITION"
454
455      KERNEL_SIZE=$(stat -t /flash/KERNEL | awk '{print $2}')
456      SYSTEM_SIZE=$(stat -t /flash/SYSTEM | awk '{print $2}')
457      SRC_SIZE=$(( $KERNEL_SIZE + $SYSTEM_SIZE ))
458
459      DEST_SIZE=$(df $TMPDIR/part1 | awk '/[0-9]%/{print $4}')
460      DEST_SIZE=$(( $DEST_SIZE * 1024 ))
461      if [ -f $TMPDIR/part1/KERNEL ]; then
462        KERNEL_SIZE=$(stat -t $TMPDIR/part1/KERNEL | awk '{print $2}')
463        DEST_SIZE=$(( $DEST_SIZE + $KERNEL_SIZE ))
464      fi
465      if [ -f $TMPDIR/part1/SYSTEM ]; then
466        SYSTEM_SIZE=$(stat -t $TMPDIR/part1/SYSTEM | awk '{print $2}')
467        DEST_SIZE=$(( $DEST_SIZE + $SYSTEM_SIZE ))
468      fi
469
470      if [ $SRC_SIZE -ge $DEST_SIZE ]; then
471        umount $TMPDIR/part1 >> $LOGFILE 2>&1
472        rmdir $TMPDIR/part1 >> $LOGFILE 2>&1
473        msg_target_space
474        menu_main
475      fi
476
477      # install system files
478      msg_progress_install "20" "installing Kernel"
479      cp /flash/KERNEL $TMPDIR/part1 >> $LOGFILE 2>&1
480
481      msg_progress_install "40" "installing System"
482      cp /flash/SYSTEM $TMPDIR/part1 >> $LOGFILE 2>&1
483      sync
484
485      # umount system partition, remove mountpoint
486      msg_progress_install "95" "unmount $TMPDIR/part1"
487      umount $TMPDIR/part1 >> $LOGFILE 2>&1
488
489      msg_progress_install "100" "remove $TMPDIR/part1"
490      rmdir $TMPDIR/part1 >> $LOGFILE 2>&1
491
492      # install complete
493      MSG_TITLE="\Z1[ OpenELEC Install Complete ]\Zn"
494      MSG_DETAIL="\nYou may now remove the install media and reboot\n"
495      dialog --colors --backtitle "$BACKTITLE" --title "$MSG_TITLE" --msgbox "$MSG_DETAIL" 6 52
496
497      menu_main
498      ;;
499    1)
500      menu_main
501      ;;
502    255)
503      do_poweroff
504      ;;
505  esac
506}
507
508msg_no_device() {
509  # show a warning dialog if we dont find not mounted devices for install and return to main menu
510  MSG_TITLE="\Z1[ WARNING ]\Zn"
511  MSG_INFOBOX=" No devices were found. "
512
513  dialog --colors --backtitle "$BACKTITLE" --title "$MSG_TITLE" --msgbox "$MSG_INFOBOX" 9 70
514
515  menu_main
516}
517
518msg_target_space() {
519  # show an error dialog for missing space
520  MSG_TITLE="\Z1[ TARGET SPACE ]\Zn"
521  MSG_INFOBOX="\nNot enough target space!\nCopying aborted.\n"
522
523  dialog --colors --backtitle "$BACKTITLE" --title "$MSG_TITLE" --msgbox "$MSG_INFOBOX" 8 70
524
525  menu_main
526}
527
528msg_install_ready() {
529  # show a dialog that we have installed
530  MSG_TITLE="\Z1[ INFORMATION ]\Zn"
531
532  dialog --colors --backtitle "$BACKTITLE" --title "$MSG_TITLE" --msgbox " $1" 7 70
533
534  menu_main
535}
536
537msg_progress_install() {
538  # show the progress dialog
539  MSG_TITLE="\Z1[ INSTALLING ]\Zn"
540
541  dbglg "$2"
542  dialog --colors --backtitle "$BACKTITLE" --title "$MSG_TITLE" --gauge "$2 ..." 6 70 $1 &
543}
544
545prompt_gpt() {
546  GPT="0"
547  UEFI="0"
548  # Get size in GB.
549  INSTALL_DEVICE_SIZE=$(($(cat /sys/block/${INSTALL_DEVICE#/dev/}/size)*512/1000/1000/1000))
550  if [ "$INSTALL_DEVICE_SIZE" -ge 2200 ] 2>/dev/null; then
551    GPT="1"
552  fi
553  # force gpt + uefi in uefi boot mode
554  if [ -d /sys/firmware/efi ]; then
555    UEFI="1"
556    GPT="1"
557  fi
558}
559
560prompt_backup_unpack() {
561  # Prompt for unpacking backup files to /storage
562  # usage:    prompt_backup_unpack
563  # uses:
564  # provides: BACKUP_UNPACK
565  BACKUP_UNPACK="0"
566  if [ -f /flash/backup.tar.bz2 -o -f /flash/backup.zip ]; then
567    MSG_TITLE="\Z1[ Restore backup files ]\Zn"
568    MSG_DETAIL="Restore backup files to storage partition.\nFile backup.tar.bz2 or/and backup.zip exist on\ninstallation USB stick."
569    DIALOG_OPTIONS="--defaultno"
570    if dialog --colors --backtitle "$BACKTITLE" --title "$MSG_TITLE" $DIALOG_OPTIONS --yesno "$MSG_DETAIL" 0 0; then
571      BACKUP_UNPACK="1"
572    fi
573  fi
574}
575
576menu_main() {
577  # show the mainmenu
578  MSG_TITLE="\Z4[ MAIN MENU ]\Zn"
579  MSG_MENU="\n\ZbQuick Install:\Zn do a default installation on a specific device \
580            \Z1\Zb(this will delete ALL data on this device!)\Zn \
581            \n\ZbRepair / Upgrade:\Zn do repair / upgrade \
582            \n\ZbShow logfile:\Zn show and save the logfile \
583            \n \
584            \nPlease select:"
585  MSG_CANCEL="Reboot"
586
587  dialog --colors --backtitle "$BACKTITLE" --cancel-label "$MSG_CANCEL" \
588    --title "$MSG_TITLE" --menu "$MSG_MENU" 20 70 5 \
589      1 "Quick Install of OpenELEC" \
590      2 "Repair / Upgrade" \
591      3 "Show logfile" 2> $TMPDIR/mainmenu
592
593  case $? in
594    0)
595      ITEM_MAINMENU=$(cat "$TMPDIR/mainmenu")
596      case $ITEM_MAINMENU in
597        1) do_install_quick; break;;
598        2) do_install_custom; break;;
599        3) logfile_show; break;;
600      esac
601      ;;
602    1)
603      do_reboot
604      ;;
605    255)
606      do_poweroff
607      ;;
608  esac
609}
610
611logfile_show() {
612  dialog --textbox "$LOGFILE" 20 70
613  clear
614  menu_main
615}
616
617do_reboot() {
618  # reboot on request
619  clear
620  sync
621  reboot -f
622}
623
624do_poweroff() {
625  # powerdown on request
626  clear
627  sync
628  poweroff -f
629}
630
631# setup needed variables
632INSTALLER_VERSION="0.2.7"
633OS_VERSION=$(lsb_release)
634BACKTITLE="OpenELEC Installer $INSTALLER_VERSION - $OS_VERSION"
635
636TMPDIR="/tmp/installer"
637LOGFILE="$TMPDIR/install.log"
638
639# prepare temporary directory
640rm -rf $TMPDIR
641mkdir -p $TMPDIR
642
643#create log file
644touch "$LOGFILE"
645
646# main
647
648while true; do
649  clear
650  menu_main
651done
652
653# exit cleanly
654exit 0