installer.sh 25.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
# openmamba installation script
# Copyright (c) 2007-2015 by Silvan Calarco <silvan.calarco@mambasoft.it>
#
# For the devfs2gdev() function:
# Copyright (c) 2006 by Davide Madrisan <davide.madrisan@gmail.com>
#
# Released under the terms of the GNU GPLv3 License

VERSION=1.1
TEXTDOMAIN=installer
TEXTDOMAINDIR=/opt/kde3/share/locale/
INSTALL_DATE=`date +%Y%m%d-%H%M`
INPUT_FILE=~/.installer.conf
MAMBAWELCOME_SCRIPT=/usr/share/openmamba/mambawelcome/mambawelcome.sh
LOG_FILE=/var/log/install-$INSTALL_DATE.log
MOUNTPOINT=/mnt/install
KERNEL_RELEASE=`uname -r`
PIDFILE=/run/openmamba-installer.pid

[ -e $PIDFILE ] && PIDCHECK=`cat $PIDFILE`

[ "$PIDCHECK" -a -e /proc/$PIDCHECK ] && {
   echo "Error: an installer process is already running; exiting."
   exit 0
}

trap "umount -R $MOUNTPOINT 2>/dev/null; rm -f $PIDFILE" 0 HUP INT QUIT ABRT KILL TERM

function abort() {
   [ "$DCOPID" ] && {
      dcop $DCOPID KommanderIf setText progressText $"Installation aborted; press Next to see the error log."
      dcop $DCOPID KommanderIf setText finallabel $"Installation aborted :-("
      dcop $DCOPID KommanderIf setEnabled next true
   }
   rm -f $PIDFILE
   exit 1
}

function finished() {
   [ "$DCOPID" ] && {
      dcop $DCOPID KommanderIf setText progressText $"Installation completed; press Next to continue."
      dcop $DCOPID KommanderIf setText progressBar 100
      dcop $DCOPID KommanderIf setText installTopLabel $"Installation completed."
      dcop $DCOPID KommanderIf setText installInfoLabel ""
      dcop $DCOPID KommanderIf setEnabled next true
      dcop $DCOPID KommanderIf setEnabled ProgressGroupBox true
   }
   rm -f $PIDFILE
   exit 1
}

function dcop_write() {
   [ "$DCOPID" ] && {
      dcop $DCOPID KommanderIf setText progressText "$1"
      dcop $DCOPID KommanderIf setText progressBar $2
   }
}

function dcop_write_details() {
   [ "$DCOPID" ] && {
      dcop $DCOPID KommanderIf setText progressText2 "$1"
   }
}

function apt_parser() {
   while read line; do
      [ "$DCOPID" ] && {
         echo $line
         dcop $DCOPID KommanderIf setText progressText2 "$line"
      }
   done
}

cp_parser_count=0
date_start=`date +%s`

function cp_parser() {
   while read line; do
      let cp_parser_count+=1
      if [ $cp_parser_count -ge 100 ]; then
            CP_LINE=`echo ${line/*$MOUNTPOINT/}`
	    MOUNT_SPACE=`df -m $MOUNTPOINT | tail -1 | awk '{ print $3; }'`
            PERCENT=`expr $MOUNT_SPACE \* 100 / $TOTAL_SPACE`
            let TIME_ELAPSED=\(`date +%s`-$date_start\)/60
         if [ "$DCOPID" ]; then
            dcop_write $"Copying files (/$d)..." `expr $PERCENT / 2 + $prog`
            dcop_write_details $"\
Copying: ${CP_LINE:0:-1}
${MOUNT_SPACE} Mbytes copied
Installation time: $TIME_ELAPSED minutes"
         else
            echo -e -n "\rCopying files (${MOUNT_SPACE}M ${TIME_ELAPSED}m ${PERCENT}%)                                            "
            echo -e -n "\rCopying files (${MOUNT_SPACE}M ${TIME_ELAPSED}m ${PERCENT}%) ${CP_LINE:0:40}..."
         fi
	 cp_parser_count=0
      fi
   done
}

function get_udisks_key() {
   LANG=C udisks --show-info $1 | grep " $2:" | head -n1 | sed "s|[[:space:]]*$2:[[:space:]]*\(.*\)|\1|"
}

[ "$1" = "-y" ] || {
   echo $"Warning: this script will perform an automatic installation erasing all the"
   echo $"         contents of device $INSTALL_DEVICE; use $0 -y to actually start the"
   echo $"         operation."
   exit 0
}

echo $$ > $PIDFILE

DCOPID=$2

echo $"\
openmamba installation script - version $VERSION
Copyright (c) 2007-2015 by Silvan Calarco <silvan.calarco@mambasoft.it>
"

dcop_write $"Starting installation..." 5

[ -r $INPUT_FILE ] || {
   echo $"Error: cannot read input file $INPUT_FILE; aborting." >&2
   abort
}

. $INPUT_FILE

FORMAT_APPEND=
FORMAT_CMD="/sbin/mkfs.$INSTALL_FSTYPE"
[ "$INSTALL_HOSTNAME" ] || INSTALL_HOSTNAME="openmamba"

case "$INSTALL_FSTYPE" in
   ext2|ext3|ext4|jfs|nilfs2)
         [ "$INSTALL_BADBLOCKS" = "1" ] && FORMAT_APPEND="-c"
         [ "$INSTALL_HOSTNAME" ] && \
            FORMAT_APPEND="$FORMAT_APPEND -L $INSTALL_HOSTNAME"
         ;;
   btrfs|xfs|reiser4)
         # FIXME: bad blocks check option missing?
         FORMAT_APPEND="$FORMAT_APPEND -f"
         [ "$INSTALL_HOSTNAME" ] && \
            FORMAT_APPEND="$FORMAT_APPEND -L $INSTALL_HOSTNAME"
         ;;
   reiserfs)
         # FIXME: bad blocks check option missing?
         FORMAT_APPEND="$FORMAT_APPEND -ff"
         [ "$INSTALL_HOSTNAME" ] && \
            FORMAT_APPEND="$FORMAT_APPEND -l $INSTALL_HOSTNAME"
         ;;
   *) [ -e "$FORMAT_CMD" ] || {
         echo $"Error: $INSTALL_FSTYPE is not a supported filesystem type; aborting." >&2
         abort
      }
      ;;   
   "") echo $"Error: filesystem type non specified; aborting." >&2
       abort
       ;;
esac

if [ "$INSTALL_HOME_DEVICE" ]; then
   FORMAT_HOME_CMD="/sbin/mkfs.$INSTALL_HOME_FSTYPE"
   case "$INSTALL_HOME_FSTYPE" in
      ext2|ext3|ext4|jfs|nilfs2)
               [ "$INSTALL_HOME_BADBLOCKS" = "1" ] && FORMAT_HOME_APPEND="-c"
               FORMAT_HOME_APPEND="$FORMAT_HOME_APPEND -L home"
               ;;
      btrfs|xfs|reiser4)
               # FIXME: bad blocks check option missing?
               FORMAT_HOME_APPEND="$FORMAT_HOME_APPEND -f -L home"
         ;;
      reiserfs)
               # FIXME: bad blocks check option missing?
               FORMAT_HOME_APPEND="$FORMAT_HOME_APPEND -ff -l home"
               ;;
      *) [ -e "$FORMAT_HOME_CMD" ] || {
            echo $"Error: $INSTALL_HOME_FSTYPE is not a supported filesystem type; aborting." >&2
            abort
         }
         ;;
      "") echo $"Error: filesystem type non specified; aborting." >&2
          abort
          ;;
   esac
fi

if [ "$INSTALL_BOOT_DEVICE" ]; then
   FORMAT_BOOT_CMD="/sbin/mkfs.$INSTALL_BOOT_FSTYPE"
   case "$INSTALL_BOOT_FSTYPE" in
      ext2|ext3|ext4|jfs|nilfs2)
               [ "$INSTALL_BOOT_BADBLOCKS" = "1" ] && FORMAT_BOOT_APPEND="-c"
               FORMAT_BOOT_APPEND="$FORMAT_BOOT_APPEND -L boot"
               ;;
      btrfs|xfs|reiser4)
               # FIXME: bad blocks check option missing?
               FORMAT_BOOT_APPEND="$FORMAT_BOOT_APPEND -f -L boot"
               ;;
      reiserfs)
               # FIXME: bad blocks check option missing?
               FORMAT_BOOT_APPEND="$FORMAT_BOOT_APPEND -ff -l home"
               ;;
      *) FORMAT_BOOT_CMD="/sbin/mkfs.$INSTALL_BOOT_FSTYPE"
         [ -e "$FORMAT_BOOT_CMD" ] || {
            echo $"Error: $INSTALL_BOOT_FSTYPE is not a supported filesystem type; aborting." >&2
            abort
         }
         ;;
      "") echo $"Error: filesystem type non specified; aborting." >&2
          abort
          ;;
   esac
fi

[ $INSTALL_DEVICE ] || {
   echo $"Error: installation device is not defined in $INPUT_FILE; aborting." >&2
   abort
}

#INSTALL_DISK_DEVICE=${INSTALL_DEVICE:0:8}

[ -e $INSTALL_DEVICE ] || {
   echo $"Error: missing install device $INSTALL_DEVICE; aborting." >&2
   abort
}

echo $"openmamba installation started"

tempfile=`tempfile 2>/dev/null`
mapfile=`tempfile 2>/dev/null`

   # generate current volumes sorted list with driver information
   devices=`udisks --enumerate-device-files | grep -v /dev/disk`
   for s in $storages; do
      native_path=`get_udisks_key $device native-path`
      [ -e $native_path/partition ] && continue
      is_system_internal=`get_udisks_key $device "system internal"`
      #linux_driver=`find_driver_for_udi $s`
      [ "${device:5:2}" = "sd" -o "${device:5:2}" = "hd" ] && \
         echo "${device:0:8}" >> $tempfile
      if [ "$INSTALL_BOOTLOADER_DEVICE" = "$device" ]; then
         INSTALL_BOOTLOADER_DEVICE_INTERNAL=$is_system_internal
      fi
   done
   # sort
   cat $tempfile|sort > $mapfile
   rm -f $tempfile

   echo $"Available disks information"
   echo  "==========================="
   cat $mapfile
   echo

   LETTERS=(a b c d e f g h i j k l m n o p q r s t u v w x y z)

   GRUB_DRIVE_NUMBER=0
   while read line; do
      set -- $line
      [ "$1" = "${INSTALL_DEVICE:0:8}" ] && REMAPPED_INSTALL_DEVICE="${INSTALL_DEVICE:0:7}${LETTERS[CURR_DRIVE_LETTER]}${INSTALL_DEVICE:8:2}"
      [ "$1" = "${INSTALL_HOME_DEVICE:0:8}" ] && REMAPPED_INSTALL_HOME_DEVICE="${INSTALL_HOME_DEVICE:0:7}${LETTERS[CURR_DRIVE_LETTER]}${INSTALL_HOME_DEVICE:8:2}"
      [ "$1" = "${INSTALL_BOOT_DEVICE:0:8}" ] && REMAPPED_INSTALL_BOOT_DEVICE="${INSTALL_BOOT_DEVICE:0:7}${LETTERS[CURR_DRIVE_LETTER]}${INSTALL_BOOT_DEVICE:8:2}"
      [ "$2" = "$INSTALL_DRIVER" ] && {
         DEV_NAME_ORIG=(${DEV_NAME_ORIG[*]} $1)
         DEV_NAME_DEST=(${DEV_NAME_DEST[*]} ${1:0:7}${LETTERS[CURR_DRIVE_LETTER]})
         GRUB_NAME_DEST=(${GRUB_NAME_DEST[*]} hd${GRUB_DRIVE_NUMBER})
         let CURR_DRIVE_LETTER+=1
         let GRUB_DRIVE_NUMBER+=1
         PREV_DEV_NAME=$1
      }
   done < $mapfile
   [ "$REMAPPED_INSTALL_DEVICE" ] || REMAPPED_INSTALL_DEVICE=$INSTALL_DEVICE
   [ "$REMAPPED_INSTALL_HOME_DEVICE" ] || REMAPPED_INSTALL_HOME_DEVICE=$INSTALL_HOME_DEVICE
   [ "$REMAPPED_INSTALL_BOOT_DEVICE" ] || REMAPPED_INSTALL_BOOT_DEVICE=$INSTALL_BOOT_DEVICE
   echo $"Install device $INSTALL_DEVICE will be known as $REMAPPED_INSTALL_DEVICE in the installed system"

   # other drivers follow the root disk device driver
   while read line; do
      set -- $line
      # differentiate letter count for e.g. hd* and sd* disks
      [ "${1:5:2}" != "${PREV_DEV_NAME:5:2}" ] && CURR_DRIVE_LETTER=0
      PREV_DEV_NAME=$1
      [ "$2" = "$INSTALL_DRIVER" ] && continue
      DEV_NAME_ORIG=(${DEV_NAME_ORIG[*]} $1)
      DEV_NAME_DEST=(${DEV_NAME_DEST[*]} ${1:0:7}${LETTERS[CURR_DRIVE_LETTER]})
      GRUB_NAME_DEST=(${GRUB_NAME_DEST[*]} hd${GRUB_DRIVE_NUMBER})
      let CURR_DRIVE_LETTER+=1
      let GRUB_DRIVE_NUMBER+=1
   done < $mapfile

   echo $"The following live versus installed system mappings have been calculated:"
   for i in `seq 1 ${#DEV_NAME_ORIG[*]}`; do
      echo "${DEV_NAME_ORIG[i-1]} -> ${DEV_NAME_DEST[i-1]} (${GRUB_NAME_DEST[i-1]})"
   done
   echo

if [ "$INSTALL_HOME_DEVICE" ]; then
   mount | grep "$INSTALL_HOME_DEVICE " && {
      echo $"Info: device $INSTALL_HOME_DEVICE already mounted; unmounting."
      umount $INSTALL_HOME_DEVICE || {
         echo $"Error: could not unmount $INSTALL_HOME_DEVICE; aborting." >&2
         abort
      }
   }
fi

