From 67379bc6d611285088b90a4b7ec5e0dfce2ad901 Mon Sep 17 00:00:00 2001 From: DmaR01 Date: Sun, 15 Dec 2013 14:04:08 -0500 Subject: [PATCH 1/2] Update rootfs-resize --- rootfs-resize | 437 +++++++++++++++++++++++++++++--------------------- 1 file changed, 250 insertions(+), 187 deletions(-) diff --git a/rootfs-resize b/rootfs-resize index 4af5b3a..02e3b0c 100644 --- a/rootfs-resize +++ b/rootfs-resize @@ -1,236 +1,299 @@ #!/usr/bin/python # -# rootfs-resize :: Resize the root parition and filesytem +# rootfs-resize # -# Version 2.0 2013-01-11 +# 2.3 :: Resize the root partition, and if the root +# partition is logical, resize the extended partition also # -# Authors: -# Chris Tyler, Seneca College 2013-01-11 +# 2.0 :: Resize the root partition and filesytem # -# This script will increase the size of the root partition by -# moving the end of the partition, and then resize the filesystem -# to fill the available space. +# Version 2.3 2013-12-12 +# Version 2.0 2013-01-11 # -# Prerequisites for successful operation: -# 1. The root filesystem must be on a partition (not an LV or other -# abstraction) on a /dev/sdX or /dev/mmcblkX device. +# Authors: +# Chris Tyler, Seneca College 2013-01-11 +# Daniel Martino, Seneca College 2013-12-12 # -# 2. The root filesystem type must be ext2, ext3, or ext4. # -# 3. There must be room available to increase the size of the -# root partition by moving the end. The start of the root partition -# will not be moved. +# This script will increase the size of the root partition by +# moving the end of the partition, and then resize the filesystem +# to fill the available space. # -# 4. The file /.nofsresize must not exist. +# Prerequisites for successful operation: +# 1. The root filesystem must be on a partition (not an LV or other +# abstraction) on a /dev/sdX or /dev/mmcblkX device. # -# 5. The kernel must not have been booted with the 'nofsresize' -# command-line option. +# 2. The root filesystem type must be ext2, ext3, or ext4. # -# 6. The file /.rootfs-repartition must exist to start phase 1 -# (partition adjustment). The system will be rebooted immediately -# if the partition adjustment is successful. +# 3. There must be room available to increase the size of the +# root partition by moving the end. The start of the root partition +# will not be moved. # -# 7. The file /.rootfs-resize, which is created when the partitions -# are adjusted in phase 1, must exist to start phase 2. +# 4. The file /.nofsresize must not exist. # -# 8. If the file /.swapsize exists when phase 2 is processed, -# and it contains a text representation of a non-zero whole number, -# and the file /swap0 does not exist, then a swapfile named /swap0 -# will be created. The size of the swapfile will be the number in -# /.swapsize interpreted as megabytes. This swapfile will be added to -# /etc/fstab and activated. +# 5. The kernel must not have been booted with the 'nofsresize' +# command-line option. # -# Requirements (Fedora package name as of F17): -# - python 2.7+ (python) -# - pyparted (pyparted) -# - psutil (python-psutil) -# - /sbin/resize2fs (e2fsprogs) -# - /sbin/mkswap (util-linux) +# 6. The file /.rootfs-repartition must exist to start phase 1 +# (partition adjustment). The system will be rebooted immediately +# if the partition adjustment is successful. # -# Optional requirements (recommended): -# - /usr/bin/ionice (util-linux) -# - /sbin/swapon (util-linux) +# 7. The file /.rootfs-resize, which is created when the partitions +# are adjusted in phase 1, must exist to start phase 2. +# +# 8. If the file /.swapsize exists when phase 2 is processed, +# and it contains a text representation of a non-zero whole number, +# and the file /swap0 does not exist, then a swapfile named /swap0 +# will be created. The size of the swapfile will be the number in +# /.swapsize interpreted as megabytes. This swapfile will be added to +# /etc/fstab and activated. +# +# Requirements (Fedora package name as of F17): +# - python 2.7+ (python) +# - pyparted (pyparted) +# - psutil (python-psutil) +# - /sbin/resize2fs (e2fsprogs) +# - /sbin/mkswap (util-linux) +# +# Optional requirements (recommended): +# - /usr/bin/ionice (util-linux) +# - /sbin/swapon (util-linux) -# Copyright (C)2013 Chris Tyler, Seneca College, and others -# (see Authors, above). +# Copyright (C)2013 Chris Tyler, Seneca College, and others +# (see Authors, above). # -# 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 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. +# 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 +# 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 # -import parted -import re -import sys -import psutil -import ast -import os -import glob +import re, parted, glob +import os, ast, sys, psutil + abort_blocked = False; -# Check that the kernel command line parameter 'nofsresize' was NOT specified +# Check the kernel command line parameter 'nofsresize' was NOT specified for line in open('/proc/cmdline'): - if 'nofsresize' in line: - abort_blocked = True + if 'nofsresize' in line: + abort_blocked = True # Check that /.nofsresize is NOT present if os.path.isfile('/.nofsresize'): - abort_blocked = True + abort_blocked = True # Abort due to either of the above if abort_blocked: - sys.exit(1) + sys.exit(1) # Find the root device -for line in open('/proc/self/mountinfo'): - if '/ / ' in line: - root_major_minor = line.split(' ')[2] - break - -major=int(root_major_minor.split(":")[0]) -minor=int(root_major_minor.split(":")[1]) +for line in open("/proc/self/mountinfo"): + if '/ / ' in line: + r_major_minor = line.split(' ')[2] + break + +major=int(r_major_minor.split(":")[0]) +minor=int(r_major_minor.split(":")[1]) # We now have the major/minor of the root device. # Scan devices to find the corresponding block device. -# This is necessary because the device name reported in -# /proc/self/mountinfo may be /dev/root +# This is necessary because the device name +# reported in /proc/self/mountinfo may be /dev/root +# We will also check to see if it's a logical partition +# by checking the partition number. Partition numbers greater +# than 4 are logical. root_device = '' for blockdev in glob.glob("/dev/sd??*")+glob.glob("/dev/mmcblk*p*"): - rdev=os.stat(blockdev).st_rdev - if os.major(rdev) == major and os.minor(rdev) == minor: - root_device = blockdev - break - -# If the root device is a partion, find the disk containing it + rdev=os.stat(blockdev).st_rdev + if os.major(rdev) == major and os.minor(rdev) == minor: + root_device = blockdev + endnum = root_device[-1] + + # If the end is a number, convert to an integer + # then check the number which will determine if + # it's a logical partition or not + if endnum.isdigit(): + endnum = int(root_device[-1]) + if endnum <= 4: + logical = False + else: + logical = True + else: + break + +# If the root device is a partition, find the disk containing it disk_device = '' for pattern in [ '/dev/sd.', '/dev/mmcblk.' ]: - match = re.match(pattern, root_device) - if match: - disk_device = root_device[:match.span()[1]] - break + match = re.match(pattern, root_device) + if match: + disk_device = root_device[:match.span()[1]] + break -# Exit if we didn't find a disk_device +# Exit if no disk_device is found if not disk_device: - sys.exit(2) - + sys.exit(2) + # PHASE 1 # If /.rootfs-repartition exists, repartition the disk, then reboot if os.path.isfile('/.rootfs-repartition'): + + # Create block device and disk label objects + device = parted.Device(disk_device) + disk = parted.Disk(device) + + # Do sanity checks + constraint = device.optimalAlignedConstraint + + + # Check if root partition is logical + # If True, find and resize the extended partition + if logical: + + # Find the extended partition + try: + ext = disk.getExtendedPartition() + + # Try to grow the extended partition + # The try/except is another workaround for pyparted ticket #50. + try: + new_end_ext = ext.getMaxGeometry(constraint).end + + except TypeError: + new_end_ext = ext.getMaxGeometry(constraint.getPedConstraint()).end + + # Check if the extended partition can grow + if ext.geometry.end < new_end_ext: + disk.setPartitionGeometry(partition = ext, constraint = constraint, start = ext.geometry.start, end = new_end_ext) + try: + disk.commit() + except: + pass + else: + print 'Unable to resize extended partition (Partition at max size or couldn\'t locate partition).' + except: + pass + + # If logical partition is not found, skip + # and proceed to resizing the root partition + else: + pass + + + # Blank previous device variables to avoid disk corruption + del device, disk, constraint + + # Rereate block device and disk label objects + device = parted.Device(disk_device) + disk = parted.Disk(device) + + # Find root partition and do sanity checks + constraint = device.optimalAlignedConstraint + root = disk.getPartitionByPath(root_device) + + try: + new_end_root = root.getMaxGeometry(constraint).end + except TypeError: + new_end_root = root.getMaxGeometry(constraint.getPedConstraint).end + + + # If root is an ext[234] filesystem and the partition end can grow, + # change the partition ending and then reboot + if (root.fileSystem.type == 'ext2' + or root.fileSystem.type == 'ext3' + or root.fileSystem.type == 'ext4' ) \ + and root.geometry.end < new_end_root: + disk.setPartitionGeometry(partition = root, constraint = constraint, start = root.geometry.start, end = new_end_root) + + + + # disk.commit() will usually throw an exception because the kernel + # is using the rootfs and refuses to accept the new partition table + # ... so we reboot + try: + disk.commit() + except: + pass + + else: + print 'Unable to resize root partition (max size, or unresizable fs type).' + + + # Change flagfiles and reboot + open('/.rootfs-resize','w').close() + os.unlink('/.rootfs-repartition') + os.system('/sbin/reboot') - # Create block device and disk label objects - device = parted.Device(disk_device) - disk = parted.Disk(device) - - # Find root partition and do sanity checks - root = disk.getPartitionByPath(root_device) - - constraint = device.optimalAlignedConstraint - - # Find the proposed end of the root partition. - # This try/except is a workaround for pyparted ticket #50 - - # see https://fedorahosted.org/pyparted/ticket/50 - try: - new_end = root.getMaxGeometry(constraint).end - except TypeError: - new_end = root.getMaxGeometry(constraint.getPedConstraint()).end - - # If it's a ext[234] filesystem and the partition end can grow, - # change the partition ending and then reboot - if (root.fileSystem.type == 'ext2' - or root.fileSystem.type == 'ext3' - or root.fileSystem.type == 'ext4' ) \ - and root.geometry.end < new_end: - - disk.setPartitionGeometry(partition = root, constraint = constraint, - start = root.geometry.start, end = new_end ) - - # disk.commit() will usually throw an exception because the kernel - # is using the rootfs and refuses to accept the new partition table - # ... so we reboot - try: - disk.commit() - except: - pass - - else: - print 'Unable to resize root partition (max size, or unresizable fs type).' - - # Change flagfiles and reboot - open('/.rootfs-resize','w').close() - os.unlink('/.rootfs-repartition') - os.system('/sbin/reboot') # PHASE 2 -# If /.rootfs-resize exists, resize the filesystem +# if /.rootfs-resize exists, resize the filesystem +# Use ionice if available elif os.path.isfile('/.rootfs-resize'): - - # Use ionice if available - if os.path.isfile('/usr/bin/ionice'): - os.system('/usr/bin/ionice -c2 -n7 /sbin/resize2fs %s' % root_device ) - else: - os.system('/sbin/resize2fs %s' % root_device ) - - # Create swap if requested - if os.path.isfile('/.swapsize'): - - swapsizefile = open('/.swapsize') - try: - swapsize = ast.literal_eval(swapsizefile.readline()) - except: - swapsize = 0 - - swapsizefile.close() - - # Create /swap0 as a swapfile if it doesn't exist and - # the requested size in MB is greater than 0 - if ( not os.path.isfile('/swap0') ) and swapsize > 0: - - # Lower the I/O priority to minimum best-effort - psutil.Process(os.getpid()).set_ionice(psutil.IOPRIO_CLASS_BE, 7) - - # Create swap file as swap0.part (so we recreate if aborted) - MB = ' ' * (1024*1024) - swapfile = open('/swap0.part','w') - for X in range(swapsize): - swapfile.write(MB) - - # Make it a swapfile - os.system('/sbin/mkswap /swap0.part') - - # Rename the swapfile to /swap0 - os.rename('/swap0.part','/swap0') - - # Add /swap0 to the fstab if not already present - abort_fstab = False - for line in open('/etc/fstab'): - if re.match('/swap0', line): - abort_fstab = True - break - - if not abort_fstab: - fstab = open('/etc/fstab','a') - fstab.write('/swap0\t\t\tswap\tswap\n') - fstab.close() - - # Activate all swap spaces if possible - if os.path.isfile('/sbin/swapon'): - os.system('/sbin/swapon -a') - - # Delete swap flagfile - os.unlink('/.swapsize') - - # Delete resize flagfile - os.unlink('/.rootfs-resize') - + + # Use ionice if available + if os.path.isfile('/usr/bin/ionice'): + os.system('/usr/bin/ionice -c2 -n7 /sbin/resize2fs %s' % root_device) + else: + os.system('/sbin/resize2fs %s' % root_device) + + # Create swap is requested + if os.path.isfile('/.swapsize'): + swapsizefile = open('/.swapsize') + + try: + swapsize = ast_literal_eval(swapsizefile.readline()) + except: + swapsize = 0 + + swapsizefile.close() + + # Create /swap0 as a swapfile if it doesn't exist + # and the requested size in MB is greater than 0 + if (not os.path.isfile('/swap0') ) and swapsize > 0: + + # Lower the I/O priority to minimum best-effort + psutil.Process(os.getpid()).set_ionice(psutil.IOPRIO_CLASS_BE, 7) + + # Create swap file as swap0.part (so we recreate if aborted) + MB = ' ' * (1024*1024) + swapfile = open('/swap0.part', 'w') + for X in range(swapsize): + swapfile.write(MB) + + # Make it a swapfile + os.system('/sbin/mkswap /swap0.part') + + # Rename the swapfile to /swap0 + os.rename('/swap0.part', '/swap0') + + # Add /swap0 to the fstab if not already present + abort_fstab = False + for line in open("/etc/fstab"): + if re.match("/swap0", line): + abort_fstab = True + break + + if not abort_fstab: + fstab = open("/etc/fstab", "a") + fstab.write("/swap0\t\tswap\tswap\n") + fstab.close() + + # Active all swap spaces if possible + if os.path.isfile("/sbin/swapon"): + os.system("/sbin/swapon -a") + + # Delete swap flagfile + os.unlink("/.swapsize") + + # Delete resize flagfile + os.unlink("/.rootfs-resize") From 27c454c61644e381622b767d46c120496b0f5a55 Mon Sep 17 00:00:00 2001 From: DmaR01 Date: Sun, 15 Dec 2013 14:05:26 -0500 Subject: [PATCH 2/2] Update README.md --- README.md | 71 ++++++++++++++++--------------------------------------- 1 file changed, 20 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 1d436aa..e31d3f6 100644 --- a/README.md +++ b/README.md @@ -1,61 +1,30 @@ rootfs-resize ============= -Version 2.0 - 2013-01-14 +Version 2.3 - 2013-12-12 ------------------------ -This is a rewrite, in python, of the previous rootfs-resize -bash script. This version uses libparted (via pyparted) and -better logic for improved robustness. - -rootfs-resize is a python service script designed for use on -computers that use an SD card for storage, such as ARM development -boards. Since SD cards can be purchased in various sizes, and -since even cards of the same nominal size may have slightly -different capacities, it is impossible to create an SD card -image guaranteed to exactly fit a card. It is also inefficient -to have an image that is larger than the files contained within -that image, because the "extra space" will slow compression, -download, and SD-card burn activities. - -As a result, the Fedora ARM SD card images typically contain -a boot filesystem (usually FAT/VFAT) followed by a Linux extX -root filesystem. The second filesystem is sized so that it -is just big enough for the contained files. - -The rootfs-resize script attempts to resize the root (typically -second) partition on the SD card to fill the device. It -does this by adjusting the end of the root partition and then -rebooting. On the next boot, a resize2fs is performed -to resize the filesystem. - -The name of the block device containing the root filesystem must -be /dev/sdX or /dev/mmcblk0pX, and it must contain an ext[234] -partition. - -In order to resize the partition, the file /.rootfs-repartition -must be present. After the partition resize operation, this file -is removed and the file /.rootfs-resize is created (Phase 1); on -next boot, when the resize2fs is started, the /.rootfs-resize file is -removed (Phase 2). - -If the file /.swapsize exists and contains an integer number -in text format during Phase 2, a swap file (/swap0) of that size -in megabytes is created after the filesystem is resized. This swap -file is added to /etc/fstab and activated. - -The operation of this service may be disabled in either of these -two ways (in addition to disabling the service unit file): +rootfs-resize 0.3 release -1. By passing a kernel command line argument of "nofsresize" -2. By creating the file /.nofsresize +This is a rewrite, in python, of the previous rootfs-resize bash script. This version uses libparted (via pyparted) and better logic for improved robustness. + +rootfs-resize is a python service script designed for use on computers that use an SD card for storage, such as ARM development boards. Since SD cards can be purchased in various sizes, and since even cards of the same nominal size may have slightly different capacities, it is impossible to create an SD card image guaranteed to exactly fit a card. It is also inefficient to have an image that is larger than the files contained within that image, because the "extra space" will slow compression, download, and SD-card burn activities. + +As a result, the Fedora ARM SD card images typically contain a boot filesystem (usually FAT/VFAT) followed by a Linux extX root filesystem. The second filesystem is sized so that it is just big enough for the contained files. + +The rootfs-resize script attempts to resize the root partition on the SD card to fill the device. It does this by adjusting the end of the root partition and then rebooting. It will also determine if the root partition is logical, and if it is it will find the extended partition and resize it. On the next boot, a resize2fs is performed to resize the filesystem. -If either of these suppressions flags are present, the script -will not run. +The name of the block device containing the root filesystem must be /dev/sdX or /dev/mmcblk0pX, and it must contain an ext[234] partition. -This script and the corresponding systemd unit file are licensed -under the GPL v2, or at the user's discretion, any later version. +In order to resize the partition, the file /.rootfs-repartition must be present. After the partition resize operation, this file is removed and the file /.rootfs-resize is created (Phase 1); on next boot, when the resize2fs is started, the /.rootfs-resize file is removed (Phase 2). + +If the file /.swapsize exists and contains an integer number in text format during Phase 2, a swap file (/swap0) of that size in megabytes is created after the filesystem is resized. This swap file is added to /etc/fstab and activated. + +The operation of this service may be disabled in either of these two ways (in addition to disabling the service unit file): + +1. By passing a kernel command line argument of "nofsresize" +2. By creating the file /.nofsresize -The web page and git repository for this code is accessible at the -[Github Repo](https://github.com/ctyler/rootfs-resize) +If either of these suppressions flags are present, the script will not run. +This script and the corresponding systemd unit file are licensed under the GPL v2, or at the user's discretion, any later version.