Statistics
| Branch: | Tag: | Revision:

root / snf-image-helper / tasks / 10FixPartitionTable.in @ 1f6fc658

History | View | Annotate | Download (5.3 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
            # This is needed because logical partitions need to have at least
112
            # 1 sector gap between them. We make the gap 2048 sectors to
113
            # properly align them.
114
            new_end=$((new_end - 2048))
115
            enlarge_partition "$SNF_IMAGE_DEV" "$extended" "extended" "${swap_end}s"
116
        else
117
            enlarge_partition "$SNF_IMAGE_DEV" "$extended" "extended" "${new_end}s"
118
        fi
119
    fi
120
elif [ $(is_extended_partition "$SNF_IMAGE_DEV" "$last_part_id") = "yes" ]; then
121
    last_part_type="extended"
122
    extended="$last_part"
123
    if [ $swap_num -ge 5]; then
124
        new_end=$swap_end
125
    fi
126
else
127
    last_part_type="primary"
128
    if [ $swap_num -ge 5 ]; then
129
        # This is needed because the swap partition should be added inside a
130
        # new extended partition. In order to align the swap partition, we
131
        # need to create some extra space between the (aligned) primary and
132
        # the swap.
133
        new_end=$((new_end - 2048))
134
    fi
135
fi
136

    
137
enlarge_partition "$SNF_IMAGE_DEV" "$last_part" "$last_part_type" "${new_end}s"
138

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

    
155
# Inform the kernel about the changes
156
partprobe "$SNF_IMAGE_DEV"
157

    
158
exit 0
159

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