????

Your IP : 18.117.184.236


Current Path : /proc/thread-self/root/proc/thread-self/root/proc/self/root/proc/self/root/lib/Acronis/
Upload File :
Current File : //proc/thread-self/root/proc/thread-self/root/proc/self/root/proc/self/root/lib/Acronis/mkard.sh

#!/bin/bash
# Acronis RamDisk configuration utility.
# Copyright (C) Acronis, 2013
# Initially written by Simonov Vladimir (Vladimir.Simonov@acronis.com)
# $Id$
#

export LANG=C

RHEL_PREDEF_LIST="cifs.ko des_generic.ko ecb.ko md4.ko nfs.ko dm-mirror.ko"

KERNEL_VER=$1

[ -z "$KERNEL_VER" ] && KERNEL_VER=`uname -r`

echo Preparing Acronis Media configuration based on $KERNEL_VER kernel.

MOD_DIR=/lib/modules/$KERNEL_VER
[ ! -d $MOD_DIR ] \
	&& echo "$MOD_DIR not found" && exit 1

X86_64=""
if [ ! -z "`echo $KERNEL_VER | grep x86_64`" ] ; then
	X86_64=64
fi

BDIR=`dirname $0`
if [ "$BDIR" == "." ] ; then
	BDIR=`pwd`
fi

ARD_DIR=${BDIR}/BackupAndRecoveryAgent
[ ! -d $ARD_DIR ] \
	&& echo "$ARD_DIR not found" && exit 1
ARD_NAME=$ARD_DIR/agent_ramdisk${X86_64}.dat
[ ! -f  $ARD_NAME ] \
	&& echo "$ARD_NAME not found" && exit 1
AKRNL_NAME=$ARD_DIR/kernel${X86_64}.dat
[ ! -f  $AKRNL_NAME ] \
	&& echo "$AKRNL_NAME not found" && exit 1
KERNEL_NAME=/boot/vmlinuz-$KERNEL_VER
[ ! -f  $KERNEL_NAME ] \
	&& echo "$KERNEL_NAME not found" && exit 1
ARD_NAME2=$ARD_DIR/bootstrap_ramdisk${X86_64}.dat
[ -f  $ARD_NAME2 ] \
        && echo "Using $ARD_NAME2" && ARD_NAME_SAVED="$ARD_NAME" && ARD_NAME="$ARD_NAME2"

NOT_FOUND_MODULES=""
NOT_FOUND_FIRMWARE=""

function save_orig_data()
{
	local rd=$1; shift
	local rd_orig=${rd}.orig
	local krnl=$1; shift
	local krnl_orig=${krnl}.orig

	if [ ! -f $rd_orig ] ; then
		cp $rd $rd_orig
		[ X"$?" != X"0" ] && echo Copy $rd failed && exit 1
	fi
	if [ ! -f $krnl_orig ] ; then
		cp $krnl $krnl_orig
		[ X"$?" != X"0" ] && echo Copy $krnl failed && exit 1
	fi
}

# RHEL 6.x support - avoid "mount -t squashfs"
function unpack_squashfs()
{
	local fname=$1
	if [ -e lib/$fname.sq ]; then
#		mkdir lib/$fname.tmp
#		mount -t squashfs lib/$fname.sq lib/$fname.tmp -o loop
#		cp -a lib/$fname.tmp lib/$fname
#		umount lib/$fname.tmp
#		rm -rf lib/$fname.tmp lib/$fname.sq
		mkdir lib/$fname
		rm -f lib/$fname.sq
	fi
}

function get_unpack_cmd()
{
	local fname=$1
	file "${fname}" | grep -i -q "XZ compressed data"
	if [ X"$?" == X"0" ]; then
		echo -n "xz -d -c"
	else
		file "${fname}" | grep -i -q "gzip compressed data"
		if [ X"$?" == X"0" ]; then
			echo -n "gunzip -c"
		else
			echo -n "cat"
		fi	
	fi
}

function extract_ard()
{
	local name="${ARD_NAME}.orig"
	local cmd=`get_unpack_cmd "${name}"`
	mkdir -p $BDIR/ac
	cd $BDIR/ac
	${cmd} "${name}" | cpio --quiet -i -d --no-absolute-filenames
	[ X"$?" != X"0" ] && echo "Extract Acronis RD failed" && exit 1
# RHEL 6.x support - avoid "mount -t squashfs"
# extract original modules list from etc/modinfo.dump
	unpack_squashfs firmware
	unpack_squashfs modules
	cat etc/modinfo.dump|tr '\0' '\n'|grep filename|sed -e "s|filename:.*\/||" > ../mod_list.orig
	cd - > /dev/null
}

