Solved! (was Re: getting linux to mount a FAT16 part at boot timeso I can give it an initrd path?)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Yay for Linux!  Here's how I solved the problem of distributing a
kernel and a root filesystem as files on a compact flash card in a
single, standard FAT partition:

1.  copy the kernel onto the flash card
2.  make an ext2 image file and copy that onto the flash
3.  instruct my bootloader to load the kernel and boot it, with the
    root= argument set to point to the flash's msdos partition
4.  create a /dev directory on the flash card
5.  compile the included program and call it /linuxrc on the msdos
    partition

The way it works:

1.  linux boots, and mounts my flash card's msdos partition as root.
2.  linux executes my linuxrc program, which
    a.  finds the ext2 filesystem image file, and configures loopback
        on /dev/loop/0
    b.  mounts /dev/loop/0 on /mnt and chdirs and chroots to it
    c.  does pivot_root
    d.  remounts devfs on the new root /dev
    e.  runs the REAL linuxrc!

/**
***	linuxrc.c:   Mount /initrd.bin loopback as ext2 and pivot_root to it
***
***	Usage:
***		vmlinux init=linuxrc
***
***	David Wuertele		Tue May 20 16:14:35 2003
***
**/

#include <fcntl.h>
#include <stdio.h>

#include <linux/posix_types.h>
#undef dev_t
#define dev_t __kernel_dev_t
#include <linux/loop.h>
#undef dev_t

int
main (int argc, char *const argv[])
{
  struct loop_info loopinfo;
  int fd, ffd;

  ffd = open ("/initrd.bin", O_RDWR);
  fd = open ("/dev/loop/0", O_RDWR);

  memset (&loopinfo, 0, sizeof(loopinfo));
  strncpy (loopinfo.lo_name, "/initrd.bin", LO_NAME_SIZE);
  loopinfo.lo_offset = 0;
  loopinfo.lo_encrypt_key_size = 0;
  ioctl (fd, LOOP_SET_FD, ffd);
  ioctl (fd, LOOP_SET_STATUS, &loopinfo);
  close (fd);
  close (ffd);
  mount ("/dev/loop/0", "/mnt", "ext2", 0, NULL);
  chdir ("/mnt");
  chroot ("/mnt");
  pivot_root (".", "/mnt/tmproot");
  chdir ("/");
  mount ("devfs", "/dev", "devfs", 0, NULL);

  execv ("/linuxrc", argv);
  return 0;
}

Thanks for all your help, guys!

Dave

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux