blob: 97f540ce57f1e8bb42449e7dc9f78e4286b1ccfd [file] [log] [blame]
Bill Richardsonf1372d92010-06-11 09:15:55 -07001#!/bin/bash -eu
2
3# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6#
Bill Richardson263ffdf2012-08-24 14:39:21 -07007# Run tests for cgpt utility.
Bill Richardsonf1372d92010-06-11 09:15:55 -07008
9# Load common constants and variables.
10. "$(dirname "$0")/common.sh"
11
Bill Richardson263ffdf2012-08-24 14:39:21 -070012CGPT=$(readlink -f "$1")
13[ -x "$CGPT" ] || error "Can't execute $CGPT"
Bill Richardson793e1b42010-08-20 07:58:43 -070014
15# Run tests in a dedicated directory for easy cleanup or debugging.
16DIR="${TEST_DIR}/cgpt_test_dir"
17[ -d "$DIR" ] || mkdir -p "$DIR"
Bill Richardson263ffdf2012-08-24 14:39:21 -070018warning "testing $CGPT in $DIR"
Bill Richardson793e1b42010-08-20 07:58:43 -070019cd "$DIR"
Bill Richardsonf1372d92010-06-11 09:15:55 -070020
21echo "Create an empty file to use as the device..."
22NUM_SECTORS=1000
Bill Richardson793e1b42010-08-20 07:58:43 -070023DEV=fake_dev.bin
Bill Richardson23429d32012-04-30 11:33:13 -070024rm -f ${DEV}
Bill Richardsonf1372d92010-06-11 09:15:55 -070025dd if=/dev/zero of=${DEV} conv=notrunc bs=512 count=${NUM_SECTORS} 2>/dev/null
Bill Richardsonf1372d92010-06-11 09:15:55 -070026
27
28echo "Create a bunch of partitions, using the real GUID types..."
29DATA_START=100
30DATA_SIZE=20
31DATA_LABEL="data stuff"
32DATA_GUID='ebd0a0a2-b9e5-4433-87c0-68b6b72699c7'
33DATA_NUM=1
34
35KERN_START=200
36KERN_SIZE=30
37KERN_LABEL="kernel stuff"
38KERN_GUID='fe3a2a5d-4f32-41a7-b725-accc3285a309'
39KERN_NUM=2
40
41ROOTFS_START=300
42ROOTFS_SIZE=40
43ROOTFS_LABEL="rootfs stuff"
44ROOTFS_GUID='3cb8e202-3b7e-47dd-8a3c-7ff2a13cfcec'
45ROOTFS_NUM=3
46
47ESP_START=400
48ESP_SIZE=50
49ESP_LABEL="ESP stuff"
50ESP_GUID='c12a7328-f81f-11d2-ba4b-00a0c93ec93b'
51ESP_NUM=4
52
53FUTURE_START=500
54FUTURE_SIZE=60
55FUTURE_LABEL="future stuff"
56FUTURE_GUID='2e0a753d-9e48-43b0-8337-b15192cb1b5e'
57FUTURE_NUM=5
58
59RANDOM_START=600
60RANDOM_SIZE=70
61RANDOM_LABEL="random stuff"
62RANDOM_GUID='2364a860-bf63-42fb-a83d-9ad3e057fcf5'
63RANDOM_NUM=6
64
Bill Richardson263ffdf2012-08-24 14:39:21 -070065$CGPT create ${DEV}
Bill Richardsonf1372d92010-06-11 09:15:55 -070066
Bill Richardson263ffdf2012-08-24 14:39:21 -070067$CGPT add -b ${DATA_START} -s ${DATA_SIZE} -t ${DATA_GUID} \
Bill Richardsonf1372d92010-06-11 09:15:55 -070068 -l "${DATA_LABEL}" ${DEV}
Bill Richardson263ffdf2012-08-24 14:39:21 -070069$CGPT add -b ${KERN_START} -s ${KERN_SIZE} -t ${KERN_GUID} \
Bill Richardsonf1372d92010-06-11 09:15:55 -070070 -l "${KERN_LABEL}" ${DEV}
Bill Richardson263ffdf2012-08-24 14:39:21 -070071$CGPT add -b ${ROOTFS_START} -s ${ROOTFS_SIZE} -t ${ROOTFS_GUID} \
Bill Richardsonf1372d92010-06-11 09:15:55 -070072 -l "${ROOTFS_LABEL}" ${DEV}
Bill Richardson263ffdf2012-08-24 14:39:21 -070073$CGPT add -b ${ESP_START} -s ${ESP_SIZE} -t ${ESP_GUID} \
Bill Richardsonf1372d92010-06-11 09:15:55 -070074 -l "${ESP_LABEL}" ${DEV}
Bill Richardson263ffdf2012-08-24 14:39:21 -070075$CGPT add -b ${FUTURE_START} -s ${FUTURE_SIZE} -t ${FUTURE_GUID} \
Bill Richardsonf1372d92010-06-11 09:15:55 -070076 -l "${FUTURE_LABEL}" ${DEV}
Bill Richardson263ffdf2012-08-24 14:39:21 -070077$CGPT add -b ${RANDOM_START} -s ${RANDOM_SIZE} -t ${RANDOM_GUID} \
Bill Richardsonf1372d92010-06-11 09:15:55 -070078 -l "${RANDOM_LABEL}" ${DEV}
79
80
81echo "Extract the start and size of given partitions..."
82
Bill Richardson263ffdf2012-08-24 14:39:21 -070083X=$($CGPT show -b -i $DATA_NUM ${DEV})
84Y=$($CGPT show -s -i $DATA_NUM ${DEV})
Bill Richardson3430b322010-11-29 14:24:51 -080085[ "$X $Y" = "$DATA_START $DATA_SIZE" ] || error
Bill Richardsonf1372d92010-06-11 09:15:55 -070086
Bill Richardson263ffdf2012-08-24 14:39:21 -070087X=$($CGPT show -b -i $KERN_NUM ${DEV})
88Y=$($CGPT show -s -i $KERN_NUM ${DEV})
Bill Richardson3430b322010-11-29 14:24:51 -080089[ "$X $Y" = "$KERN_START $KERN_SIZE" ] || error
Bill Richardsonf1372d92010-06-11 09:15:55 -070090
Bill Richardson263ffdf2012-08-24 14:39:21 -070091X=$($CGPT show -b -i $ROOTFS_NUM ${DEV})
92Y=$($CGPT show -s -i $ROOTFS_NUM ${DEV})
Bill Richardson3430b322010-11-29 14:24:51 -080093[ "$X $Y" = "$ROOTFS_START $ROOTFS_SIZE" ] || error
Bill Richardsonf1372d92010-06-11 09:15:55 -070094
Bill Richardson263ffdf2012-08-24 14:39:21 -070095X=$($CGPT show -b -i $ESP_NUM ${DEV})
96Y=$($CGPT show -s -i $ESP_NUM ${DEV})
Bill Richardson3430b322010-11-29 14:24:51 -080097[ "$X $Y" = "$ESP_START $ESP_SIZE" ] || error
Bill Richardsonf1372d92010-06-11 09:15:55 -070098
Bill Richardson263ffdf2012-08-24 14:39:21 -070099X=$($CGPT show -b -i $FUTURE_NUM ${DEV})
100Y=$($CGPT show -s -i $FUTURE_NUM ${DEV})
Bill Richardson3430b322010-11-29 14:24:51 -0800101[ "$X $Y" = "$FUTURE_START $FUTURE_SIZE" ] || error
Bill Richardsonf1372d92010-06-11 09:15:55 -0700102
Bill Richardson263ffdf2012-08-24 14:39:21 -0700103X=$($CGPT show -b -i $RANDOM_NUM ${DEV})
104Y=$($CGPT show -s -i $RANDOM_NUM ${DEV})
Bill Richardson3430b322010-11-29 14:24:51 -0800105[ "$X $Y" = "$RANDOM_START $RANDOM_SIZE" ] || error
Bill Richardsonf1372d92010-06-11 09:15:55 -0700106
107
108echo "Set the boot partition.."
Bill Richardson263ffdf2012-08-24 14:39:21 -0700109$CGPT boot -i ${KERN_NUM} ${DEV} >/dev/null
Bill Richardsonf1372d92010-06-11 09:15:55 -0700110
111echo "Check the PMBR's idea of the boot partition..."
Bill Richardson263ffdf2012-08-24 14:39:21 -0700112X=$($CGPT boot ${DEV})
113Y=$($CGPT show -u -i $KERN_NUM $DEV)
Bill Richardson3430b322010-11-29 14:24:51 -0800114[ "$X" = "$Y" ] || error
115
116
117echo "Test the cgpt prioritize command..."
118
119# Input: sequence of priorities
120# Output: ${DEV} has kernel partitions with the given priorities
121make_pri() {
122 local idx=0
Bill Richardson263ffdf2012-08-24 14:39:21 -0700123 $CGPT create ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800124 for pri in "$@"; do
125 idx=$((idx+1))
Bill Richardson263ffdf2012-08-24 14:39:21 -0700126 $CGPT add -t kernel -l "kern$idx" -b $((100 + 2 * $idx)) -s 1 -P $pri ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800127 done
128}
129
130# Output: returns string containing priorities of all kernels
131get_pri() {
132 echo $(
Bill Richardson263ffdf2012-08-24 14:39:21 -0700133 for idx in $($CGPT find -t kernel ${DEV} | sed -e s@${DEV}@@); do
134 $CGPT show -i $idx -P ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800135 done
136 )
137}
138
139# Input: list of priorities
140# Operation: expects ${DEV} to contain those kernel priorities
141assert_pri() {
142 local expected="$*"
143 local actual=$(get_pri)
144 [ "$actual" = "$expected" ] || \
145 error 1 "expected priority \"$expected\", actual priority \"$actual\""
146}
147
148
149# no kernels at all. This should do nothing.
Bill Richardson263ffdf2012-08-24 14:39:21 -0700150$CGPT create ${DEV}
151$CGPT add -t rootfs -b 100 -s 1 ${DEV}
152$CGPT prioritize ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800153assert_pri ""
154
155# common install/upgrade sequence
156make_pri 2 0 0
Bill Richardson263ffdf2012-08-24 14:39:21 -0700157$CGPT prioritize -i 1 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800158assert_pri 1 0 0
Bill Richardson263ffdf2012-08-24 14:39:21 -0700159$CGPT prioritize -i 2 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800160assert_pri 1 2 0
Bill Richardson263ffdf2012-08-24 14:39:21 -0700161$CGPT prioritize -i 1 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800162assert_pri 2 1 0
Bill Richardson263ffdf2012-08-24 14:39:21 -0700163$CGPT prioritize -i 2 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800164assert_pri 1 2 0
165
166# lots of kernels, all same starting priority, should go to priority 1
167make_pri 8 8 8 8 8 8 8 8 8 8 8 0 0 8
Bill Richardson263ffdf2012-08-24 14:39:21 -0700168$CGPT prioritize ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800169assert_pri 1 1 1 1 1 1 1 1 1 1 1 0 0 1
170
171# now raise them all up again
Bill Richardson263ffdf2012-08-24 14:39:21 -0700172$CGPT prioritize -P 4 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800173assert_pri 4 4 4 4 4 4 4 4 4 4 4 0 0 4
174
175# set one of them higher, should leave the rest alone
Bill Richardson263ffdf2012-08-24 14:39:21 -0700176$CGPT prioritize -P 5 -i 3 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800177assert_pri 4 4 5 4 4 4 4 4 4 4 4 0 0 4
178
179# set one of them lower, should bring the rest down
Bill Richardson263ffdf2012-08-24 14:39:21 -0700180$CGPT prioritize -P 3 -i 4 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800181assert_pri 1 1 2 3 1 1 1 1 1 1 1 0 0 1
182
183# raise a group by including the friends of one partition
Bill Richardson263ffdf2012-08-24 14:39:21 -0700184$CGPT prioritize -P 6 -i 1 -f ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800185assert_pri 6 6 4 5 6 6 6 6 6 6 6 0 0 6
186
187# resurrect one, should not affect the others
188make_pri 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Bill Richardson263ffdf2012-08-24 14:39:21 -0700189$CGPT prioritize -i 2 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800190assert_pri 0 1 0 0 0 0 0 0 0 0 0 0 0 0
191
192# resurrect one and all its friends
193make_pri 0 0 0 0 0 0 0 0 1 2 0 0 0 0
Bill Richardson263ffdf2012-08-24 14:39:21 -0700194$CGPT prioritize -P 5 -i 2 -f ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800195assert_pri 5 5 5 5 5 5 5 5 3 4 5 5 5 5
196
197# no options should maintain the same order
Bill Richardson263ffdf2012-08-24 14:39:21 -0700198$CGPT prioritize ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800199assert_pri 3 3 3 3 3 3 3 3 1 2 3 3 3 3
200
201# squish all the ranks
202make_pri 1 1 2 2 3 3 4 4 5 5 0 6 7 7
Bill Richardson263ffdf2012-08-24 14:39:21 -0700203$CGPT prioritize -P 6 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800204assert_pri 1 1 1 1 2 2 3 3 4 4 0 5 6 6
205
206# squish the ranks by not leaving room
207make_pri 1 1 2 2 3 3 4 4 5 5 0 6 7 7
Bill Richardson263ffdf2012-08-24 14:39:21 -0700208$CGPT prioritize -P 7 -i 3 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800209assert_pri 1 1 7 1 2 2 3 3 4 4 0 5 6 6
210
211# squish the ranks while bringing the friends along
212make_pri 1 1 2 2 3 3 4 4 5 5 0 6 7 7
Bill Richardson263ffdf2012-08-24 14:39:21 -0700213$CGPT prioritize -P 6 -i 3 -f ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800214assert_pri 1 1 6 6 1 1 2 2 3 3 0 4 5 5
215
216# squish them pretty hard
217make_pri 1 1 2 2 3 3 4 4 5 5 0 6 7 7
Bill Richardson263ffdf2012-08-24 14:39:21 -0700218$CGPT prioritize -P 2 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800219assert_pri 1 1 1 1 1 1 1 1 1 1 0 1 2 2
220
221# squish them really really hard (nobody gets reduced to zero, though)
222make_pri 1 1 2 2 3 3 4 4 5 5 0 6 7 7
Bill Richardson263ffdf2012-08-24 14:39:21 -0700223$CGPT prioritize -P 1 -i 3 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800224assert_pri 1 1 1 1 1 1 1 1 1 1 0 1 1 1
225
226# squish if we try to go too high
227make_pri 15 15 14 14 13 13 12 12 11 11 10 10 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 0
Bill Richardson263ffdf2012-08-24 14:39:21 -0700228$CGPT prioritize -i 3 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800229assert_pri 14 14 15 13 12 12 11 11 10 10 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 1 1 0
Bill Richardson263ffdf2012-08-24 14:39:21 -0700230$CGPT prioritize -i 5 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800231assert_pri 13 13 14 12 15 11 10 10 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 1 1 1 1 0
232# but if I bring friends I don't have to squish
Bill Richardson263ffdf2012-08-24 14:39:21 -0700233$CGPT prioritize -i 1 -f ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800234assert_pri 15 15 13 12 14 11 10 10 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 1 1 1 1 0
Bill Richardsonf1372d92010-06-11 09:15:55 -0700235
Bill Richardson23429d32012-04-30 11:33:13 -0700236
237# Now make sure that we don't need write access if we're just looking.
238echo "Test read vs read-write access..."
239chmod 0444 ${DEV}
240
241# These should fail
Bill Richardson263ffdf2012-08-24 14:39:21 -0700242$CGPT create -z ${DEV} 2>/dev/null && error
243$CGPT add -i 2 -P 3 ${DEV} 2>/dev/null && error
244$CGPT repair ${DEV} 2>/dev/null && error
245$CGPT prioritize -i 3 ${DEV} 2>/dev/null && error
Bill Richardson23429d32012-04-30 11:33:13 -0700246
247# Most 'boot' usage should fail too.
Bill Richardson263ffdf2012-08-24 14:39:21 -0700248$CGPT boot -p ${DEV} 2>/dev/null && error
Bill Richardson23429d32012-04-30 11:33:13 -0700249dd if=/dev/zero of=fake_mbr.bin bs=100 count=1 2>/dev/null
Bill Richardson263ffdf2012-08-24 14:39:21 -0700250$CGPT boot -b fake_mbr.bin ${DEV} 2>/dev/null && error
251$CGPT boot -i 2 ${DEV} 2>/dev/null && error
Bill Richardson23429d32012-04-30 11:33:13 -0700252
253# These should pass
Bill Richardson263ffdf2012-08-24 14:39:21 -0700254$CGPT boot ${DEV} >/dev/null
255$CGPT show ${DEV} >/dev/null
256$CGPT find -t kernel ${DEV} >/dev/null
Bill Richardson23429d32012-04-30 11:33:13 -0700257
Bill Richardsonf1372d92010-06-11 09:15:55 -0700258echo "Done."
259
260happy "All tests passed."