function repack_ard()
{
	local name="${ARD_NAME_SAVED}"
	if [ -z "${name}" ]; then
		return
	fi
	local cmd=`get_unpack_cmd "${name}"`
	${cmd} "${name}" | gzip -c > "${name}.gz"
	[ X"$?" != X"0" ] && echo "Repack ramdisk to gz format failed" && exit 1
	mv -f "${name}.gz" "${name}"	
	[ X"$?" != X"0" ] && echo "Repack ramdisk to gz format failed in mv" && exit 1
}

function extract_sysrd()
{
	local image="../initramfs-${KERNEL_VER}.img"
	local prefix="cat ${image}"
	mkdir -p $BDIR/sys
	mkinitrd --with=snapapi26 --with=snumbd26 \
			$BDIR/initramfs-${KERNEL_VER}.img ${KERNEL_VER}
	[ X"$?" != X"0" ] && echo "mkinitrd failed" && exit 1
	cd $BDIR/sys
	file ${image} | grep "cpio archive" > /dev/null 2>&1
	[ X"$?" == X"0" -a -f /usr/lib/dracut/skipcpio ] && \
		prefix="/usr/lib/dracut/skipcpio ${image}"
	${prefix} | gunzip -c - | cpio --quiet -i -d --no-absolute-filenames
	[ X"$?" != X"0" ] && echo "Extract System RD failed" && exit 1
	cd - > /dev/null
}

function extract_sysrd9()
{
	local image="../initramfs-${KERNEL_VER}.img"
	local prefix="cat ${image}"
	mkdir -p $BDIR/sys
	/usr/bin/dracut --add-drivers "snapapi26 snumbd26" \
			$BDIR/initramfs-${KERNEL_VER}.img ${KERNEL_VER}
	[ X"$?" != X"0" ] && echo "/usr/bin/dracut failed" && exit 1
	cd $BDIR/sys
	file ${image} | grep "cpio archive" > /dev/null 2>&1
	[ X"$?" == X"0" -a -f /usr/lib/dracut/skipcpio ] && \
		prefix="/usr/lib/dracut/skipcpio ${image}"
	${prefix} | gunzip -c - | cpio --quiet -i -d --no-absolute-filenames
	[ X"$?" != X"0" ] && echo "Extract System RD failed" && exit 1
	cd - > /dev/null
}

function copy_old()
{
	local i
	local from=$1; shift
	for i in $OLD_MODS_LIST ; do
		local mname=`basename $i`
		local oname=`find $from -name $mname`
		local xz=0
		if [ -z "$oname" ] ; then
			oname=`find $from -name ${mname}.xz`
			let xz=1
		fi
		if [ -z "$oname" ] ; then
			NOT_FOUND_MODULES="$NOT_FOUND_MODULES $mname"
		else
#			echo Adding module $mname
			if [ $xz -eq 0 ]; then
				cp -L -f $oname $AMODULES_DIR/
			else
				xz -d -c $oname > $AMODULES_DIR/$mname
			fi
			[ X"$?" != X"0" ] && echo "Failed to copy $oname"
		fi
	done
}

function copy_modules_from_dir()
{
	local list=`find $1 -name \*.ko`
	if [ -n "$list" ] ; then
		echo Adding modules from $1
		cp -L -f $list $AMODULES_DIR/
		[ X"$?" != X"0" ] && echo "Failed to copy modules from $1"
	fi
	list=`find $1 -name \*.ko.xz`
	local i
	echo Adding XZ modules from $1
	for i in $list ; do
		local out=`basename $i|sed -e "s|\.xz||"`
		xz -d -c $i > $AMODULES_DIR/$out
		[ X"$?" != X"0" ] && echo "Failed to copy XZ module $i from $1"
	done
}

function copy_modules_list()
{
	local dir=$1; shift
	local i

	for i in $* ; do
		local list=`find $dir -name \*${i}\*`
		local j
		for j in $list ; do
			echo Adding file $j from $dir
			local cmd=`get_unpack_cmd "$j"`
			local out=`basename $j|sed -e "s|\.xz||"`
			${cmd} "${j}" > $AMODULES_DIR/$out
			[ X"$?" != X"0" ] && echo "Failed to copy file $i from $1"
		done
	done
}

