From f21cbb516591763282dace49fb6df040294dd765 Mon Sep 17 00:00:00 2001 From: Dave Young Date: Tue, 27 Nov 2012 12:48:56 +0800 Subject: [PATCH] handle readonly mounted filesystem We need fs dump target being mounted firstly before creating mkdumprd This is because we want to get the mount options from kernel mountinfo instead of simply mount it without considering mount options. To avoid the filesystem being used by something other than kdump we suggest them to mount it as 'ro', mkdumprd will remount it as 'rw' when necessary and remount it back to 'ro' In 2nd kernel kdump will still use 'rw' to mount it though. Tested local read-only mounted fs dump. [v1->v2]: improve documentation add error handling for `mount -o remount,ro` Fixed the changelog per Vivek's comment The code was reviewed by Vivek. Signed-off-by: Dave Young --- kexec-kdump-howto.txt | 4 ++++ mkdumprd | 51 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/kexec-kdump-howto.txt b/kexec-kdump-howto.txt index cb8a725..b45f35a 100644 --- a/kexec-kdump-howto.txt +++ b/kexec-kdump-howto.txt @@ -328,6 +328,10 @@ For both local filesystem and nfs dump the dump target must be mounted before building kdump initramfs. That means one needs to put an entry for the dump file system in /etc/fstab so that after reboot when kdump service starts, it can find the dump target and build initramfs instead of failing. +Usually the dump target should be used only for kdump. If you worry about +someone uses the filesystem for something else other than dumping vmcore +you can mount it as read-only. Mkdumprd will still remount it as read-write +for creating dump directory and will move it back to read-only afterwards. Raw partition diff --git a/mkdumprd b/mkdumprd index e89334d..4c20798 100644 --- a/mkdumprd +++ b/mkdumprd @@ -78,10 +78,13 @@ get_rootdev() { } to_mount() { - local _dev _mntopts _pdev + local _dev _t _o _mntopts _pdev _dev=$(to_dev_name $1) - _mntopts=$(findmnt -k -f -n -r -o TARGET,FSTYPE,OPTIONS $_dev) - [ -z "$_mntopts" ] && return + _t=$(findmnt -k -f -n -r -o TARGET,FSTYPE $_dev) + _o=$(findmnt -k -f -n -r -o OPTIONS $_dev) + [ -z "$_t" -o -z "$_o" ] && return + _o=${_o/#ro/rw} #mount fs target as rw in 2nd kernel + _mntopts="$_t $_o" #for non-nfs _dev converting to use udev persistent name if [ -b "$_dev" ]; then _pdev="$(get_persistent_dev $_dev)" @@ -96,6 +99,15 @@ to_mount_point() { echo $(findmnt -k -f -n -r -o TARGET $1) } +is_readonly_mount() { + local _mnt + _mnt=$(findmnt -k -f -n -r -o OPTIONS $1) + + #fs/proc_namespace.c: show_mountinfo(): + #seq_puts(m, mnt->mnt_flags & MNT_READONLY ? " ro" : " rw"); + [[ "$_mnt" =~ ^ro ]] +} + #Function: get_ssh_size #$1=dump target get_ssh_size() { @@ -111,17 +123,43 @@ get_ssh_size() { echo -n $_size } -#Function: get_fs_size +#mkdir if save path does not exist on dump target filesystem #$1=dump target -get_fs_size() { +mkdir_save_path() { local _mnt=$(to_mount_point $1) + local _remount="no" + local _ret + [ ! -d ${_mnt}/$SAVE_PATH ] && { + if is_readonly_mount $1; then + echo "Mounting $1 as read-write for creating dump directory.." + mount -o remount,rw $1 + [ $? -ne 0 ] && { + echo "Mounting $1 as read-write failed." + exit 1; + } + _remount="yes" + fi mkdir -p ${_mnt}/$SAVE_PATH - [ $? -ne 0 ] && { + _ret=$? + [ "$_remount" = "yes" ] && { + echo "Remounting $1 as read-only." + mount -o remount,ro $1 || { + echo "Remounting $1 as read-only failed." + exit 1 + } + } + [ $_ret -ne 0 ] && { echo "Creating ${_mnt}/$SAVE_PATH failed." exit 1 } } +} + +#Function: get_fs_size +#$1=dump target +get_fs_size() { + local _mnt=$(to_mount_point $1) echo -n $(df -P "${_mnt}/$SAVE_PATH"|tail -1|awk '{print $4}') } @@ -222,6 +260,7 @@ do echo "Dump target $config_val is probably not mounted." exit 1 fi + mkdir_save_path $config_val check_size fs $config_val ;; raw)