Diff for /wikisrc/tutorials/how_to_create_bootable_netbsd_image.mdwn between versions 1.1 and 1.2

version 1.1, 2011/11/20 20:55:21 version 1.2, 2012/02/05 07:14:36
Line 1 Line 1
 **Contents**  **Contents**
   
 [[!toc levels=2]]  [[!toc levels=2]]
   
 #  Introduction  #  Introduction
   
 The purpose of this document is to guide you to create a [RAM disk](http://en.wikipedia.org/wiki/RAM_disk) image and a custom kernel in order to boot your mini NetBSD off a [Compact Flash](http://en.wikipedia.org/wiki/CompactFlash) or use it to debug it in an emulated environment, such as [qemu](http://en.wikipedia.org/wiki/QEMU).  The purpose of this document is to guide you to create a [RAM disk](http://en.wikipedia.org/wiki/RAM_disk) image and a custom kernel in order to boot your mini NetBSD off a [Compact Flash](http://en.wikipedia.org/wiki/CompactFlash) or use it to debug it in an emulated environment, such as [qemu](http://en.wikipedia.org/wiki/QEMU). 
   
 The ramdisk image will have to be inserted into your kernel and then extracted to memory of your embedded device (or your emulator) and used as the root file system, which then can be used by your kernel as if it resided on a "normal" storage media like a hard drive.  The ramdisk image will have to be inserted into your kernel and then extracted to memory of your embedded device (or your emulator) and used as the root file system, which then can be used by your kernel as if it resided on a "normal" storage media like a hard drive. 
   
 The steps below were tested in a NetBSD 4.99.20 i386 box.  The steps below were tested in a NetBSD 4.99.20 i386 box. 
   
 #  Create the ramdisk  #  Create the ramdisk
   
 First we need to create the ramdisk that will get embedded into the kernel. The ramdisk contains a filesystem with whatever tools are needed, usually [[basics/init|init(8)]] and some tools like sysinst, ls(1), etc.  First we need to create the ramdisk that will get embedded into the kernel. The ramdisk contains a filesystem with whatever tools are needed, usually [[basics/init|init(8)]] and some tools like sysinst, ls(1), etc. 
   
 ##  Standard ramdisk  ##  Standard ramdisk
   
 To create the standard ramdisk (assuming your source tree lies at `/usr/src` and you have `/usr/obj` and `/usr/tools`):  To create the standard ramdisk (assuming your source tree lies at `/usr/src` and you have `/usr/obj` and `/usr/tools`): 
          
     # cd /usr/src      # cd /usr/src
     # ./build.sh -O ../obj -T ../tools -u tools      # ./build.sh -O ../obj -T ../tools -u tools
     # cd /usr/src/etc/      # cd /usr/src/etc/
     # make MAKEDEV      # make MAKEDEV
     # cd /usr/src/distrib/i386/ramdisks/ramdisk-big      # cd /usr/src/distrib/i386/ramdisks/ramdisk-big
     # make TOOLDIR=/usr/tools      # make TOOLDIR=/usr/tools
          
   
 ##  Custom ramdisk  ##  Custom ramdisk
   
 If you want to customize the contents of the filesystem, customize the `list` file. Let's say for example that we need the [[basics/uname|uname(1)]] utility to be included in the ramdisk, which is not by default.  If you want to customize the contents of the filesystem, customize the `list` file. Let's say for example that we need the [[basics/uname|uname(1)]] utility to be included in the ramdisk, which is not by default. 
          
   
 # cd /usr/src  # cd /usr/src
     # ./build.sh -O ../obj -T ../tools -u tools      # ./build.sh -O ../obj -T ../tools -u tools
     # cd /usr/src/etc/      # cd /usr/src/etc/
     # make MAKEDEV      # make MAKEDEV
     # cd /usr/src/distrib/i386/ramdisks/ramdisk-big      # cd /usr/src/distrib/i386/ramdisks/ramdisk-big
     # cp list list.old      # cp list list.old
          
   
 Then we edit the `list` file, by adding the following line:  Then we edit the `list` file, by adding the following line: 
          
     PROG    bin/uname      PROG    bin/uname
          
   
 And after having done it:  And after having done it: 
          
     # make TOOLDIR=/usr/tools      # make TOOLDIR=/usr/tools
          
   
 Either way, you will get something like this:  Either way, you will get something like this: 
          
     #    create  ramdisk-big/ramdisk-big.fs      #    create  ramdisk-big/ramdisk-big.fs
     Calculated size of `ramdisk-big.fs.tmp': 5120000 bytes, 65 inodes      Calculated size of `ramdisk-big.fs.tmp': 5120000 bytes, 65 inodes
     Extent size set to 4096      Extent size set to 4096
     ramdisk-big.fs.tmp: 4.9MB (10000 sectors) block size 4096, fragment size 512      ramdisk-big.fs.tmp: 4.9MB (10000 sectors) block size 4096, fragment size 512
             using 1 cylinder groups of 4.88MB, 1250 blks, 96 inodes.              using 1 cylinder groups of 4.88MB, 1250 blks, 96 inodes.
     super-block backups (for fsck -b #) at:      super-block backups (for fsck -b #) at:
      32,       32,
     Populating `ramdisk-big.fs.tmp'      Populating `ramdisk-big.fs.tmp'
     Image `ramdisk-big.fs.tmp' complete      Image `ramdisk-big.fs.tmp' complete
          
   
 And verify with:  And verify with: 
          
     $ ls -lh ramdisk-big.fs      $ ls -lh ramdisk-big.fs 
     -rwxr-xr-x  1 root  wheel  4.9M Jun 19 08:33 ramdisk-big.fs      -rwxr-xr-x  1 root  wheel  4.9M Jun 19 08:33 ramdisk-big.fs
          
   
 #  Build the kernel  #  Build the kernel
   
 Next we shall build our custom kernel with ramdisk support. We may choose any INSTALL* kernel configuration. Here, we will use INSTALL_TINY:  Next we shall build our custom kernel with ramdisk support. We may choose any INSTALL* kernel configuration. Here, we will use INSTALL_TINY: 
          
     # cd /usr/src/sys/arch/i386/conf      # cd /usr/src/sys/arch/i386/conf
     # cp INSTALL_TINY MY_INSTALL_TINY      # cp INSTALL_TINY MY_INSTALL_TINY
          
   
 Then we edit `MY_INSTALL_TINY` file, and we go to the section:  Then we edit `MY_INSTALL_TINY` file, and we go to the section: 
          
     # Enable the hooks used for initializing the root memory-disk.      # Enable the hooks used for initializing the root memory-disk.
     options         MEMORY_DISK_HOOKS      options         MEMORY_DISK_HOOKS
     options         MEMORY_DISK_IS_ROOT     # force root on memory disk      options         MEMORY_DISK_IS_ROOT     # force root on memory disk
     options         MEMORY_DISK_SERVER=0    # no userspace memory disk support      options         MEMORY_DISK_SERVER=0    # no userspace memory disk support
     options         MEMORY_DISK_ROOT_SIZE=3100      # size of memory disk, in blocks      options         MEMORY_DISK_ROOT_SIZE=3100      # size of memory disk, in blocks
          
   
 The size of `MEMORY_DISK_ROOT_SIZE` must be equal or bigger than the size of your image. To calculate the kernel value you can use following rule:  The size of `MEMORY_DISK_ROOT_SIZE` must be equal or bigger than the size of your image. To calculate the kernel value you can use following rule: 
          
     MEMORY_DISK_ROOT_SIZE=10000 would give 10000*512/1024 = 5000 kb      MEMORY_DISK_ROOT_SIZE=10000 would give 10000*512/1024 = 5000 kb
          
   
 We check that the following lines are un-commented:  We check that the following lines are un-commented: 
          
     pseudo-device   md              1       # memory disk device (ramdisk)      pseudo-device   md              1       # memory disk device (ramdisk)
     file-system     MFS             # memory file system      file-system     MFS             # memory file system
          
   
 Once we are done with the configuration file, we proceed with building our kernel:  Once we are done with the configuration file, we proceed with building our kernel: 
          
     # cd /usr/src      # cd /usr/src
     # ./build.sh  -O ../obj -T ../tools -u kernel=MY_INSTALL_TINY      # ./build.sh  -O ../obj -T ../tools -u kernel=MY_INSTALL_TINY
          
   
 #  Insert ramdisk to kernel  #  Insert ramdisk to kernel
   
 Having built our kernel, we may now insert the ramdisk to the kernel itself:  Having built our kernel, we may now insert the ramdisk to the kernel itself: 
          
     # cd /usr/src/distrib/i386/instkernel      # cd /usr/src/distrib/i386/instkernel
     # cp Makefile Makefile.old      # cp Makefile Makefile.old
          
   
 Then we edit the `Makefile`, to make sure that the `RAMDISKS` and `MDSETTARGETS` variables are set properly. After the modifications, mine looks like this:  Then we edit the `Makefile`, to make sure that the `RAMDISKS` and `MDSETTARGETS` variables are set properly. After the modifications, mine looks like this: 
          
     #       $NetBSD: Makefile,v 1.6 2007/03/06 21:47:45 bouyer Exp $                        #       $NetBSD: how_to_create_bootable_netbsd_image.mdwn,v 1.1 2011/11/20 20:55:21 mspo Exp $                  
          
     .include <bsd.own.mk>      .include <bsd.own.mk>
     .include "${NETBSDSRCDIR}/distrib/common/Makefile.distrib"      .include "${NETBSDSRCDIR}/distrib/common/Makefile.distrib"
          
     #       create ${RAMDISK_*} variables                                                  #       create ${RAMDISK_*} variables                                             
     #                                                                                      #                                                                                 
     RAMDISKS=       RAMDISK_B       ramdisk-big      RAMDISKS=       RAMDISK_B       ramdisk-big
          
     .for V F in ${RAMDISKS}      .for V F in ${RAMDISKS}
     ${V}DIR!=       cd ${.CURDIR}/../ramdisks/${F} && ${PRINTOBJDIR}      ${V}DIR!=       cd ${.CURDIR}/../ramdisks/${F} && ${PRINTOBJDIR}
     ${V}=           ${${V}DIR}/${F}.fs      ${V}=           ${${V}DIR}/${F}.fs
     .endfor      .endfor
          
     MDSETTARGETS=   MY_INSTALL_TINY         ${RAMDISK_B}    -      MDSETTARGETS=   MY_INSTALL_TINY         ${RAMDISK_B}    -
          
     MDSET_RELEASEDIR=       binary/kernel      MDSET_RELEASEDIR=       binary/kernel
          
     .include "${DISTRIBDIR}/common/Makefile.mdset"      .include "${DISTRIBDIR}/common/Makefile.mdset"
          
     .include <bsd.prog.mk>      .include <bsd.prog.mk>
          
   
 Next write:  Next write: 
          
     make KERNOBJDIR=/usr/obj/sys/arch/i386/compile  TOOLDIR=/usr/tools      make KERNOBJDIR=/usr/obj/sys/arch/i386/compile  TOOLDIR=/usr/tools
          
   
 Should you encounter errors of the following form, try increasing the `MEMORY_DISK_ROOT_SIZE` at your kernel configuration file.  Should you encounter errors of the following form, try increasing the `MEMORY_DISK_ROOT_SIZE` at your kernel configuration file. 
          
     i386--netbsdelf-mdsetimage: fs image (5120000 bytes) too big for buffer (1587200 bytes)      i386--netbsdelf-mdsetimage: fs image (5120000 bytes) too big for buffer (1587200 bytes)
     *** Error code 1      *** Error code 1
          
   
 Provided that everything went ok, you will have the following files in your current directory:  Provided that everything went ok, you will have the following files in your current directory: 
          
     # ls -lh netbsd*      # ls -lh netbsd*
     -rwxr-xr-x  1 root  wheel   21M Jun 20 11:07 netbsd-MY_INSTALL_TINY      -rwxr-xr-x  1 root  wheel   21M Jun 20 11:07 netbsd-MY_INSTALL_TINY
     -rw-r--r--  1 root  wheel  1.5M Jun 20 11:07 netbsd-MY_INSTALL_TINY.gz      -rw-r--r--  1 root  wheel  1.5M Jun 20 11:07 netbsd-MY_INSTALL_TINY.gz
     -rw-r--r--  1 root  wheel   58K Jun 20 11:07 netbsd-MY_INSTALL_TINY.symbols.gz      -rw-r--r--  1 root  wheel   58K Jun 20 11:07 netbsd-MY_INSTALL_TINY.symbols.gz
          
   
 #  Make image bootable  #  Make image bootable
   
 Finally:  Finally: 
          
     # cd /usr/src/distrib/i386/floppies/bootfloppy-big      # cd /usr/src/distrib/i386/floppies/bootfloppy-big
     # cp Makefile Makefile.old      # cp Makefile Makefile.old
          
   
 Edit the Makefile if your kernel config has a different name than INSTALL*. Replace `FLOPPYKERNEL= netbsd-INSTALL.gz` with `FLOPPYKERNEL= netbsd-MY_INSTALL_TINY.gz` (Where MY_INSTALL_TINY is of course the name of the custom kernel.)  Edit the Makefile if your kernel config has a different name than INSTALL*. Replace `FLOPPYKERNEL= netbsd-INSTALL.gz` with `FLOPPYKERNEL= netbsd-MY_INSTALL_TINY.gz` (Where MY_INSTALL_TINY is of course the name of the custom kernel.) 
          
     # make TOOLDIR=/usr/tools      # make TOOLDIR=/usr/tools
     [...]      [...]
     Final result:      Final result:
     -rw-r--r--  1 root  wheel  2949120 Jun 20 11:15 boot-big1.fs      -rw-r--r--  1 root  wheel  2949120 Jun 20 11:15 boot-big1.fs
          
   
 Now you are ready to test your image, with qemu for example:  Now you are ready to test your image, with qemu for example: 
          
     $ cd /usr/src/distrib/i386/floppies/bootfloppy-big      $ cd /usr/src/distrib/i386/floppies/bootfloppy-big
     $ qemu boot-big1.fs      $ qemu boot-big1.fs
          
   
 #  Screenshots  #  Screenshots
   
 [![][18]][19]  [![][18]][19]
   
    [18]: /images/120px-Qemu_boot_1.png     [18]: /images/120px-Qemu_boot_1.png
    [19]: /images/Qemu_boot_1.png (Qemu boot 1.png)     [19]: /images/Qemu_boot_1.png (Qemu boot 1.png)
   
 Booting  Booting 
   
 [![][20]][21]  [![][20]][21]
   
    [20]: /images/120px-Qemu_boot_2.png     [20]: /images/120px-Qemu_boot_2.png
    [21]: /images/Qemu_boot_2.png (Qemu boot 2.png)     [21]: /images/Qemu_boot_2.png (Qemu boot 2.png)
   
 Booting  Booting 
   
 [![][22]][23]  [![][22]][23]
   
    [22]: /images/120px-Qemu_boot_3.png     [22]: /images/120px-Qemu_boot_3.png
    [23]: /images/Qemu_boot_3.png (Qemu boot 3.png)     [23]: /images/Qemu_boot_3.png (Qemu boot 3.png)
   
 Shell  Shell 
   
 #  Credits  #  Credits
   
 This article is based on [[Yazzy's]] work, who was kind enough to permit us include an article in this wiki. The original document may be found [here](http://www.yazzy.org/docs/NetBSD/netbsd-on-cf.txt).  This article is based on [[Yazzy's]] work, who was kind enough to permit us include an article in this wiki. The original document may be found [here](http://www.yazzy.org/docs/NetBSD/netbsd-on-cf.txt). 
   
   
 #  Additional related links  #  Additional related links
   
   1. [Embedded NetBSD](http://brianrose.net/family/fetch.php?dir=Brian/NetBSD/Embedded&filename=content.html)    1. [Embedded NetBSD](http://brianrose.net/family/fetch.php?dir=Brian/NetBSD/Embedded&filename=content.html)
   2. [Creating a custom install/boot floppies for i386](http://www.netbsd.org/docs/guide/en/chap-misc.html#chap-misc-creating-bootfloppies)    2. [Creating a custom install/boot floppies for i386](http://www.netbsd.org/docs/guide/en/chap-misc.html#chap-misc-creating-bootfloppies)
   
   

Removed from v.1.1  
changed lines
  Added in v.1.2


CVSweb for NetBSD wikisrc <wikimaster@NetBSD.org> software: FreeBSD-CVSweb