if [ "$INSTALL_BOOT_DEVICE" ]; then
   mount | grep "$INSTALL_BOOT_DEVICE " && {
      echo $"Info: device $INSTALL_BOOT_DEVICE already mounted; unmounting."
      umount $INSTALL_BOOT_DEVICE || {
         echo $"Error: could not unmount $INSTALL_BOOT_DEVICE; aborting." >&2
         abort
      }
   }
fi

mount | grep "$INSTALL_DEVICE " && {
   echo $"Info: device $INSTALL_DEVICE already mounted; unmounting."
   [ -e $MOUNTPOINT/dev ] && umount $MOUNTPOINT/dev
   [ -e $MOUNTPOINT/proc ] && umount $MOUNTPOINT/proc
   mount | grep " $MOUNTPOINT/home " && umount $MOUNTPOINT/home
   umount $INSTALL_DEVICE || {
      echo $"Error: could not unmount $INSTALL_DEVICE; aborting." >&2
      abort
   }
}

mount | grep "on $MOUNTPOINT/dev" && {
   echo $"Info: device already mounted on $MOUNTPOINT/dev; unmounting." >&2
   umount $MOUNTPOINT/dev || {
      echo $"Error: could not unmount $MOUNTPOINT/dev; aborting." >&2
      abort
   }
}

mount | grep "on $MOUNTPOINT" && {
   echo $"Info: device already mounted on $MOUNTPOINT; unmounting." >&2
   umount $MOUNTPOINT || {
      echo $"Error: could not unmount $MOUNTPOINT; aborting." >&2
      abort
   }
} || {
   mkdir -p $MOUNTPOINT || {
      echo $"Error: could not create mount point $MOUNTPOINT; aborting." >&2
      abort
   }
}

[ -x $FORMAT_CMD ] || {
   echo $"Error: missing initialization tool $FORMAT_CMD; aborting." >&2
   abort
}

dcop_write $"Formatting device $INSTALL_DEVICE..." 10
echo $"Formatting device $INSTALL_DEVICE"

$FORMAT_CMD $INSTALL_DEVICE $FORMAT_APPEND

[ $? -ne 0 ] && {
   echo $"Error: could not format device $INSTALL_DEVICE; aborting." >&2
   abort
}

dcop_write $"Mounting $INSTALL_FSTYPE filesystem..." 10
echo $"Mounting $INSTALL_FSTYPE filesystem"
mount $INSTALL_DEVICE $MOUNTPOINT || {
   echo $"Error: could not mount filesystem; aborting." >&2
   abort
}

if [ "$INSTALL_HOME_DEVICE" ]; then
   if [ "$INSTALL_HOME_FORMAT" == "1" ]; then
      dcop_write $"Formatting device $INSTALL_HOME_DEVICE..." 10
      echo $"Formatting device $INSTALL_HOME_DEVICE"
      $FORMAT_HOME_CMD $INSTALL_HOME_DEVICE $FORMAT_HOME_APPEND
      [ $? -ne 0 ] && {
         echo $"Error: could not format device $INSTALL_HOME_DEVICE; aborting." >&2
         abort
      }
   fi

   dcop_write $"Mounting home filesystem..." 10
   echo $"Mounting home filesystem"
   mkdir -p $MOUNTPOINT/home
   mount $INSTALL_HOME_DEVICE $MOUNTPOINT/home || {
      echo $"Error: could not mount home filesystem; aborting." >&2
      abort
   }
fi

if [ "$INSTALL_BOOT_DEVICE" ]; then
   if [ "$INSTALL_BOOT_FORMAT" == "1" ]; then
      dcop_write $"Formatting device $INSTALL_BOOT_DEVICE..." 10
      echo $"Formatting device $INSTALL_BOOT_DEVICE"
      $FORMAT_BOOT_CMD $INSTALL_BOOT_DEVICE $FORMAT_BOOT_APPEND
      [ $? -ne 0 ] && {
         echo $"Error: could not format device $INSTALL_BOOT_DEVICE; aborting." >&2
         abort
      }
   fi

   dcop_write $"Mounting boot filesystem..." 10
   echo $"Mounting boot filesystem"
   mkdir -p $MOUNTPOINT/boot
   mount $INSTALL_BOOT_DEVICE $MOUNTPOINT/boot || {
      echo $"Error: could not mount boot filesystem; aborting." >&2
      abort
   }
fi

echo -n $"Copying files..."
dcop_write $"Copying files..." 15
prog=15

for d in etc bin boot lib lib64 opt sbin srv usr var root; do
   [ "$INSTALL_CLONE_MODE" != "1" ] && canonical_dir=/squashfs/$d || canonical_dir=`readlink -f /$d`
   [ -e $canonical_dir ] || continue
   DIR_SPACE=`du -sxm $canonical_dir | awk '{ print $1; }'`
   TOTAL_SPACE=`expr $TOTAL_SPACE + $DIR_SPACE`
