Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

bootstrapping with microperl

by zentara (Cardinal)
on Jan 18, 2003 at 20:00 UTC ( [id://228040]=CUFP: print w/replies, xml ) Need Help??

This may not be a new use, but many (like me) may not have tried it until now. Microperl is worth your attention, especially if you are like me and have 2 left feet when it comes to shell scripts, awk and sed. Have you heard of microperl, but never tried it? Here is a success story. It definitely is worth using.

I've developed my own system for making full backups to cdr, and use perl of course. A problem arises when restoring to a blank system, in that there is no perl available to run the restore script. Well I was thinking about using a shell script (kind of clunky with strings), or a C program(since it's a pretty simple operation). Then I came across a node which pointed me to microperl, which comes with 5.8. It fills the bill! It is only 908k on my system, which easily fits on a cdrom. So with microperl and my 2 small scripts, I get a bootstrap perl restore system. The backup system relies on multi-volume tar archives, which are made for each second level sub-directory, then gunzipped and written to a ext2 image mounted via loop. When the image gets filled, it gets written to cdr, and the next iso is started. I delve 2 layers into /usr since it's so big. So I end up with a bunch of files named:

_etc-1.tar.gz
_root-1.tar.gz
_root-2.tar.gz
_root-3.tar.gz
_lib-1.tar.gz
_lib-2.tar.gz
_usr_lib-1.tar.gz
_usr_lib-2.tar.gz
etc..
etc..
Now I just throw a copy of microperl, and my 2 scripts on to each cd, and I can boot with any floppy, mount my cd's and restore. Microperl dosn't do it all, and seems to have some small peculiarities compared to full perl. Notably, I needed to use `ls` and grep to simulate a glob or readdir.

First you make microperl, by "make -f Makefile.micro" in the perl58 distribution. The first thing I noticed was it didn't include alot of standard functions like opendir, so you need to use backticks and system alot. BUT you do get grep,arrays,hashes, and regexes; so it eliminates the need for awk,sed,expr, and the other apps usually needed by shell scripts. So it's actually a space saver at 908k. Also the system calls seemed to like double quotes, to pass parameters.


#!microperl #to substitute for a readdir or glob #I needed to ls then grep my @files = `ls`; @files = grep($_ =~ /.tar.gz$/,@files); foreach(@files){print "\ngunzipping->$_\n"; system("gunzip $_")} my @files = `ls`; @files = grep($_ =~ /.tar$/,@files); (@bases) = map{$_ =~ m/^(.*)-\d+.tar$/ }@files; print "@bases\n"; my %hash; $hash{$_}++ foreach (@bases); print "$_: $hash{$_}\n " foreach (sort {$hash{$a} <=> $hash{$b}} keys +%hash); foreach $basename (keys %hash){ #initialize first file if (-e "$basename-1.tar"){ $old= "$basename-1.tar"; $new= "$basename.tar"; rename $old,$new or warn $!; } #the restore-rotate.pl script just swaps files like they were tapes system ('tar','-x','-M',"-F ./restore-rotate.pl $basename","-f$basenam +e.tar"); unlink "$basename.tar"; #cleanup print "----------------------------------\n"; } system("mkdir proc"); system("mkdir tmp"); system("mkdir home"); exit;

update (broquaint): s{^#+$}(<hr/>)

Replies are listed 'Best First'.
Re: bootstrapping with microperl
by rob_au (Abbot) on Jan 18, 2003 at 23:17 UTC
    Microperl dosn't do it all, and seems to have some small peculiarities compared to full perl. Notably, I needed to use `ls` and grep to simulate a glob or readdir.

    This is because functions which need to be implemented in a system-specific manner are not incorporated into the microperl binary. This "feature" is mentioned in the Perl Journal article by Simon Cozens here.

    As for the size of the compiled microperl binary, there is much room for hacking. For instance the supplied Makefile.micro incorporates no optimisation flags in the compile process - With the additional of a -O3 optimisation flag, a stripped microperl binary can be made as small as 705Kb. Building against a C library optimised for embedded programming may shrink this binary further.

    However as Jarkko states, it is an experimental production and if you find problems with your build - Don't report them, fix them :-)

     

    Update - Following prompting by PodMaster, I compressed the stripped microperl binary which had been built with the -O3 optimisation flag with UPX - The resultant binary was 308Kb in size.

    kathmandu:/home/build/perl-5.8.0# upx -9 microperl -omicroperl.upx Ultimate Packer for eXecutables Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001 UPX 1.20 Markus F.X.J. Oberhumer & Laszlo Molnar May 23 +rd 2001 File size Ratio Format Name -------------------- ------ ----------- ----------- 721956 -> 315924 43.75% linux/386 microperl.upx Packed 1 file.

     

    perl -le 'print+unpack("N",pack("B32","00000000000000000000001000011011"))'

      Hi, that UPX is very cool. ++ . To continue discussion on microperl: Would you know how to modify the Makefile.micro to get it to produce a statically linked microperl? It's still linked to some shared libs:
      libm.so.6 => /lib/libm.so.6 
      libc.so.6 => /lib/libc.so.6 
      /lib/ld-linux.so.2 => /lib/ld-linux.so.2 
      
      libc.so.6 and ld-linux.so.2 seem pretty standard in boot disks, but I was thinking of statically linking libm.so.6 .
        It is very easy to build microperl statically - To do this, the flag -static needs to be added to the compiler flags. For example:

        LD = $(CC) DEFINES = -DPERL_CORE -DPERL_MICRO OPTIMIZE = -O3 -static CFLAGS = $(DEFINES) $(OPTIMIZE) LIBS = -lm _O = .o

        On systems that support dynamic linking, this prevents linking with the shared libraries. On other systems, this option has no effect.

         

        perl -le 'print+unpack("N",pack("B32","00000000000000000000001000011110"))'

Re: bootstrapping with microperl
by zentara (Cardinal) on Jan 20, 2003 at 19:34 UTC
    Well I found that I my system, suse8.1, gcc3.2 that the static option had to be entered in a different spot with LIBS
    LD = $(CC)
    DEFINES = -DPERL_CORE -DPERL_MICRO
    OPTIMIZE = -O3
    CFLAGS = $(DEFINES) $(OPTIMIZE)
    LIBS = -static -lm
    _O = .o
    
    
    The interesting news out of this is that my static microperl, which has been packed with upx , is only 540k.
    3684060  microperl-staticO3
    1173816  microperl-staticO3-stripped
    538938  microperl.upx
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://228040]
Approved by Aristotle
Front-paged by toma
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2025-03-26 07:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (67 votes). Check out past polls.

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.