source: lliurex-mate-welcome/trunk/fuentes/lliurex-mate-welcome-rpi2-partition-resize

Last change on this file was 3933, checked in by alviboi, 3 years ago
  • Property svn:executable set to *
File size: 3.6 KB
Line 
1#!/bin/sh
2
3# Copyright (c) 2012 Alex Bradbury <asb@asbradbury.org>
4# Copyright (C) 2016 Martin Wimpress <code@ubuntu-mate.org>
5#
6# Permission is hereby granted, free of charge, to any person obtaining a
7# copy of this software and associated documentation files (the
8# "Software"), to deal in the Software without restriction, including
9# without limitation the rights to use, copy, modify, merge, publish,
10# distribute, sublicense, and/or sell copies of the Software, and to
11# permit persons to whom the Software is furnished to do so, subject to
12# the following conditions:
13#
14# The above copyright notice and this permission notice shall be included
15# in all copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25pass_notify() {
26  # 1 = Success
27  # 2 = /dev/root non-existent or not a symlink
28  # 3 = Not an SD card.
29  # 4 = Unsupported layout.
30  # 5 = Not the last partition.
31  echo "${1}" > /tmp/notify_rpi_status
32
33  # Miscellaneous information to show in notification.
34  echo "${2}" > /tmp/notify_rpi_text
35}
36
37get_init_sys() {
38  if command -v systemctl > /dev/null && systemctl | grep -q '\-\.mount'; then
39    SYSTEMD=1
40  elif [ -f /etc/init.d/cron ] && [ ! -h /etc/init.d/cron ]; then
41    SYSTEMD=0
42  else
43    echo "Unrecognised init system"
44    return 1
45  fi
46}
47
48do_expand_rootfs() {
49  get_init_sys
50  if [ $SYSTEMD -eq 1 ]; then
51    ROOT_PART=$(mount | sed -n 's|^/dev/\(.*\) on / .*|\1|p')
52  else
53    if ! [ -h /dev/root ]; then
54      pass_notify 2 '/dev/root'
55      return 0
56    fi
57    ROOT_PART=$(readlink /dev/root)
58  fi
59
60  PART_NUM=${ROOT_PART#mmcblk0p}
61  if [ "$PART_NUM" = "$ROOT_PART" ]; then
62    pass_notify 3 "$ROOT_PART"
63    return 0
64  fi
65
66  # NOTE: the NOOBS partition layout confuses parted. For now, let's only
67  # agree to work with a sufficiently simple partition layout
68  if [ "$PART_NUM" -ne 2 ]; then
69    pass_notify 4 ''
70    return 0
71  fi
72
73  LAST_PART_NUM=$(parted /dev/mmcblk0 -ms unit s p | tail -n 1 | cut -f 1 -d:)
74  if [ $LAST_PART_NUM -ne $PART_NUM ]; then
75    pass_notify 5 "$ROOT_PART"
76    return 0
77  fi
78
79  # Get the starting offset of the root partition
80  PART_START=$(parted /dev/mmcblk0 -ms unit s p | grep "^${PART_NUM}" | cut -f 2 -d: | sed 's/[^0-9]//g')
81  [ "$PART_START" ] || return 1
82  # Return value will likely be error for fdisk as it fails to reload the
83  # partition table because the root fs is mounted
84  fdisk /dev/mmcblk0 <<EOF
85p
86d
87$PART_NUM
88n
89p
90$PART_NUM
91$PART_START
92
93p
94w
95EOF
96
97  # now set up an init.d script
98cat <<EOF > /etc/init.d/resize2fs_once &&
99#!/bin/sh
100### BEGIN INIT INFO
101# Provides:          resize2fs_once
102# Required-Start:
103# Required-Stop:
104# Default-Start: 3
105# Default-Stop:
106# Short-Description: Resize the root filesystem to fill partition
107# Description:
108### END INIT INFO
109
110. /lib/lsb/init-functions
111
112case "\$1" in
113  start)
114    log_daemon_msg "Starting resize2fs_once" &&
115    /sbin/resize2fs /dev/$ROOT_PART &&
116    update-rc.d resize2fs_once remove &&
117    rm /etc/init.d/resize2fs_once &&
118    log_end_msg \$?
119    ;;
120  *)
121    echo "Usage: \$0 start" >&2
122    exit 3
123    ;;
124esac
125EOF
126  chmod +x /etc/init.d/resize2fs_once &&
127  update-rc.d resize2fs_once defaults &&
128  touch /.resized &&
129  pass_notify 1 ''
130}
131
132do_expand_rootfs
Note: See TracBrowser for help on using the repository browser.