Perl-Sensitive Sunglasses
| PerlMonks

### bootstrapping with microperl by zentara (Archbishop) | Log in Create a new user The Monastery Gates Super Search Seekers of Perl Wisdom Meditations PerlMonks Discussion Obfuscation Reviews Cool Uses For Perl Perl News Q&A Tutorials Poetry Recent Threads Newest Nodes Donate What's New

on Jan 18, 2003 at 20:00 UTC ( #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;
    [download]
update (broquaint): `s{^#+$}(<hr/>)`
Comment on bootstrapping with microperl Select or Download Code
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.
      [download]
`perl -le 'print+unpack("N",pack("B32","00000000000000000000001000011011"))'` | [reply] [d/l] [select]
Re: Re: bootstrapping with microperl by zentara (Archbishop) on Jan 19, 2003 at 14:27 UTC
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 . | [reply]
Re: Re: Re: bootstrapping with microperl by rob_au (Abbot) on Jan 19, 2003 at 21:08 UTC
  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
      [download]
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"))'` | [reply] [d/l] [select]
Re: Re: Re: Re: bootstrapping with microperl by zentara (Archbishop) on Jan 20, 2003 at 13:28 UTC
Re: bootstrapping with microperl by zentara (Archbishop) 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
| [reply]
Back to Cool Uses for Perl |
Log In?
Username: |
Password: |
remember me What's my password? Create A New User
Node Status?
node history Node Type: CUFP [id://228040] Approved by Aristotle Front-paged by toma help
Chatterbox?
and all is quiet... How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8) marto LanX atcroft karlgoethebier Not_a_Number Eily Lady_Aleena pryrt As of 2017-06-16 12:55 GMT
Sections?
Seekers of Perl Wisdom Cool Uses for Perl Meditations PerlMonks Discussion Categorized Q&A Tutorials Obfuscated Code Perl Poetry Perl News about
Information?
PerlMonks FAQ Guide to the Monastery What's New at PerlMonks Voting/Experience System Tutorials Reviews Library Perl FAQs Other Info Sources
Find Nodes?
Nodes You Wrote Super Search List Nodes By Users Newest Nodes Recently Active Threads Selected Best Nodes Best Nodes Worst Nodes Saints in our Book
Leftovers?
The St. Larry Wall Shrine Buy PerlMonks Gear Offering Plate Awards Random Node Quests Craft Snippets Code Catacombs Editor Requests blogs.perl.org Perlsphere Perl Ironman Blog Perl Weekly Perl.com Perl 5 Wiki Perl Jobs Perl Mongers Perl Directory Perl documentation MetaCPAN CPAN
Voting Booth?
How many monitors do you use while coding? None - I don't code 0 - Ticker-tape is a purer representation of a Turing Machine 1 - A laptop screen or single monitor desktop workstation 1 - And it's cracked. And I like practicing my trade while lying on a bed of nails 2 - The standard dev kit suits me fine 3 - Fewer than three monitors wouldn't present enough insight into what I'm working on 4 - Everyone else has two or three. I'm not like everyone else 5 - This configuration reminds me of the 1987 5-cylinder VW Passat my parents let me drive in high school 6 - I saw such a thing on the Internet, and had to have it 6 - Because I haven't figured out how to add more 7 - I need all the luck I can get, for this code to work 8 - Because my power strip had eight outlets available 9 - A 3x3 array was the the most I could fit into my Faraday shielded SHTF headquarters More than 9 - Most of them are just displaying graphs / mock code to make the boss think I'm doing something critical Results (412 votes). Check out past polls.