Statistics
| Branch: | Tag: | Revision:

root / snf-image-helper / tasks / 10FixPartitionTable.in @ 01da0f7d

History | View | Annotate | Download (4.8 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" != "msdos" ]; then # We are planning to add gpt support
45
    log_error "Device: \'${SNF_IMAGE_DEV}' contains unsupported partition "
46
    "table type: \`$table_type'. For now only msdos partitions are supported."
47
fi
48

    
49
last_part=$(get_last_partition "$table")
50
last_part_id=$(cut -d':' -f1 <<< "$last_part")
51

    
52
# Check if swap is defined...
53
if [ -n "$SNF_IMAGE_PROPERTY_SWAP" ]; then
54
    if [[ "$SNF_IMAGE_PROPERTY_SWAP" =~ ^([0-9]+):([0-9]+)$ ]]; then
55
        swap_num=${BASH_REMATCH[1]}
56
        swap_size=${BASH_REMATCH[2]}
57
        swap_unit="MB"
58
    else
59
        log_error "SWAP property \`$SNF_IMAGE_PROPERTY_SWAP' is not valid"
60
    fi
61
fi
62

    
63
if [ -z "$swap_num" ]; then
64
    swap_num=0
65
fi
66

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

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

    
79
    swap_end="$new_end"
80

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

    
85
extended=""
86

    
87
if [ "$table_type" != "msdos" ]; then
88
    # Primary, extended and logical partitions is a concept for msdos partition
89
    # tables. Parted's mkpart will use part-type as partition name if applied
90
    # on a gpt table.
91
    last_part_type=""
92
elif [ $last_part_id -gt 4 ]; then
93
    last_part_type="logical"
94
    extended=$(get_extended_partition "$table")
95
    last_primary=$(get_last_primary_partition "$table")
96

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

    
100
    if [ "$ext_id" != "$last_prim_id" ]; then
101
        # Mark last primary as the last partition
102
        last_part="$extended"
103
        last_part_id="$ext_id"
104
        last_part_type="primary"
105
    else
106
        # Enlarge the extended partition
107
        if [ $swap_num -ge 5 ]; then
108
            enlarge_partition "$SNF_IMAGE_DEV" "$extended" "extended" "${swap_end}s"
109
        else
110
            enlarge_partition "$SNF_IMAGE_DEV" "$extended" "extended" "${new_end}s"
111
        fi
112
    fi
113
elif [ $(is_extended_partition "$SNF_IMAGE_DEV" "$last_part_id") = "yes" ]; then
114
    last_part_type="extended"
115
    extended="$last_part"
116
    if [ $swap_num -ge 5]; then
117
        new_end=$swap_end
118
    fi
119
else
120
    last_part_type="primary"
121
    if [ $swap_num -ge 5 ]; then
122
        # This is needed because the swap partition should be added inside a
123
        # new extended partition. In order to align the swap partition, we
124
        # need to create some extra space between the (aligned) primary and
125
        # the swap.
126
        new_end=$((new_end - 2048))
127
    fi
128
fi
129

    
130
enlarge_partition "$SNF_IMAGE_DEV" "$last_part" "$last_part_type" "${new_end}s"
131

    
132
if [ $swap_num -gt 0 ]; then
133
    swap_part="$swap_num:${swap_start}s:${swap_end}s:0:linux-swap(v1)::;"
134
    if [ "$table_type" != "msdos" ]; then
135
        swap_ptype=""
136
    elif [ $swap_num -ge 5 ]; then
137
        if [ -z "$extended" ]; then
138
            extended="0:$((swap_start - 2))s:${swap_end}s:0:::;"
139
            create_partition "$SNF_IMAGE_DEV" "$extended" "extended"
140
        fi
141
        swap_ptype="logical"
142
    else
143
        swap_ptype="primary"
144
    fi
145
    create_partition "$SNF_IMAGE_DEV" "$swap_part" "$swap_ptype"
146
fi
147

    
148
# Inform the kernel about the changes
149
partprobe "$SNF_IMAGE_DEV"
150

    
151
exit 0
152

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