Re: Booting into /bin/bash

From: Richard B. Johnson (root@chaos.analogic.com)
Date: Mon Sep 11 2000 - 16:48:33 EST


On Mon, 11 Sep 2000, Adam wrote:

> > Does anybody know off the top of their head if there is an easy
> > way to have ^C work with /bin/bash as a shell, without having
> > to set up ptys?? Just setting terminal parameters to allow signals
> > doesn't do anything.
>
> not exactly the answer, but what I do is just run command 'open bash' few
> times and create in this way few ready-to-use shells.
>

Whmm. I have a shell-script that makes a RAM-Disk bootable "Rescue Disk".
It allows one to boot from two floppies and repair stuff, even execute
vi, fdisk, mke2fs, tar, tar-gz, etc. Just about everything one would
need (even modules) to rescue a system.

However, ^C does not stop anything. No signal gets sent to anybody.
I don't want to make it too large because it won't fit on a floppy
if I do.

I thought I did everything right, but....

The shell-script is appended.

Cheers,
Dick Johnson

Penguin : Linux version 2.2.15 on an i686 machine (797.90 BogoMips).

"Memory is like gasoline. You use it up when you are running. Of
course you get it all back when you reboot..."; Actual explanation
obtained from the Micro$oft help desk.


#!/bin/sh
#
#
export VER=$1
RAMDISK_IMAGE=/tmp/RamImage-${VER}
RAMDISK=/tmp/Ramdisk
TMPF=/tmp/TmpExe
TMPC=/tmp/TmpC.c
DISKSIZE=4096
SYS=/usr/src/linux-${VER}/arch/i386/boot/bzImage
if [ "$1" = "" ] ;
   then
       echo "Usage:"
       echo "Make_Rescue <version>"
       exit 1
fi
if [ ! -f ${SYS} ] ;
   then
    echo "File not found, ${SYS}"
    exit 1
fi
if ! dd if=/dev/fd0 of=/dev/null bs=1k count=1 2>/dev/null ;
    then
       echo "Floppy drive error!"
       echo "Maybe no diskette in the drive?"
       exit 1
fi
#
# Make a RAM Disk file and mount it using the loop device.
# Remove the lost+found directory to save space.
#

