aboutsummarylogtreecommitdiffstats
path: root/growfs-hook
blob: 19e63e5ee35d92b417e7c4f373a2946da50a112e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/sh

run_hook() {
  echo "GrowRootFS: starting"

  # if a file indicates we should do nothing, then just exit
  for f in /var/lib/cloud/instance/root-grown /etc/growroot-disabled \
      /etc/growroot-grown; do
    [ -f "${rootmnt}$f" ] && echo "GrowRootFS: disabled by file" && return 0
  done

  # figure out what disk '$root' is on
  { [ ! -L "${root}" ] && rootdev=${root} || rootdev=$(readlink -f "${root}") ; } || { echo "failed to get target of link for ${root}" && return 0 ; }

  case "${rootdev}" in
    *[0-9]) : ;;
    # the root is a disk, not a partition (does not end in a digit)
    # no need to do anything in this case, kernel already knows the full size.
    *) echo "GrowRootFS: ${rootdev} is a disk, not a partition, no action" && return 0 ;;
  esac

  # remove all consective numbers from the end of rootdev to get 'rootdisk'
  rootdisk=${rootdev}
  while [ "${rootdisk%[0-9]}" != "${rootdisk}" ]; do
    rootdisk=${rootdisk%[0-9]};
  done
  partnum=${rootdev#${rootdisk}}

  # if the basename of the root device (ie 'xvda1' or 'sda1') exists
  # in /sys/block/ then it is a block device, not a partition
  # (xen xvda1 is an example of such a funny named block device)
  [ -e "/sys/block/${rootdev##*/}" ] && echo "GrowRootFS: ${rootdev} is a block device" && return 0

  # if growpart fails, exit.
  # we capture stderr because on success of dry-run, it writes
  # to stderr what it would do.
  out=$(growpart --dry-run "${rootdisk}" "${partnum}" 2>&1) || { echo "GrowRootFS: ${out}" && return 1; }

  # if growpart would change something, --dry-run will write something like
  #  CHANGE: partition=1 start=2048 old: size=1024000 end=1026048 new: size=2089192,end=2091240
  # anything else, exit
  case "${out}" in
    CHANGE:*) :;;
    *) echo "GrowRootFS: ${out}" && return 0;;
  esac

  ## Wait for any of the initial udev events to finish
  ## This is to avoid any other processes using the block device that the
  ## root partition is on, which would cause the sfdisk 'BLKRRPART' to fail.
  udevadm settle --timeout ${rootdelay:-30} || { echo "GrowRootFS: WARNING: udevadm settle prior to growpart failed" && return 1 ; }

  if out=$(growpart "${rootdisk}" "${partnum}" 2>&1); then
    case "$out" in
      CHANGED:*) echo "GrowRootFS: $out";;
      NOCHANGE:*)
        echo "GrowRootFS: WARNING: expected to grow partition, but did not";;
      *) echo "GrowRootFS: unexpected output: ${out}"
    esac
  else
    echo "GrowRootFS: WARNING: resize failed: $out"
  fi

  ## Wait for the partition re-read events to complete
  ## so that the root partition is available when we try and mount it.
  udevadm settle --timeout ${rootdelay:-30}

  # Need to figure out the rootfs type somehow
  # For now we only support ext2/3/4
  fsck -f ${rootdev}
  resize2fs -p ${rootdev}

  ## write to /etc/grownroot-grown. most likely this wont work (readonly)
  { date --utc > "${rootmnt}/etc/growroot-grown" ; } >/dev/null 2>&1 || :

  echo "GrowRootFS: done"
  sleep 1

}