done

for d in etc bin boot lib lib64 opt sbin srv usr var root; do
   [ "$INSTALL_CLONE_MODE" != "1" ] && canonical_dir=/squashfs/$d || canonical_dir=`readlink -f /$d`
   [ -e $canonical_dir ] || continue
   cp -av $canonical_dir $MOUNTPOINT/ | cp_parser
   [ ${PIPESTATUS[0]} -eq 0 ] || {
      echo $"Error: cannot copy $canonical_dir to $MOUNTPOINT; aborting." >&2
      abort
   }
done

MOUNT_SPACE=`df -m $MOUNTPOINT | tail -1 | awk '{ print $3; }'`
let TIME_ELAPSED=\(`date +%s`-$date_start\)/60
dcop_write_details $"\

${MOUNT_SPACE} Mbytes copied
Installation time: $TIME_ELAPSED minutes"

CONFIGURED_FILES="/etc/vconsole.conf /etc/locale.conf \
                  /etc/X11/xorg.conf /etc/X11/xorg.conf.d/*.conf \
                  /etc/localtime /etc/timezone"
for f in $CONFIGURED_FILES; do
   [ -r $f ] && cp -P $f ${MOUNTPOINT}/${f}
done
sed -i "/^LANG=.*/d;1s/^/LANG=$LANG\n/" /etc/locale.conf
echo

mkdir -p $MOUNTPOINT/{dev,home,media,mnt,proc,run,sys,tmp}
chmod 1777 $MOUNTPOINT/tmp
chmod 0750 $MOUNTPOINT/root

dcop_write $"Binding system dirs to new mountpoint..." 73
echo $"Binding /dev, /proc, /run and /sys to new mountpoint"
mount -o bind /dev $MOUNTPOINT/dev || {
   echo $"Error: could not bind /dev to new mountpoint; aborting." >&2
   abort
}
mount -o bind /proc $MOUNTPOINT/proc || {
   echo $"Error: could not bind /proc to new mountpoint; aborting." >&2
   abort
}
mount -o bind /run $MOUNTPOINT/run || {
   echo $"Error: could not bind /run to new mountpoint; aborting." >&2
   abort
}
mount -o bind /sys $MOUNTPOINT/sys || {
   echo $"Error: could not bind /sys to new mountpoint; aborting." >&2
   abort
}

# set root partition in fstab
# must be done before mkinitrd
#currroot=`readlink /dev/root`
eval `blkid $INSTALL_DEVICE -o udev`
[ "$ID_FS_UUID" ] && INSTALL_DEVICE_UUID=$ID_FS_UUID || unset INSTALL_DEVICE_UUID
grep -v ^/dev $MOUNTPOINT/etc/fstab | grep -v ^UUID= > $MOUNTPOINT/etc/fstab.new
if [ "$INSTALL_DEVICE_UUID" ]; then
   echo "UUID=$INSTALL_DEVICE_UUID	/		$INSTALL_FSTYPE	defaults,noatime	1	1" >> $MOUNTPOINT/etc/fstab.new
else
   echo "$REMAPPED_INSTALL_DEVICE	/		$INSTALL_FSTYPE	defaults,noatime	1	1" >> $MOUNTPOINT/etc/fstab.new
fi

if [ "$INSTALL_HOME_DEVICE" ]; then
   eval `blkid $INSTALL_HOME_DEVICE -o udev`
   [ "$ID_FS_UUID" ] && INSTALL_HOME_DEVICE_UUID=$ID_FS_UUID || unset INSTALL_HOME_DEVICE_UUID
   [ "$INSTALL_HOME_FORMAT" == "1" ] || INSTALL_HOME_FSTYPE="auto"
   if [ "$INSTALL_HOME_DEVICE_UUID" ]; then
      echo "UUID=$INSTALL_HOME_DEVICE_UUID	/home		$INSTALL_HOME_FSTYPE	defaults,noatime	2	2" >> $MOUNTPOINT/etc/fstab.new
   else
      echo "$REMAPPED_INSTALL_HOME_DEVICE	/home		$INSTALL_HOME_FSTYPE	defaults,noatime	2	2" >> $MOUNTPOINT/etc/fstab.new
   fi
fi

if [ "$INSTALL_BOOT_DEVICE" ]; then
   eval `blkid $INSTALL_BOOT_DEVICE -o udev`
   [ "$ID_FS_UUID" ] && INSTALL_BOOT_DEVICE_UUID=$ID_FS_UUID || unset INSTALL_BOOT_DEVICE_UUID
   [ "$INSTALL_BOOT_FORMAT" == "1" ] || INSTALL_BOOT_FSTYPE="auto"
   if [ "$INSTALL_BOOT_DEVICE_UUID" ]; then
      echo "UUID=$INSTALL_BOOT_DEVICE_UUID	/boot		$INSTALL_BOOT_FSTYPE	defaults	1	1" >> $MOUNTPOINT/etc/fstab.new
   else
      echo "$REMAPPED_INSTALL_BOOT_DEVICE	/boot		$INSTALL_BOOT_FSTYPE	defaults	1	1" >> $MOUNTPOINT/etc/fstab.new
   fi
