Kaarsemaker.net


make_custom_initramfs
5.46 KB

195 lines

Download

launchpadduser.py
my_ssh

Preview

 1 #!/usr/bin/python
 2 #
 3 # Simple script to make it easier to create a custom initramfs that runs
 4 # something you want in init-premount (that is: in the boot process,
 5 # before disks are mounted). Beware, this is a very restrictive environment
 6 #
 7 # (c) 2009 Dennis Kaarsemaker - Licensed under the GNU GPL v3+
 8 
 9 from __future__ import with_statement
10 
11 # Part 1: Settings
12 # name -- should contain only letters and numbers
13 name = 'hpfirmware'
14 # update-initramfs.conf, must be a valid sh script
15 # See /usr/share/initramfs-tools/update-initramfs.conf
  1 #!/usr/bin/python
  2 #
  3 # Simple script to make it easier to create a custom initramfs that runs
  4 # something you want in init-premount (that is: in the boot process,
  5 # before disks are mounted). Beware, this is a very restrictive environment
  6 #
  7 # (c) 2009 Dennis Kaarsemaker - Licensed under the GNU GPL v3+
  8 
  9 from __future__ import with_statement
 10 
 11 # Part 1: Settings
 12 # name -- should contain only letters and numbers
 13 name = 'hpfirmware'
 14 # update-initramfs.conf, must be a valid sh script
 15 # See /usr/share/initramfs-tools/update-initramfs.conf
 16 update_initramfs_conf = """
 17 update_initramfs=yes
 18 backup_initramfs=no
 19 """
 20 # initramfs.conf, must be a valid sh script
 21 # See /usr/share/initramfs-tools/initramfs.conf
 22 initramfs_conf = """
 23 MODULES=netboot
 24 BUSYBOX=y
 25 BOOT=local
 26 DEVICE=eth0
 27 NFSROOT=auto
 28 """
 29 # Modules
 30 modules = """
 31 # Needed for dell optiplex 755
 32 heci
 33 e1000_ich9
 34 e1000e
 35 """
 36 
 37 # Part 2: Hook. See existing initramfs hooks for examples.
 38 #
 39 # This must be a valid sh script. It can be used to add or remove things
 40 # from the initramfs that is being built. The existing initramfs-tools
 41 # live in /usr/share/initramfs-tools/hooks.
 42 hook = """PREREQ=""
 43 prereqs()
 44 {
 45     echo "$PREREQ"
 46 }
 47 
 48 case $1 in
 49 prereqs)
 50      prereqs
 51      exit 0
 52      ;;
 53 esac
 54 
 55 . /usr/share/initramfs-tools/hook-functions
 56 
 57 # Drop usplash stuff
 58 rm -f ${DESTDIR}/sbin/usplash
 59 rm -f ${DESTDIR}/lib/libusplash.so.0
 60 rm -f ${DESTDIR}/lib/libx86.so.1
 61 rm -f ${DESTDIR}/sbin/usplash_write
 62 rm -f ${DESTDIR}/etc/usplash.conf
 63 rm -f ${DESTDIR}/usr/lib/usplash/usplash-artwork.so
 64 
 65 #Drop fuse/ntfs-3g
 66 rm -f ${DESTDIR}/sbin/mount.fuse
 67 rm -f ${DESTDIR}/lib/modules/2.6.27-11-generic/kernel/fs/fuse/fuse.ko
 68 rm -f ${DESTDIR}/bin/ntfs-3g
 69 rm -f ${DESTDIR}/lib/libfuse.so.2
 70 rm -f ${DESTDIR}/lib/librt.so.1
 71 rm -f ${DESTDIR}/lib/libdl.so.2
 72 rm -f ${DESTDIR}/lib/libntfs-3g.so.28
 73 rm -f ${DESTDIR}/lib/libpthread.so.0
 74 
 75 # Copy over some binaries we need
 76 mkdir -p $DESTDIR/usr/bin/ $DESTDIR/usr/sbin
 77 copy_exec /usr/sbin/dmidecode /usr/sbin
 78 rm -f ${DESTDIR}/bin/gzip
 79 copy_exec /bin/gzip /bin
 80 copy_exec /bin/rm /bin
 81 copy_exec /bin/tar /bin
 82 copy_exec /usr/bin/tail /usr/bin
 83 copy_exec /bin/mktemp /bin
 84 copy_exec /bin/echo /bin
 85 copy_exec /bin/date /bin
 86 copy_exec /usr/bin/wget /usr/bin
 87 copy_exec /usr/bin/which /usr/bin
 88 copy_exec /usr/bin/yes /usr/bin
 89 """
 90 
 91 # Part 3: Script.
 92 #
 93 # This bust be a valid sh (ash, not bash, so no bashisms) script. It is run
 94 # in the very restrictive initramfs environment. Make sure you add a call to
 95 # reboot at the end of this script. Look at existing scripts for examples.
 96 # These live in /usr/share/initramfs-tools/scripts/
 97 #
 98 # This new script will be run in init-premount, so before any disk mounting
 99 # is attempted
100 script = """PREREQ="udev"
101 prereqs()
102 {
103      echo "$PREREQ"
104 }
105 
106 case $1 in
107 prereqs)
108      prereqs
109      exit 0
110      ;;
111 esac
112 
113 iface=eth0
114 for arg in $(cat /proc/cmdline); do
115     case $arg in
116         iface=*)
117             iface=${arg#iface=}
118             ;;
119     esac
120 done
121 
122 slumber=20
123 while [ 0 ]; do
124     /bin/ipconfig $iface
125     ret=$?
126     if [ $ret -eq 0 ]; then
127         break
128     fi
129     if [ $slumber -lt 1 ]; then
130         echo "Can't bring up $iface"
131         sleep 5
132         reboot
133     fi
134     sleep 0.5
135     slumber=$(( ${slumber} - 1))
136     echo "Slumber: $slumber"
137 done
138 echo "This machine is:"
139 /usr/sbin/dmidecode -s system-manufacturer
140 /usr/sbin/dmidecode -s bios-version
141 /usr/sbin/dmidecode -s bios-release-date
142 /usr/sbin/dmidecode -s system-product-name
143 
144 machine=`/usr/sbin/dmidecode -s system-product-name |  awk '{ print $2 $3 }'`
145 echo "Getting firmware for $machine ..."
146 /usr/bin/wget http://10.147.103.2/firmware/hp/server/$machine.scexe
147 yes | sh $machine.scexe 
148 reboot
149 """
150 
151 # Part 4: The actual work. Do not change unless you know what you are doing.
152 
153 # Create our workdir
154 import os.path
155 import tempfile
156 import shutil
157 import subprocess
158 
159 def make_initramfs():
160     # Copy kernel
161     kernel_version = subprocess.Popen(['uname', '-r'], stdout=subprocess.PIPE).communicate()[0].strip()
162     shutil.copy(os.path.join('/boot', 'vmlinuz-' + kernel_version), os.path.join(workdir,'vmlinuz-%s' % name))
163     # Create our mkinitramfs config
164     hookf = os.path.join(workdir,'initramfs-tools','hooks','custom_%s' % name)
165     scriptf = os.path.join(workdir,'initramfs-tools','scripts','init-premount','custom_%s' % name)
166     os.makedirs(os.path.dirname(hookf))
167     os.makedirs(os.path.dirname(scriptf))
168     with open(hookf,'w') as fd:
169         fd.write(hook)
170     os.chmod(hookf, 0755)
171     with open(scriptf,'w') as fd:
172         fd.write(script)
173     os.chmod(scriptf, 0755)
174     with open(os.path.join(workdir,'initramfs-tools','initramfs.conf'),'w') as fd:
175         fd.write(initramfs_conf)
176     with open(os.path.join(workdir,'initramfs-tools','update-initramfs.conf'),'w') as fd:
177         fd.write(update_initramfs_conf)
178     with open(os.path.join(workdir,'initramfs-tools','modules'),'w') as fd:
179         fd.write(modules)
180     # Call mkinitramfs
181     subprocess.Popen(['mkinitramfs', '-v', # add '-k' here to keep the initramfs build root for inspection
182                       '-d', os.path.join(workdir, 'initramfs-tools'),
183                       '-o', os.path.join(workdir, 'initrd-%s.img' % name),
184                       kernel_version]).wait()
185     shutil.rmtree(os.path.join(workdir,'initramfs-tools'))
186 
187 workdir = tempfile.mkdtemp()
188 try:
189     make_initramfs()
190     print "Your custom vmlinuz and initramfs can be found in %s and %s" % \
191           (os.path.join(workdir, 'vmlinuz-%s' % name),
192            os.path.join(workdir, 'initrd-%s' % name))
193 except:
194     print "Creating initramfs failed. Inspect %s for details" % workdir
195     raise

Show all