umount ${RAMDISK} 2>/dev/null
rm -rf ${RAMDISK} 2>/dev/null
mkdir ${RAMDISK} 2>/dev/null
dd if=/dev/zero of=${RAMDISK_IMAGE} bs=1k count=${DISKSIZE}
/sbin/mke2fs -Fq ${RAMDISK_IMAGE} ${DISKSIZE}
mount -o loop -t ext2 ${RAMDISK_IMAGE} ${RAMDISK}
rmdir ${RAMDISK}/lost+found
#
# Make the required directories in the RAM Disk.
#
mkdir -m 777 ${RAMDISK}/dev
mkdir -m 777 ${RAMDISK}/etc
mkdir -m 777 ${RAMDISK}/lib
mkdir -m 777 ${RAMDISK}/usr
mkdir -m 777 ${RAMDISK}/usr/local
mkdir -m 777 ${RAMDISK}/usr/bin
mkdir -m 777 ${RAMDISK}/sbin
mkdir -m 777 ${RAMDISK}/tmp
mkdir -m 777 ${RAMDISK}/proc
mkdir -m 777 ${RAMDISK}/mnt
#
# Make the required devices.
#
mknod ${RAMDISK}/dev/null c 1 3
mknod ${RAMDISK}/dev/ram0 b 1 0
mknod ${RAMDISK}/dev/ram1 b 1 1
mknod ${RAMDISK}/dev/mem c 1 1
mknod ${RAMDISK}/dev/ttyS0 c 4 64
mknod ${RAMDISK}/dev/tty0 c 4 0
mknod ${RAMDISK}/dev/tty1 c 4 1
mknod ${RAMDISK}/dev/tty2 c 4 2
mknod ${RAMDISK}/dev/tty3 c 4 3
mknod ${RAMDISK}/dev/tty4 c 4 4
mknod ${RAMDISK}/dev/tty c 5 0
mknod ${RAMDISK}/dev/ttyp0 c 3 0
mknod ${RAMDISK}/dev/ttyp1 c 3 1
mknod ${RAMDISK}/dev/ttyp2 c 3 2
mknod ${RAMDISK}/dev/ttyp3 c 3 3
mknod ${RAMDISK}/dev/ttyp4 c 3 4
mknod ${RAMDISK}/dev/ttyp5 c 3 5
mknod ${RAMDISK}/dev/ptyp0 c 2 0
mknod ${RAMDISK}/dev/ptyp1 c 2 1
mknod ${RAMDISK}/dev/ptyp2 c 2 2
mknod ${RAMDISK}/dev/ptyp3 c 2 3
mknod ${RAMDISK}/dev/ptyp4 c 2 4
mknod ${RAMDISK}/dev/ptyp5 c 2 5
mknod ${RAMDISK}/dev/zero c 1 5
mknod ${RAMDISK}/dev/sda b 8 0
mknod ${RAMDISK}/dev/sda1 b 8 1
mknod ${RAMDISK}/dev/sda2 b 8 2
mknod ${RAMDISK}/dev/sdb b 8 16
mknod ${RAMDISK}/dev/sdb1 b 8 17
mknod ${RAMDISK}/dev/sdb2 b 8 18
mknod ${RAMDISK}/dev/sdc b 8 32
mknod ${RAMDISK}/dev/sdc1 b 8 33
mknod ${RAMDISK}/dev/sdc2 b 8 34
mknod ${RAMDISK}/dev/st0 c 9 0
mknod ${RAMDISK}/dev/st1 c 9 1
mknod ${RAMDISK}/dev/st2 c 9 2
mknod ${RAMDISK}/dev/st3 c 9 4
mknod ${RAMDISK}/dev/st4 c 9 5
mknod ${RAMDISK}/dev/fd0 b 2 0
mknod ${RAMDISK}/dev/hda b 3 0
mknod ${RAMDISK}/dev/hda1 b 3 1
mknod ${RAMDISK}/dev/hda2 b 3 2
#
# Set some compatibility links.
#
ln -s /dev/tty0 ${RAMDISK}/dev/systty
ln -s /dev/tty0 ${RAMDISK}/dev/console
ln -s /dev/ram1 ${RAMDISK}/dev/ram
ln -s /lib ${RAMDISK}/usr/lib
ln -s /sbin ${RAMDISK}/bin
ln -s /lib ${RAMDISK}/usr/local/lib
ln -s /lib/ld-linux.so.2 ${RAMDISK}/lib/ld.so
ln -s /sbin/bash ${RAMDISK}/sbin/sh
ln -s /sbin/gunzip ${RAMDISK}/sbin/gzip
#
#
# Copy some files and libraries.
#
files="modprobe bash rm mkdir rmdir cat ls cp mount umount \
insmod gunzip tar df"

for x in $files ; do cp `which $x` ${TMPF} ; strip ${TMPF} ;\
  cp ${TMPF} ${RAMDISK}/sbin/$x ; done
 
cp /lib/libc.so.6 ${TMPF}
strip ${TMPF}
cp ${TMPF} ${RAMDISK}/lib/libc.so.6

cp /lib/libm.so.6 ${TMPF}
strip ${TMPF}
cp ${TMPF} ${RAMDISK}/lib/libm.so.6

