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