fi

mv $MOUNTPOINT/etc/fstab.new $MOUNTPOINT/etc/fstab

if [ "$INSTALL_DRIVER" ]; then
   /sbin/lsmod | grep "^$INSTALL_DRIVER " && {
      echo "alias scsi_hostadapter $INSTALL_DRIVER" >> $MOUNTPOINT/etc/modprobe.d/initramfs.conf
      MKINITRAMFS_ADD="--with=$INSTALL_DRIVER"
   } || MKINITRAMFS_ADD=""
fi

# remove not-running installed kernel
RUNNING_KERNEL=`uname -r | sed "s|[0-9.]*||"`
for i in `ls $MOUNTPOINT/boot/vmlinuz-*`; do
   INSTALLED_KERNEL=`echo ${i} | sed "s|.*/vmlinuz-[0-9.]*||"`
   [ "$INSTALLED_KERNEL" = "$RUNNING_KERNEL" ] && continue
   dcop_write $"Removing ""$INSTALLED_KERNEL kernel..." 74
   echo $"Removing ""$INSTALLED_KERNEL kernel..."
   chroot $MOUNTPOINT smart remove -y kernel-$INSTALLED_KERNEL
done

dcop_write $"Creating initramfs..." 75
echo $"Creating initramfs"
chroot $MOUNTPOINT \
   mkinitrd /boot/initramfs-$KERNEL_RELEASE.img \
   $KERNEL_RELEASE -f $MKINITRAMFS_ADD || \
   {
      echo $"Error: initramfs creation failed; aborting." >&2
      abort
   }
#   --fstab=/etc/fstab ||

if [ "$INSTALL_BOOTLOADER_DEVICE" -o "$INSTALL_BOOTLOADER_DEVICE_EFI" ]; then
   CMDLINE=`cat /proc/cmdline`
   echo $"Current kernel command line is '$CMDLINE'"
   NOMODESET_ADD=`echo $CMDLINE | grep "nomodeset" | sed "s|.*\(nomodeset\).*|\1|"`
   if [ "$NOMODESET_ADD" ]; then
      GRUB_CMDLINE_ADD="$NOMODESET_ADD"
      echo $"Adding '$NOMODESET_ADD' to kernel command line"
   fi
   if [ "$GRUB_CMDLINE_ADD" ]; then
      sed -i "s|GRUB_CMDLINE_LINUX=\"\(.*\)\"|GRUB_CMDLINE_LINUX=\"\1 $GRUB_CMDLINE_ADD\"|" $MOUNTPOINT/etc/default/grub
   fi
fi

if [ "$INSTALL_BOOTLOADER_DEVICE" ]; then
   #
   # MBR/BOOT SECTOR
   #
   dcop_write $"Installing bootloader on $INSTALL_BOOTLOADER_DEVICE..." 85
   INSTALL_BOOTLOADER_DEVICE_BYID=$INSTALL_BOOTLOADER_DEVICE
   for f in `ls /dev/disk/by-id/*`; do 
      if [ "$INSTALL_BOOTLOADER_DEVICE" = "`readlink -f $f`" ]; then
         INSTALL_BOOTLOADER_DEVICE_BYID=$f
	 break
      fi
   done

   echo "GRUB_INSTALL_DEVICE=$INSTALL_BOOTLOADER_DEVICE_BYID" >> $MOUNTPOINT/etc/sysconfig/grub
   if [ "$INSTALL_BOOTLOADER_DEVICE_INTERNAL" != "1" ]; then
      echo $"Installing grub bootloader on removable device $INSTALL_BOOTLOADER_DEVICE"
      echo "(hd0)	$INSTALL_BOOTLOADER_DEVICE" > $MOUNTPOINT/boot/grub/device.map
      grub-install $INSTALL_BOOTLOADER_DEVICE --root-directory=$MOUNTPOINT --target=i386-pc --recheck --no-floppy --force
   else
      echo $"Installing grub bootloader on $INSTALL_BOOTLOADER_DEVICE"
      > $MOUNTPOINT/boot/grub/device.map
      for i in `seq 1 ${#DEV_NAME_ORIG[*]}`; do
         echo "(${GRUB_NAME_DEST[i-1]})	${DEV_NAME_ORIG[i-1]}" >> $MOUNTPOINT/boot/grub/device.map
      done
      grub-install $INSTALL_BOOTLOADER_DEVICE --root-directory=$MOUNTPOINT --target=i386-pc --recheck --no-floppy --force
   fi

   if [ "${INSTALL_BOOTLOADER_DEVICE:8:2}" = "" ]; then
      # MBR install: set /boot partition as bootable as required by grub2
      # CHECK: this does not work when install device is an extended partition, but in this case
      #        a 1-4 boot partition is already set by another system
      if [ ${INSTALL_DEVICE:8:2} -le 4 ]; then
         parted ${INSTALL_DEVICE:0:8} \
                set ${INSTALL_DEVICE:8:2} boot on || \
            echo $"Warning: there was an error setting /boot partition as bootable." >&2
      fi
   elif [ "$INSTALL_BOOTLOADER_DEVICE_SETBOOTABLE" = "1" ]; then
      parted ${INSTALL_BOOTLOADER_DEVICE:0:8} \
             set ${INSTALL_BOOTLOADER_DEVICE:8:2} boot on || \
         echo $"Warning: there was an error setting bootloader partition as bootable." >&2
   fi

