Home | History | Annotate | Line # | Download | only in gpt
t_gpt.sh revision 1.10
      1  1.10  christos # $NetBSD: t_gpt.sh,v 1.10 2015/12/05 18:53:29 christos Exp $
      2   1.1  christos #
      3   1.2  christos # Copyright (c) 2015 The NetBSD Foundation, Inc.
      4   1.1  christos # All rights reserved.
      5   1.1  christos #
      6   1.1  christos # This code is derived from software contributed to The NetBSD Foundation
      7   1.2  christos # by Christos Zoulas
      8   1.1  christos #
      9   1.1  christos # Redistribution and use in source and binary forms, with or without
     10   1.1  christos # modification, are permitted provided that the following conditions
     11   1.1  christos # are met:
     12   1.1  christos # 1. Redistributions of source code must retain the above copyright
     13   1.1  christos #    notice, this list of conditions and the following disclaimer.
     14   1.1  christos # 2. Redistributions in binary form must reproduce the above copyright
     15   1.1  christos #    notice, this list of conditions and the following disclaimer in the
     16   1.1  christos #    documentation and/or other materials provided with the distribution.
     17   1.1  christos #
     18   1.1  christos # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19   1.1  christos # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20   1.1  christos # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21   1.1  christos # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22   1.1  christos # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23   1.1  christos # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24   1.1  christos # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25   1.1  christos # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26   1.1  christos # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27   1.1  christos # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28   1.1  christos # POSSIBILITY OF SUCH DAMAGE.
     29   1.1  christos #
     30   1.1  christos 
     31   1.7  christos bootblk=/usr/mdec/gptmbr.bin
     32   1.1  christos size=10240
     33   1.5  christos newsize=20480
     34   1.1  christos shdr=34
     35   1.1  christos disk=gpt.disk
     36   1.1  christos uuid="........-....-....-....-............"
     37   1.1  christos zero="00000000-0000-0000-0000-000000000000"
     38   1.4  christos src=$(atf_get_srcdir)
     39   1.1  christos 
     40   1.8  christos silence() {
     41   1.8  christos 	atf_check -s exit:0 -o empty -e empty "$@"
     42   1.8  christos }
     43   1.8  christos 
     44   1.8  christos match() {
     45   1.8  christos 	local match="$1"
     46   1.8  christos 	shift
     47   1.8  christos 	atf_check -s exit:0 -e empty -o match:"$match" "$@"
     48   1.8  christos }
     49   1.8  christos 
     50   1.8  christos matcherr() {
     51   1.8  christos 	local match="$1"
     52   1.8  christos 	shift
     53   1.8  christos 	atf_check -s exit:0 -o empty -e match:"$match" "$@"
     54   1.8  christos }
     55   1.8  christos 
     56   1.8  christos file() {
     57   1.8  christos 	local file="$1"
     58   1.8  christos 	shift
     59   1.8  christos 	atf_check -s exit:0 -e empty -o file:"$file" "$@"
     60   1.8  christos }
     61   1.8  christos 
     62   1.8  christos save() {
     63   1.8  christos 	local save="$1"
     64   1.8  christos 	shift
     65   1.8  christos 	atf_check -s exit:0 -e empty -o save:"$save" "$@"
     66   1.8  christos }
     67   1.8  christos 
     68   1.8  christos zerodd() {
     69   1.8  christos 	silence dd conv=notrunc msgfmt=quiet if=/dev/zero of="$disk" "$@"
     70   1.8  christos }
     71   1.8  christos 
     72   1.1  christos prepare() {
     73   1.7  christos 	rm -f "$disk"
     74   1.8  christos 	zerodd seek="$size" count=1
     75   1.1  christos }
     76   1.1  christos 
     77   1.1  christos prepare_2part() {
     78   1.1  christos 	prepare
     79   1.8  christos 	silence gpt create "$disk"
     80   1.8  christos 	match "$(partaddmsg 1 34 1024)" gpt add -t efi -s 1024 "$disk"
     81   1.8  christos 	match "$(partaddmsg 2 1058 9150)" gpt add "$disk"
     82   1.1  christos }
     83   1.1  christos 
     84   1.3  christos # Calling this from tests does not work. BUG!
     85   1.1  christos check_2part() {
     86   1.8  christos 	file "$src/gpt.2part.show.normal" gpt show "$disk"
     87   1.8  christos 	file "$src/gpt.2part.show.uuid" gpt show -u "$disk"
     88   1.1  christos }
     89   1.1  christos 
     90   1.5  christos partaddmsg() {
     91   1.1  christos 	echo "^$disk: Partition $1 added: $uuid $2 $3\$"
     92   1.1  christos }
     93   1.1  christos 
     94   1.5  christos partresmsg() {
     95   1.5  christos 	echo "^$disk: Partition $1 resized: $2 $3\$"
     96   1.5  christos }
     97   1.5  christos 
     98   1.6  christos partremmsg() {
     99   1.6  christos 	echo "^$disk: Partition $1 removed\$"
    100   1.6  christos }
    101   1.6  christos 
    102   1.6  christos partlblmsg() {
    103   1.6  christos 	echo "^$disk: Partition $1 label changed\$"
    104   1.6  christos }
    105   1.6  christos 
    106   1.7  christos partbootmsg() {
    107   1.7  christos 	echo "^$disk: Partition $1 marked as bootable\$"
    108   1.7  christos 
    109   1.7  christos }
    110   1.7  christos 
    111   1.1  christos recovermsg() {
    112   1.1  christos 	echo "^$disk: Recovered $1 GPT [a-z]* from $2\$"
    113   1.1  christos }
    114   1.1  christos 
    115   1.8  christos migratemsg() {
    116   1.8  christos 	echo -n "^gpt: $disk: Partition $1 unknown type MSDOS, "
    117   1.8  christos 	echo 'using "Microsoft Basic Data"$'
    118   1.8  christos }
    119   1.8  christos 
    120   1.1  christos atf_test_case create_empty
    121   1.1  christos create_empty_head() {
    122   1.1  christos 	atf_set "descr" "Create empty disk"
    123   1.1  christos }
    124   1.1  christos 
    125   1.1  christos create_empty_body() {
    126   1.1  christos 	prepare
    127   1.8  christos 	silence gpt create "$disk"
    128   1.8  christos 	file "$src/gpt.empty.show.normal" gpt show "$disk"
    129   1.1  christos }
    130   1.1  christos 
    131   1.1  christos atf_test_case create_2part
    132   1.1  christos create_2part_head() {
    133   1.1  christos 	atf_set "descr" "Create 2 partition disk"
    134   1.1  christos }
    135   1.1  christos 
    136   1.1  christos create_2part_body() {
    137   1.1  christos 	prepare_2part
    138   1.1  christos 	check_2part
    139   1.1  christos }
    140   1.1  christos 
    141   1.1  christos atf_test_case backup_2part
    142   1.1  christos backup_2part_head() {
    143   1.1  christos 	atf_set "descr" "Backup 2 partition disk"
    144   1.1  christos }
    145   1.1  christos 
    146   1.1  christos backup_2part_body() {
    147   1.1  christos 	prepare_2part
    148   1.8  christos 	save test.backup gpt backup "$disk"
    149   1.8  christos 	file "$src/gpt.backup" sed -e "s/$uuid/$zero/g" "test.backup"
    150   1.1  christos }
    151   1.1  christos 
    152   1.1  christos atf_test_case restore_2part
    153   1.1  christos restore_2part_head() {
    154   1.1  christos 	atf_set "descr" "Restore 2 partition disk"
    155   1.1  christos }
    156   1.1  christos 
    157   1.1  christos restore_2part_body() {
    158   1.1  christos 	prepare_2part
    159   1.8  christos 	save test.backup gpt backup "$disk"
    160   1.1  christos 	prepare
    161   1.8  christos 	silence gpt restore -i test.backup "$disk"
    162   1.1  christos 	check_2part
    163   1.1  christos }
    164   1.1  christos 
    165   1.1  christos atf_test_case recover_backup
    166   1.1  christos recover_backup_head() {
    167   1.1  christos 	atf_set "descr" "Recover the backup GPT header and table"
    168   1.1  christos }
    169   1.1  christos 
    170   1.1  christos recover_backup_body() {
    171   1.1  christos 	prepare_2part
    172   1.8  christos 	zerodd seek="$((size - shdr))" count="$shdr"
    173   1.8  christos 	match "$(recovermsg secondary primary)" gpt recover "$disk"
    174   1.1  christos 	check_2part
    175   1.1  christos }
    176   1.1  christos 
    177   1.1  christos atf_test_case recover_primary
    178   1.1  christos recover_primary_head() {
    179   1.1  christos 	atf_set "descr" "Recover the primary GPT header and table"
    180   1.1  christos }
    181   1.1  christos 
    182   1.1  christos recover_primary_body() {
    183   1.1  christos 	prepare_2part
    184   1.8  christos 	zerodd seek=1 count="$shdr"
    185   1.8  christos 	match "$(recovermsg primary secondary)" gpt recover "$disk"
    186   1.1  christos 	check_2part
    187   1.1  christos }
    188   1.1  christos 
    189   1.5  christos atf_test_case resize_2part
    190   1.5  christos resize_2part_head() {
    191   1.5  christos 	atf_set "descr" "Resize a 2 partition disk and partition"
    192   1.5  christos }
    193   1.5  christos 
    194   1.5  christos resize_2part_body() {
    195   1.5  christos 	prepare_2part
    196   1.8  christos 	zerodd seek="$newsize" count=1
    197   1.8  christos 	silence gpt resizedisk "$disk"
    198   1.8  christos 	file "$src/gpt.resizedisk.show.normal" gpt show "$disk"
    199   1.8  christos 	match "$(partresmsg 2 1058 19390)" gpt resize -i 2 "$disk"
    200   1.8  christos 	file "$src/gpt.resizepart.show.normal" gpt show "$disk"
    201   1.5  christos }
    202   1.5  christos 
    203   1.6  christos atf_test_case remove_2part
    204   1.6  christos remove_2part_head() {
    205   1.6  christos 	atf_set "descr" "Remove a partition from a 2 partition disk"
    206   1.6  christos }
    207   1.6  christos 
    208   1.6  christos remove_2part_body() {
    209   1.6  christos 	prepare_2part
    210   1.8  christos 	match "$(partremmsg 1)" -e empty gpt remove \
    211   1.7  christos 	    -i 1 "$disk"
    212   1.8  christos 	file "$src/gpt.removepart.show.normal" \
    213   1.7  christos 	    gpt show "$disk"
    214   1.6  christos }
    215   1.6  christos 
    216   1.6  christos atf_test_case label_2part
    217   1.6  christos label_2part_head() {
    218   1.7  christos 	atf_set "descr" "Label partitions in a 2 partition disk"
    219   1.6  christos }
    220   1.6  christos 
    221   1.6  christos label_2part_body() {
    222   1.6  christos 	prepare_2part
    223   1.8  christos 	match "$(partlblmsg 1)" gpt label -i 1 -l potato "$disk"
    224   1.8  christos 	match "$(partlblmsg 2)" gpt label -i 2 -l tomato "$disk"
    225   1.8  christos 	file "$src/gpt.2part.show.label" \
    226   1.7  christos 	    gpt show -l "$disk"
    227   1.7  christos }
    228   1.7  christos 
    229   1.7  christos atf_test_case bootable_2part
    230   1.7  christos bootable_2part_head() {
    231   1.7  christos 	atf_set "descr" "Make partition 2 bootable in a 2 partition disk"
    232   1.7  christos }
    233   1.7  christos 
    234   1.7  christos bootable_2part_body() {
    235   1.7  christos 	prepare_2part
    236   1.8  christos 	match "$(partbootmsg 2)" gpt biosboot -i 2 "$disk"
    237   1.7  christos 	local bootsz="$(ls -l "$bootblk" | awk '{ print $5 }')"
    238   1.8  christos 	silence dd msgfmt=quiet if="$disk" of=bootblk bs=1 count="$bootsz"
    239   1.8  christos 	silence cmp "$bootblk" bootblk
    240   1.8  christos 	save bootattr gpt show -i 2 "$disk"
    241   1.8  christos 	match "^  legacy BIOS boot partition\$" tail -1 bootattr
    242   1.8  christos }
    243   1.8  christos 
    244   1.8  christos atf_test_case migrate_disklabel
    245   1.8  christos migrate_disklabel_head() {
    246   1.8  christos 	atf_set "descr" "Migrate an MBR+disklabel disk to GPT"
    247   1.8  christos }
    248   1.8  christos 
    249   1.8  christos migrate_disklabel_body() {
    250   1.8  christos 	prepare
    251  1.10  christos 	silence fdisk -fi "$disk"
    252  1.10  christos 	silence fdisk -fu0s "169/63/$((size / 10))" "$disk"
    253  1.10  christos 	silence disklabel -R "$disk" "$src/gpt.disklabel"
    254   1.8  christos 	matcherr "$(migratemsg 5)" gpt migrate "$disk"
    255   1.9  christos 	file "$src/gpt.disklabel.show.normal" gpt show "$disk"
    256   1.6  christos }
    257   1.6  christos 
    258   1.1  christos atf_init_test_cases() {
    259   1.1  christos 	atf_add_test_case create_empty
    260   1.1  christos 	atf_add_test_case create_2part
    261   1.1  christos 	atf_add_test_case backup_2part
    262   1.6  christos 	atf_add_test_case remove_2part
    263   1.1  christos 	atf_add_test_case restore_2part
    264   1.1  christos 	atf_add_test_case recover_backup
    265   1.1  christos 	atf_add_test_case recover_primary
    266   1.5  christos 	atf_add_test_case resize_2part
    267   1.6  christos 	atf_add_test_case label_2part
    268   1.7  christos 	atf_add_test_case bootable_2part
    269   1.8  christos 	atf_add_test_case migrate_disklabel
    270   1.1  christos }
    271