WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Local initramfs generation script (stripping unneeded modules)  (Read 11833 times)

Offline gutmensch

  • Retired Admins
  • Hero Member
  • *****
  • Posts: 605
  • I can make it disappear, have no fear!
    • remembrance blog
Heya,

just wanted to share with you all a script I'm using for generating local initramfs' for thin clients - as they don't need all modules, neither from base nor from the kernel extensions... and it can save quite some MB of space (e.g. tinycore.gz + extension modules about 10 MB, after creation mycore.gz about 6 MB).

The script builds up a new initramfs (let's say mycore.gz), which only includes all modules that you have loaded at this moment! It also incorporates all modules coming from extensions (e.g. filesystems-3.0.21-tinycore.tcz) and puts a marker for that extension into /usr/local/tce.installed (that means you cannot easily change the modules you generated with this type of local initramfs, be aware of that!)

Usage: Put script and core.gz in one folder and run
Code: [Select]
sudo ./local_initramfs.sh core.gz mycore.gz
It will also generate .modlist files for an easy overview about included modules.

Comments are very welcome!

Remember: No support for scripting action placed in this forum, just a proof of concept - use at your own risk! ;-)

Code: (bash) [Select]
#!/bin/sh

# Help
[ "$1" = "--help" ] && { echo Usage: $0 core.gz mycore.gz ; exit 0 ; }

# Tests
[ ! -e "$1" ] && { echo need existing core.gz path as first parameter. ; exit 1 ; }
[ ! -n "$2" ] && { echo need output gz name as parameter \(e.g. mycore.gz\). ; exit 1 ; }
[ `id -u` -ne 0 ] && { echo must be root. ; exit 1 ; }
[ -z "`which advdef`" ] && { echo install advcomp. ; exit 1 ; }
[ "`which find`" != "/usr/local/bin/find" ] && { echo install findutils. ; exit 1 ; }

# Source tc functions
. /etc/init.d/tc-functions

# Vars
TCKERNEL=`uname -r`
WORKDIR=`pwd`
TCEDIR="/etc/sysconfig/tcedir"
OLDSIZE=0
SRCFSNAME=`basename $1`
SRCFSSIZE=`ls -l $1 | awk '{print $5}'`
DESTFSNAME=`basename $2`

# Starting
echo "${YELLOW}Using initramfs ${WHITE}$SRCFSNAME [ `dc $SRCFSSIZE 1000000 div p | sed 's%\([0-9]*\.[0-9]\{2\}\).*%\1%'` MB ].${YELLOW}"
[ -f "$2" ] && { echo Removing old target file. ; rm -f "$2" ; }
tgztemp=`mktemp -d -p $WORKDIR`
chmod -R ugo+rwx $tgztemp
cp $1 $tgztemp/
cd $tgztemp
zcat $SRCFSNAME | cpio -i -H newc -d 2>/dev/null
find . -name *.ko.gz | sed 's%^\./%%g' > $WORKDIR/$SRCFSNAME.modlist
rm $SRCFSNAME

# Copying extension modules
for mod_ext_dir in `find /tmp/tcloop -maxdepth 1 -type d -name "*$TCKERNEL*"` ; do
  if [ -d "$mod_ext_dir/usr/local/lib/modules/$TCKERNEL" ] || [ -d "$mod_ext_dir/lib/modules/$TCKERNEL" ]; then
    short_dir=`basename $mod_ext_dir`
    mod_ext_size=`ls -l $TCEDIR/optional/$short_dir.tcz | awk '{print $5}'`
    OLDSIZE=`echo "$OLDSIZE $mod_ext_size add p" | dc`
    mkdir -p usr/local/tce.installed 2>/dev/null
    echo "Including modules from: ${WHITE}`basename $mod_ext_dir`${YELLOW}."
    touch usr/local/tce.installed/`basename $mod_ext_dir` && chown tc:staff usr/local/tce.installed/`basename $mod_ext_dir`
    cp -apfr $mod_ext_dir/usr/local/lib/modules/$TCKERNEL/* lib/modules/$TCKERNEL/ 2>/dev/null
    cp -apfr $mod_ext_dir/lib/modules/$TCKERNEL/* lib/modules/$TCKERNEL/ 2>/dev/null
  fi
done

# Removing unneeded modules
for mod_file in `find lib/modules/$TCKERNEL -name *.ko.gz` ; do
  sstr="$(basename $mod_file | sed -e 's%.ko.gz%%' -e 's%-%_%g' )"
  loaded=$(lsmod | grep -e "^$sstr[[:space:]]\{1\}")
  [ -z "$loaded" ] && rm -f $mod_file || echo "Saving ${RED}`basename $mod_file`${YELLOW}."
done

# Remove empty directories
find lib/modules/$TCKERNEL -type d -empty | xargs rm -rf

# Running depmod
/sbin/depmod -a -b $tgztemp $TCKERNEL || echo depmod failed.

# Packing new gz
[ -d "usr/local/tce.installed" ] && find usr/local/tce.installed/ -name "*$TCKERNEL" | sed 's%^\./%%g' > $WORKDIR/$DESTFSNAME.modlist
find . -name *.ko.gz | sed 's%^\./%%g' >> $WORKDIR/$DESTFSNAME.modlist
find | cpio -o -H newc | gzip -2 > $WORKDIR/temp.gz.$$
cd $WORKDIR
advdef -z4 temp.gz.$$ 1>/dev/null 2>&1
mv temp.gz.$$ $2
rm -rf $tgztemp

# Summary
OLDSIZE=$(echo "$OLDSIZE `ls -l $1 | awk '{print $5}'` add 1000000 div p" | dc | sed 's%\([0-9]*\.[0-9]\{2\}\).*$%\1%')
NEWSIZE=$(echo "`ls -l $2 | awk '{print $5}'` 1000000 div p" | dc | sed 's%\([0-9]*\.[0-9]\{2\}\).*$%\1%')
echo "Summary of used and saved space:"
echo "-------------------------------------------"
echo "${WHITE}Before :   $SRCFSNAME + mod exts   :  $OLDSIZE MB"
echo "${GREEN}After${WHITE}  :   ${GREEN}$DESTFSNAME${WHITE}            :  ${GREEN}$NEWSIZE MB"
echo "${YELLOW}-------------------------------------------${NORMAL}"

Updated for TC 4.x.
« Last Edit: March 20, 2012, 04:32:59 PM by gutmensch »
If I seem unduly clear to you, you must have misunderstood what I said. (Alan Greenspan)

Offline jls

  • Hero Member
  • *****
  • Posts: 2135
Re: Local initramfs generation script (stripping unneeded modules)
« Reply #1 on: March 19, 2011, 09:45:25 AM »
Code: [Select]
Saving loop.ko.gz...
Saving ppdev.ko.gz...
find: unrecognized: -empty
BusyBox v1.18.3 (2011-02-11 12:08:19 PST) multi-call binary.

Usage: find [PATH]... [EXPRESSION]

Search for files. The default PATH is the current directory,
default EXPRESSION is '-print'

after installing findutils I don't see the error anymore
but I see this mesage:
Code: [Select]
Summary of used and saved space:
---------------------------------------------
Before :   tinycore.gz + mod exts   :  1 MB
After  :   scenic-microcore.gz            :  2 MB
---------------------------------------------

Notes: I used microcore.gz instead of tinycore.gz

trying the new microcore alsa doesn't work anymore.
 I have
Code: [Select]
modprobe snd-opl3sa2
in /opt/bootlocal.sh and I got:
Code: [Select]
modprobe: can't load module soundcore (kernel.tclocal/sound/soundcore.ko.gz): No such file or directory

modprobe: can't load module soundcore (kernel.tclocal/sound/soundcore.ko.gz): No such file or directory
« Last Edit: March 19, 2011, 10:47:38 AM by jls_legalize »
dCore user

Offline gutmensch

  • Retired Admins
  • Hero Member
  • *****
  • Posts: 605
  • I can make it disappear, have no fear!
    • remembrance blog
Re: Local initramfs generation script (stripping unneeded modules)
« Reply #2 on: March 19, 2011, 10:17:29 AM »
Alright, updated in the original post - works now for me with stock microcore.gz and tinycore.gz, thanks!

Don't know what's going wrong with your special microcore.gz - it doesn't seem to be the original one?

[edit]
Are you using copy2fs mode for extensions? I think this script doesn't support this atm ;) but it should only be a matter of switching from kernel to kernel.tclocal dir in the script (if I'm correct).
[/edit]
« Last Edit: March 19, 2011, 10:27:51 AM by gutmensch »
If I seem unduly clear to you, you must have misunderstood what I said. (Alan Greenspan)

Offline jls

  • Hero Member
  • *****
  • Posts: 2135
Re: Local initramfs generation script (stripping unneeded modules)
« Reply #3 on: March 19, 2011, 11:04:47 AM »
I don't use copy2fs and also I use the official microcore.gz.
When I boot the new microcore I see an empty dir: /lib/modules/2.6.33.3-tinycore/kernel.tclocal/

I don't know if this can be a problem but I use persistent home & opt
« Last Edit: March 19, 2011, 11:06:22 AM by jls_legalize »
dCore user

Offline gutmensch

  • Retired Admins
  • Hero Member
  • *****
  • Posts: 605
  • I can make it disappear, have no fear!
    • remembrance blog
Re: Local initramfs generation script (stripping unneeded modules)
« Reply #4 on: March 19, 2011, 11:29:25 AM »
please post
1. complete output of script
2. the content of microcore.gz.modlist
3. the content of yourmicrocore.gz.modlist
4. output of lsmod
5. output of ls /tmp/tcloop
If I seem unduly clear to you, you must have misunderstood what I said. (Alan Greenspan)

Offline jls

  • Hero Member
  • *****
  • Posts: 2135
Re: Local initramfs generation script (stripping unneeded modules)
« Reply #5 on: March 19, 2011, 12:11:10 PM »
your script output:
Code: [Select]
tc@fisso:/tmp$ sudo local_initramfs.sh microcore.gz scenic_microcore.gz
Including modules from: alsa-modules-2.6.33.3-tinycore
Including modules from: input-joystick-2.6.33.3-tinycore
Saving soundcore.ko.gz...
Saving snd-wss-lib.ko.gz...
Saving snd-opl3sa2.ko.gz...
Saving snd-opl3-lib.ko.gz...
Saving snd-mpu401-uart.ko.gz...
Saving snd.ko.gz...
Saving snd-timer.ko.gz...
Saving snd-rawmidi.ko.gz...
Saving snd-pcm.ko.gz...
Saving snd-page-alloc.ko.gz...
Saving snd-hwdep.ko.gz...
Saving snd-seq-device.ko.gz...
Saving ramzswap.ko.gz...
Saving squashfs.ko.gz...
Saving 3c509.ko.gz...
Saving scsi_wait_scan.ko.gz...
Saving parport.ko.gz...
Saving parport_pc.ko.gz...
Saving loop.ko.gz...
Saving ppdev.ko.gz...
Summary of used and saved space:
---------------------------------------------
Before :   microcore.gz + mod exts   :  6 MB
After  :   scenic_microcore.gz            :  2 MB
---------------------------------------------
tc@fisso:/tmp$


Code: [Select]
tc@fisso:/tmp$ lsmod
Module                  Size  Used by    Not tainted
snd_opl3sa2             7320  0
snd_wss_lib            14212  1 snd_opl3sa2
snd_pcm                37628  1 snd_wss_lib
snd_page_alloc          4016  2 snd_wss_lib,snd_pcm
snd_opl3_lib            4888  1 snd_opl3sa2
snd_timer              10564  3 snd_wss_lib,snd_pcm,snd_opl3_lib
snd_hwdep               3004  1 snd_opl3_lib
snd_mpu401_uart         2780  1 snd_opl3sa2
snd_rawmidi            10004  1 snd_mpu401_uart
snd_seq_device          2808  2 snd_opl3_lib,snd_rawmidi
snd                    26200  9 snd_opl3sa2,snd_wss_lib,snd_pcm,snd_opl3_lib,snd_timer,snd_hwdep,snd_mpu401_uart,snd_rawmidi,snd_seq_device
soundcore               2640  1 snd
3c509                   5968  0
ppdev                   3732  0
squashfs               14728 23
parport_pc             18656  0
parport                18560  2 ppdev,parport_pc
scsi_wait_scan           276  0
ramzswap               10240  1
loop                    8068 46
tc@fisso:/tmp$ ls /tmp/tcloop/
Mplayer-nodeps/                   input-joystick-2.6.33.3-tinycore/
Xlibs/                            kmaps/
Xprogs/                           libasound/
Xvesa/                            libpci/
actkbd/                           ncurses/
advcomp/                          ncurses-common/
alsa/                             opera/
alsa-modules-2.6.33.3-tinycore/   pci-utils/
alsa-oss/                         tar/
findutils/                        vim/
flit/                             wbar/
flwm_topside/
tc@fisso:/tmp$

dCore user

Offline gutmensch

  • Retired Admins
  • Hero Member
  • *****
  • Posts: 605
  • I can make it disappear, have no fear!
    • remembrance blog
Re: Local initramfs generation script (stripping unneeded modules)
« Reply #6 on: March 19, 2011, 12:27:03 PM »
that looks ok, mine is also "2 MB" (the script is rounding to the lower value) with alsa modules included, real size is about 2600000 bytes...

can he still not load the soundcore module after booting with it?
« Last Edit: March 19, 2011, 12:47:14 PM by gutmensch »
If I seem unduly clear to you, you must have misunderstood what I said. (Alan Greenspan)

Offline jls

  • Hero Member
  • *****
  • Posts: 2135
Re: Local initramfs generation script (stripping unneeded modules)
« Reply #7 on: March 21, 2011, 10:18:10 AM »
Code: [Select]
tc@fisso:~$ sudo modprobe snd-opl3sa2
modprobe: can't load module soundcore (kernel.tclocal/sound/soundcore.ko.gz): No such file or directory
tc@fisso:~$
infact the module is in kernel not in kernel.tclocal
dCore user

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 11041
Re: Local initramfs generation script (stripping unneeded modules)
« Reply #8 on: March 21, 2011, 10:28:57 AM »
Then it looks to me like depmod has not run properly. If you run "sudo depmod -a" on the running, after-script system, does the modprobe then work?
The only barriers that can stop you are the ones you create yourself.

Offline jls

  • Hero Member
  • *****
  • Posts: 2135
Re: Local initramfs generation script (stripping unneeded modules)
« Reply #9 on: March 21, 2011, 11:15:52 AM »
yes
dCore user

Offline gutmensch

  • Retired Admins
  • Hero Member
  • *****
  • Posts: 605
  • I can make it disappear, have no fear!
    • remembrance blog
Re: Local initramfs generation script (stripping unneeded modules)
« Reply #10 on: March 23, 2011, 12:04:52 PM »
alright, got the same issue like you@jls and now added "-a" to depmod... for my stuff I'm also removing kernel.tclocal but that's up to everyone I think :)

[edit]
now more colors and precise calculations with bc, yeehaww.
[/edit]
If I seem unduly clear to you, you must have misunderstood what I said. (Alan Greenspan)

Offline tinypoodle

  • Hero Member
  • *****
  • Posts: 3857
Re: Local initramfs generation script (stripping unneeded modules)
« Reply #11 on: March 23, 2011, 12:45:53 PM »
Very interesting approach which I think could be of great benefit to many users.

Few observations:
The script seems to ignore OSS modules.
Instead of using 'bc' which creates a dependency, perhaps 'dc' which is in base could be used?
"Software gets slower faster than hardware gets faster." Niklaus Wirth - A Plea for Lean Software (1995)

Offline jls

  • Hero Member
  • *****
  • Posts: 2135
Re: Local initramfs generation script (stripping unneeded modules)
« Reply #12 on: March 23, 2011, 01:12:41 PM »
now it works without depmod
dCore user

Offline gutmensch

  • Retired Admins
  • Hero Member
  • *****
  • Posts: 605
  • I can make it disappear, have no fear!
    • remembrance blog
Re: Local initramfs generation script (stripping unneeded modules)
« Reply #13 on: March 23, 2011, 04:22:24 PM »
@tiny: thanks for testing! :) right now I'm not seeing any reason why it should ignore oss modules, I just modprobed snd-pcm-oss and snd-mixer-oss and it saved them like expected (of course it only saves the modules, which are loaded at runtime, so be sure to have them loaded when you want to have them in your image).

@dc: pheeewww. great idea, really! I love stack computing of numbers, it's so "logical" :D but a good idea anyway, will try to get it done!

[edit]
Alright, removed bc dependency, thanks for the hint@tiny!
[/edit]
« Last Edit: March 23, 2011, 05:33:55 PM by gutmensch »
If I seem unduly clear to you, you must have misunderstood what I said. (Alan Greenspan)

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 11041
Re: Local initramfs generation script (stripping unneeded modules)
« Reply #14 on: March 24, 2011, 05:15:20 AM »
@OSS, IMHO the script should also check for modules at $mod_ext_dir/lib/modules. It's not the standard place, but it's not always possible to place the modules in the usr/local path.
The only barriers that can stop you are the ones you create yourself.