elif [ "$INSTALL_BOOTLOADER_DEVICE_EFI" ]; then
   #
   # EFI
   #
   dcop_write $"Installing bootloader on ""$INSTALL_BOOTLOADER_DEVICE_EFI (EFI)..." 85
   INSTALL_BOOTLOADER_DEVICE_EFI_BYID=$INSTALL_BOOTLOADER_DEVICE_EFI
   for f in `ls /dev/disk/by-id/*`; do 
      if [ "$INSTALL_BOOTLOADER_DEVICE_EFI" = "`readlink -f $f`" ]; then
         INSTALL_BOOTLOADER_DEVICE_EFI_BYID=$f
	 break
      fi
   done
   echo "GRUB_INSTALL_DEVICE_EFI=$INSTALL_BOOTLOADER_DEVICE_EFI_BYID" >> $MOUNTPOINT/etc/sysconfig/grub

   echo $"Installing grub bootloader on ""$INSTALL_BOOTLOADER_DEVICE_EFI (EFI)"
   [ -e $MOUNTPOINT/boot/efi ] || mkdir -p $MOUNTPOINT/boot/efi
   mount $INSTALL_BOOTLOADER_DEVICE_EFI $MOUNTPOINT/boot/efi
   grub-install --root-directory=$MOUNTPOINT --efi-directory=$MOUNTPOINT/boot/efi --target=x86_64-efi \
      --bootloader-id="openmamba Boot Manager" --recheck --no-floppy
   umount $MOUNTPOINT/boot/efi
fi

