summaryrefslogblamecommitdiffstats
path: root/mltk
blob: ac79798e99237719a29ca1aa936c2ffd1f325e53 (plain) (tree)
1
2
3
           
                                                                               
  













                                                                               
 

                               
             
 






                                  
                                  
                                     
                                   
                                  
                                 
 
 
           







                                                                                    

 

                                                                            

                                                                                                           
                  


                                                                                                                                  
                  


                                                     

                                                                              
                                                                           
                  
                                                                                            


                                                                 






                                                                                                                                               
                  
                                                    
                                                            
 
 

                                            
                                                                                 



                      



                                                           

                                    

                                                                                                           
 
 
               

                        

                                      

                                     
                          

                                     

                          
                                                   




                                   










                                                                                     
                                  



                                  












                                                              

                                                                                           
                                                                                                              
                                                                                                                               

                                                                                       



                                                     
                                                                                

                                                                                                              

                                                










                                                                                 

                                                                                                                                                                         
                                                                                    
            
        
                            
                                                                                                                           


       

                                                                    

                                                                             
                                                                              
          

                                                                    
                                       
                                                                         

                                                            
          

 








                      
 

              
 
   
#!/bin/bash
# -----------------------------------------------------------------------------
# 
# Copyright (c) 2011 - OpenSLX GmbH
#
# This program is free software distributed under the GPL version 2.
# See http://openslx.org/COPYING
#
# If you have any feedback please consult http://openslx.org/feedback and
# send your suggestions, praise, or complaints to feedback@openslx.org
#
# General information about OpenSLX can be found at http://openslx.org/
# -----------------------------------------------------------------------------
#				
#				Mini-Linux Toolkit
#				
# -----------------------------------------------------------------------------

SELF="$(readlink -f $0)"
ROOT_DIR="$(dirname "${SELF}")"
MLTK_PID="$$"

qnd_exit() {
	unset_quiet
	kill "$MLTK_PID"
	[ $# -ge 1 ] && kill "$1"
}

. "${ROOT_DIR}/helper/logging.inc"
. "${ROOT_DIR}/helper/useradd.inc"
. "${ROOT_DIR}/helper/downloader.inc"
. "${ROOT_DIR}/helper/fileutil.inc"
. "${ROOT_DIR}/helper/binutil.inc"
. "${ROOT_DIR}/helper/system.inc"


banner () {
	echo -e "\033[38;5;202m\t           __   __   __    "
	echo -e "\033[38;5;202m\t.--------.|  | |  |_|  |--."
	echo -e "\033[38;5;208m\t|        ||  |_|   _|    < "
	echo -e "\033[38;5;214m\t|__|__|__||____|____|__|__|"
	echo -e "\033[38;5;214m\t "
	echo -e "\033[38;5;220m\t                     ** OpenSLX Project // 2013 **"
	echo -e "\033[38;5;226m\t                        http://lab.openslx.org/"
	echo -e "\033[0m"
}

print_usage() {
	echo "Toolkit for creating preboot mini-linux for OpenSLX NG (mltk)"
	echo "Usage: $(basename ${SELF}) remote target_dir [-d] [-c [module]*] [-b [module]*] [-p profile]"
	echo "       $(basename ${SELF}) server [-d] [-c] [-b] [-n]"
	echo -e ""
	echo -e "  Mode:"
	echo -e "     server \t server mode: packs stage3.1 and stage3.2 as initramfs/squashfs."
	echo -e "     remote \t remote mode: builds a minimal systemd-based rootfs based on the activated modules in <target_dir>"
	echo -e ""
	echo -e "  Mode options:"
	echo -e "     -b            build module(s)."
	echo -e "     -c            clean module(s)."
	echo -e "     -d            activates debug output for current target"
	echo -e "     -p profile    build all modules from given profile"
	echo -e "     -n 	    don't generate squashfs from stage 3.2"
	echo -e ""
	echo -e "  In mode 'remote', you can pass names of specific modules to clean/build."
	echo -e "  Otherwise, all modules will be built/cleaned."
	echo -e ""
	echo -e "  Examples:"
	echo -e "     remote stage3.1 -b   (build all the modules activated (linked) in remote/stage3.1 in remote/build/stage3.1"
	echo -e "     remote stage3.1 -b plymouth    (build plymouth in remote/build/stage3.1)"
	echo -e "     remote stage3.2 -c -b base policykit sshd   (clean all modules, build base, policykit and sshd in remote/build/stage3.2)"
	echo -e "     remote stage3.2 -c -b   (clean all modules, build all modules in remote/build/stage3.2)"
	echo -e "     remote stage3.2 -c base sshd -b sshd ldm -d   (clean base and sshd, build sshd and ldm, be verbose)"
	echo -e "     remote stage3.2 -c -p default   (clean all modules, build modules listed in profile default)"
	echo -e "     server -c -b   (clean and build the initramfs archives under /server/build)"
	echo -e ""
	echo -e "  Existing modules for remote are:"
	echo -e "    $(echo $(ls ${ROOT_DIR}/remote/tools))"
}

initial_checks() {
	if [ "x$(whoami)" != "xroot" ]; then
		perror "ERROR: You need to have root rights to install packages."
		exit 1
	else
		banner
	fi
	# print system information
	pinfo "Arch triplet of this machine: $ARCH_TRIPLET"
	pinfo "Kernel version: $KERNEL_VERSION"
	pinfo "System's packet manager is $PACKET_MANAGER"
	
	# setup_tools and build_core
	SETUP_TARGET="${ROOT_DIR}/remote/setup_target"
	[ ! -e "${SETUP_TARGET}" ] && perror "Missing script 'remote/setup_target', re-clone git. Exiting."
}

read_params() {
	local MODE=""
	local SUBMODE=""
	# select target: core or tools
	case "$1" in
		server)
			MODE="SERVER"
			;;
		remote)
			MODE="REMOTE"
			;;
		*)
			pwarning "Unknown mode: $1"
			print_usage
			exit 1
			;;
	esac
	shift

	# set the global TARGET if in remote mode.
	if [[ $MODE == REMOTE ]]; then
		if [[ $1 != "-"* ]]; then
			TARGET=$1
			shift
		else
			perror "Remote mode requires a target directory. None given."
		fi
	fi

	# handle rest of arguments
	while [ "$#" -gt "0" ]; do
		local PARAM="$1"
		shift

		# options to current target
		if [[ "$PARAM" == "-"* ]]; then
			case "$PARAM" in
				-c)
					SUBMODE="CLEAN"
					;;
				-b)
					SUBMODE="BUILD"
					;;
				-d)
					eval ${MODE}_DEBUG="1"
					continue
					;;
				-p)
					[ "$#" -lt "1" ] && perror "Missing argument to -p"
					[ "$MODE" != "REMOTE" ] && perror "-p can only be used in remote mode"
					. "remote/profiles/${1}.profile" || perror "Profile '$1' not found in remote/profiles/"
					REMOTE_BUILD="1"
					REMOTE_LIST_BUILD="$REMOTE_LIST_BUILD $MODULES"
					unset MODULES
					shift
					continue
					;;
				-n)						
					[ "$MODE" != "SERVER" ] && perror "-n can only be used in server mode"
					SERVER_NO_SQUASHFS="1"
					continue
					;;
				*)
					pwarning "Unknown flag to target: $PARAM"
					print_usage
					exit 1
					;;
			esac
			eval ${MODE}_${SUBMODE}="1"
			continue
		fi

		# module name
		[[ $MODE != REMOTE ]] && pwarning "You cannot specify module names in server mode." && print_usage && exit 1
		[[ $SUBMODE != CLEAN && $SUBMODE != BUILD ]] && pwarning "Module name given in remote mode, but no action specified (eg. build)" && print_usage && exit 1
		eval "${MODE}_LIST_${SUBMODE}=\"\$${MODE}_LIST_${SUBMODE} \$PARAM\""
	done
	
	# exit if no command
	[[ $SERVER_CLEAN == 0 && $SERVER_BUILD == 0 && $REMOTE_CLEAN == 0 && $REMOTE_BUILD == 0 ]] && print_usage && exit 1
}

run() {
	if [[ $REMOTE_CLEAN == 1 || $REMOTE_BUILD == 1 ]]; then
		[[ $REMOTE_DEBUG == 1 ]] && unset_quiet || set_quiet
		. "${SETUP_TARGET}" || perror "Cannot source ${SETUP_TARGET}"
	        [[ $REMOTE_CLEAN == 1 ]] && clean_modules $REMOTE_LIST_CLEAN
	        [[ $REMOTE_BUILD == 1 ]] && generate_target $REMOTE_LIST_BUILD
	fi
	if [[ $SERVER_CLEAN == 1 || $SERVER_BUILD == 1 ]]; then
		[[ $SERVER_DEBUG == 1 ]] && unset_quiet || set_quiet
		local TOOL_STR="[CORE]"
		. "${SETUP_CORE}" || perror "Cannot source ${SETUP_CORE}"
	        [[ $SERVER_CLEAN == 1 ]] && clean_core
	        [[ $SERVER_BUILD == 1 ]] && generate_stage31
	fi
}

SERVER_DEBUG="0"
SERVER_BUILD="0"
SERVER_CLEAN="0"
SERVER_NO_SQUASHFS="0"
REMOTE_DEBUG="0"
REMOTE_CLEAN="0"
REMOTE_BUILD="0"
REMOTE_LIST_CLEAN=""
REMOTE_LIST_BUILD=""

initial_checks
read_params $@

run