#!/bin/bash # Copyright (c) 2012 - 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 feedback to feedback@openslx.org # # General information about OpenSLX can be found at http://openslx.org # # Server side script to generate stage3.1,2 initial ramfses for OpenSLX Linux # stateless clients ############################################################################# # first parameter is a hash pointing to the target directory # /srv/openslx/build # in the future the prefix should be set via slxsettings ... MODULE_DIR=${ROOT_DIR}/server STAGE31_STATIC_DIR=${ROOT_DIR}/data/stage3.1 STAGE31_DIR=${MODULE_DIR}/stage3.1 STAGE32_DIR=${ROOT_DIR}/remote/stage3.2 # initial checks initial_checks() { if [ ! -d ${STAGE32_DIR} ]; then echo "No stage3.2 directory found. Please run \"./mltk tools build\" first." exit 1 fi if [ ! -e ${STAGE32_DIR}/openslx/bin/busybox ]; then echo "Busybox not found, run 'setup_tools busybox' first." exit 1 fi # shouldn't squashfs-tools be automatically installed as we need them nevertheless!? # what if it is already on the machine? faster to check with which than the apt-get check? if [ -z $(which mksquashfs) ]; then echo "mksquashfs not found, please install squashfs-tools first." exit 1 fi #check for kernel modules, if not present copy from system if [ ! -d ${STAGE32_DIR}/lib/modules ]; then echo "Couldn't find kernel modules in stage3.2." echo "Copying modules for kernel $(uname -r)..." mkdir -p ${STAGE32_DIR}/lib/modules cp -r /lib/modules/$(uname -r) ${STAGE32_DIR}/lib/modules/ fi # TODO: check for aufs and squaskfs modules } generate_rootfs() { # produce stage3.1 [ ! -d ${STAGE31_DIR} ] && mkdir -p ${STAGE31_DIR} # create basic directory structure mkdir -p ${STAGE31_DIR}/{bin,dev,proc,run,lib/modules,etc,mnt,sys} # copy device files from running system cp -a /dev/{console,kmsg,mem,null,tty,tty0,tty1,tty9,urandom,zero} \ ${STAGE31_DIR}/dev # copy busybox, its libs and static data to stage3.1 cp -r ${STAGE32_DIR}/openslx/* ${STAGE31_STATIC_DIR}/* ${STAGE31_DIR} # fix for aufs & squashfs modules needed for stage 3.1 cp /lib/modules/$(uname -r)/kernel/fs/squashfs/squashfs.ko ${STAGE31_DIR}/lib/modules/ cp /lib/modules/$(uname -r)/kernel/ubuntu/aufs/aufs.ko ${STAGE31_DIR}/lib/modules/ # fetch the libraries needed for busybox BASICLIBS="" for i in $(ldd ${STAGE31_DIR}/bin/busybox); do if [ $(echo $i | grep '^/' | grep -c ld) -eq 1 \ -o $(echo $i | grep '^/' | grep -c libc.so) -eq 1 ]; then BASICLIBS="$BASICLIBS $i $(readlink -f "$i")" fi done (tar cpv $BASICLIBS | tar xpv -C ${STAGE31_DIR}) &>/dev/null } generate_squashfs() { # finalize the initramfs target [ -e ${STAGE31_DIR}/mnt/openslx.sqfs ] && rm ${STAGE31_DIR}/mnt/openslx.sqfs mksquashfs ${STAGE32_DIR} ${STAGE31_DIR}/mnt/openslx.sqfs -comp xz -b 1M -no-recovery 2>/dev/null } generate_initramfs() { cd ${STAGE31_DIR} find . | cpio --format="newc" --create | gzip -9 > ${MODULE_DIR}/initramfs cd - &>/dev/null } generate_stage31() { initial_checks generate_rootfs generate_squashfs generate_initramfs # TODO better kernel copy cp /boot/vmlinuz-$(uname -r) ${MODULE_DIR}/kernel } clean_core() { echo -n "Cleaning ${STAGE31_DIR}..." [ -d ${STAGE31_DIR} ] && rm -rf ${STAGE31_DIR} echo " done." echo -n "Cleaning ${MODULE_DIR}/initramfs..." [ -e ${MODULE_DIR}/initramfs ] && rm ${MODULE_DIR}/initramfs echo " done." }