From: Karel Zak Date: Wed, 6 Dec 2017 13:38:44 +0000 (+0100) Subject: tests: use flock to modify fstab X-Git-Tag: v2.32-rc1~145 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=e140506af35821857a47c1013bad03b12f31062d;p=thirdparty%2Futil-linux.git tests: use flock to modify fstab Note that for btrfs test we need to use low-level ts_fstab_addline, because we add multiple lines there. Signed-off-by: Karel Zak --- diff --git a/tests/functions.sh b/tests/functions.sh index 0409b62835..181fe7972b 100644 --- a/tests/functions.sh +++ b/tests/functions.sh @@ -628,6 +628,7 @@ function ts_fstab_open { function ts_fstab_close { echo "# -->" >> /etc/fstab + sync /etc/fstab } function ts_fstab_addline { @@ -639,7 +640,12 @@ function ts_fstab_addline { echo "$SPEC $MNT $FS $OPT 0 0" >> /etc/fstab } +function ts_fstab_lock { + ts_lock "fstab" +} + function ts_fstab_add { + ts_fstab_lock ts_fstab_open ts_fstab_addline $* ts_fstab_close @@ -655,6 +661,9 @@ function ts_fstab_clean { } s/# //; /^$/d" /etc/fstab + + sync /etc/fstab + ts_unlock "fstab" } function ts_fdisk_clean { @@ -694,7 +703,7 @@ function ts_lock { # Note that flock(2) lock is released on FD close. function ts_unlock { - 200<&- + 200<&- || : } function ts_scsi_debug_init { diff --git a/tests/ts/mount/fstab-broken b/tests/ts/mount/fstab-broken index 67dfa6e812..947e3af7ae 100755 --- a/tests/ts/mount/fstab-broken +++ b/tests/ts/mount/fstab-broken @@ -31,6 +31,7 @@ ts_skip_nonroot MNT=$TS_MOUNTPOINT mkdir -p $MNT +ts_fstab_lock ts_fstab_open echo "tmpd $MNT tmpfs" >> /etc/fstab ts_fstab_close @@ -72,7 +73,7 @@ else fi ts_finalize_subtest -ts_fstab_clean +ts_fstab_clean # this unlocks too ts_log "Success" ts_finalize diff --git a/tests/ts/mount/fstab-btrfs b/tests/ts/mount/fstab-btrfs index 6abbd0be1d..090f52304e 100755 --- a/tests/ts/mount/fstab-btrfs +++ b/tests/ts/mount/fstab-btrfs @@ -78,15 +78,18 @@ $TS_CMD_UMOUNT "$TS_MOUNTPOINT_CREATE" ts_init_subtest "btrfs" +ts_fstab_lock +ts_fstab_open # Tests with fs == btrfs # mounting default subvolume, deep in the structure, without entry in fstab -ts_fstab_add "$DEVICE" "$TS_MOUNTPOINT_DEFAULT" "btrfs" "" +ts_fstab_addline "$DEVICE" "$TS_MOUNTPOINT_DEFAULT" "btrfs" "" # mounting default subvolume, deep in the structure -ts_fstab_add "$DEVICE" "$TS_MOUNTPOINT_SUBVOL" "btrfs" "subvol=$NON_DEFAULT_SUBVOL" +ts_fstab_addline "$DEVICE" "$TS_MOUNTPOINT_SUBVOL" "btrfs" "subvol=$NON_DEFAULT_SUBVOL" # mounting non-default subvolume -ts_fstab_add "$DEVICE" "$TS_MOUNTPOINT_SUBVOLID" "btrfs" "subvolid=$NON_DEFAULT_SUBVOLID" +ts_fstab_addline "$DEVICE" "$TS_MOUNTPOINT_SUBVOLID" "btrfs" "subvolid=$NON_DEFAULT_SUBVOLID" # test bind mount pointing to subvolume root -ts_fstab_add "$TS_MOUNTPOINT_SUBVOLID" "$TS_MOUNTPOINT_BIND" "auto" "bind" +ts_fstab_addline "$TS_MOUNTPOINT_SUBVOLID" "$TS_MOUNTPOINT_BIND" "auto" "bind" +ts_fstab_close $TS_CMD_MOUNT -a >> $TS_OUTPUT 2>&1 $TS_CMD_MOUNT -a >> $TS_OUTPUT 2>&1 @@ -99,20 +102,24 @@ $TS_CMD_UMOUNT "$TS_MOUNTPOINT_SUBVOLID" >> $TS_OUTPUT 2>&1 # check that everything was unmounted $TS_CMD_MOUNT | grep "$TS_MOUNTPOINT_ANY" >> $TS_OUTPUT 2>&1 -ts_fstab_clean +ts_fstab_clean # this unlock too ts_log "Success" ts_finalize_subtest + ts_init_subtest "auto" +ts_fstab_lock +ts_fstab_open # Tests with fs == auto # mounting default subvolume, deep in the structure, without entry in fstab -ts_fstab_add "$DEVICE" "$TS_MOUNTPOINT_DEFAULT" "auto" "" +ts_fstab_addline "$DEVICE" "$TS_MOUNTPOINT_DEFAULT" "auto" "" # mounting default subvolume, deep in the structure -ts_fstab_add "$DEVICE" "$TS_MOUNTPOINT_SUBVOL" "auto" "subvol=$NON_DEFAULT_SUBVOL" +ts_fstab_addline "$DEVICE" "$TS_MOUNTPOINT_SUBVOL" "auto" "subvol=$NON_DEFAULT_SUBVOL" # mounting non-default subvolume -ts_fstab_add "$DEVICE" "$TS_MOUNTPOINT_SUBVOLID" "auto" "subvolid=$NON_DEFAULT_SUBVOLID" +ts_fstab_addline "$DEVICE" "$TS_MOUNTPOINT_SUBVOLID" "auto" "subvolid=$NON_DEFAULT_SUBVOLID" # test bind mount pointing to subvolume sub-directory -ts_fstab_add "$TS_MOUNTPOINT_SUBVOL/bind-mnt" "$TS_MOUNTPOINT_BIND" "auto" "bind" +ts_fstab_addline "$TS_MOUNTPOINT_SUBVOL/bind-mnt" "$TS_MOUNTPOINT_BIND" "auto" "bind" +ts_fstab_close $TS_CMD_MOUNT -a >> $TS_OUTPUT 2>&1 $TS_CMD_MOUNT -a >> $TS_OUTPUT 2>&1 @@ -125,7 +132,7 @@ $TS_CMD_UMOUNT "$TS_MOUNTPOINT_SUBVOLID" >> $TS_OUTPUT 2>&1 # check that everything was unmounted $TS_CMD_MOUNT | grep "$TS_MOUNTPOINT_ANY" >> $TS_OUTPUT 2>&1 -ts_fstab_clean +ts_fstab_clean # this unlock too ts_log "Success" ts_finalize_subtest diff --git a/tests/ts/mount/fstab-devname b/tests/ts/mount/fstab-devname index 8e391c806a..6648784d3f 100755 --- a/tests/ts/mount/fstab-devname +++ b/tests/ts/mount/fstab-devname @@ -50,7 +50,5 @@ ts_is_mounted $DEVICE || ts_die "B) Cannot find $DEVICE in /proc/mounts" $TS_CMD_UMOUNT $DEVICE || ts_die "B) Cannot umount $DEVICE" ts_fstab_clean - ts_log "Success" ts_finalize - diff --git a/tests/ts/mount/fstab-loop b/tests/ts/mount/fstab-loop index 9e17943b10..4104a62a0b 100755 --- a/tests/ts/mount/fstab-loop +++ b/tests/ts/mount/fstab-loop @@ -35,12 +35,17 @@ mkfs.ext3 -F $IMG &> /dev/null || ts_die "Cannot make ext3 on $IMG" [ -d "$TS_MOUNTPOINT-1" ] || mkdir -p $TS_MOUNTPOINT-1 [ -d "$TS_MOUNTPOINT-2" ] || mkdir -p $TS_MOUNTPOINT-2 -ts_fstab_add "$IMG" "$TS_MOUNTPOINT-1" "ext3" "loop" +ts_fstab_lock +ts_fstab_open + +ts_fstab_addline "$IMG" "$TS_MOUNTPOINT-1" "ext3" "loop" $TS_CMD_MOUNT -a >> $TS_OUTPUT 2>&1 -ts_fstab_add "$IMG" "$TS_MOUNTPOINT-2" "ext3" "loop" +ts_fstab_addline "$IMG" "$TS_MOUNTPOINT-2" "ext3" "loop" $TS_CMD_MOUNT -a >> $TS_OUTPUT 2>&1 +ts_fstab_close + $TS_CMD_UMOUNT "$TS_MOUNTPOINT-1" >> $TS_OUTPUT 2>&1 $TS_CMD_UMOUNT "$TS_MOUNTPOINT-2" >> $TS_OUTPUT 2>&1