55ok

Mini

Direktori : /bin/
Upload File :
Current File : //bin/install-tools

#!/bin/bash 
# 
# Copyright (c) 2017-2019 Virtuozzo International GmbH, All rights reserved. 
 
# Define PATH manually because udev nullifies it 
export PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin 
 
LOCKNAME="vz-guest-tools-lin" 
LOCKFILE="9:/var/lock/$LOCKNAME.lock" 
LOCKFILE_BG="8:/var/lock/$LOCKNAME-bg.lock" 
 
function find_util() { 
	local util=$1 
	for dir in /sbin /usr/sbin /bin /usr/bin; do 
		[ ! -x $dir/$util ] && continue 
		echo $dir/$util 
		return 
	done 
	# Last chance: it's in PATH 
	echo $util 
} 
 
PID=$$ 
function syslogger() { 
	[ ! -x $LOGGER ] && return 
	$LOGGER -t "VZTOOLS_udev{$PID}" $* 
} 
 
function cleanup() { 
	if [ $1 -eq 0 -a -x $EJECT ]; then 
		umount $DIR 2>&1 | syslogger 
		if ! findmnt $DEVNAME >/dev/null 2>&1; then 
			# CD is not mounted elsewhere => eject the disk 
			# Note: If you need to always eject the disk make sure to unmount all mount 
			# points first: eject unmounts only the latest one and leaves the rest 
			# of them mounted. 
			$EJECT $DEVNAME 2>&1 | syslogger 
			[ $? -ne 0 ] && syslogger "Failed to eject $DEVNAME" 
		fi 
	else 
		# For debugging 
		umount $DIR 2>&1 | syslogger 
		[ $? -ne 0 ] && syslogger "Failed to umount $DEVNAME" 
	fi 
	rm -rf $DIR > /dev/null 2>&1 
	restore_root_options 
	lock_end "$LOCKFILE" 
	exit $1 
} 
 
function error() { 
	echo $* 
	syslogger $* 
	cleanup 1 
} 
 
function lock_begin() { 
	local LOCK_FILE=${1##*:} 
	local LOCK_HDL=${1%%:*} 
	# Open lock file for protection from simultaneous running of several instances 
	if ! eval "exec $LOCK_HDL>$LOCK_FILE"; then 
		syslogger "Failed to open lock file '$LOCK_FILE', aborting" 
		exit 1 
	fi 
 
	# Grab the lock, if not already grabbed by another instance 
	if ! flock -xn $LOCK_HDL; then 
		syslogger "Another installer instance is running, aborting" 
		exit 0 
	fi 
} 
 
function lock_end() { 
	local LOCK_FILE=${1##*:} 
	local LOCK_HDL=${1%%:*} 
	rm -f "$LOCK_FILE" 
	flock -u $LOCK_HDL 
} 
 
function ensure_rw_root() { 
	# In Debian/Ubuntu udev is started when root is read-only, and it remains read-only 
	# within the udev namespace; installer cannot change anything. 
	# Check if this is the case and force remount root as read-write. 
	IFS=',' read -a opts < <(findmnt -o OPTIONS -n /) 
	for opt in "${opts[@]}"; do 
		if [ "$opt" == "ro" ]; then 
			syslogger "Root is read-only, remounting as read-write" 
			mount -o remount,rw / 
			REMOUNTED=1 
			break 
		fi 
	done 
} 
 
function restore_root_options() { 
	if [ "$REMOUNTED" == "1" ]; then 
		syslogger "Restoring read-only root" 
		sync 
		mount -o remount,ro / 
	fi 
} 
 
LOGGER=`find_util logger` 
EJECT=`find_util eject` 
BLKID=`find_util blkid` 
TOOLS_INSTALLER="install" 
LABEL="vz-tools-lin" 
 
lock_begin "$LOCKFILE" 
 
# Restart as background process to avoid killing by udev on timeout 
if [ "x$1" == "xbackground" ]; then 
	lock_begin "$LOCKFILE_BG" 
	shift 
	syslogger "Restarting $0 in background" 
	# Free the lock 
	lock_end "$LOCKFILE" 
	if which at >/dev/null 2>&1; then 
		syslogger "Using 'at' for background" 
		echo $0 "$@" | at now 
	elif ps aux | grep -q '\(/usr\)\?/s\?bin/crond\?'; then 
		syslogger "Using cron for background, may take up to 1 minute to activate the job" 
		ensure_rw_root 
		CRONFILE=$(mktemp /etc/cron.d/install-tools-XXXX) 
		echo "* * * * * root rm -f $CRONFILE; $0 \"$@\"" >$CRONFILE 
		restore_root_options 
	else 
		# Fallback: just start in background and hope for the best 
		syslogger "'at' not found, cron is not running, using shell '&' for background" 
		$0 "$@" & >/dev/null 2>&1 
	fi 
	lock_end "$LOCKFILE_BG" 
	exit 0 
fi 
 
ensure_rw_root 
 
DIR=`mktemp -d /tmp/toolsXXX` 
 
# Check that started manually or from udev 
if [ "x$DEVNAME" = "x" ]; then 
	DEVNAME=`$BLKID -l -o device -t LABEL=$LABEL 2>/dev/null | grep -E -m 1 ".*"` 
	[ "x$DEVNAME" = "x" ] && error "Can't find ISO with label $LABEL" 
else 
	# Check that udev failed 
	[ "x$ID_FS_LABEL" != "x$LABEL" ] && error "Incorrect tools ISO" 
fi 
 
mount -r $DEVNAME $DIR > /dev/null 2>&1 || error "Failed to mount $DEVNAME $LABEL disk" 
 
[ ! -x $DIR/$TOOLS_INSTALLER ] && error "Incorrect tools ISO" 
 
$DIR/$TOOLS_INSTALLER 2>&1 | syslogger 
test ${PIPESTATUS[0]} -eq 0 || error "Failed to install tools" 
 
syslogger "Virtuozzo guest tools successfully installed" 
 
cleanup 0 

Zerion Mini 1.0