if [ "$INSTALL_BOOTLOADER_DEVICE" -o "$INSTALL_BOOTLOADER_DEVICE_EFI" ]; then
   # mount all disks and dbus for Grub other linuxes detection to work (30_other_linux)
   mount -o bind /mnt $MOUNTPOINT/mnt
   mount -o bind /var/run/dbus $MOUNTPOINT/var/run/dbus
   for d in /mnt/*; do
      [ -d $d -a "$d" != "${MOUNTPOINT}" ] && mount -o bind $d ${MOUNTPOINT}${d}
   done
   chroot $MOUNTPOINT update-grub
   for d in /mnt/*; do
      [ -d $d -a "$d" != "${MOUNTPOINT}" ] && umount ${MOUNTPOINT}${d}
   done
   umount $MOUNTPOINT/var/run/dbus
   umount $MOUNTPOINT/mnt

#   for o in `seq 1 ${#DEV_NAME_ORIG[*]}`; do
#      [ ${DEV_NAME_ORIG[o-1]} != ${DEV_NAME_DEST[o-1]} ] & continue
#      echo $"Renaming device "${DEV_NAME_ORIG[o-1]}$" to "${DEV_NAME_DEST[o-1]}
#      sed -i "s|/dev/${DEV_NAME_ORIG[o-1]:5:3}|/dev/${DEV_NAME_DEST[o-1]:5:3}|" \
#         $MOUNTPOINT/boot/grub/devices.map \
#         $MOUNTPOINT/etc/sysconfig/grub
#      CURR_ROOT_LETTER=0
#      sed -i "s|root=/dev/${INSTALL_DEVICE:5:5}|root=/dev/${DEV_NAME_DEST[o-1]:5:3}|" \
#         $MOUNTPOINT/boot/grub/grub.cfg
#   done

   [ "${INSTALL_DEVICE}" != "${REMAPPED_INSTALL_DEVICE}" ] && \
      sed -i "s|root=/dev/${INSTALL_DEVICE:5:5}|root=/dev/${REMAPPED_INSTALL_DEVICE:5:5}|" \
             $MOUNTPOINT/boot/grub/grub.cfg
else
   echo $"Info: skipping bootloader installation"
fi

## MBR backup
#echo "Installing MBR record on $INSTALL_BOOTLOADER_DEVICE"
#dd if=$INSTALL_BOOTLOADER_DEVICE of=$MOUNTPOINT/boot/mbr-backup bs=1 count=512
#install-mbr $INSTALL_BOOTLOADER_DEVICE

dcop_write $"Configuring system startup files..." 85
echo $"Configuring system startup files"

chroot $MOUNTPOINT systemctl enable mambawelcome.target
chroot $MOUNTPOINT systemctl enable mambawelcome.service

if [ -d /cdrom/openmamba/base ]; then

   # openmamba install_packages script section
   dcop_write $"Installing packages..." 90
   echo $"Installing packages"

   PKGGROUPS_DB=$MOUNTPOINT/usr/share/openmamba/pkggroups.db
   . $PKGGROUPS_DB

#   trap "umount $MOUNTPOINT/var/lib/apt/cdrom 2>/dev/null" 0 1 2 5 15

   mount -o bind /cdrom $MOUNTPOINT/var/lib/apt/cdrom
   chroot $MOUNTPOINT apt-cdrom add -m -d /var/lib/apt/cdrom

   # disable the service command
   mv $MOUNTPOINT/sbin/service $MOUNTPOINT/sbin/service.preinst
   ln -sf /bin/true $MOUNTPOINT/sbin/service

   echo $"Launching chrooted apt-get..."
   chroot $MOUNTPOINT apt-get -y install $ALL_PKGS \
                    -o Acquire::CDROM::mount=/var/lib/apt/cdrom \
                    -o Acquire::CDROM::Copy=false \
                    -o Acquire::CDROM::Copy-All=false | apt_parser

   # re-enable the service command
   rm -f $MOUNTPOINT/sbin/service
   mv $MOUNTPOINT/sbin/service.preinst $MOUNTPOINT/sbin/service

   umount $MOUNTPOINT/var/lib/apt/cdrom 2>/dev/null

   # installation completed, remove cdrom volume from apt sources list
   sed -i "s|^\(rpm cdrom:.*\)|#\1|" $MOUNTPOINT/etc/apt/sources.list
fi

dcop_write $"Removing/cleaning unnecessary files and packages..." 95
echo $"Removing/cleaning unnecessary files and packages"
# remove unnecessary files and packages for fixed disk install
rpm -r $MOUNTPOINT -e livecd-tools
[ "$INSTALL_STABLE_UPDATES" = "1" ] && rpm -r $MOUNTPOINT -e openmamba-devel 2>/dev/null

# remove ssh live medium keys
rm -f $MOUNTPOINT/etc/ssh/ssh_host*_key $MOUNTPOINT/etc/ssh/ssh_host*_key.pub

# fix paths in root home directory and perform some cleanup
for d in $MOUNTPOINT/root/.kde $MOUNTPOINT/root/.kde4 $MOUNTPOINT/root/.qt; do
   [ -e $d ] && find $d -type f -exec sed -i "s|/squashfs||" {} \;
done
rm -f $MOUNTPOINT/root/.DCOPserver* $MOUNTPOINT/root/.ICEauthority

[ "$INSTALL_USER" -a -e $MOUNTPOINT/$MAMBAWELCOME_SCRIPT ] && {
   dcop_write $"Configuring users and host..." 87
   echo $"Configuring users and host"
   chroot $MOUNTPOINT sh $MAMBAWELCOME_SCRIPT -y
}

# FIXME: test gconftool-2 --shutdown
[ "`pidof gconfd-2`" ] && killall -9 gconfd-2

umount $MOUNTPOINT/dev
umount $MOUNTPOINT/proc
umount $MOUNTPOINT/run
umount $MOUNTPOINT/sys
[ "$INSTALL_HOME_DEVICE" ] && umount $MOUNTPOINT/home
[ "$INSTALL_BOOT_DEVICE" ] && umount $MOUNTPOINT/boot

# NOTE: move INPUT_FILE instead of copying to prevent mambawelcome from starting from
#       bootusb after an installation
cp $INPUT_FILE $MOUNTPOINT/root/
mv $INPUT_FILE ~/.installer.conf.$INSTALL_DATE

REDIRECTED_FILE=`readlink /proc/$$/fd/1`
[ -f $REDIRECTED_FILE ] && {
   cp -a $REDIRECTED_FILE ${MOUNTPOINT}${LOG_FILE}
   [ -f ${MOUNTPOINT}${REDIRECTED_FILE} ] && rm -f ${MOUNTPOINT}${REDIRECTED_FILE}
}

## now remove sensible information from input file
#sed -i "/^INSTALL_ROOT_PASSWORD=.*/d" $INPUT_FILE
#sed -i "/^INSTALL_USER_PASSWORD=.*/d" $INPUT_FILE

umount $MOUNTPOINT
finished

echo $"Installation done. You can now reboot to start the freshly installed system."

rm -f $PIDFILE
exit 0