| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# |
| 4 |
# Copyright (C) 2007 Karel Zak <[email protected]> |
| 5 |
# |
| 6 |
# This file is part of util-linux. |
| 7 |
# |
| 8 |
# This file is free software; you can redistribute it and/or modify |
| 9 |
# it under the terms of the GNU General Public License as published by |
| 10 |
# the Free Software Foundation; either version 2 of the License, or |
| 11 |
# (at your option) any later version. |
| 12 |
# |
| 13 |
# This file is distributed in the hope that it will be useful, |
| 14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
# GNU General Public License for more details. |
| 17 |
# |
| 18 |
TS_TOPDIR="${0%/*}/../.." |
| 19 |
TS_DESC="move" |
| 20 |
|
| 21 |
. "$TS_TOPDIR"/functions.sh |
| 22 |
ts_init "$*" |
| 23 |
|
| 24 |
ts_check_test_command "$TS_CMD_MOUNT" |
| 25 |
ts_check_test_command "$TS_CMD_UMOUNT" |
| 26 |
ts_check_test_command "$TS_CMD_FINDMNT" |
| 27 |
ts_check_test_command "$TS_CMD_MOUNTPOINT" |
| 28 |
|
| 29 |
ts_skip_nonroot |
| 30 |
ts_skip_docker |
| 31 |
|
| 32 |
function mount_and_check { |
| 33 |
# last arg must be an existing or to-be-mounted mountpoint |
| 34 |
local mountpoint="${@: -1}" |
| 35 |
|
| 36 |
$TS_CMD_MOUNT "$@" &> /dev/null \ |
| 37 |
|| ts_die "error: mount $*" |
| 38 |
|
| 39 |
$TS_CMD_MOUNTPOINT --quiet "$mountpoint" \ |
| 40 |
|| ts_die "error: mountpoint $mountpoint" |
| 41 |
} |
| 42 |
|
| 43 |
DIR_PRIVATE="$TS_OUTDIR/mnt-move-private" |
| 44 |
|
| 45 |
DIR_SRC="$DIR_PRIVATE/mnt-move-src" |
| 46 |
DIR_A="$DIR_PRIVATE/mnt-move-A" |
| 47 |
DIR_B="$DIR_PRIVATE/mnt-move-B" |
| 48 |
|
| 49 |
# this may fail if there are mounted dirs left |
| 50 |
rm -rf "$DIR_PRIVATE" || ts_die "cleanup failed, check manually!" |
| 51 |
mkdir "$DIR_PRIVATE" || ts_die "error: mkdir DIR_PRIVATE" |
| 52 |
|
| 53 |
# create bind mount and make it private to be sure |
| 54 |
# (kernel cannot move mount with shared parent) |
| 55 |
mount_and_check --bind $DIR_PRIVATE $DIR_PRIVATE |
| 56 |
mount_and_check --make-rprivate $DIR_PRIVATE |
| 57 |
|
| 58 |
# Is the bind mount still rw? |
| 59 |
mkdir $DIR_SRC $DIR_A $DIR_B \ |
| 60 |
|| ts_die "error: mkdir on private bind mount" |
| 61 |
|
| 62 |
# bind |
| 63 |
mount_and_check --bind $DIR_SRC $DIR_A |
| 64 |
|
| 65 |
# move |
| 66 |
mount_and_check --move $DIR_A $DIR_B |
| 67 |
|
| 68 |
# BTW a basic test for findmnt(8) and mountpoint(1) |
| 69 |
for f in `find $DIR_PRIVATE2 $DIR_PRIVATE`; do |
| 70 |
xo="$($TS_CMD_MOUNTPOINT -q "$f" 2>&1)" |
| 71 |
x=$? |
| 72 |
# mountpoint(1) returns 32 if mountpoint does not exist, map it to 1 to |
| 73 |
# be compatible with findmnt(8) |
| 74 |
if [ "$x" = "32" ]; then |
| 75 |
x=1 |
| 76 |
fi |
| 77 |
yo="$($TS_CMD_FINDMNT --kernel --mountpoint "$f" 2>&1)" |
| 78 |
y=$? |
| 79 |
|
| 80 |
[ "$x" = "$y" ] || ts_log "error: findmount vs. mountpoint: $x, $y, $f" |
| 81 |
[ -z "$xo" ] || ts_log "error: mountpoint is not quiet: $x, $y, $f" |
| 82 |
if [ "$y" = "0" -a "$yo" = "" ] || [ "$y" = "1" -a "$yo" != "" ]; then |
| 83 |
ts_log "error, findmount return value vs. output: $x, $y, $f" |
| 84 |
fi |
| 85 |
done |
| 86 |
|
| 87 |
# clean up |
| 88 |
$TS_CMD_UMOUNT $DIR_B || ts_log "error: umount DIR_B" |
| 89 |
rmdir $DIR_SRC $DIR_A $DIR_B || ts_log "error: rmdir DIR_B" |
| 90 |
$TS_CMD_UMOUNT $DIR_PRIVATE || ts_log "error: umount DIR_PRIVATE" |
| 91 |
rmdir $DIR_PRIVATE || ts_log "error: rmdir DIR_PRIVATE" |
| 92 |
|
| 93 |
ts_log "Success" |
| 94 |
ts_finalize |
| 95 |
|