cp /lib/libtermcap.so.2 ${TMPF}
strip ${TMPF}
cp ${TMPF} ${RAMDISK}/lib/libtermcap.so.2
rm ${TMPF}
#
# Get some modules.
#
cp /lib/modules/${VER}/scsi/scsi_mod.o ${RAMDISK}/lib
cp /lib/modules/${VER}/scsi/sd_mod.o ${RAMDISK}/lib
cp /lib/modules/${VER}/scsi/BusLogic.o ${RAMDISK}/lib
cp /lib/modules/${VER}/scsi/st.o ${RAMDISK}/lib
cp /lib/ld-linux.so.2 ${RAMDISK}/lib
#
# Make termcap so vi will work.
#
cat - <<EOF >${RAMDISK}/etc/termcap
# From: Eric S. Raymond <esr@snark.thyrsus.com> 23 July 1995
linux|linux console:\\
        :am:bs:eo:mi:ms:ut:xn:xo:\\
        :Co#8:co#80:it#8:li#25:pa#64:\\
        :&7=^Z:@7=\E[4~:AB=\E[4%p1%dm:AF=\E[3%p1%dm:\\
        :AL=\E[%dL:DC=\E[%dP:DL=\E[%dM:F1=\E[23~:F2=\E[24~:\\
        :F3=\E[25~:F4=\E[26~:F5=\E[28~:F6=\E[29~:F7=\E[31~:\\
        :F8=\E[32~:F9=\E[33~:FA=\E[34~:IC=\E[%d@:K2=\E[G:\\
        :S2=\E[11m:S3=\E[10m:Sb=\E[%+(m:Sf=\E[%+^^m:\\
        :ac=\`\004a\261f\370g\361h\260j\331k\277l\332m\\
\300n\305o~q\304r\362s_t\303u\264v\301w\302x\263y\371z\\
\372{\373|\374}\375~\376.\031-\030\054\021+^P0\333:\\
        :ae=\E[10m:al=\E[L:as=\E[11m:bl=^G:cd=\E[J:ce=\E[K:\\
        :cl=\E[H\E[J:cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:\\
        :ct=\E[3g:dc=\E[P:dl=\E[M:do=^J:ei=\E[4l:ho=\E[H:\\
        :ic=\E[@:im=\E[4h:k1=\E[[A:k2=\E[[B:k3=\E[[C:\\
        :k4=\E[[D:k5=\E[[E:k6=\E[17~:k7=\E[18~:k8=\E[19~:\\
        :k9=\E[20~:k;=\E[21~:kD=\E[3~:kI=\E[2~:kN=\E[6~:\\
        :kP=\E[5~:kb=^H:kd=\E[B:kh=\E[1~:kl=\E[D:kr=\E[C:\\
        :ku=\E[A:le=^H:mb=\E[5m:md=\E[1m:me=\E[0;10m:\\
        :mr=\E[7m:nd=\E[C:nw=^M^J:op=\E[37;40m:r1=\Ec:rc=\E8:\\
        :..sa=\E[0;10%?%p1%t;7%;%?%p2%t;4%;%?%p3%t;7%;%?%p4%t\\
;5%;%?%p6%t;1%;%?%p7%t;8%;%?%p8%t;11%;%?%p9%t;11%;m:\\
        :sc=\E7:se=\E[m:sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:\\
        :u6=\E[%d;%dR:u7=\E[6n:u8=\E[?6c:u9=\E[c:ue=\E[24m:\\
        :up=\E[A:us=\E[4m:vb=\E[?5h\E[?5l:ve=\E[?25h:\\
        :vi=\E[?25l:
EOF
cat - <<EOF >${RAMDISK}/.bashrc
alias ls='ls --color'
EOF
#
# Make a little program called init.
#
cat - <<EOF >${TMPC}
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/mount.h>
#include <signal.h>
#include <termios.h>
char *argv[] = { "/bin/bash", NULL };
char *environ[] = { "PATH=/bin:/sbin:/usr/bin", "TERM=linux" };
char *modules[]= { "/lib/scsi_mod.o", "/lib/sd_mod.o",
                   "/lib/BusLogic.o", "/lib/st.o", NULL };
char *inst[] = {"/sbin/insmod", NULL, NULL };
char msg[]="Put your tools floppy in drive A: and hit [Enter] ";
char prc[]="/proc";
static const char fixscr[]= "\033[H\033[J\017\033[m$<2>"
"\033[m$<2>\033>\033[?3l\033[?4l\033[?5l\033[?7h\033[?8h\033[1;25r";

int install(char *module) {
    int stat;
    stat = 0;
    switch(fork()) {
    case 0:
        inst[1] = module;
        (void)execv(inst[0], inst);
    case -1:
        stat = -1;
    default:
        (void)wait(&stat);
        stat >>= 0x08;
    }
    return stat;
}

void reaper(int unused) {
    while(wait3(&unused, WNOHANG, NULL) > 0)
                   ;
}
void stopper(int unused) { pause(); }
void starter(int unused) {}
void quit(int unused) { fprintf(stderr,"^C\n"); exit(1); }

int main()
{
    int stat, i;
    sigset_t sig;
    struct termios term;
    struct sigaction sa;

    i = 0;
    (void)mount(&prc[1], prc, &prc[1], 0, NULL);
    while(!!modules[i])
        (void)install(modules[i++]);
    (void)write(1, msg, sizeof(msg) -1);
    (void)read(0, msg, sizeof(msg));
    (void)mount("/dev/fd0", "/usr/bin", "ext2", 0, NULL);
    (void)sigemptyset(&sig);
    (void)sigprocmask(SIG_SETMASK, &sig, NULL);
    memset(&sa, 0x00, sizeof(sa));
    sa.sa_handler = reaper;
    sa.sa_flags = SA_RESTART;
    (void)sigaction(SIGCHLD, &sa, NULL);
    sa.sa_handler = stopper;
    (void)sigaction(SIGTSTP, &sa, NULL);
    sa.sa_handler = stopper;
    (void)sigaction(SIGSTOP, &sa, NULL);
    sa.sa_handler = starter;
    (void)sigaction(SIGCONT, &sa, NULL);
    sa.sa_handler = SIG_IGN;
    (void)sigaction(SIGINT, &sa, NULL);
    memset(&term, 0x00, sizeof(term));
    term.c_cc[VINTR] = (char) 'C' - 64;
    term.c_cc[VQUIT] = (char) '\\\\' - 64;
    term.c_cc[VKILL] = (char) 'X' - 64;
    term.c_cc[VEOF] = (char) 'D' - 64;
    term.c_cc[VSTART] = (char) 'Q' - 64;
    term.c_cc[VSTOP] = (char) 'S' - 64;
    term.c_cc[VSUSP] = (char) 'Z' - 64;
    term.c_cc[VERASE] = (char) 127;
    term.c_cc[VTIME] = (char) 0;
    term.c_cc[VMIN] = (char) 1;
    term.c_oflag = OPOST|ONLCR;
    term.c_iflag = ICRNL|IXON;
    term.c_lflag = ICANON|ECHO|ECHOPRT|ECHOCTL|ECHOKE|IEXTEN;
    term.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
    for(;;) {
        (void)tcsetattr(0, TCSANOW, &term);
        (void)write(0, fixscr, sizeof(fixscr) -1 );
        switch(fork()) {
        case 0:
            sa.sa_handler = quit;
            (void)sigaction(SIGINT, &sa, NULL);
            (void)execve(argv[0], argv, environ);
        case -1:
            fprintf(stderr, "Fork failed\n");
            break;
        default:
            (void)wait(&stat);
        }
        (void)sleep(1);
    }
}
EOF
gcc -Wall -o ${RAMDISK}/sbin/init ${TMPC}
rm ${TMPC}

#
#
# Unmount the RAM Disk. Remove its mount-point but save the file itself.
#
sync
df ${RAMDISK}
umount ${RAMDISK}
rmdir ${RAMDISK}
sync
#
# Make an ext2 file-system on a floppy and mount it. Remove the
# lost+found directory to save space.
#
umount /mnt 2>/dev/null
/sbin/mke2fs -q /dev/fd0
mount -t ext2 /dev/fd0 /mnt
rmdir /mnt/lost+found
#
# Compress the RAM Disk image into a file on the mounted file-system.
# Remove the original RAM Disk image, then copy the required boot
# files to the mounted file-system also.
#
gzip -9 < ${RAMDISK_IMAGE} >/mnt/initrd-${VER}
rm ${RAMDISK_IMAGE}
cp ${SYS} /mnt/vmlinuz-${VER}
cp /boot/boot.b /mnt/boot.b
#
# Now execute lilo to install the boot-loader onto the mounted file-
# system. Lilo allows its configuration to be taken from standard input.
#
/sbin/lilo -C - <<EOF
#
# Lilo boot-configuration script.
#
boot = /dev/fd0
map = /mnt/map
backup = /dev/null
compact
vga = normal # force sane state
 install = /mnt/boot.b
 image = /mnt/vmlinuz-${VER}
 initrd = /mnt/initrd-${VER}
 root = /dev/ram0
 label = RAMDISK_Rescue
EOF
#
# Show the results and unmount the file-system.
#
df /dev/fd0
umount /dev/fd0
#
echo "Remove boot disk from drive A: and insert another."
echo -n "This will be used for some tools. Hit [Enter] when ready... "
read
/sbin/mke2fs -q /dev/fd0
mount -t ext2 /dev/fd0 /mnt
rmdir /mnt/lost+found
cp `which vi` /mnt/vi
cp `which fdisk` /mnt/fdisk
cp `which mke2fs` /mnt/mke2fs
umount /mnt
echo "All done!"


-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Fri Sep 15 2000 - 21:00:16 EST