Path: blob/main/tests/sys/geom/class/union/union_test.sh
39635 views
# SPDX-License-Identifier: BSD-2-Clause1#2# Copyright (c) 2023 The FreeBSD Foundation3#4# This software was developed1 by Yan-Hao Wang <[email protected]>5# under sponsorship from the FreeBSD Foundation.6#7# Redistribution and use in source and binary forms, with or without8# modification, are permitted provided that the following conditions9# are met:10# 1. Redistributions of source code must retain the above copyright11# notice, this list of conditions and the following disclaimer.12# 2. Redistributions in binary form must reproduce the above copyright13# notice, this list of conditions and the following disclaimer in the14# documentation and/or other materials provided with the distribution.15#16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26# SUCH DAMAGE.27#2829. $(atf_get_srcdir)/conf.sh3031atf_test_case create cleanup32create_head()33{34atf_set "descr" "Test gunion create and destroy"35atf_set "require.user" "root"36}37create_body()38{39gunion_test_setup4041attach_md upperdev -s 1m42attach_md lowerdev -s 1m43newfs -U "/dev/${lowerdev}"4445atf_check gunion create "$upperdev" "$lowerdev"46guniondev="${upperdev}-${lowerdev}.union"47atf_check -o inline:"/dev/${guniondev}\n" ls "/dev/${guniondev}"48atf_check -o ignore fsck -p -f "/dev/${guniondev}"4950atf_check gunion destroy "$guniondev"51atf_check -s not-exit:0 -o ignore -e ignore ls "/dev/${guniondev}"52}53create_cleanup()54{55gunion_test_cleanup56}5758atf_test_case basic cleanup59basic_head()60{61atf_set "descr" "Check gunion doesn't affect lowerdev status and lowerdev can't be mounted when being in a gunion"62atf_set "require.user" "root"63}64basic_body()65{66gunion_test_setup6768attach_md upperdev -s 1m69attach_md lowerdev -s 1m70newfs -U "/dev/${lowerdev}"71mkdir lowermnt72mkdir gunionmnt7374mount "/dev/${lowerdev}" lowermnt75echo "lower file" > lower_file76cp lower_file lowermnt/lower_file77sync78umount lowermnt7980gunion create "$upperdev" "$lowerdev"81guniondev="${upperdev}-${lowerdev}.union"82atf_check -s not-exit:0 -o ignore -e ignore mount "/dev/${lowerdev}" lowermnt8384mount "/dev/${guniondev}" gunionmnt85echo "update lower file" >> gunionmnt/lower_file86echo "gunion file" > gunion_file87cp gunion_file gunionmnt/gunion_file88sync89umount gunionmnt9091gunion destroy "$guniondev"92mount "/dev/${lowerdev}" lowermnt93checksum lowermnt/lower_file lower_file94atf_check -s not-exit:0 -o ignore -e ignore ls lowermnt/gunion_file95}96basic_cleanup()97{98gunion_test_cleanup99}100101atf_test_case commit cleanup102commit_head()103{104atf_set "descr" "Test basic gunion commit without option"105atf_set "require.user" "root"106}107commit_body()108{109gunion_test_setup110111attach_md upperdev -s 1m112attach_md lowerdev -s 1m113newfs -U "/dev/${lowerdev}"114mkdir lowermnt115mkdir gunionmnt116117mount "/dev/${lowerdev}" lowermnt118echo "lower file" > lower_file119cp lower_file lowermnt/lower_file120sync121umount lowermnt122123gunion create "$upperdev" "$lowerdev"124guniondev="${upperdev}-${lowerdev}.union"125mount "/dev/${guniondev}" gunionmnt126checksum gunionmnt/lower_file lower_file127128echo "update lower file" >> lower_file129cp -f lower_file gunionmnt/lower_file130echo "gunion file" > gunion_file131cp gunion_file gunionmnt/gunion_file132sync133umount gunionmnt134atf_check gunion commit "$guniondev"135gunion destroy "$guniondev"136137atf_check -o ignore fsck -p -f "/dev/${lowerdev}"138mount "/dev/${lowerdev}" lowermnt139checksum lowermnt/lower_file lower_file140checksum lowermnt/gunion_file gunion_file141}142commit_cleanup()143{144gunion_test_cleanup145}146147atf_test_case offset cleanup148offset_head()149{150atf_set "descr" "Test gunion create with -o offset option"151atf_set "require.user" "root"152}153offset_body()154{155gunion_test_setup156157attach_md upperdev -s 1m158attach_md lowerdev -s 1m159gpart create -s GPT "/dev/${lowerdev}"160gpart add -t freebsd-ufs "$lowerdev"161newfs "/dev/${lowerdev}p1"162gpt_entry_1=$(gpart show "/dev/${lowerdev}")163mkdir gunionmnt164165secsize="$(diskinfo "/dev/${lowerdev}" | awk '{print $2}')"166p1_start_sector="$(gpart show -p "/dev/${lowerdev}" | grep ${lowerdev}p1 | awk '{print $1}')"167offset_size="$((secsize * p1_start_sector))"168169gunion create -o "$offset_size" "$upperdev" "$lowerdev"170guniondev="${upperdev}-${lowerdev}.union"171172atf_check -o ignore fsck -p -f "/dev/${guniondev}"173atf_check mount "/dev/${guniondev}" gunionmnt174umount gunionmnt175gunion destroy "$guniondev"176177gpt_entry_2=$(gpart show "/dev/${lowerdev}")178atf_check_equal "$gpt_entry_1" "$gpt_entry_2"179}180offset_cleanup()181{182gunion_test_cleanup183}184185atf_test_case size cleanup186size_head()187{188atf_set "descr" "Test gunion create with -s size option"189atf_set "require.user" "root"190}191size_body()192{193gunion_test_setup194195attach_md upperdev -s 2m196attach_md lowerdev -s 1m197newfs -U "/dev/${lowerdev}"198199gunion create -s 2m "$upperdev" "$lowerdev"200guniondev="${upperdev}-${lowerdev}.union"201echo "$guniondev" > guniondev202203size="$(diskinfo "/dev/$guniondev" | awk '{print $3}')"204atf_check_equal "2097152" "$size" # 2 MB = 2097152 bytes205}206size_cleanup()207{208gunion_test_cleanup209}210211atf_test_case secsize cleanup212secsize_head()213{214atf_set "descr" "Test gunion create with -S secsize option"215atf_set "require.user" "root"216}217secsize_body()218{219gunion_test_setup220221attach_md upperdev -s 1m222attach_md lowerdev -s 1m223newfs -S 512 -U "/dev/${lowerdev}"224lower_secsize="$(diskinfo "/dev/${lowerdev}" | awk '{print $2}')"225atf_check_equal "512" "$lower_secsize"226227gunion create -S 1024 "$upperdev" "$lowerdev"228guniondev="${upperdev}-${lowerdev}.union"229echo "$guniondev" > guniondev230231secsize="$(diskinfo "/dev/${guniondev}" | awk '{print $2}')"232atf_check_equal "1024" "$secsize"233}234secsize_cleanup()235{236gunion_test_cleanup237}238239atf_test_case gunionname cleanup240gunionname_head()241{242atf_set "descr" "Test gunion create with -Z gunionname option"243atf_set "require.user" "root"244}245gunionname_body()246{247gunion_test_setup248249attach_md upperdev -s 1m250attach_md lowerdev -s 1m251newfs -U "/dev/${lowerdev}"252253gunion create -Z gunion1 "$upperdev" "$lowerdev"254echo "gunion1.union" > guniondev255atf_check -o inline:"/dev/gunion1.union\n" ls /dev/gunion1.union256}257gunionname_cleanup()258{259gunion_test_cleanup260}261262atf_test_case revert cleanup263revert_head()264{265atf_set "descr" "Test gunion revert"266atf_set "require.user" "root"267}268revert_body()269{270gunion_test_setup271272attach_md upperdev -s 1m273attach_md lowerdev -s 1m274newfs -U "/dev/${lowerdev}"275mkdir lowermnt276mkdir gunionmnt277278mount "/dev/${lowerdev}" lowermnt279echo "lower file" > lower_file280cp lower_file lowermnt/lower_file281sync282umount lowermnt283284atf_check gunion create "$upperdev" "$lowerdev"285guniondev="${upperdev}-${lowerdev}.union"286mount "/dev/${guniondev}" gunionmnt287288echo "update lower file" >> gunionmnt/lower_file289echo "gunion file" > gunion_file290cp gunion_file gunionmnt/gunion_file291sync292umount gunionmnt293atf_check gunion revert "$guniondev"294295mount "/dev/${guniondev}" gunionmnt296checksum gunionmnt/lower_file lower_file297atf_check -s not-exit:0 -o ignore -e ignore ls gunionmnt/gunion_file298299umount gunionmnt300gunion destroy "$guniondev"301}302revert_cleanup()303{304gunion_test_cleanup305}306307atf_init_test_cases()308{309atf_add_test_case create310atf_add_test_case basic311atf_add_test_case commit312atf_add_test_case offset313atf_add_test_case size314atf_add_test_case secsize315atf_add_test_case gunionname316atf_add_test_case revert317}318319checksum()320{321src=$1322work=$2323324if [ ! -e "$src" ]; then325atf_fail "file not exist"326fi327if [ ! -e "$work" ]; then328atf_fail "file not exist"329fi330331src_checksum=$(md5 -q "$src")332work_checksum=$(md5 -q "$work")333334if [ "$work_checksum" != "$src_checksum" ]; then335atf_fail "md5 checksum didn't match with ${src} and ${work}"336fi337}338339340