Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
20 years ago

uClinux feature support

Small question to NIOS2 uClinux users...

Has anyone used, nios2 uClinux with initrd support or RAMDisk ?

Is anyone using any alternative other than romfs.img support mounted as

readonly-root fs supplied as part of evalutaion kit ?

Any alternative suggestions to mount and use a r/w filesystem will be deeply appreciated.

1 Reply

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Here is something you can try if you want to use a RAMdisk as the root fs:

    Busybox comes with the chroot and pivot_root utilities, so you can use those to change the root fs to your RAMdisk once it is mounted.

    What you have to do is create a ramdisk image that has all the files and directories needed for your root filesystem.

    A ramdisk image can be created on a Linux host as shown:

    dd if=/dev/zero of=/dev/ram0 bs=1k count=2048
    /sbin/mke2fs -s0 -vm0 /dev/ram0 2048
    /sbin/tune2fs -i 0 /dev/ram0
    mount /dev/ram0 /mnt
    rmdir /mnt/lost+found
    { populate your filesystem here}
    umount /mnt
    dd if=/dev/ram0 of=ramfs2048 bs=1k count=2048
    ./holes ramfs2048 > ramfs2048.img

    The holes utility compresses the ramdisk image, here is the source for that utility:

    /* make a file with holes */
    # include <unistd.h># include <stdlib.h># include <stdio.h># include <sys/types.h># include <sys/stat.h># include <fcntl.h>
    char buf;
    int
    main(int argc, char *argv)
    {
      int count;
      int pos;
      int zcount;
      int b;
      int len;
      int in = open(argv, O_RDONLY);
      
      len = lseek(in, 0, SEEK_END);
    fprintf(stderr, "TOTAL LEN=%x", len);
      
      lseek(in, 0, SEEK_SET);
      
      len = htonl(len);
      
      write(1, &len, 4);
    fprintf(stderr, "\n", len);
      pos = zcount = count = 0;
      while(read(in, &buf, 1) > 0) {
        pos++;
        if (count || buf) {
          count++;
          if (buf == 0) 
            zcount++;
          if ((zcount == 8) || (count == 2048)) {
              b = htonl(pos-count);
              write(1,&b,4);
    fprintf(stderr, "POS=%x:", pos-count, b);
              b = htonl(count);
              write(1,&b,4);
    fprintf(stderr, "LEN=%x", count, b);
            write(1,buf,count);
    fprintf(stderr, "    -->   DATA=%x\n", buf);
        count = zcount = 0;
          }
        }
      }
      if (count) {
        b = htonl(pos-count);
        write(1,&b,4);
    fprintf(stderr, "(%d)=%x\n", 4, b);
        b = htonl(count);
        write(1,&b,4);
        write(1,buf,count);
    fprintf(stderr, "(%d)=%x    -->   (%d)=%x\n", 4, b, count, buf);
      }
    }

    Once you have your ramdisk image, you can include it in your Microtronix Nios II Filesystem Project. You can then edit your /etc/inittab and /etc/init.d/rcS files to use pivot_root and chroot on start up to switch the root filesystem to a RAMdisk during start up.

    For example, your /etc/inittab file might look something like this:

    ::askfirst:-/bin/sh
    ::sysinit:/etc/init.d/rcS

    And your /etc/init.d/rcS file could include something like this :

    ...#  for pivot root
    /bin/expand /ramdisk.img /dev/ram0
    /bin/mount -t ext2 /dev/ram0 /mnt/ide0 -n
    cd /mnt/ide0
    mkdir old-root
    pivot_root . old-root
    exec chroot . sh <dev/console >dev/console 2>&1
    umount /old-root
    ...