Fix various bugs in snf-image-helper
[snf-image] / snf-image-helper / tasks / 10FixPartitionTable.in
1 #! /bin/bash
2
3 ### BEGIN TASK INFO
4 # Provides:             FixPartitionTable
5 # RunBefore:            FilesystemResizeUnmounted
6 # Short-Description:    Resize filesystem to use all the available space
7 ### END TASK INFO
8
9 set -e
10 . "@commondir@/common.sh"
11
12 if [ ! -b "$SNF_IMAGE_DEV" ]; then
13     log_error "Device file:\`${SNF_IMAGE_DEV}' is not a block device"
14 fi
15
16 if [ $(get_partition_count "$SNF_IMAGE_DEV") -eq 0 ]; then
17     log_error "Device: \`${SNF_IMAGE_DEV}' does not contain any partition"
18 fi
19
20 retval=$(get_last_partition "$SNF_IMAGE_DEV")
21
22 id=$(echo $retval | cut -d: -f1)
23 pstart=$(echo $retval | cut -d: -f2)
24 pend=$(echo $retval | cut -d: -f3)
25 ptype=$(echo $retval | cut -d: -f5)
26
27 if [ $id -gt 4 ]; then
28     log_error "We don't support logical volumes"
29 fi
30
31 if [ x"$ptype" = "x" ]; then
32     # Don't know how to handle this
33     warn "Last partition with id: \`$id' is empty or has unknown filesystem"
34     warn "I won't resize the partition"
35     exit 0
36 fi
37
38 new_pend=$(get_last_free_sector "$SNF_IMAGE_DEV")
39
40 if [ -z "$new_pend" ] ; then
41     # Nothing to do
42     exit 0
43 fi
44
45 # Extend the partition
46
47 $PARTED -s -m "$SNF_IMAGE_DEV" rm "$id"
48 $PARTED -s -m "$SNF_IMAGE_DEV" mkpart primary "$ptype" "$pstart" "$new_pend"
49
50 # Inform the kernel about the changes
51 partprobe "$SNF_IMAGE_DEV"
52
53 exit 0
54
55 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :