From 526e938b46971645c742a74e3e23b0047f36017f Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 28 Dec 2010 21:37:46 +0000 Subject: [PATCH] Angefangen git-svn-id: http://svn.brehm-online.com/svn/my-stuff/initramfs/trunk@162 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa --- init.sh | 103 +++++++++++++++++++++++++++++++++++++ keymap | Bin 0 -> 2823 bytes makefile.tmpl | 72 ++++++++++++++++++++++++++ makeinitramfs.py | 130 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 305 insertions(+) create mode 100755 init.sh create mode 100644 keymap create mode 100644 makefile.tmpl create mode 100755 makeinitramfs.py diff --git a/init.sh b/init.sh new file mode 100755 index 0000000..bf183f1 --- /dev/null +++ b/init.sh @@ -0,0 +1,103 @@ +#!/bin/busybox ash +# -*- mode: sh -*- + +err () { echo "ERROR: $@"; } +msg () { [ "${quiet}" != "y" ] && echo $@; } + +rescue_shell() { + echo "Irgendwas ging hier schief ... Ich mach hier mal 'ne Shell auf." + busybox --install -s + exec /bin/sh +} + +export CONSOLE="/dev/tty1" +export FIRST_RAID_DEVICE="/dev/md1" +export ROOT="/dev/md3" +#export CRYPTROOT="/dev/sda4" +#export CRYPTNAME="root" +export CONSOLEFONT='ter-v16n' + +mount -t sysfs none /sys || rescue_shell +mount -t proc none /proc || rescue_shell + +read CMDLINE /proc/sys/kernel/printk + +echo "/sbin/modprobe" > /proc/sys/kernel/modprobe + +msg "Creating device nodes ..." +mdev -s +echo /sbin/mdev > /proc/sys/kernel/hotplug + +# assure, that import nodes exist +[ ! -c "$NEWROOT/dev/console" ] && mknod "$NEWROOT/dev/console" c 5 1 +[ ! -c "$NEWROOT/dev/null" ] && mknod "$NEWROOT/dev/null" c 1 3 + +# setup splash screen +#. /etc/initrd.splash +#msg "Setting up splashscreen ..." +#splash init + +# set keymap and console font +msg "Enabling unicode ..." +kbd_mode -u -C /dev/tty1 +printf "\033%%G" >> /dev/console +msg "Loading keymap ..." +loadkmap < /etc/keymap +msg "Setting font ..." +setfont /usr/share/consolefonts/${CONSOLEFONT}.psf.gz -C /dev/tty1 +printf "\033(K" >> /dev/console + +# create device mapper control file +if [ -e "/sys/class/misc/device-mapper" ]; then + if [ ! -c "/dev/mapper/control" ]; then + # create device, if required + msg 'Creating device mapper control node ...' + read dev_t < /sys/class/misc/device-mapper/dev + mknod "/dev/mapper/control" c `echo "$dev_t" | tr ':' ' '` + fi +else + err "Device mapper unavailable, aborting ..." + rescue_shell +fi + +# unlock crypto device +#msg 'Opening luks container ...' +#while ! cryptsetup luksOpen "${CRYPTROOT}" "${CRYPTNAME}" > /dev/null; do +# sleep 2 +#done + +msg "Detecting Linux RAID devices ..." +raidautorun "${FIRST_RAID_DEVICE}" + +# discover and enable lvm volumes +msg 'Scanning lvm volumes ...' +lvm vgscan --ignorelockingfailure > /dev/null +msg 'Enabling lvm volumes ...' +lvm vgchange --ignorelockingfailure -ay > /dev/null + +# try to resume +resume + +# mount root filesystem (readonly so that fsck doesn't choke) +msg 'Mounting root filesystem ...' +mount -o ro "${ROOT}" "/mnt" || rescue_shell + +msg 'Cleaning up ...' +echo > /proc/sys/kernel/hotplug +umount /proc +umount /sys + +msg 'Switching root ...' +exec switch_root /mnt /sbin/init diff --git a/keymap b/keymap new file mode 100644 index 0000000000000000000000000000000000000000..720fb800b4a878e086a451be683f7b514634abe5 GIT binary patch literal 2823 zcmd^>$8sA-6h+_bM$g>FLk0;3P?VUna?UyDoC75)QIx1iQL?CiQMt-nm%|_U8@zIr zH~xTGdF6$A27p9DfGE=Cl`#d>pnLkE&)oa&V03(Jdg9h3Gt;Lx{SRNNNULZyt)aEF zj@Hu#+DMydCH)!n(R;G=RHkH1p2-V&BQNEFJdsKHCm14D%D*Mg<&NBydvaeM%D6m| zzk|Pm>qLGRP0MS!Ew5x$9?OIb({dVd;|Z<(W#p^ymMhjAW5XW%47Lnqm9L?#79G<@ zdpfRJE3Gx@wDuIKtrTZra%Z29lvaLHrxg1}HL&)YnLXt-F?lWA$OUfY1Sh%3ZJgn( zUNUQSMrBo4wMZS*Nk0VL)I+_rg8FHIw$To{8S>+lT#$WoTF%O4IVXqYgq)N=g29M? zmz6 zmpXoj@>Txrw2V7u5rlW4nqO3fKd!!wKdkao@N-bQr*G*BU88q&l}g`t(v*B3%+;UN zOdb8hHP=BtsZjq9K}r8#quL{GykMRF@KwSjP0FNA#$-*- zWuj|B|1;$&MxMh3#Yrepsp0rrsF^esT%i;tD^(-^I~%A@?&5Cl;a=|JejeaK9&-A} z=Fy+jFpt#Kzl|2}|Ie(o&KAz^s?_9PTJ}FF*Z(yRxUsMMzt*a1|1Y%vXKL(ZiPNjC zqS=D=ug4#bhxxY{{zQ~i3+GQ%(bdd9tio!n!CI`tdThW(Y{E*A`UUS{@f1@S!!x|V z8@$8=Ji(-!8$uE;|M47ma2NM*9}h8(M{XuM{4Px6HE!b-M)4RE7{+ph{U7DO>whwA zl+h+;jPZx*YFNk z%fGd=rv9X&=YL)OUH;F}zv7F<`oEn2{{eq_XDHUF9nNLZ1cMwJ(27Ra4{2ngE2I2J v9x-_E0mLClL^JKt^RETX&?rD5<$7ffe_j9A&%a9l*Y!VY=YK-Y_y6Z3;E|sD literal 0 HcmV?d00001 diff --git a/makefile.tmpl b/makefile.tmpl new file mode 100644 index 0000000..a0e63ad --- /dev/null +++ b/makefile.tmpl @@ -0,0 +1,72 @@ +# -*- mode: conf -*- +# init script (damn important!!!) +file /init ${package}/init.sh 755 0 0 + +# directory structure +dir /proc 755 0 0 +dir /sys 755 0 0 +dir /dev 755 0 0 +dir /lib 755 0 0 +dir /bin 755 0 0 +dir /sbin 755 0 0 +dir /mnt 755 0 0 +dir /etc 755 0 0 +dir /usr 755 0 0 +dir /usr/share 755 0 0 + +# basic tools +file /bin/busybox /bin/busybox 755 0 0 +slink /sbin/mdev /bin/busybox 755 0 0 + +# lvm support +dir /dev/mapper 755 0 0 +file /sbin/lvm /sbin/lvm.static 755 0 0 +dir /etc/lvm 755 0 0 +file /etc/lvm/lvm.conf /etc/lvm/lvm.conf 644 0 0 + +# framebuffer support +#file /sbin/v86d /sbin/v86d 755 0 0 + +# cryptsetup support +#file /sbin/cryptsetup /sbin/cryptsetup 755 0 0 + +# console font +dir /usr/share/consolefonts 755 0 0 +file /usr/share/consolefonts/ter-v16n.psf.gz /usr/share/consolefonts/ter-v16n.psf.gz 644 0 0 + +# keymap +file /etc/keymap ${package}/keymap 644 0 0 + +# uswsusp support +#file /sbin/resume /usr/lib/suspend/resume 755 0 0 +#file /etc/suspend.conf /etc/suspend.conf 644 0 0 + +# splash support +#dir /lib/splash 644 0 0 +#dir /lib/splash/sys 644 0 0 +#dir /lib/splash/proc 644 0 0 +#file /sbin/fbcondecor_helper /sbin/fbcondecor_helper 755 0 0 +#slink /sbin/splash_helper /sbin/fbcondecor_helper 755 0 0 +#file /etc/initrd.splash /usr/share/splashutils/initrd.splash 644 0 0 + +# splash theme +#dir /etc/splash 755 0 0 +#dir /etc/splash/natural_gentoo 755 0 0 +#file /etc/splash/natural_gentoo/1280x800.cfg /etc/splash/natural_gentoo/1280x800.cfg 644 0 0 +#dir /etc/splash/natural_gentoo/images 755 0 0 +#file /etc/splash/natural_gentoo/images/silent-1280x800.jpg /etc/splash/natural_gentoo/images/silent-1280x800.jpg 644 0 0 +#file /etc/splash/natural_gentoo/images/verbose-1280x800.jpg /etc/splash/natural_gentoo/images/verbose-1280x800.jpg 644 0 0 + +# important device files +nod /dev/mem 600 0 0 c 1 1 +nod /dev/null 666 0 0 c 1 3 +nod /dev/zero 600 0 0 c 1 5 +nod /dev/console 600 0 0 c 5 1 +nod /dev/tty0 620 0 0 c 4 0 +nod /dev/tty1 600 0 0 c 4 1 + +# device directories +dir /dev/fb 755 0 0 +dir /dev/misc 755 0 0 +dir /dev/vc 755 0 0 + diff --git a/makeinitramfs.py b/makeinitramfs.py new file mode 100755 index 0000000..664d1fb --- /dev/null +++ b/makeinitramfs.py @@ -0,0 +1,130 @@ +#!/usr/bin/env python + +# Copyright (c) 2008, 2009, 2010 Sebastian Wiesner + +# This program is free software. It comes without any warranty, to +# the extent permitted by applicable law. You can redistribute it +# and/or modify it under the terms of the Do What The Fuck You Want +# To Public License, Version 2, as published by Sam Hocevar. See +# http://sam.zoy.org/wtfpl/COPYING for more details. + + +from __future__ import print_function + +import os +import sys +import gzip +import subprocess +from string import Template +from contextlib import closing +from optparse import OptionParser +from shutil import copyfileobj, copy +from tempfile import NamedTemporaryFile +from distutils.spawn import find_executable + + +__version__ = '0.1' + + +PACKAGE = os.path.dirname(os.path.abspath(__file__)) +TEMPLATE = os.path.join(PACKAGE, 'makefile.tmpl') + + +def expand_template(): + """ + Expands the makefile template. + + :param context: Template context + """ + with open(TEMPLATE) as stream: + return Template(stream.read()).substitute(package=PACKAGE) + + +def compile_initramfs(dogzip=False): + """ + Compiles the complete image. + + :param dogzip: If True, compress the image + :return: path of compiled image + """ + with NamedTemporaryFile('w', prefix='makefile', delete=False) as stream: + stream.write(expand_template()) + makefile = stream.name + target = 'initramfs' + print('Generating', target, '...') + opener = gzip.open if dogzip else open + with closing(opener(target, 'wb')) as stream: + proc = subprocess.Popen( + ['/usr/src/linux/usr/gen_init_cpio', makefile], + stdout=subprocess.PIPE) + copyfileobj(proc.stdout, stream) + return target + + +def sudo_install_initramfs(source, kernel): + cmd = ['sudo', 'install', '-m', '644', source, '/boot'] + subprocess.check_call(cmd) + + +def su_install_initramfs(source): + cmd = ['su', '-c', 'install', '-m', '644', source, '/boot'] + subprocess.check_call(cmd) + + +def install_initramfs(source): + """ + Installs initramfs from ``source`` on /boot. + + If this process doesn't have root privileges, sudo and su are tried, + otherwise the initramfs is just copied + + :param source: source file + """ + print('Installing at /boot ...') + if os.geteuid() != 0: + sudo = find_executable('sudo') + if sudo: + return sudo_install_initramfs(source) + su = find_executable('su') + if su: + return su_install_initramfs(source) + else: + copy(source, os.path.join('/boot', os.path.basename(source))) + + +def _license(*args): + print("""\ +Copyright (c) 2008, 2009 Sebastian Wiesner + +This program is free software. It comes without any warranty, to +the extent permitted by applicable law. You can redistribute it +and/or modify it under the terms of the Do What The Fuck You Want +To Public License, Version 2, as published by Sam Hocevar. See +http://sam.zoy.org/wtfpl/COPYING for more details.""") + sys.exit() + + +def main(): + parser = OptionParser( + usage='%prog [-i] [-z]', + description='Initramfs build script', + epilog="""\ +(C) 2009 Sebastian Wiesner """, + version=__version__) + parser.add_option('-i', '--install', help='Install image at /boot', + action='store_true') + parser.add_option('-z', '--gzip', help='Compress the image', + action='store_true') + parser.add_option('-l', '--license', help='Print license and exit', + action='callback', callback=_license) + options, args = parser.parse_args() + initramfs_file = compile_initramfs(options.gzip) + if options.install: + try: + install_initramfs(initramfs_file) + except EnvironmentError as error: + print(error, file=sys.stderr) + + +if __name__ == '__main__': + main() -- 2.39.5