summaryrefslogblamecommitdiffstats
path: root/testModule/scripts/prepare-disks
blob: 2e7dd6737f0e1a0282f2af45dc9965d56f099d3b (plain) (tree)
1
2
3
4
5
6




                                                                               
                                    













                                                                               
                                                          






                                                                                         
                                                           

                        















                                                                               
                                                        





                                                                           
                                                   




                                                           
                                                 











                                                                
                                                                      







                                                                           
                                                                   













                                                                               
#!/bin/bash
###############################################################################
# GLOBALS
#

declare -rg FLAG="/tmp/openslx.disk"
declare -rg OPENSLX_SYS_MOUNT="/opt/openslx/system"

#
# END GLOBALS
###############################################################################

###############################################################################
# FUNCTION DEFINITIONS
#
# helper to mount the OPENSLX_SYS partition to /opt/openslx/system
# Usage: mount_sys_part <path_to_sys_partition>
mount_sys_part() {
	if [ ! -b "$1" ]; then
		warn "'$1' is not a block device?"
		echo "!block: $1" >> /tmp/openslx.disk.log
		return 1
	fi

	local OPENSLX_SYS_DEVICE="$1"
	mkdir -p ${OPENSLX_SYS_MOUNT}
	if ! mount -t auto "${OPENSLX_SYS_DEVICE}" "${OPENSLX_SYS_MOUNT}"; then
		warn "Mounting '${OPENSLX_SYS_DEVICE}' to '${OPENSLX_SYS_MOUNT}' failed."
		echo "mount phail" >> /tmp/openslx.disk.log
		return 1
	fi
	return 0

}
#
# END FUNCTION DEFINITIONS
###############################################################################

###############################################################################
# MAIN CODE
#

command -v warn >/dev/null || . /lib/dracut-lib.sh

# let check the arguments
if [ "$#" -ne 2 ]; then
	warn "'$0' need 2 arguments: '$0 [OPENSLX_SYS|OPENSLX_TMP] <dev_path>'"
	echo "Not enough args!" >> /tmp/openslx.disk.log
	exit 1
fi
# $1 sane?
if [ "x$1" != "xOPENSLX_SYS" ] && [ "x$1" != "xOPENSLX_TMP" ]; then
	warn "First arg needs to be either 'OPENSLX_SYS' or 'OPENSLX_TMP'."
	warn "Given: $1"
	echo "OPENSLX? $1" >> /tmp/openslx.disk.log
	exit 1
fi
# $2 sane?
if [ ! -b "/dev/$2" ]; then
	warn "Second arg appears not to be a block device!"
	echo "block? $2" >> /tmp/openslx.disk.log
	exit 1
fi

# ok all seems well
PART_TYPE="$1"
PART_DEV="/dev/$2"

# lets check if we are already running
INSTANCES="$(grep "$PART_TYPE" "$FLAG" | busybox wc -l)"
if [ "$INSTANCES" -ge 1 ]; then
	# uhoh we are not alone!
	warn "'$0' already running for $PART_TYPE ... ignoring."
	echo "already running for $PART_TYPE" >> /tmp/openslx.disk.log
	exit 1
fi

# now comes the funny part. We write our pid to $FLAG in order to make sure
# we are the only instance of this script running.
echo "$PART_TYPE.$$" >> "$FLAG"

# if we are still here, then we can go on and process the partition
echo "Processing: $PART_TYPE -> $PART_DEV" >> /tmp/openslx.disk.log

if [ "$PART_TYPE" = "OPENSLX_TMP" ]; then
	# mark it for later mounting!
	echo "$PART_DEV" > /tmp/openslx.disk.tmp
	exit 0
fi
if [ "$PART_TYPE" = "OPENSLX_SYS" ]; then
	# mount it now, since qemu-nbd needs it asap!
	exit $(mount_sys_part "$PART_DEV")
fi

#
# END MAIN CODE
###############################################################################