10FixPartitionTable.in 6.02 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
if [[ "$SNF_IMAGE_PROPERTY_OSFAMILY" =~ ^(open|net)bsd$ ]]; then
39
40
41
    @scriptsdir@/disklabel.py -d "$($BLOCKDEV --getsz "$SNF_IMAGE_DEV")" -p "$SNF_IMAGE_DEV"
    # Tell the kernel to recreate the disk partitions.
    # We cannot use partprobe to do this because partprobe uses BLKPG ioctl
42
    # and creates the partition device files by itself one by one. Since parted
43
44
45
46
    # does not support disklabels, the partitions with id larger than 4 will
    # not be created.
    # We need to tell the kernel to read the partition table by itself.
    $BLOCKDEV --rereadpt "$SNF_IMAGE_DEV"
47
48
49
    exit 0
fi

50
table=$(get_partition_table "$SNF_IMAGE_DEV")
51

52
if [ $(get_partition_count "$table") -eq 0 ]; then
53
54
55
    log_error "Device: \`${SNF_IMAGE_DEV}' does not contain any partition"
fi

56
table_type=$(get_partition_table_type "$table")
57

58
59
60
61
62
if [ "$SNF_IMAGE_PROPERTY_OSFAMILY" = "freebsd" -a "$table_type" != "gpt" ]; then
    log_error "The image contains a(n) $table_type partition table. " \
        "For FreeBSD images only GUID Partition Tables are supported."
fi

63
if [ "$table_type" == "gpt" ]; then
64
    "$SGDISK" --move-second-header "$SNF_IMAGE_DEV"
65
elif [ "$table_type" != "msdos" ]; then
66
    log_error "Device: \'${SNF_IMAGE_DEV}' contains unsupported partition " \
67
              "table type: \`$table_type'. Only MSDOS & GPT partitions are" \
68
              "supported."
69
70
fi

71
72
73
last_part=$(get_last_partition "$table")
last_part_id=$(cut -d':' -f1 <<< "$last_part")

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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=""

109
if [ "$table_type" != "msdos" ]; then
110
    # For GPT partitions, get the partition GUID code as partition type
111
    last_part_type="$($SGDISK -i "$last_part_id" "$SNF_IMAGE_DEV" | grep "^Partition GUID code:" | cut -d"(" -f1 | cut -d: -f2 | xargs echo)"
112
113
114
115
116
117
118
119
120
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
121
        # Mark last primary as the last partition
122
123
124
125
        last_part="$extended"
        last_part_id="$ext_id"
        last_part_type="primary"
    else
126
127
        # Enlarge the extended partition
        if [ $swap_num -ge 5 ]; then
128
129
130
131
            # 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))
132
133
134
135
            enlarge_partition "$SNF_IMAGE_DEV" "$extended" "extended" "${swap_end}s"
        else
            enlarge_partition "$SNF_IMAGE_DEV" "$extended" "extended" "${new_end}s"
        fi
136
137
138
    fi
elif [ $(is_extended_partition "$SNF_IMAGE_DEV" "$last_part_id") = "yes" ]; then
    last_part_type="extended"
139
140
141
142
    extended="$last_part"
    if [ $swap_num -ge 5]; then
        new_end=$swap_end
    fi
143
144
else
    last_part_type="primary"
145
146
147
148
149
150
151
    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
152
153
fi

154
155
156
157
158
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
159
        swap_ptype="swap" # in GPT this is used as a partition name
160
161
162
163
164
165
166
167
168
169
170
    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
171

172
# Inform the kernel about the changes
173
174
175
176
177
partprobe "$SNF_IMAGE_DEV"

exit 0

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