mkdumprd: add function perror_exit

We use function to pass stdout to a variable, like get_persistent_dev
but it will echo some error message and exit in some cases, instead of
redirect all the echo to stderr, this patch adds a function perror_exit
to fix this and simplify/cleanup related code.

Also add another function perror() for cases where no need to exit.

Signed-off-by: Dave Young <dyoung@redhat.com>
Signed-off-by: Baoquan He <bhe@redhat.com>
Acked-by: Vivek Goyal <vgoyal@redhat.com>
This commit is contained in:
Baoquan He 2013-03-25 17:04:22 +08:00
parent dc8e283ff5
commit bbe46f9e22

View File

@ -15,13 +15,21 @@ SAVE_PATH=$(grep ^path $conf_file| cut -d' ' -f2)
extra_modules="" extra_modules=""
dracut_args=("--hostonly" "-o" "plymouth dash") dracut_args=("--hostonly" "-o" "plymouth dash")
perror_exit() {
echo $@ >&2
exit 1
}
perror() {
echo $@ >&2
}
get_persistent_dev() { get_persistent_dev() {
local i _tmp _dev local i _tmp _dev
_dev=$(udevadm info --query=name --name="$1" 2>/dev/null) _dev=$(udevadm info --query=name --name="$1" 2>/dev/null)
[ -z "$_dev" ] && { [ -z "$_dev" ] && {
echo "Kernel dev name of $1 is not found." perror_exit "Kernel dev name of $1 is not found."
exit 1
} }
for i in /dev/mapper/* /dev/disk/by-uuid/* /dev/disk/by-id/*; do for i in /dev/mapper/* /dev/disk/by-uuid/* /dev/disk/by-id/*; do
@ -32,8 +40,7 @@ get_persistent_dev() {
fi fi
done done
echo "Persistent device name of $1 is not found." perror_exit "Persistent device name of $1 is not found."
exit 1
} }
add_dracut_arg() { add_dracut_arg() {
@ -103,10 +110,9 @@ get_ssh_size() {
local _opt _out _size local _opt _out _size
_opt="-i $SSH_KEY_LOCATION -o BatchMode=yes -o StrictHostKeyChecking=yes" _opt="-i $SSH_KEY_LOCATION -o BatchMode=yes -o StrictHostKeyChecking=yes"
_out=$(ssh -q -n $_opt $1 "df -P $SAVE_PATH") _out=$(ssh -q -n $_opt $1 "df -P $SAVE_PATH")
if [ $? -ne 0 ]; then [ $? -ne 0 ] && {
echo "checking remote ssh server available size failed." perror_exit "checking remote ssh server available size failed."
exit 1 }
fi
#ssh output removed the line break, so print $11 instead of $4 #ssh output removed the line break, so print $11 instead of $4
_size=$(echo -n $_out|tail -1 | awk '{print $11}') _size=$(echo -n $_out|tail -1 | awk '{print $11}')
echo -n $_size echo -n $_size
@ -123,10 +129,8 @@ mkdir_save_path() {
[ ! -d ${_mnt}/$SAVE_PATH ] && { [ ! -d ${_mnt}/$SAVE_PATH ] && {
if is_readonly_mount $1; then if is_readonly_mount $1; then
echo "Mounting $1 as read-write for creating dump directory.." echo "Mounting $1 as read-write for creating dump directory.."
mount -o remount,rw $1 mount -o remount,rw $1 || {
[ $? -ne 0 ] && { perror_exit "Mounting $1 as read-write failed."
echo "Mounting $1 as read-write failed."
exit 1;
} }
_remount="yes" _remount="yes"
fi fi
@ -135,13 +139,11 @@ mkdir_save_path() {
[ "$_remount" = "yes" ] && { [ "$_remount" = "yes" ] && {
echo "Remounting $1 as read-only." echo "Remounting $1 as read-only."
mount -o remount,ro $1 || { mount -o remount,ro $1 || {
echo "Remounting $1 as read-only failed." perror_exit "Remounting $1 as read-only failed."
exit 1
} }
} }
[ $_ret -ne 0 ] && { [ $_ret -ne 0 ] && {
echo "Creating ${_mnt}/$SAVE_PATH failed." perror_exit "Creating ${_mnt}/$SAVE_PATH failed."
exit 1
} }
} }
} }
@ -204,8 +206,7 @@ verify_core_collector() {
if is_ssh_dump_target || is_raw_dump_target; then if is_ssh_dump_target || is_raw_dump_target; then
if [ "${1%% *}" = "makedumpfile" ]; then if [ "${1%% *}" = "makedumpfile" ]; then
! strstr "$1" "-F" && { ! strstr "$1" "-F" && {
echo "The specified dump target needs makedumpfile \"-F\" option." perror_exit "The specified dump target needs makedumpfile \"-F\" option."
exit 1
} }
fi fi
fi fi
@ -237,8 +238,7 @@ do
;; ;;
ext[234]|xfs|btrfs|minix|nfs) ext[234]|xfs|btrfs|minix|nfs)
if ! findmnt $config_val >/dev/null; then if ! findmnt $config_val >/dev/null; then
echo "Dump target $config_val is probably not mounted." perror_exit "Dump target $config_val is probably not mounted."
exit 1
fi fi
if [ "$config_opt" = "nfs" ]; then if [ "$config_opt" = "nfs" ]; then
@ -251,8 +251,7 @@ do
raw) raw)
#checking raw disk writable #checking raw disk writable
dd if=$config_val count=1 of=/dev/null > /dev/null 2>&1 || { dd if=$config_val count=1 of=/dev/null > /dev/null 2>&1 || {
echo "Bad raw disk $config_val" perror_exit "Bad raw disk $config_val"
exit 1
} }
add_dracut_arg "--device" "$(get_persistent_dev $config_val)" add_dracut_arg "--device" "$(get_persistent_dev $config_val)"
check_size raw $config_val check_size raw $config_val
@ -264,8 +263,7 @@ do
add_dracut_module "ssh-client" add_dracut_module "ssh-client"
add_dracut_sshkey "$SSH_KEY_LOCATION" add_dracut_sshkey "$SSH_KEY_LOCATION"
else else
echo "Bad ssh dump target $config_val" perror_exit "Bad ssh dump target $config_val"
exit 1
fi fi
;; ;;
core_collector) core_collector)
@ -289,4 +287,3 @@ dracut "${dracut_args[@]}" -M "$@"
_rc=$? _rc=$?
sync sync
exit $_rc exit $_rc