blob: ffd6f68d3e8128ce7ff8f1d3d02f4c4189b5debb [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#
Gaurav Shah8b7baaf2010-09-07 18:19:21 -07007# Run tests for CGPT.
Bill Richardsonf1372d92010-06-11 09:15:55 -07008
9# Load common constants and variables.
10. "$(dirname "$0")/common.sh"
11
Bill Richardson793e1b42010-08-20 07:58:43 -070012GPT=$(readlink -f "$1")
Bill Richardsonf1372d92010-06-11 09:15:55 -070013[ -x "$GPT" ] || error "Can't execute $GPT"
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"
18warning "testing $GPT in $DIR"
19cd "$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 Richardsonf1372d92010-06-11 09:15:55 -070024dd if=/dev/zero of=${DEV} conv=notrunc bs=512 count=${NUM_SECTORS} 2>/dev/null
Bill Richardsonf1372d92010-06-11 09:15:55 -070025
26
27echo "Create a bunch of partitions, using the real GUID types..."
28DATA_START=100
29DATA_SIZE=20
30DATA_LABEL="data stuff"
31DATA_GUID='ebd0a0a2-b9e5-4433-87c0-68b6b72699c7'
32DATA_NUM=1
33
34KERN_START=200
35KERN_SIZE=30
36KERN_LABEL="kernel stuff"
37KERN_GUID='fe3a2a5d-4f32-41a7-b725-accc3285a309'
38KERN_NUM=2
39
40ROOTFS_START=300
41ROOTFS_SIZE=40
42ROOTFS_LABEL="rootfs stuff"
43ROOTFS_GUID='3cb8e202-3b7e-47dd-8a3c-7ff2a13cfcec'
44ROOTFS_NUM=3
45
46ESP_START=400
47ESP_SIZE=50
48ESP_LABEL="ESP stuff"
49ESP_GUID='c12a7328-f81f-11d2-ba4b-00a0c93ec93b'
50ESP_NUM=4
51
52FUTURE_START=500
53FUTURE_SIZE=60
54FUTURE_LABEL="future stuff"
55FUTURE_GUID='2e0a753d-9e48-43b0-8337-b15192cb1b5e'
56FUTURE_NUM=5
57
58RANDOM_START=600
59RANDOM_SIZE=70
60RANDOM_LABEL="random stuff"
61RANDOM_GUID='2364a860-bf63-42fb-a83d-9ad3e057fcf5'
62RANDOM_NUM=6
63
64$GPT create ${DEV}
65
66$GPT add -b ${DATA_START} -s ${DATA_SIZE} -t ${DATA_GUID} \
67 -l "${DATA_LABEL}" ${DEV}
68$GPT add -b ${KERN_START} -s ${KERN_SIZE} -t ${KERN_GUID} \
69 -l "${KERN_LABEL}" ${DEV}
70$GPT add -b ${ROOTFS_START} -s ${ROOTFS_SIZE} -t ${ROOTFS_GUID} \
71 -l "${ROOTFS_LABEL}" ${DEV}
72$GPT add -b ${ESP_START} -s ${ESP_SIZE} -t ${ESP_GUID} \
73 -l "${ESP_LABEL}" ${DEV}
74$GPT add -b ${FUTURE_START} -s ${FUTURE_SIZE} -t ${FUTURE_GUID} \
75 -l "${FUTURE_LABEL}" ${DEV}
76$GPT add -b ${RANDOM_START} -s ${RANDOM_SIZE} -t ${RANDOM_GUID} \
77 -l "${RANDOM_LABEL}" ${DEV}
78
79
80echo "Extract the start and size of given partitions..."
81
82X=$($GPT show -b -i $DATA_NUM ${DEV})
83Y=$($GPT show -s -i $DATA_NUM ${DEV})
Bill Richardson3430b322010-11-29 14:24:51 -080084[ "$X $Y" = "$DATA_START $DATA_SIZE" ] || error
Bill Richardsonf1372d92010-06-11 09:15:55 -070085
86X=$($GPT show -b -i $KERN_NUM ${DEV})
87Y=$($GPT show -s -i $KERN_NUM ${DEV})
Bill Richardson3430b322010-11-29 14:24:51 -080088[ "$X $Y" = "$KERN_START $KERN_SIZE" ] || error
Bill Richardsonf1372d92010-06-11 09:15:55 -070089
90X=$($GPT show -b -i $ROOTFS_NUM ${DEV})
91Y=$($GPT show -s -i $ROOTFS_NUM ${DEV})
Bill Richardson3430b322010-11-29 14:24:51 -080092[ "$X $Y" = "$ROOTFS_START $ROOTFS_SIZE" ] || error
Bill Richardsonf1372d92010-06-11 09:15:55 -070093
94X=$($GPT show -b -i $ESP_NUM ${DEV})
95Y=$($GPT show -s -i $ESP_NUM ${DEV})
Bill Richardson3430b322010-11-29 14:24:51 -080096[ "$X $Y" = "$ESP_START $ESP_SIZE" ] || error
Bill Richardsonf1372d92010-06-11 09:15:55 -070097
98X=$($GPT show -b -i $FUTURE_NUM ${DEV})
99Y=$($GPT show -s -i $FUTURE_NUM ${DEV})
Bill Richardson3430b322010-11-29 14:24:51 -0800100[ "$X $Y" = "$FUTURE_START $FUTURE_SIZE" ] || error
Bill Richardsonf1372d92010-06-11 09:15:55 -0700101
102X=$($GPT show -b -i $RANDOM_NUM ${DEV})
103Y=$($GPT show -s -i $RANDOM_NUM ${DEV})
Bill Richardson3430b322010-11-29 14:24:51 -0800104[ "$X $Y" = "$RANDOM_START $RANDOM_SIZE" ] || error
Bill Richardsonf1372d92010-06-11 09:15:55 -0700105
106
107echo "Set the boot partition.."
108$GPT boot -i ${KERN_NUM} ${DEV} >/dev/null
109
110echo "Check the PMBR's idea of the boot partition..."
111X=$($GPT boot ${DEV})
112Y=$($GPT show -u -i $KERN_NUM $DEV)
Bill Richardson3430b322010-11-29 14:24:51 -0800113[ "$X" = "$Y" ] || error
114
115
116echo "Test the cgpt prioritize command..."
117
118# Input: sequence of priorities
119# Output: ${DEV} has kernel partitions with the given priorities
120make_pri() {
121 local idx=0
122 $GPT create ${DEV}
123 for pri in "$@"; do
124 idx=$((idx+1))
125 $GPT add -t kernel -l "kern$idx" -b $((100 + 2 * $idx)) -s 1 -P $pri ${DEV}
126 done
127}
128
129# Output: returns string containing priorities of all kernels
130get_pri() {
131 echo $(
132 for idx in $($GPT find -t kernel ${DEV} | sed -e s@${DEV}@@); do
133 $GPT show -i $idx -P ${DEV}
134 done
135 )
136}
137
138# Input: list of priorities
139# Operation: expects ${DEV} to contain those kernel priorities
140assert_pri() {
141 local expected="$*"
142 local actual=$(get_pri)
143 [ "$actual" = "$expected" ] || \
144 error 1 "expected priority \"$expected\", actual priority \"$actual\""
145}
146
147
148# no kernels at all. This should do nothing.
149$GPT create ${DEV}
150$GPT add -t rootfs -b 100 -s 1 ${DEV}
151$GPT prioritize ${DEV}
152assert_pri ""
153
154# common install/upgrade sequence
155make_pri 2 0 0
156$GPT prioritize -i 1 ${DEV}
157assert_pri 1 0 0
158$GPT prioritize -i 2 ${DEV}
159assert_pri 1 2 0
160$GPT prioritize -i 1 ${DEV}
161assert_pri 2 1 0
162$GPT prioritize -i 2 ${DEV}
163assert_pri 1 2 0
164
165# lots of kernels, all same starting priority, should go to priority 1
166make_pri 8 8 8 8 8 8 8 8 8 8 8 0 0 8
167$GPT prioritize ${DEV}
168assert_pri 1 1 1 1 1 1 1 1 1 1 1 0 0 1
169
170# now raise them all up again
171$GPT prioritize -P 4 ${DEV}
172assert_pri 4 4 4 4 4 4 4 4 4 4 4 0 0 4
173
174# set one of them higher, should leave the rest alone
175$GPT prioritize -P 5 -i 3 ${DEV}
176assert_pri 4 4 5 4 4 4 4 4 4 4 4 0 0 4
177
178# set one of them lower, should bring the rest down
179$GPT prioritize -P 3 -i 4 ${DEV}
180assert_pri 1 1 2 3 1 1 1 1 1 1 1 0 0 1
181
182# raise a group by including the friends of one partition
183$GPT prioritize -P 6 -i 1 -f ${DEV}
184assert_pri 6 6 4 5 6 6 6 6 6 6 6 0 0 6
185
186# resurrect one, should not affect the others
187make_pri 0 0 0 0 0 0 0 0 0 0 0 0 0 0
188$GPT prioritize -i 2 ${DEV}
189assert_pri 0 1 0 0 0 0 0 0 0 0 0 0 0 0
190
191# resurrect one and all its friends
192make_pri 0 0 0 0 0 0 0 0 1 2 0 0 0 0
193$GPT prioritize -P 5 -i 2 -f ${DEV}
194assert_pri 5 5 5 5 5 5 5 5 3 4 5 5 5 5
195
196# no options should maintain the same order
197$GPT prioritize ${DEV}
198assert_pri 3 3 3 3 3 3 3 3 1 2 3 3 3 3
199
200# squish all the ranks
201make_pri 1 1 2 2 3 3 4 4 5 5 0 6 7 7
202$GPT prioritize -P 6 ${DEV}
203assert_pri 1 1 1 1 2 2 3 3 4 4 0 5 6 6
204
205# squish the ranks by not leaving room
206make_pri 1 1 2 2 3 3 4 4 5 5 0 6 7 7
207$GPT prioritize -P 7 -i 3 ${DEV}
208assert_pri 1 1 7 1 2 2 3 3 4 4 0 5 6 6
209
210# squish the ranks while bringing the friends along
211make_pri 1 1 2 2 3 3 4 4 5 5 0 6 7 7
212$GPT prioritize -P 6 -i 3 -f ${DEV}
213assert_pri 1 1 6 6 1 1 2 2 3 3 0 4 5 5
214
215# squish them pretty hard
216make_pri 1 1 2 2 3 3 4 4 5 5 0 6 7 7
217$GPT prioritize -P 2 ${DEV}
218assert_pri 1 1 1 1 1 1 1 1 1 1 0 1 2 2
219
220# squish them really really hard (nobody gets reduced to zero, though)
221make_pri 1 1 2 2 3 3 4 4 5 5 0 6 7 7
222$GPT prioritize -P 1 -i 3 ${DEV}
223assert_pri 1 1 1 1 1 1 1 1 1 1 0 1 1 1
224
225# squish if we try to go too high
226make_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
227$GPT prioritize -i 3 ${DEV}
228assert_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
229$GPT prioritize -i 5 ${DEV}
230assert_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
231# but if I bring friends I don't have to squish
232$GPT prioritize -i 1 -f ${DEV}
233assert_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 -0700234
235echo "Done."
236
237happy "All tests passed."