Epics@GSI Webhome

Raspberry Pi Conservative Expand Rootfs Raspi Config Wrapper

Introduction

Code

    #!/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 = <CMD>;
    close CMD;
    
    #read 'raspi-config'
    open FILE, $raspiConfigLocation
       or die "Cannot open '$raspiConfigLocation': $!";
    my $raspiConfig = do { local $/; <FILE> };
    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 modified 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";
    


-- PeterZumbruch - 18 Feb 2016
-- PeterZumbruch - 2018-03-02
Topic attachments
I Attachment Action Size Date Who Comment
conservative_expand-rootfs.pl.txttxt conservative_expand-rootfs.pl.txt manage 2 K 2016-01-22 - 16:37 PeterZumbruch wrapper perl script to make a conservative expansion of the rootfs
Topic revision: r3 - 2018-03-02, PeterZumbruch - This page was cached on 2021-10-18 - 22:04.

This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding GSI Wiki? Send feedback | Imprint (german) | Privacy Policy (german)