root / snf-image-helper / tasks / 10FixPartitionTable.in @ 21be5a41
History | View | Annotate | Download (5 kB)
1 |
#! /bin/bash |
---|---|
2 |
|
3 |
# Copyright (C) 2011 GRNET S.A. |
4 |
# |
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. |
9 |
# |
10 |
# This program is distributed in the hope that it will be useful, but |
11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 |
# General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License |
16 |
# along with this program; if not, write to the Free Software |
17 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
18 |
# 02110-1301, USA. |
19 |
|
20 |
### BEGIN TASK INFO |
21 |
# Provides: FixPartitionTable |
22 |
# RunBefore: FilesystemResizeUnmounted |
23 |
# Short-Description: Enlarge last partition to use all the available space |
24 |
### END TASK INFO |
25 |
|
26 |
set -e |
27 |
. "@commondir@/common.sh" |
28 |
|
29 |
# Check if the task should be prevented from running. |
30 |
check_if_excluded |
31 |
|
32 |
if [ ! -b "$SNF_IMAGE_DEV" ]; then |
33 |
log_error "Device file:\`${SNF_IMAGE_DEV}' is not a block device" |
34 |
fi |
35 |
|
36 |
table=$(get_partition_table "$SNF_IMAGE_DEV") |
37 |
|
38 |
if [ $(get_partition_count "$table") -eq 0 ]; then |
39 |
log_error "Device: \`${SNF_IMAGE_DEV}' does not contain any partition" |
40 |
fi |
41 |
|
42 |
table_type=$(get_partition_table_type "$table") |
43 |
|
44 |
if [ "$table_type" == "gpt" ]; then |
45 |
"@scriptsdir@/fix_gpt.py" "$SNF_IMAGE_DEV" $($BLOCKDEV --getsz "$SNF_IMAGE_DEV") |
46 |
elif [ "$table_type" != "msdos" ]; then |
47 |
log_error "Device: \'${SNF_IMAGE_DEV}' contains unsupported partition " |
48 |
"table type: \`$table_type'. Only msdos & gpt partitions are supported." |
49 |
fi |
50 |
|
51 |
last_part=$(get_last_partition "$table") |
52 |
last_part_id=$(cut -d':' -f1 <<< "$last_part") |
53 |
|
54 |
# Check if swap is defined... |
55 |
if [ -n "$SNF_IMAGE_PROPERTY_SWAP" ]; then |
56 |
if [[ "$SNF_IMAGE_PROPERTY_SWAP" =~ ^([0-9]+):([0-9]+)$ ]]; then |
57 |
swap_num=${BASH_REMATCH[1]} |
58 |
swap_size=${BASH_REMATCH[2]} |
59 |
swap_unit="MB" |
60 |
else |
61 |
log_error "SWAP property \`$SNF_IMAGE_PROPERTY_SWAP' is not valid" |
62 |
fi |
63 |
fi |
64 |
|
65 |
if [ -z "$swap_num" ]; then |
66 |
swap_num=0 |
67 |
fi |
68 |
|
69 |
# Most partition setups leave 2048s in the end. For GPT partitions you need at |
70 |
# least 34s for the secondary GPT header. |
71 |
new_end="-2049" |
72 |
|
73 |
if [ $swap_num -ne 0 ]; then |
74 |
free=$(get_last_free_sector "$SNF_IMAGE_DEV" "$swap_unit") |
75 |
free_size=$(cut -d: -f4 <<< "$free") |
76 |
free_size_val=${free_size/$swap_unit/} |
77 |
if [ $free_size_val -le $swap_size ]; then |
78 |
log_error "Not enough space for swap partition" |
79 |
fi |
80 |
|
81 |
swap_end="$new_end" |
82 |
|
83 |
swap_start=$((new_end - (swap_size * 2048) + 1)) # in sectors |
84 |
new_end=$((swap_start - 1)) |
85 |
fi |
86 |
|
87 |
extended="" |
88 |
|
89 |
if [ "$table_type" != "msdos" ]; then |
90 |
# Primary, extended and logical partitions is a concept for msdos partition |
91 |
# tables. Parted's mkpart will use part-type as partition name if applied |
92 |
# on a gpt table and leaving this empty is fragile: For a strange reason |
93 |
# for swap partitions the command fails (???) |
94 |
last_part_type="primary" |
95 |
elif [ $last_part_id -gt 4 ]; then |
96 |
last_part_type="logical" |
97 |
extended=$(get_extended_partition "$table") |
98 |
last_primary=$(get_last_primary_partition "$table") |
99 |
|
100 |
ext_id=$(cut -d':' -f1 <<< "$extended") |
101 |
last_prim_id=$(cut -d':' -f1 <<< "$last_primary") |
102 |
|
103 |
if [ "$ext_id" != "$last_prim_id" ]; then |
104 |
# Mark last primary as the last partition |
105 |
last_part="$extended" |
106 |
last_part_id="$ext_id" |
107 |
last_part_type="primary" |
108 |
else |
109 |
# Enlarge the extended partition |
110 |
if [ $swap_num -ge 5 ]; then |
111 |
enlarge_partition "$SNF_IMAGE_DEV" "$extended" "extended" "${swap_end}s" |
112 |
else |
113 |
enlarge_partition "$SNF_IMAGE_DEV" "$extended" "extended" "${new_end}s" |
114 |
fi |
115 |
fi |
116 |
elif [ $(is_extended_partition "$SNF_IMAGE_DEV" "$last_part_id") = "yes" ]; then |
117 |
last_part_type="extended" |
118 |
extended="$last_part" |
119 |
if [ $swap_num -ge 5]; then |
120 |
new_end=$swap_end |
121 |
fi |
122 |
else |
123 |
last_part_type="primary" |
124 |
if [ $swap_num -ge 5 ]; then |
125 |
# This is needed because the swap partition should be added inside a |
126 |
# new extended partition. In order to align the swap partition, we |
127 |
# need to create some extra space between the (aligned) primary and |
128 |
# the swap. |
129 |
new_end=$((new_end - 2048)) |
130 |
fi |
131 |
fi |
132 |
|
133 |
enlarge_partition "$SNF_IMAGE_DEV" "$last_part" "$last_part_type" "${new_end}s" |
134 |
|
135 |
if [ $swap_num -gt 0 ]; then |
136 |
swap_part="$swap_num:${swap_start}s:${swap_end}s:0:linux-swap(v1)::;" |
137 |
if [ "$table_type" != "msdos" ]; then |
138 |
swap_ptype="swap" # in gpt this is used as a partition name |
139 |
elif [ $swap_num -ge 5 ]; then |
140 |
if [ -z "$extended" ]; then |
141 |
extended="0:$((swap_start - 2))s:${swap_end}s:0:::;" |
142 |
create_partition "$SNF_IMAGE_DEV" "$extended" "extended" |
143 |
fi |
144 |
swap_ptype="logical" |
145 |
else |
146 |
swap_ptype="primary" |
147 |
fi |
148 |
create_partition "$SNF_IMAGE_DEV" "$swap_part" "$swap_ptype" |
149 |
fi |
150 |
|
151 |
# Inform the kernel about the changes |
152 |
partprobe "$SNF_IMAGE_DEV" |
153 |
|
154 |
exit 0 |
155 |
|
156 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : |