#!/usr/bin/perl -w # # Author: P.Zumbruch@gsi.de # January 22, 2016 use strict; use File::Temp qw/ tempfile tempdir /; #find location of 'raspi-config' open CMD,'-|', 'which raspi-config' or die $@; my $raspiConfigLocation = ; close CMD; #read 'raspi-config' open FILE, $raspiConfigLocation or die "Cannot open '$raspiConfigLocation': $!"; my $raspiConfig = do { local $/; }; close FILE; ### #define additional code segments to leave some space of 256MB at the very end of the SD card # - size calculations my $partSizeCommands = << 'EOF'; #additional code segment to leave some space of 256MB at the very end of the SD card if [ -z "$PART_SIZE" ] then local DEVICE_SIZE=$(parted MY_DEVICE -ms unit s p | grep MY_DEVICE | cut -d: -f 2 | sed -e 's/[A-Z]//ig'); local DEVICE_SECTOR_SIZE=$(parted MY_DEVICE -ms unit s p | grep MY_DEVICE | cut -d: -f 4 | sed -e 's/[A-Z]//ig'); local PART_SIZE=$(( $DEVICE_SIZE - ( 1 + 256 * 1024 * 1024 / $DEVICE_SECTOR_SIZE) )); fi EOF ; #replace dummy "MY_DEVICE" by real device name used in 'raspi-config' my $partSizeCommandsDevice = $raspiConfig =~ s/.+parted (\/dev\/.+?)\s.+/$1/sr; $partSizeCommands =~ s/MY_DEVICE/$partSizeCommandsDevice/g; # - unmodified fdisk sequence my $fdiskCommands = << 'EOF'; p d $PART_NUM n p $PART_NUM $PART_START p w EOF ; # generate modifed fdisk sequence including PART_SIZE my $fdiskCommandsInsert = $fdiskCommands =~ s/(\$PART_NUM\n\$PART_START\n)(\n)/$1\$PART_SIZE$2/rm; # generate search pattern my $fdiskCommandsSearch = $fdiskCommands =~ s/\$/\\\$/rg; #replace fdisk sequence $raspiConfig =~ s/(.*?\n)$fdiskCommandsSearch/$partSizeCommands$1$fdiskCommandsInsert/m; #print $raspiConfig; #create new enhanced 'raspi-config' to temporary file my $filename; (undef, $filename) = tempfile( "", SUFFIX => '.raspi-config', TMPDIR => 1, OPEN => 1 ); open OUTPUT, ">$filename" or die "Cannot create output file: $!"; print OUTPUT $raspiConfig ."\n"; close OUTPUT; chmod 0755, "$filename"; #execute vi sudo new 'raspi-config' !system "sudo $filename --expand-rootfs" or warn "sudo $filename --expand-rootfs failed:$!\n"; #delete temporary 'raspi-config' file unlink $filename or warn "failed removing $filename: $!\n";