2 # test gdisk and sgdisk by creating a dd file
3 # Copyright (C) 2011 Guillaume Delacour <gui@iroqwa.org>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 # Requires: coreutils (mktemp, dd) and 64M of disk space in /tmp (temp dd disk)
22 # This script test gdisk commands through the following scenario:
23 # - Initialize a new GPT table
24 # - Create a single Linux partition
25 # - Change name of partition
26 # - Change type of partition
27 # - Backup to file the GPT table
28 # - Delete the single partition
29 # - Restore from backup file the GPT table
30 # - Wipe the GPT table
33 # Try to generate a wrong GPT table to detect problems (test --verify)
34 # Create MBR partition table with fdisk and migrate it with gdisk
47 # temp disk for testing gdisk
52 # the test partition to create
54 TEST_PART_DEFAULT_NAME="Linux filesystem"
56 # newname for the partition
57 TEST_PART_NEWNAME=$(tr -dc "[:alpha:]" < /dev/urandom | head -c 8)
58 # and new type (swap for example)
59 TEST_PART_NEWTYPE="8200"
61 # GPT data backup to filename
62 GPT_BACKUP_FILENAME=$(mktemp)
64 # Pretty print string (Red if FAILED or green if SUCCESS)
65 # $1: string to pretty print
67 if [ "$1" = "SUCCESS" ]
76 printf "\033[0;${color}m**$1**\033[m $2\n"
79 # Verify that the partition exist and has the given type/name
80 # $1: Partition type to verify (ex.: 8300)
81 # $2: Partition name to verify (ex.: Linux filesystem)
84 partition=$($GDISK_BIN -l $TEMP_DISK | tail -n 1)
85 echo $partition | grep -q "$1[[:space:]]$2$"
89 pretty_print "SUCCESS" "$3"
91 pretty_print "FAILED" "$3"
97 #####################################
99 #####################################
101 DISK_GUID=$($GDISK_BIN -l $TEMP_DISK | grep "^Disk identifier (GUID):" | awk '{print $4}')
106 #####################################
107 # Create a new empty table
108 #####################################
112 $GDISK_BIN $TEMP_DISK << EOF
120 $SGDISK_BIN $TEMP_DISK -${OPT_CLEAR}
124 # verify that the table is empty
125 # only the columns should appear in the table
126 verify_part "Code" "Name" "Create new empty GPT table"
132 #####################################
133 # First create a new partition
134 #####################################
138 $GDISK_BIN $TEMP_DISK << EOF
150 $SGDISK_BIN $TEMP_DISK -${OPT_NEW}=1 -${OPT_CHANGE_NAME}=1:"${TEST_PART_DEFAULT_NAME}"
154 verify_part "$TEST_PART_TYPE" "$TEST_PART_DEFAULT_NAME" "Create new partition"
159 #####################################
160 # Change name of partition
161 #####################################
162 change_partition_name() {
165 $GDISK_BIN $TEMP_DISK << EOF
174 $SGDISK_BIN $TEMP_DISK -${OPT_CHANGE_NAME}=1:${TEST_PART_NEWNAME}
178 verify_part "$TEST_PART_TYPE" "$TEST_PART_NEWNAME" "Change partition 1 name ($TEST_PART_DEFAULT_NAME -> $TEST_PART_NEWNAME)"
183 change_partition_type() {
184 #####################################
185 # Change type of partition
186 #####################################
189 $GDISK_BIN $TEMP_DISK << EOF
198 $SGDISK_BIN $TEMP_DISK -${OPT_CHANGE_TYPE}=1:${TEST_PART_NEWTYPE}
202 verify_part "$TEST_PART_NEWTYPE" "$TEST_PART_NEWNAME" "Change partition 1 type ($TEST_PART_TYPE -> $TEST_PART_NEWTYPE)"
207 #####################################
208 # Backup GPT data to file
209 #####################################
213 $GDISK_BIN $TEMP_DISK << EOF
222 $SGDISK_BIN $TEMP_DISK -${OPT_BACKUP}=${GPT_BACKUP_FILENAME}
226 # if exist and not empty; we will test it after
227 if [ -s $GPT_BACKUP_FILENAME ]
229 pretty_print "SUCCESS" "GPT data backuped sucessfully"
231 pretty_print "FAILED" "Unable to create GPT backup file !"
237 #####################################
238 # Now, we can delete the partition
239 #####################################
243 $GDISK_BIN $TEMP_DISK << EOF
251 $SGDISK_BIN $TEMP_DISK -${OPT_DELETE}=1
255 # verify that the table is empty (just one partition):
256 # only the columns should appear in the table
257 verify_part "Code" "Name" "Delete partition 1"
262 #####################################
264 #####################################
266 $GDISK_BIN $TEMP_DISK << EOF
275 verify_part "$TEST_PART_NEWTYPE" "$TEST_PART_NEWNAME" "Restore the GPT backup"
280 #####################################
282 #####################################
285 # get UID of disk before changing it
291 $GDISK_BIN $TEMP_DISK << EOF
301 $SGDISK_BIN $TEMP_DISK -U=R
305 # get GUID after change
306 NEW_DISK_GUID=get_diskguid
309 if [ "$DISK_GUID" != "$NEW_DISK_GUID" ]
311 pretty_print "SUCCESS" "GUID of disk has been sucessfully changed"
313 pretty_print "FAILED" "GUID of disk is the same as the previous one"
317 #####################################
319 #####################################
323 $GDISK_BIN $TEMP_DISK << EOF
332 $SGDISK_BIN $TEMP_DISK -${OPT_ZAP}
335 # verify that the table is empty (just one partition):
336 # only the columns should appear in the table
337 verify_part "Code" "Name" "Wipe GPT table"
341 #####################################
343 #####################################
345 $SGDISK_BIN $TEMP_DISK << EOF
348 pretty_print "SUCCESS" "EOF successfully exit gdisk"
351 ###################################
353 ###################################
355 # create a file to simulate a real device
356 dd if=/dev/zero of=$TEMP_DISK bs=1024 count=$TEMP_DISK_SIZE
360 pretty_print "SUCCESS" "Temp disk sucessfully created"
362 pretty_print "FAILED" "Unable to create temp disk !"
366 # test gdisk and sgdisk
367 for binary in gdisk sgdisk
370 printf "\033[0;34m**Testing $binary binary**\033[m\n"
372 create_table "$binary"
373 create_partition "$binary"
374 change_partition_name "$binary"
375 change_partition_type "$binary"
376 backup_table "$binary"
377 delete_partition "$binary"
378 restore_table # only with gdisk
379 change_disk_uid "$binary"
381 eof_stdin # only with gdisk
385 rm -f $TEMP_DISK $GPT_BACKUP_FILENAME