#!/bin/bash
#================
# FILE          : linuxrc
#----------------
# PROJECT       : openSUSE KIWI Image System
# COPYRIGHT     : (c) 2006 SUSE LINUX Products GmbH. All rights reserved
#               :
# AUTHOR        : Marcus Schaefer <ms@suse.de>
#               :
# BELONGS TO    : Operating System images
#               :
# DESCRIPTION   : This file is changed to become the real
#               : linuxrc script which is used to prepare the
#               : operating system for the main image
#               :
#               :
# STATUS        : BETA
#----------------
#======================================
# Exports (General)
#--------------------------------------
export PATH="/sbin:/bin:/usr/sbin:/usr/bin"
export IFS_ORIG=$IFS
export input=/part.input
export ARCH=`arch`
export DEBUG=0

#======================================
# Exports (Booting)
#--------------------------------------
export INITRD_MODULES=""
export LOCAL_BOOT="no"
export KERNEL_LIST

#======================================
# Exports (Alias)
#--------------------------------------
export SYSALIAS="undefined"
export NAME=0

#======================================
# Exports (Status)
#--------------------------------------
export SYSTEM_INTEGRITY
export SYSTEM_MD5STATUS
export systemIntegrity="unknown"
export haveDisk=0
export partOK=0
export haveRamDisk=0

#======================================
# Exports (clicfs usage)
#--------------------------------------
export kiwi_hybrid=true
export kiwi_hybridpersistent=true

#======================================
# Exports kernel options
#--------------------------------------
export kernel_cmdline=($@)

#======================================
# Functions
#--------------------------------------
. /include
initialize

#======================================
# Start logging
#--------------------------------------
errorLogStart

#======================================
# Update library path
#--------------------------------------
ldconfig

#======================================
# 1) Mounting local file systems
#--------------------------------------
mountSystemFilesystems &>/dev/null

#======================================
# 2) Prepare module load support 
#--------------------------------------
touch /etc/modules.conf
touch /lib/modules/*/modules.dep
runHook init

#======================================
# 3) run udevd
#--------------------------------------
udevStart

#======================================
# 4) Include proc/cmdline information
#--------------------------------------
includeKernelParameters
if [ ! -z "$IMAGE" ];then
    # /.../
    # if the image information is already in place at this stage
    # it comes from the cmdline data which means we are not booting
    # from the network but want to boot the local system
    # ----
    LOCAL_BOOT="yes"
fi

#======================================
# 5) start boot shell
#--------------------------------------
startShell

#======================================
# 6) probe/load network module
#--------------------------------------
if [ $LOCAL_BOOT = "no" ];then
    loadNetworkCard
fi

#======================================
# 7) Setup network interface and DNS
#--------------------------------------
runHook prenetwork
if [ $LOCAL_BOOT = "no" ];then
    setupNetwork
fi
runHook postnetwork

#======================================
# 8) SALT minion phase
#--------------------------------------

. /saltboot.sh

# prepare Grub configuration
kernelList /mnt
setupBootLoaderGrub /mnt /mnt "" `findmnt -n -r -o SOURCE --target /mnt`

#======================================
# 24) send DHCP_RELEASE, reset cache
#--------------------------------------
runHook preNetworkRelease
if [ $LOCAL_BOOT = "no" ];then
    releaseNetwork
fi

#======================================
# 25) copy system dependant files
#--------------------------------------
#if [ $LOCAL_BOOT = "no" ];then
#    setupConfigFiles
#fi

#======================================
# 26) update system dependant files
#--------------------------------------
setupInittab /mnt

echo 256 > /proc/sys/kernel/real-root-dev
#======================================
# 27) umount system filesystems
#--------------------------------------
umountSystemFilesystems

#======================================
# 28) copy initrd files to image
#--------------------------------------
if [ $LOCAL_BOOT = "no" ] && [ $systemIntegrity = "clean" ];then
    if canWrite /mnt;then
        importBranding
        cp /preinit /mnt
        cp /include /mnt
    fi
fi

#======================================
# 29) kill boot shell
#--------------------------------------
killShell

#======================================
# 30) Activate new root
#--------------------------------------
runHook preactivate
activateImage

#======================================
# 31) Unmount initrd / system init
#--------------------------------------
bootImage
