Home | History | Annotate | Line # | Download | only in gpt
t_gpt.sh revision 1.2
      1 #!/bin/sh
      2 # $NetBSD: t_gpt.sh,v 1.2 2015/12/04 01:06:43 christos Exp $
      3 #
      4 # Copyright (c) 2015 The NetBSD Foundation, Inc.
      5 # All rights reserved.
      6 #
      7 # This code is derived from software contributed to The NetBSD Foundation
      8 # by Christos Zoulas
      9 #
     10 # Redistribution and use in source and binary forms, with or without
     11 # modification, are permitted provided that the following conditions
     12 # are met:
     13 # 1. Redistributions of source code must retain the above copyright
     14 #    notice, this list of conditions and the following disclaimer.
     15 # 2. Redistributions in binary form must reproduce the above copyright
     16 #    notice, this list of conditions and the following disclaimer in the
     17 #    documentation and/or other materials provided with the distribution.
     18 #
     19 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29 # POSSIBILITY OF SUCH DAMAGE.
     30 #
     31 
     32 size=10240
     33 shdr=34
     34 disk=gpt.disk
     35 uuid="........-....-....-....-............"
     36 zero="00000000-0000-0000-0000-000000000000"
     37 
     38 prepare() {
     39 	rm -f $disk
     40 	dd if=/dev/zero of=$disk seek=$size count=1
     41 }
     42 
     43 prepare_2part() {
     44 	prepare
     45 	atf_check -s exit:0 -o empty -e empty gpt create $disk
     46 	atf_check -s exit:0 -o match:"$(partmsg 1 34 1024)" -e empty \
     47 	    gpt add -t efi -s 1024 $disk
     48 	atf_check -s exit:0 -o match:"$(partmsg 2 1058 9150)" -e empty \
     49 	    gpt add $disk
     50 }
     51 
     52 check_2part() {
     53 	atf_check -s exit:0 -o file:gpt.2part.show.normal \
     54 	    -e empty gpt show $disk
     55 	atf_check -s exit:0 -o file:gpt.2part.show.guid \
     56 	    -e empty gpt show -g $disk
     57 }
     58 
     59 partmsg() {
     60 	echo "^$disk: Partition $1 added: $uuid $2 $3\$"
     61 }
     62 
     63 recovermsg() {
     64 	echo "^$disk: Recovered $1 GPT [a-z]* from $2\$"
     65 }
     66 
     67 atf_test_case create_empty
     68 create_empty_head() {
     69 	atf_set "descr" "Create empty disk"
     70 }
     71 
     72 create_empty_body() {
     73 	prepare
     74 	atf_check -s exit:0 -o empty -e empty gpt create $disk
     75 	atf_check -s exit:0 -o file:gpt.empty.show.normal \
     76 	    -e empty gpt show $disk
     77 }
     78 
     79 atf_test_case create_2part
     80 create_2part_head() {
     81 	atf_set "descr" "Create 2 partition disk"
     82 }
     83 
     84 create_2part_body() {
     85 	prepare_2part
     86 	check_2part
     87 }
     88 
     89 atf_test_case backup_2part
     90 backup_2part_head() {
     91 	atf_set "descr" "Backup 2 partition disk"
     92 }
     93 
     94 backup_2part_body() {
     95 	prepare_2part
     96 	atf_check -s exit:0 -o save:test.backup -e empty gpt backup $disk
     97 	atf_check -s exit:0 -o file:gpt.backup -e empty \
     98 	    sed -e "s/$uuid/$zero/g" test.backup
     99 }
    100 
    101 atf_test_case restore_2part
    102 restore_2part_head() {
    103 	atf_set "descr" "Restore 2 partition disk"
    104 }
    105 
    106 restore_2part_body() {
    107 	prepare_2part
    108 	atf_check -s exit:0 -o save:test.backup -e empty gpt backup $disk
    109 	prepare
    110 	atf_check -s exit:0 -o empty -e empty gpt restore -i test.backup $disk
    111 	check_2part
    112 }
    113 
    114 atf_test_case recover_backup
    115 recover_backup_head() {
    116 	atf_set "descr" "Recover the backup GPT header and table"
    117 }
    118 
    119 recover_backup_body() {
    120 	prepare_2part
    121 	dd conv=notrunc if=/dev/zero of=$disk seek=$((size - shdr)) count=$shdr
    122 	atf_check -s exit:0 -o match:"$(recovermsg secondary primary)" \
    123 	    -e empty gpt recover $disk
    124 	check_2part
    125 }
    126 
    127 atf_test_case recover_primary
    128 recover_primary_head() {
    129 	atf_set "descr" "Recover the primary GPT header and table"
    130 }
    131 
    132 recover_primary_body() {
    133 	prepare_2part
    134 	dd conv=notrunc if=/dev/zero of=$disk seek=1 count=$shdr
    135 	atf_check -s exit:0 -o match:"$(recovermsg primary secondary)" \
    136 	    -e empty gpt recover $disk
    137 	check_2part
    138 }
    139 
    140 atf_init_test_cases() {
    141 	atf_add_test_case create_empty
    142 	atf_add_test_case create_2part
    143 	atf_add_test_case backup_2part
    144 	atf_add_test_case restore_2part
    145 	atf_add_test_case recover_backup
    146 	atf_add_test_case recover_primary
    147 }
    148