Page 3 of 3 FirstFirst 123
Results 21 to 23 of 23

Thread: Booting from SD card using GRUB on hard drive

  1. #21
    Join Date
    Aug 2010
    Beans
    1

    Wink Re: Booting from SD card using GRUB on hard drive

    Sorry to be such a noob, but this still is not making sense to me. it looks like it is saying to add the modules to the SD card not the usb. So how will it be able to load the modules from a sd card that it cant see untill the modules are loaded? My laptop does not support booting from the built in SD slot. Ubuntu 12.04 has no problen installing to SD just cant boot to it because bios does not support it. So what I want to do is use a usb stick as a sort of boot key, containing whatever drivers are needed to get the SD mounted and then boot to it. I tried a tutorial here http://a110wiki.de/wiki/Booting_from_SD, but this is specific to the a110 and grub, not grub2, so I wasn't sure how exactly to change the script, or edit the grub.conf file. It seems like what your doing here is the same thing without using kexec. I'm not seeing how this can work though, Seems like I need the modules and grub2 on the usb which will load whatever it needs to get the SD going and then boot from it. If anyone knows how to do this, maybe you could post a step by step tutorial that is a little easier to understand for all us idiots that dont want the dongle hanging off the side of our laptops. Any Help would be much appreciated. Thanks.

  2. #22
    Join Date
    May 2012
    Beans
    1

    Re: Booting from SD card using GRUB on hard drive

    @Sandbomb: Yes, me too. I've also been confused by some parts of the previous descriptions. So I read the GRUB manual to better understand the content of the GRUB config file "/boot/grub/grub.cfg". Adding this to that what I've learned in this threat and earlier lead me to success.

    Here the result:


    The Problem

    PC does not accept a SD-card as boot device. But I want to have my alternative OS being located on a SD-card partition.


    Assumptions

    • The PC has a bootable device
      I have installed a previous version of Ubuntu on it. Ubuntu is not necessary, but like this I can refer on it and only you have to deal with your variations.
    • GRUB 2 is propperly installed on that bootable device (that's not the GRUB of the SD-card installation)
    • The PC cannot boot using the SD-card as boot device (the initial problem)
    • You already have installed your new Linux with GRUB 2 on your SD-card (and are frustrated because it does not boot...)


    Notes

    Only you yourself are responsible for any lost data or any other inconveniances that might result from your actions by following these words. So don't forget to make the necessary backups of your data and update your emergency disks for the worst case.
    Please read and understand the whole thing, before you start!
    Be aware, that this is only a step-by-step guide for a proof of concept. The result of this solution is not suitable for production use, as it requires manual updates after each kernel-update the Linux on the SD-card.



    Method to solve the problem

    1. We extend the "initrd" to support the sd-card hardware.
    2. Then we place the files, required to boot Linux, on a device that is accessable for GRUB.
    3. Finally we patch the configuration of the GRUB 2 residing on the bootable device to have the correct menu entry for our OS.

    For those guys, that are proficient in configuring GRUB and Linux in the terminal, just jump to the bottom of the guide (end of III.) to see the sample and the pointing out of the essential trick.




    Step by Step


    I. Add the modules for SD-card support to the "initrd"-file of your SD-card Linux.
    As the SD-card linux does not boot, we have to do that with an other Linux and use the "chroot" feature.

    1. Boot to a Linux
    2. Insert the SD-card with the not-bootable Linux
    3. Mount the SD-card partition with the Linux, if the automounter does not do the job.
      I assume the mount at "/media/sdcard". Substitute this by your real mount path.
    4. Open a terminal and issue the following commands. They will prepare and start the chroot environment.
      Code:
      $ cd /media/sdcard
      $ sudo mount -o bind /dev ./dev
      $ sudo mount -o bind /proc ./proc
      $ sudo mount -o bind /sys ./sys
      $ sudo mount -o bind /tmp ./tmp
      $ sudo chroot .
      #
      Now we are in the environment of the SD-card Linux as user root.
    5. Add the required modules to support the SD-card at the start of Linux (after GRUB 2 passes over the control)
      Edit the file
      Code:
      /etc/initramfs-tools/modules
      by adding these module names:
      (On other hardware/Linux the required modules might differ For my system with Ubuntu 12.04 these modules have done the job.)
      Code:
      mmc_block
      sdmod
    6. Back on the command line execute the following command to repackage the "initrd"-file:
      Code:
      # update-initramfs -u
    7. To leave the "chroot" environment execute
      Code:
      # exit
    II. Copy the Linux boot files to the bootable device
    CD to the root directory of your bootable device.
    In case of the internal hdd its "/", for an USB-Stick something like "/media/usb"
    Typically you will find there the "boot" directory of the GRUB installed on your bootable device.
    Code:
    $ sudo mkdir boot.guest
    $ ls -l /boot                                 # to find out the real kernel version
    $ sudo cp /boot/*3.2.0-23* boot.guest/        # substitute the kernel version by yours.
    NOTE: After a kernel update of the SD-card's Linux this step has to be repeated.
    III. Add the menu entries to the GRUB 2 configuration on the bootable device
    Depending on your Linux distribution, there might be some differences of the GRUB integration in the distribution. As the GRUB on the bootable device is the code executing version, it is preferable to let that GRUB generate some menu entries, but not just copy/paste the relevant entries from the SD-card's GRUB:

      1. If not already up 'n running, start the OS that hosts and manages the GRUB 2 installation of the bootable device.
      2. Insert your SD-card with the not-booting Linux installation.
      3. Execute:
        Code:
        $ sudo update-grub
        This should generate probably 2 additional entries for your not booting SD-card installation. These entries will not work, but they are the base for our further patch.
      4. Might be that errors occure (something like "no device"). Then the script could not find any GRUB device entry for your SD-card in the file
        Code:
        /boot/grub/device.map
        • Check the device filename of your SD-card (This example: "/dev/sdc")
        • Edit (with root priviledges) "device.map" manually and add a new entry for your SD-card. Example:

        Code:
        (hd0) /dev/sda
        (hd1) /dev/sdb
        (hd2) /dev/sdc
        Then repeat step 3.


    Typically the updated GRUB 2 configuration is stored in
    Code:
    /boot/grub/grub.cfg
    • Open it for later copy/paste (read-only is enough)

    We will add a custom script to provide our custom entries directly after our linux distribution on the bootable device (Details her fit to Ubuntu). The position is defined by the number the script-name will start.

    1. Code:
      $ cd /etc/grub.d
      $ sudo cp 40_custom 15_custom_sdcard
    2. Open with root priviledges the new file "15_custom_sdcard" for further editing.
    3. Change to the previously opened "grub.cfg".
      Search and copy the two menu entries for the SD-card Linux boot.
    4. Paste the copied text to the end of the "15_custom_sdcard".
    5. Find the correct GRUB device name "(hd?,?)" for your boot device. You will find it in the the already opened "grub.cfg" at the menu entry for the Linux on your boot device.
      Patch the following line in both menu entries of "15_custom_sdcard" to the values you've found.
      Code:
      set root=(hd?,?)
    6. Now we patch the "search" line of both menu entries. Typically a UUID is set as search string. Substitute the given UUID (very log hex-number) by the UUID of your boot device.
      Besides many other ways you typically can find it at the already opened "grub.cfg" within the menu entry for your Linux on the boot device.
    7. As the next we correct the path to kernel and initrd files. Currently they are stated to be in "/boot". This we correct to the new path
      Code:
      /boot.guest/.....
    8. Optionally you can change the first line of the menu entries to get a more conveniant description. (Watch out to edit only within the quotation marks.)
    9. Save and close "15_custom_sdcard".
      Close "grub.cfg".
    10. Execute
      Code:
      $ sudo update-grub
    11. Everything done. - Verify the content of your newly generated "grub.cfg" for the presence of your new menu entries.


    NOTE: After a kernel-update of the SD-card Linux, the statements "linux" and "initrd" have to be updated to the new filenames.




    To get a visual imagination of how the "15_custom_sdcard" should look like, here an example. Note: The detailed values will not fit to your system.


    Code:
    #!/bin/sh
    exec tail -n +3 $0
    # This file provides an easy way to add custom menu entries.  Simply type the
    # menu entries you want to add after this comment.  Be careful not to change
    # the 'exec tail' line above.
    
    
    
    menuentry 'Ubuntu 12.04 LTS (sd-card)' --class ubuntu --class gnu-linux --class gnu --class os {
        insmod ext2
        set root='(hd0,1)'
        search --no-floppy --fs-uuid --set=root 12345678-222d-333e-444f-123456789abc
        linux    /boot.guest/vmlinuz-3.2.0-24-generic root=UUID=1abcdef1-a1a1-b2b2-c3c3-1abcdef12345 ro   quiet splash $vt_handoff
        initrd    /boot.guest/initrd.img-3.2.0-24-generic
    }
    menuentry 'Ubuntu 12.04 LTS (sd-card) (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os {
        insmod ext2
        set root='(hd0,1)'
        search --no-floppy --fs-uuid --set=root 12345678-222d-333e-444f-123456789abc
        echo    'Loading Linux 3.2.0-24-generic …'
        linux    /boot.guest/vmlinuz-3.2.0-24-generic root=UUID=1abcdef1-a1a1-b2b2-c3c3-1abcdef12345 ro recovery nomodeset 
        echo    'Loading initial ramdisk …'
        initrd    /boot.guest/initrd.img-3.2.0-24-generic
    }
    To point out the essential trick:

    • The "set root" statment points to the bootable disk, where the "outsourced" kernel and initrd reside.
    • Also the "search" statment points to that bootable disk.
    • But the "linux" statement has to point to the SD-card !!!


    IV. Reboot your system and in the GRUB menu choose the SD-card Linux


    V. What's next to do?

    • To get all this to production quality, some should extend the GRUB script "15_custom_sdcard" to make an autodetect for the newest filenames of linux and initrd in "/boot.guest/". Then replacing these names with apropriate variables in the script will make editing obsolete.
      (Should be a 5-minute job for a GRUB experienced coder.)
    • Write a script that automatically will copy new versions of the kernel files to the outsourced directory. (dito effort as above.)

    VI. Alternative solution:
    Surely it is possible to install GRUB 2 of the SD-card Linux to the bootable device directly by something like

    Code:
    $ sudo grub-install --boot-directory=/mnt/boot /dev/sdb
    A manual copy action after a kernel update would be obsolete.



    This alternative might be especially usefull if "/dev/sdb" represents your bootable USB-device.
    For "/dev/sdb" being your internal harddrive with your primary OS, you should first think about the consequences concerning your primary OS.


    Mentioning my personal taste, I prefer to have the complete OS on the SD-card and make a copy to the outsourced boot folder. So the grade of portability between other computers is higher. As a consequence, I would also keep an up-to-date copy of the file "15_custom_sdcard" on the SD-card.
    So have some fun!
    gecco

  3. #23
    Join Date
    Jul 2011
    Beans
    4

    Re: Booting from SD card using GRUB on hard drive

    Thank you so much, gecco. I've just been looking for something like this.

Page 3 of 3 FirstFirst 123

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •