first draft

This commit is contained in:
lars 2005-11-25 13:06:20 +00:00
parent 759c08c984
commit b27cb6e32b

245
knoppix-remastering/remaster.sh Executable file
View file

@ -0,0 +1,245 @@
#!/bin/sh
#
set -ue
#################### some settings ######################
# the base directory of your local development files
ROOT_DIR=$(dirname "$0")
# the user may define a different location for the local configuration by setting "LOCAL_CONF_DIR"
# otherwise use the default (local.conf.d)
LOCALCONF_DIR=${LOCALCONF_DIR:-${ROOT_DIR}/local.conf.d}
MASTER=$LOCALCONF_DIR/master.img
BUILD_DIR=$LOCALCONF_DIR/build
SCRIPTS_DIR=$LOCALCONF_DIR/chroot_scripts.d
PKG_INSTALL_LIST_DIR=$LOCALCONF_DIR/package-install.lst.d
PKG_REMOVE_LIST_DIR=$LOCALCONF_DIR/package-remove.lst.d
ROOT_DIR=$BUILD_DIR/rootfs
BOOT_DIR=$BUILD_DIR/bootfs
TMP_DIR=$BUILD_DIR/tmp
BACKUP_DIR=$BUILD_DIR/backup
IMAGE_FILE=$BUILD_DIR/remastered.iso
################# quite constant things #################
# the relative name of the image file on the cd
KNOPPIX_IMAGE=KNOPPIX/KNOPPIX
################### general functions ###################
# maybe this could be useful later
# for now it DOES NOT WORK
#
# get the path of a configuration file - local configuration files
# supersede default files
# parameter: base name of the configuration file
function get_config_file()
{
[ -e "$LOCALCONF_DIR/$1" ] && echo "$LOCALCONF_DIR/$1" && return 0
[ -e "$DEFAULTCONF_DIR/$1" ] && echo "$DEFAULTCONF_DIR/$1" && return 0
echo "configuration file ($1) not found!" >&2
exit 1
}
# just adds a tabulator at the beginning of every line (nice for sub-processes)
# you should redirect stderr to stdout before, by adding '2>&1' to the previous command
# example: "do_something_useful 2>&1 | indentation"
function indentation()
{
sed 's/^/\t/'
}
function error_die()
{
echo "$2" >&2
exit $1
}
##################### active functions #################
function master_get()
{
umount_procfs
local MASTER_MNT=$TMP_DIR/mnt-master-iso
local ROOT_MNT=$TMP_DIR/mnt-roof-cloop
echo "Mounting the master's image ..."
local MNT_OPTIONS=
# use loop option for mount, if the master is a file
[ -f "$MASTER" ] && MNT_OPTIONS="-o loop"
mount $MNT_OPTIONS "$MASTER" "$MASTER_MNT" 2>&1 | indentation
echo "Removing old bootfs files ..."
rm -rf "$BOOT_DIR" && mkdir "$BOOT_DIR" 2>&1 | indentation
echo "Copying bootfs files ..."
local item_dir
# ignore the big KNOPPIX image
find "$MASTER_MNT" -type f | sed 's#^$MASTER_MNT/##' | grep -v "^$KNOPPIX_IMAGE_FILE$" | while read item
do item_dir=$(dirname "$item")
[ -e "$BOOT_DIR/$item_dir" ] || mkdir -p "$BOOT_DIR/$item_dir"
cp -a "$MASTER_MNT/$item" "$BOOT_DIR/$item_dir/"
done 2>&1 | indentation
echo "Mounting the compressed image file ..."
mount "$MASTER_MNT/$KNOPPIX_IMAGE_FILE" "$ROOT_MNT" 2>&1 | intendation
echo "Copying the compressed root system ..."
cp -a "$ROOT_MNT/." "$ROOT_DIR"
umount "$ROOT_MNT"
umount "MASTER_MNT"
}
function master_backup()
{
umount_procfs
local root_parent=$(dirname "$ROOT_DIR")
local root_entry=$(basename "$ROOT_DIR")
local boot_parent=$(dirname "$BOOT_DIR")
local boot_entry=$(basename "$BOOT_DIR")
[ -e "$BACKUP_DIR" ] || mkdir -p "$BACKUP_DIR"
echo "Creating archive of the root filesystem (this may take _very_ long) ..."
tar czf "$BACKUP_DIR/rootfs.tar.gz" -C "$root_parent" "$root_entry"
echo "Creating archive of the boot filesystem ..."
tar czf "$BACKUP_DIR/bootfs.tar.gz" -C "$boot_parent" "$boot_entry"
}
function master_restore()
{
umount_procfs
local boot_backup_file=$BACKUP_DIR/bootfs.tar.gz
local boot_parent=$(dirname "$BOOT_DIR")
local root_backup_file=$BACKUP_DIR/rootfs.tar.gz
local root_parent=$(dirname "$ROOT_DIR")
# restoring the boot directory (the small one)
if [ -e "$boot_backup_file" ]
then if [ -e "$BOOT_DIR" ]
then echo "Removing old files from boot filesystem ..."
rm -rf "$BOOT_DIR"
fi
echo "Unpacking the boot filesystem archive ..."
tar xzf "$boot_backup_file" -C "$boot_parent"
else echo "Warning: no archive for boot filesystem found ($boot_backup_file) - skipping ..."
fi
# restoring the root directory (the big one)
if [ -e "$root_backup_file" ]
then if [ -e "$ROOT_DIR" ]
then echo "Removing old files from root filesystem ..."
rm -rf "$ROOT_DIR"
fi
echo "Unpacking the root filesystem archive ..."
tar xzf "$root_backup_file" -C "$root_parent"
else echo "Warning: no archive for root filesystem found ($root_backup_file) - skipping ..."
fi
}
umount_procfs()
{
# check for a mounted procfs
mount | grep -q " $ROOT_DIR/proc " && umount "$ROOT_DIR/proc"
true
}
function create_iso()
{
umount_procfs
echo "Creating the iso ..."
mkisofs $MKISOFS_OPTIONS -o "$IMAGE_FILE" "$ROOT_DIR"
}
function configure()
{
if [ ! -e "$IMAGE_DIR" ]; then
echo -e "Directory \"$IMAGE_DIR\" not found!"
echo -e "Did you run \"$0 dfsbuild\"?"
exit 1
fi
echo "Copying files to the box ..."
[ -e "$TMP_DIR" ] && rm -rf "$TMP_DIR"
# svn export --force "$TEMPLATE_DIR/." "$IMAGE_DIR"
# only ignore the owner attribut
cp -r --preserve=mode,timestamps,links "$TEMPLATE_DIR/." "$IMAGE_DIR" 2>&1 | indentation
echo "Configuring the cryptobox ..."
find "$SCRIPTS_DIR" -L -type f | sort | while read scriptname
do echo "Executing $scriptname ..."
chroot_image "$scriptname" 2>&1 | indentation
done
}
function blanknburn_cdrw()
{
cdrecord -v dev=$CDWRITER blank=fast
cdrecord -v dev=$CDWRITER "$IMAGE_FILE"
}
################ do it! ######################
[ $# -eq 0 ] && echo "[`basename $0`] - no arguments supplied - maybe you want to use '--help'"
# initialize local directories (easier for users)
for a in "$LOCALCONF_DIR" "$BUILD_DIR" "$SCRIPTS_DIR" "$PKG_INSTALL_LIST_DIR" "$PKG_REMOVE_LIST_DIR" "$TMP_DIR" "$BACKUP_DIR"
do if [ ! -e "$a" ]
then echo "Creating directory '$a' ... "
mkdir -p "$a"
fi
done
# check for uid=0 (necessary for all operations)
[ "$(id -u)" -ne 0 ] && echo "this script ($0) has to be called as root" >&2 && exit 1
while [ $# -gt 0 ]
do case "$1" in
init )
master_get
;;
backup )
master_backup
;;
restore )
master_restore
;;
config )
configure_cb normal
;;
iso )
create_iso
;;
burn )
blanknburn_cdrw
;;
release )
$0 init config iso
md5sum $IMAGE_FILE > ${IMAGE_FILE}.md5sum
sha1sum $IMAGE_FILE > ${IMAGE_FILE}.sha1sum
;;
help|--help )
echo "Syntax: `basename $0` ( release | init | config | iso | burn | help )"
echo " (you may specify more than one action)"
echo
;;
* )
echo -e "unknown action: $1"
echo
$0 help
exit 1
;;
esac
shift
done