10FixPartitionTable.in 5.31 KB
Newer Older
1
2
#! /bin/bash

3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Copyright (C) 2011 GRNET S.A. 
#
# 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
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

20
21
22
### BEGIN TASK INFO
# Provides:		FixPartitionTable
# RunBefore:		FilesystemResizeUnmounted
23
# Short-Description:	Enlarge last partition to use all the available space
24
25
26
27
28
### END TASK INFO

set -e
. "@commondir@/common.sh"

29
trap task_cleanup EXIT
30
report_task_start
31
32
33
# Check if the task should be prevented from running.
check_if_excluded

34
35
36
37
if [ ! -b "$SNF_IMAGE_DEV" ]; then
    log_error "Device file:\`${SNF_IMAGE_DEV}' is not a block device"
fi

38
table=$(get_partition_table "$SNF_IMAGE_DEV")
39

40
if [ $(get_partition_count "$table") -eq 0 ]; then
41
42
43
    log_error "Device: \`${SNF_IMAGE_DEV}' does not contain any partition"
fi

44
table_type=$(get_partition_table_type "$table")
45

46
47
48
if [ "$table_type" == "gpt" ]; then
    "@scriptsdir@/fix_gpt.py" "$SNF_IMAGE_DEV" $($BLOCKDEV --getsz "$SNF_IMAGE_DEV")
elif [ "$table_type" != "msdos" ]; then
49
    log_error "Device: \'${SNF_IMAGE_DEV}' contains unsupported partition "
50
    "table type: \`$table_type'. Only msdos & gpt partitions are supported."
51
52
fi

53
54
55
last_part=$(get_last_partition "$table")
last_part_id=$(cut -d':' -f1 <<< "$last_part")

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Check if swap is defined...
if [ -n "$SNF_IMAGE_PROPERTY_SWAP" ]; then
    if [[ "$SNF_IMAGE_PROPERTY_SWAP" =~ ^([0-9]+):([0-9]+)$ ]]; then
        swap_num=${BASH_REMATCH[1]}
        swap_size=${BASH_REMATCH[2]}
        swap_unit="MB"
    else
        log_error "SWAP property \`$SNF_IMAGE_PROPERTY_SWAP' is not valid"
    fi
fi

if [ -z "$swap_num" ]; then
    swap_num=0
fi

# Most partition setups leave 2048s in the end. For GPT partitions you need at
# least 34s for the secondary GPT header.
new_end="-2049"

if [ $swap_num -ne 0 ]; then
    free=$(get_last_free_sector "$SNF_IMAGE_DEV" "$swap_unit")
    free_size=$(cut -d: -f4 <<< "$free")
    free_size_val=${free_size/$swap_unit/}
    if [ $free_size_val -le $swap_size ]; then
        log_error "Not enough space for swap partition"
    fi

    swap_end="$new_end"

    swap_start=$((new_end - (swap_size * 2048) + 1)) # in sectors
    new_end=$((swap_start - 1))
fi

extended=""

91
92
93
if [ "$table_type" != "msdos" ]; then
    # Primary, extended and logical partitions is a concept for msdos partition
    # tables. Parted's mkpart will use part-type as partition name if applied
94
95
96
    # on a gpt table and leaving this empty is fragile: For a strange reason
    # for swap partitions the command fails (???)
    last_part_type="primary"
97
98
99
100
101
102
103
104
105
elif [ $last_part_id -gt 4 ]; then
    last_part_type="logical"
    extended=$(get_extended_partition "$table")
    last_primary=$(get_last_primary_partition "$table")

    ext_id=$(cut -d':' -f1 <<< "$extended")
    last_prim_id=$(cut -d':' -f1 <<< "$last_primary")

    if [ "$ext_id" != "$last_prim_id" ]; then
106
        # Mark last primary as the last partition
107
108
109
110
        last_part="$extended"
        last_part_id="$ext_id"
        last_part_type="primary"
    else
111
112
        # Enlarge the extended partition
        if [ $swap_num -ge 5 ]; then
113
114
115
116
            # This is needed because logical partitions need to have at least
            # 1 sector gap between them. We make the gap 2048 sectors to
            # properly align them.
            new_end=$((new_end - 2048))
117
118
119
120
            enlarge_partition "$SNF_IMAGE_DEV" "$extended" "extended" "${swap_end}s"
        else
            enlarge_partition "$SNF_IMAGE_DEV" "$extended" "extended" "${new_end}s"
        fi
121
122
123
    fi
elif [ $(is_extended_partition "$SNF_IMAGE_DEV" "$last_part_id") = "yes" ]; then
    last_part_type="extended"
124
125
126
127
    extended="$last_part"
    if [ $swap_num -ge 5]; then
        new_end=$swap_end
    fi
128
129
else
    last_part_type="primary"
130
131
132
133
134
135
136
    if [ $swap_num -ge 5 ]; then
        # This is needed because the swap partition should be added inside a
        # new extended partition. In order to align the swap partition, we
        # need to create some extra space between the (aligned) primary and
        # the swap.
        new_end=$((new_end - 2048))
    fi
137
138
fi

139
140
141
142
143
enlarge_partition "$SNF_IMAGE_DEV" "$last_part" "$last_part_type" "${new_end}s"

if [ $swap_num -gt 0 ]; then
    swap_part="$swap_num:${swap_start}s:${swap_end}s:0:linux-swap(v1)::;"
    if [ "$table_type" != "msdos" ]; then
144
        swap_ptype="swap" # in gpt this is used as a partition name
145
146
147
148
149
150
151
152
153
154
155
    elif [ $swap_num -ge 5 ]; then
        if [ -z "$extended" ]; then
            extended="0:$((swap_start - 2))s:${swap_end}s:0:::;"
            create_partition "$SNF_IMAGE_DEV" "$extended" "extended"
        fi
        swap_ptype="logical"
    else
        swap_ptype="primary"
    fi
    create_partition "$SNF_IMAGE_DEV" "$swap_part" "$swap_ptype"
fi
156

157
# Inform the kernel about the changes
158
159
160
161
162
partprobe "$SNF_IMAGE_DEV"

exit 0

# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :