On Wed, Dec 26, 2012 at 1:49 PM, Mark Knecht <markkne...@gmail.com> wrote:
<SNIP>
> Actually, I think the initramfs wiki covers this, albeit it's 'not
> easy'. Their words.
>
> I think I'm covered for now.
>
> Cheers,
> Mark

While extracting it from the kernel image may be involved, and for now
using Neil's example init script which probably isn't exactly right
for my needs, there is an intermediate step I quickly discovered.
After building the new kernel there is an initramfs_data_cpio file
created in /usr/src/linux/usr. I copied this file to a new directory
under /root and then ran

cpio -i -d -H newc --no-absolute-filenames < initramfs_data.cpio

which creates all of the initramfs file structure in this directory. I
can then list it and it appears everything is there:


c2stable initramfs-work # ls -lR
.:
total 2620
drwxr-xr-x 2 root root    4096 Dec 26 14:00 bin
drwxr-xr-x 2 root root    4096 Dec 26 14:00 dev
drwxr-xr-x 2 root root    4096 Dec 26 14:00 etc
-rwxr-xr-x 1 root root    2017 Dec 26 14:00 init
-rw-r--r-- 1 root root 2640384 Dec 26 13:59 initramfs_data.cpio
drwxr-xr-x 2 root root    4096 Dec 26 14:00 lib
drwxr-xr-x 2 root root    4096 Dec 26 14:00 proc
drwxr-xr-x 2 root root    4096 Dec 26 14:00 realroot
drwxr-xr-x 2 root root    4096 Dec 26 14:00 sbin
drwxr-xr-x 2 root root    4096 Dec 26 14:00 sys

./bin:
total 2020
-rwxr-xr-x 1 root root 2061600 Dec 26 14:00 busybox
lrwxrwxrwx 1 root root       7 Dec 26 14:00 sh -> busybox

./dev:
total 0
crw------- 1 root root 5, 1 Dec 26 14:00 console
crw-rw-rw- 1 root root 1, 3 Dec 26 14:00 null
crw-rw-rw- 1 root root 5, 0 Dec 26 14:00 tty
crw-rw-rw- 1 root root 1, 9 Dec 26 14:00 urandom

./etc:
total 4
-rwxr-xr-x 1 root root 3017 Dec 26 14:00 mdadm.conf

./lib:
total 4
-rwxr-xr-x 1 root root 528 Dec 26 14:00 libext2fs.so

./proc:
total 0

./realroot:
total 0

./sbin:
total 568
-rwxr-xr-x 1 root root 216744 Dec 26 14:00 e2fsck
-rwxr-xr-x 1 root root 353928 Dec 26 14:00 mdadm

./sys:
total 0
c2stable initramfs-work #


c2stable initramfs-work # cat init
#!/bin/busybox sh

rescue_shell() {
        busybox --install -s
        echo $1
        exec /bin/sh
        }

### Mount the /proc and /sys filesystems.
mount -t proc none /proc
grep -q initdebug </proc/cmdline && set -x && DEBUG=1
mount -t sysfs none /sys
mount -t devtmpfs none /dev

### Get root and init parameters from cmdline
REAL_ROOT="$(sed 's/.*root=\(\S*\).*/\1/' <proc/cmdline)"
if grep -q init= </proc/cmdline; then
        REAL_INIT="$(sed 's/.*init=\(\S*\).*/\1/' <proc/cmdline)"
else
        REAL_INIT="/sbin/init"
        fi

### Assemble RAID
[[ -f /sbin/mdadm ]] && mdadm --assemble --scan

### Initialise LVM
if [[ -f /sbin/lvm.static ]]; then
        lvm.static vgchange -a y
        lvm.static vgscan --mknodes
        [[ -n "$DEBUG" ]] && sleep 10
        fi

### Mount the root filesystem.
if [[ -f /sbin/cryptsetup ]] && cryptsetup isLuks $REAL_ROOT; then
        busybox echo -e "\e[0;32m\n=====================\nEncrypted
root device\n=====================\n\e[m"
    cryptsetup luksOpen $REAL_ROOT cryptroot
    REAL_ROOT="/dev/mapper/cryptroot"
    [[ -n "$DEBUG" ]] && echo $REAL_ROOT && sleep 10
    fi
mount -o ro $REAL_ROOT /realroot || rescue_shell "Could not mount REAL_ROOT"
[[ -n "$DEBUG" ]] && df -h /realroot && sleep 10

### Mount /usr if separate
if grep -qE '^[^#].*\s\/usr\s' /realroot/etc/fstab; then
    #mount $(awk '/\s\/usr\s/ {print $1, "/realroot"$2, "-t", $3,
"-o", $4 ",ro"}' /realroot/etc/fstab)
    mount $(awk '/^[^#].*\s\/usr\s/ {print $1, "/realroot"$2, "-t",
$3, "-o", $4 ",ro"}' /realroot/etc/fstab)
    [[ -n "$DEBUG" ]] && df -h /realroot//usr && sleep 10
    fi

grep -q waitforkey </proc/cmdline && WAITFORKEY=1
grep -q rescue </proc/cmdline && rescue_shell "...because you asked for it."

### Clean up.
umount /dev
umount /sys
umount /proc

### Boot the real thing
[[ -n "$DEBUG" ]] && echo "Ready to switch" && sleep 10
[[ -n "$WAITFORKEY" ]] && busybox showkey -a
exec busybox switch_root /realroot $REAL_INIT || rescue_shell "Failed
to switch_root"
c2stable initramfs-work #



At this point I don't know that 1) the image is actually in the
kernel, or 2) that my "init thingy" ;-) image would work, but at least
the process of putting it together is verifiable.


Cheers,
Mark

Reply via email to