From patchwork Fri Feb 10 17:06:25 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 9567119 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 57B0A601C3 for ; Fri, 10 Feb 2017 17:07:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4527A26CF9 for ; Fri, 10 Feb 2017 17:07:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3682426E77; Fri, 10 Feb 2017 17:07:04 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9CE9826CF9 for ; Fri, 10 Feb 2017 17:07:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752384AbdBJRHC (ORCPT ); Fri, 10 Feb 2017 12:07:02 -0500 Received: from mail-wm0-f68.google.com ([74.125.82.68]:32972 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751712AbdBJRGv (ORCPT ); Fri, 10 Feb 2017 12:06:51 -0500 Received: by mail-wm0-f68.google.com with SMTP id v77so8241565wmv.0 for ; Fri, 10 Feb 2017 09:06:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=mkncTd1IKB6LIJeTDZrDahf+3/6ej0gu5XACWCWrrGM=; b=PcQ+TUrPMB1QJOY0sbOej97C0/miXDtmNoE7I09Q9T6xWi3neJRS0Bg+sF/Nql71Cb bCh0yq7TItMrEd2rjZZVvnUZ7YggjRP+eTNe3NXgN2EWsP6KnWbQvtUceVdTJdsLs29R yVrVLg8fDTrPyJD8YUaT9w2K0vwWavqK3IHb9rRXr0JRTVWZNBEfZsx10DhbOhCbk7Nw mGuur93VVjoM+IyBFLbVY6wbIGdF1hHNyj+XzQIORNpBsBvrEm0d3pYFyqic0CIUytGp AanBmrUFybSwXWfYdhf1peUnHmSe9Mm2vT5A3Q4wq78gN8aKCLeBiBr2ePBjgMwFRFlB EiTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=mkncTd1IKB6LIJeTDZrDahf+3/6ej0gu5XACWCWrrGM=; b=A6L9WQ7ntVpWzQ1G+9uPptgcZMyrmUIjBQcn/zvVNRp1dvXc1PfdS3z6VQX/NYxOAI 0zMewKTIXE9S2kBGylRURpFxEsO7NNFW2uyo2Mx+EeQOl6j1gsbZn8VRCQxz8Oob9/mu mVoKVz9gKIR8FoV0KzK4rFfmm5vvP9yem/cSba7yh2/ENwZDtEa6GR651vl5fTRe6nxo lJiVEeqRLKgKjnYZQhWlu3cfwkpeSqMdqdOTiqRKk/PfkR4q9fZNs10GVXYFetRRYXjS 2729gEhKKmxCxm8QfnOUXKpXZEgRdGZegT0pzmhsYsn2YYWuMPydM8EErX7vWQhZ11FK k9bg== X-Gm-Message-State: AMke39monS/BPo7phhlIuPF4G13d7QKLkS9FbB1Ra8OCsPmYYZdkc9GjdbNoXMEhQFWBPA== X-Received: by 10.28.189.135 with SMTP id n129mr8019513wmf.92.1486746392987; Fri, 10 Feb 2017 09:06:32 -0800 (PST) Received: from localhost.localdomain (bzq-79-176-143-83.red.bezeqint.net. [79.176.143.83]) by smtp.gmail.com with ESMTPSA id o22sm3552410wro.2.2017.02.10.09.06.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 10 Feb 2017 09:06:32 -0800 (PST) From: Amir Goldstein To: Eryu Guan Cc: fstests@vger.kernel.org Subject: [PATCH] fstests: sanity check that test partitions are not mounted elsewhere Date: Fri, 10 Feb 2017 19:06:25 +0200 Message-Id: <1486746385-5364-1-git-send-email-amir73il@gmail.com> X-Mailer: git-send-email 2.7.4 Sender: fstests-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: fstests@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When $TEST_DEV is mounted at a different location than $TEST_DIR, _require_test() aborts the test with an error: TEST_DEV=/dev/sda5 is mounted but not on TEST_DIR=/mnt/test There are several problems with current sanity check: 1. the output of the error is mixed into out.bad and hard to see 2. the test partition is unmounted at the end of the test regardless of the fact that it not pass the sanity that we have exclusivity 3. scratch partition has a similar sanity check in _require_scratch(), but we may not get to it, because $SCRATCH_DEV is unmounted prior to running the tests (which could unmount another mount point). To solve all these problems, introduce a helper _check_mounted_on(). It checks if a device is mounted on a given mount point and optionally checks the mounted fs type. The sanity checks in _require_scratch() and _require_test() are converted to use the _check_mounted_on() helper. The helper is used in init_rc() to sanity check both test and scratch partitions, before tests are run and before $SCRATCH_DEV is unmounted. Signed-off-by: Amir Goldstein --- common/rc | 85 ++++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 51 insertions(+), 34 deletions(-) Eryu, As result of your request to add sanity checks to overlay base fs, I ended up finding several bugs in current sanity checks (not overlay related). So sending this one out because it is useful on its own. Overlay base fs patches are going to be on top of this change and use the helper as well. Incidently, this change also fixes an overlay setup bug, but I did not want to add this info to the already overloaded commit message, as the following base fs patches will make this bug obsolete - If you configure SCRATCH_DEV to a mount point (and not to a directory therein) then you will get a false positive error in scratch tests: $SCRATCH_DEV=/mnt/base/scratch is mounted but not on $SCRATCH_MNT=/mnt/scratch Already mounted result: /dev/sda6 on /mnt/base/scratch type xfs (rw,relatime,attr2,inode64,noquota) This is due to the wrong `grep -F $SCRATCH_DEV` which matches the mount point instead of the device in that mount. I am pretty sure we already has this discussion where you changed the pattern to `grep -F ^$dev` and I advised you to go with `grep -F "$dev on "`. Anyway, this change includes this fix. Amir. diff --git a/common/rc b/common/rc index 7e2eaee..1c773cc 100644 --- a/common/rc +++ b/common/rc @@ -1319,6 +1319,45 @@ _supported_os() _notrun "not suitable for this OS: $HOSTOS" } +# check if a FS on a device is mounted +# if so, verify that it is mounted on mount point +# if fstype is given as argument, verify that it is also +# mounted with correct fs type +# +_check_mounted_on() +{ + local devname=$1 + local dev=$2 + local mntname=$3 + local mnt=$4 + local type=$5 + + # Note that we use -F here so grep doesn't try to interpret an NFS over + # IPv6 server as a regular expression. Because of that, we cannot use + # ^$dev so we use "$dev on " to avoid matching $dev to mount point field + # for overlay case, where $dev is a directory. + local mount_rec=`_mount | grep -F "$dev on "` + [ -n "$mount_rec" ] || return 1 + + # if it's mounted, make sure its on $mnt + if ! (echo $mount_rec | grep -q "$dev on $mnt") + then + echo "$devname=$dev is mounted but not on $mntname=$mnt - aborting" + echo "Already mounted result:" + echo $mount_rec + exit 1 + fi + + if [ -n "$type" -a "`_fs_type $dev`" != "$type" ] + then + echo "$devname=$dev is mounted but not a type $type filesystem" + # raw $DF_PROG cannot handle NFS/CIFS/overlay correctly + _df_device $dev + exit 1 + fi + return 0 +} + # this test needs a scratch partition - check we're ok & unmount it # No post-test check of the device is required. e.g. the test intentionally # finishes the test with the filesystem in a corrupt state @@ -1373,21 +1412,9 @@ _require_scratch_nocheck() ;; esac - # mounted? - # Note that we use -F here so grep doesn't try to interpret an NFS over - # IPv6 server as a regular expression. - mount_rec=`_mount | grep -F $SCRATCH_DEV` - if [ "$mount_rec" ] + if _check_mounted_on SCRATCH_DEV $SCRATCH_DEV SCRATCH_MNT $SCRATCH_MNT then - # if it's mounted, make sure its on $SCRATCH_MNT - if ! echo $mount_rec | grep -q $SCRATCH_MNT - then - echo "\$SCRATCH_DEV=$SCRATCH_DEV is mounted but not on \$SCRATCH_MNT=$SCRATCH_MNT - aborting" - echo "Already mounted result:" - echo $mount_rec - exit 1 - fi - # and then unmount it + # if it's mounted, unmount it if ! _scratch_unmount then echo "failed to unmount $SCRATCH_DEV" @@ -1458,21 +1485,8 @@ _require_test() ;; esac - # mounted? - # Note that we use -F here so grep doesn't try to interpret an NFS over - # IPv6 server as a regular expression. - mount_rec=`_mount | grep -F $TEST_DEV` - if [ "$mount_rec" ] + if ! _check_mounted_on TEST_DEV $TEST_DEV TEST_DIR $TEST_DIR then - # if it's mounted, make sure its on $TEST_DIR - if ! echo $mount_rec | grep -q $TEST_DIR - then - echo "\$TEST_DEV=$TEST_DEV is mounted but not on \$TEST_DIR=$TEST_DIR - aborting" - echo "Already mounted result:" - echo $mount_rec - exit 1 - fi - else out=`_mount_or_remount_rw "$MOUNT_OPTIONS" $TEST_DEV $TEST_DIR` if [ $? -ne 1 ]; then echo $out @@ -3066,13 +3080,16 @@ init_rc() fi fi - if [ "`_fs_type $TEST_DEV`" != "$FSTYP" ] - then - echo "common/rc: Error: \$TEST_DEV ($TEST_DEV) is not a MOUNTED $FSTYP filesystem" - # raw $DF_PROG cannot handle NFS/CIFS/overlay correctly - _df_device $TEST_DEV - exit 1 + # Sanity check that TEST partition is not mounted at another mount point + # or as another fs type + _check_mounted_on TEST_DEV $TEST_DEV TEST_DIR $TEST_DIR $FSTYP + if [ -n "$SCRATCH_DEV" ]; then + # Sanity check that SCRATCH partition is not mounted at another + # mount point, because it is about to be unmounted and formatted. + # Another fs type for scratch is fine (bye bye old fs type). + _check_mounted_on SCRATCH_DEV $SCRATCH_DEV SCRATCH_MNT $SCRATCH_MNT fi + # Figure out if we need to add -F ("foreign", deprecated) option to xfs_io $XFS_IO_PROG -c stat $TEST_DIR 2>&1 | grep -q "is not on an XFS filesystem" && \ export XFS_IO_PROG="$XFS_IO_PROG -F"