function copy_firmware()
{
	local afw_dir=$BDIR/ac/lib/firmware
	local list=""
	local i
	local dname
	local mods=`find $AMODULES_DIR/ -name \*.ko`
	for i in $mods; do
		list="$list `/sbin/modinfo $i -F firmware`"
	done
	for i in $list ; do
		if [ ! -f $afw_dir/$i ]; then
			dname=`dirname $i`
			[ "$dname" != "." ] && 	mkdir -p $afw_dir/$dname
			cp /lib/firmware/$i $afw_dir/$i 2> /dev/null
			[ X"$?" != X"0" ] && \
				NOT_FOUND_FIRMWARE="$NOT_FOUND_FIRMWARE $i"
		fi
	done
}

function check_deps()
{
	local deps=`modinfo -F depends $AMODULES_DIR/*.ko| \
			tr ',' '\n' |sort -u|grep -v "^\$"|sed "s/\$/\.ko/g"`
	local i
	for i in $deps; do
	if [ ! -f $AMODULES_DIR/${i} ] ; then
		UNRESOLVED_DEPS="$UNRESOLVED_DEPS $i"
	fi
	done
}

function collect_modinfo()
{
	/sbin/modinfo -0 $AMODULES_DIR/*.ko > $BDIR/ac/etc/modinfo.dump
	echo $RHEL_PREDEF_LIST | sed "s/\.ko//g" > $BDIR/ac/etc/mods.third
}

BDIR=${BDIR}/rd
AMODULES_DIR=$BDIR/ac/lib/modules

# Start work
rm -rf $BDIR

if [ -x /usr/bin/mkinitrd ]; then
	extract_sysrd
elif [ -x /usr/bin/lsinitrd -a -x /usr/bin/dracut ]; then
	extract_sysrd9
else
	echo Error: Cannot find /usr/bin/lsinitrd or /usr/bin/mkinitrd and /usr/bin/dracut
	exit 1
fi
save_orig_data ${ARD_NAME} ${AKRNL_NAME}
extract_ard

# RHEL 6.x support - avoid "mount -t squashfs"
# Use modules list from original /etc/modinfo.dump
#OLD_MODS_LIST=`find $AMODULES_DIR -type f`
#rm -rf $AMODULES_DIR/*
OLD_MODS_LIST=`cat $BDIR/mod_list.orig`

copy_old $MOD_DIR/kernel
[ -d $MOD_DIR/extra ] && copy_modules_from_dir $MOD_DIR/extra
[ -d $MOD_DIR/weak-updates ] && copy_modules_from_dir $MOD_DIR/weak-updates
[ -d $BDIR/sys/lib/modules ] && copy_modules_from_dir $BDIR/sys/lib/modules

copy_modules_list $MOD_DIR $RHEL_PREDEF_LIST
echo Resolving modules dependencies...
UNRESOLVED_DEPS=""
check_deps
if [ -n "$UNRESOLVED_DEPS" ] ; then
	copy_modules_list $MOD_DIR $UNRESOLVED_DEPS
	UNRESOLVED_DEPS=""
	check_deps
	if [ -n "$UNRESOLVED_DEPS" ] ; then
		copy_modules_list $MOD_DIR $UNRESOLVED_DEPS
		UNRESOLVED_DEPS=""
		check_deps
		if [ -n "$UNRESOLVED_DEPS" ] ; then
			echo Unresolved modules: $UNRESOLVED_DEPS
		fi
	fi
fi
echo Adding firmware...
copy_firmware
echo "Missed firmware: $NOT_FOUND_FIRMWARE"
echo Collecting modules information...
collect_modinfo

# pack ramdisk
echo Packing ramdisk...
cd $BDIR/ac
find . | cpio --quiet -H newc -o | gzip -1 -n >  $ARD_NAME
[ X"$?" != X"0" ] && echo "Pack new ramdisk failed" && exit 1
cd - > /dev/null

# copy kernel
cp -r $KERNEL_NAME $AKRNL_NAME
[ X"$?" != X"0" ] && echo "Can not copy $KERNEL_NAME" && exit 1

repack_ard

#echo "Missed modules: $NOT_FOUND_MODULES"
echo "New Acronis Media Configuration created successfully."
exit 0