#!/bin/sh
+# test bios_grub flag in gpt labels
-# Copyright (C) 2007,2008 Free Software Foundation, Inc.
+# Copyright (C) 2007-2012 Free Software Foundation, Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-test_description="test bios_grub flag in gpt labels"
-: ${srcdir=.}
-. $srcdir/test-lib.sh
+. "${srcdir=.}/init.sh"; path_prepend_ ../parted
+ss=$sector_size_
dev=loop-file
+N=4200 # number of sectors
-test_expect_success \
- "setup: create zeroed device" \
- '{ dd if=/dev/zero bs=1024 count=64; } > $dev'
+part_sectors=2048
+start_sector=2048
+end_sector=$(expr $start_sector + $part_sectors - 1)
-test_expect_success \
- 'create gpt label' \
- 'parted -s $dev mklabel gpt >out 2>&1'
+# setup: reasonable params
+test $end_sector -lt $N || fail=1
-test_expect_success \
- 'add a partition' \
- 'parted -s $dev mkpart primary 0 1 >>out 2>&1'
+# setup: create zeroed device
+dd if=/dev/zero of=$dev bs=${ss}c count=$N 2> /dev/null || fail=1
-test_expect_success \
- 'print the table (before manual modification)' \
- 'parted -s $dev print >>out 2>&1'
+# create gpt label
+parted -s $dev mklabel gpt > empty 2>&1 || fail=1
-# Using bios_boot_magic='\x48\x61' looks nicer, but isn't portable.
-# dash's builtin printf doesn't recognize such \xHH hexadecimal escapes.
-bios_boot_magic='\110\141\150\41\111\144\157\156\164\116\145\145\144\105\106\111'
+# ensure there was no output
+compare /dev/null empty || fail=1
-printf "$bios_boot_magic" | dd of=$dev bs=1024 seek=1 conv=notrunc
+# print the table (before adding a partition)
+parted -m -s $dev unit s print > t 2>&1 || fail=1
+sed "s,.*/$dev:,$dev:," t > out || fail=1
-test_expect_success \
- 'print the table (after manual modification)' \
- 'parted -s $dev print >>out 2>&1'
+# check for expected output
+printf "BYT;\n$dev:${N}s:file:$ss:$ss:gpt::;\n" > exp || fail=1
+compare exp out || fail=1
-pwd=`pwd`
+# add a partition
+parted -s $dev u s mkpart name1 ${start_sector} ${end_sector} >out 2>&1 \
+ || fail=1
-fail=0
-{
- cat <<EOF
-Model: (file)
-Disk .../$dev: 65.5kB
-Sector size (logical/physical): 512B/512B
-Partition Table: gpt
-
-Number Start End Size File system Name Flags
- 1 17.4kB 48.6kB 31.2kB primary
+# print the table before modification
+parted -m -s $dev unit s print > t 2>&1 || fail=1
+sed "s,.*/$dev:,$dev:," t >> out || fail=1
-Model: (file)
-Disk .../$dev: 65.5kB
-Sector size (logical/physical): 512B/512B
-Partition Table: gpt
+# set the new bios_grub attribute
+parted -m -s $dev set 1 bios_grub on || fail=1
-Number Start End Size File system Name Flags
- 1 17.4kB 48.6kB 31.2kB primary bios_grub
+# print the table after modification
+parted -m -s $dev unit s print > t 2>&1 || fail=1
+sed "s,.*/$dev:,$dev:," t >> out || fail=1
+gen_exp()
+{
+ cat <<EOF
+BYT;
+$dev:${N}s:file:$ss:$ss:gpt::;
+1:${start_sector}s:${end_sector}s:${part_sectors}s::name1:;
+BYT;
+$dev:${N}s:file:$ss:$ss:gpt::;
+1:${start_sector}s:${end_sector}s:${part_sectors}s::name1:bios_grub;
EOF
-} > exp || fail=1
-
-test_expect_success \
- 'prepare actual and expected output' \
- 'test $fail = 0 &&
- mv out o2 && sed "s,^Disk .*/$dev:,Disk .../$dev:," o2 > out'
+}
-test_expect_success 'check for expected output' '$compare out exp'
+# check for expected output
+gen_exp > exp || fail=1
+compare exp out || fail=1
-test_done
+Exit $fail