# Boot FreeBSD ISO from Grub2 USB stick



## Beeblebrox (Nov 26, 2010)

I have a USB stick with a grub2 bootloader installed.  Great tool when you want boot an iso without burning CD's. 

I now want to boot the FreeBSD installer from the stick and have disc1.iso on an ext2 partition of the stick.  I chose to use disc1.iso because I figured FreeBSD memstick.iso would install the FreeBSD bootloader on the stick and remove grub2.  My menu entry in grub.cfg is:

```
insmod loopback
insmod iso9660
insmod ufs2
insmod zfs
search --no-floppy --fs-uuid --015e657c-etc..
menuentry "FreeBSD 8.1 iso" {
	set isofile="/boot/FreeBSD-8.1-RELEASE-amd64-disc1.iso"
	loopback loop (hd0,1)$isofile
	freebsd  (loop)/boot/kernel/kernel
	freebsd_loadenv  (loop)/boot/device.hints
	set FreeBSD.vfs.root.mountfrom=cd9660:/dev/ad1s1
	set FreeBSD.vfs.root.mountfrom.options=rw
}
```
Boot process moves along fine until it gets to mounting root (/). Here it drops to the [CMD=""]mountroot>[/CMD] prompt where "? = list valid disk boot devices".  In my menu entry I have also tried to change
[CMD=""]set FreeBSD.vfs.root.mountfrom=cd9660:/dev/ad1s1[/CMD]
to
[CMD=""]set FreeBSD.vfs.root.mountfrom=ufs:/dev/ad1s1[/CMD]
with no result (obviously).  The ? option at the command line does list available slices on the USB stick!

Question is:  Given that grub has completed its job and passed command the FreeBSD bootloader, what would be the correct menu entry for successful boot to the setup menu?  Of course, I may also be missing a line in grub like ramdisk= or such...

Unrelated but for the record, grub did not recognize the kfreebsd command but worked alright with the freebsd command.  Whereas when booting from my already working hdd, the command grub is using is kfreebsd.


----------



## wblock@ (Nov 26, 2010)

Does grub's loopback mount create a device that can even be seen by the FreeBSD bootloader?  If it does, what kind of device does it pretend to be: IDE hard disk (ad), IDE CD (acd), SCSI (da)?


----------



## Beeblebrox (Nov 26, 2010)

I just realized that the loopback command which grub uses probably does not get passed to the FreeBSD loader.  The [CMD=""]mountroot>  ?[/CMD] option lists (stick has 2 partns):

```
msdosfs/kng32, ext2fs/kng1, ad1s2, ad1s1, ad2
```
 where kng32 and kng1 are the linux labels for the partns.  Curiously, it does not list any partns of the hdd => loopback command is limiting the drives available to Geom?  But under loopback the root fs is not [Cfile]something:/dev/ad1s1[/file], its ---/ads1/boot/FreeBSD-8.1-RELEASE-amd64-disc1.iso so I conclude that I need to pass this info to the fBSD bootloader somehow.


----------



## wblock@ (Nov 26, 2010)

Unless grub is creating an emulated device in RAM, it seems unlikely to work.  Something would have to remain to handle reading the ext2 filesystem underneath the CD image.  And all of that goes away when the FreeBSD loader takes over, unless grub has created something that sticks around.  The FreeBSD loader should be able to see the USB stick as da0, but then it would have to be able to understand ext2, and then mdconfig(8) and mount the CD image.

syslinux can copy CD images to RAM and make them bootable, but of course it eats RAM.  It wasn't successful when I first tried it, but might be better now.


----------



## Beeblebrox (Nov 27, 2010)

I have thought about your comments and I disagree.  I think loopback creates an iso9660 fs in memory.  Without that, the FreeBSD installer would not start at all - the FreeBSD installer starts because it thinks its on a cd (iso9660 fs) - and that is what sticks around. If my premise is correct, ext2 fs is irrelevant.


----------



## wblock@ (Nov 27, 2010)

Looks like you're right.  loopback in grub2 creates a RAM-backed "drive".

When you say the installer starts, you mean the loader, right?  If sysinstall runs, then the filesystem is being read.


----------



## Beeblebrox (Nov 28, 2010)

Yes, I do mean the loader.  Load process stops at

```
ROOT MOUNT ERROR:
If you have invalid mount options, reboot, and first try the following from the loader prompt:
     set vfs.root.mountfrom.options=rw
and then remove invalid mount options from /etc/fstab.
```
some other stuff and at the end and waits at
[CMD=""]mountroot>[/CMD]
Like I stated, ? curiously lists only the partitions on the stick and none of the hdd slices...


----------



## grantbow (Mar 18, 2011)

This thread is marked [Solved] but I don't see any successful reports of booting a FreeBSD iso image file from grub2. Am I missing something?

I would love to show more people FreeBSD. It would be useful if the kernel was able to do the right thing when booted from a Grub2 USB stick that contains other files.


----------



## Beeblebrox (Mar 20, 2011)

@ grantbow:
Threads a lot of times get marked "solved" just for having been on the forum for a while.  "Solved" also, in a sense means closed regardless whether a solution exists or not.

Re your question about booting from usb, FreeBSD is bootable from a usb and there are many howto's about that (one such exhaustive sample here).  My post, as you know, was about getting it to work with the iso file intact, which saves space on the usb and additionally, switching the menu for other iso's in grub is very easy.  Currently no solution to this (that I have found) and I suspect the issue has to do with grub's loopback compatibility with FreeBSD's memory device handling.

Hope this helps!


----------



## grantbow (Mar 22, 2011)

Hello @Beeblebrox, thanks for your reply and the link. You and @wblock seem to have narrowed down many of the unknowns. As USB sticks get larger and larger the requirement to use a whole stick for an OS seems more and more wasteful to me. Grub2's feature is one seemingly attractive way around this limitation.

Is there currently no way for the FreeBSD kernel with an appropriate parameter to recognize the "RAM-backed drive" created by grub2 & mount this as it's root volume? What missing functionality seems to be required?


----------



## wblock@ (Mar 22, 2011)

Recently, I found that syslinux could PXE boot a FreeBSD install CD.  Maybe there's an equivalent for grub2?  Or use syslinux instead of grub2.

```
label freebsd-install
  menu label FreeBSD Install
  kernel memdisk
  initrd tftp://192.168.1.1/images/freebsd-install/bootonly.iso
  append iso raw
```

For syslinux, just changing the initrd to have a path to the ISO image should be enough.  (Untested.)

```
label freebsd-install
  menu label FreeBSD Install
  kernel memdisk
  initrd bootonly.iso
  append iso raw
```


----------



## Beeblebrox (Mar 23, 2011)

What I meant by: 





> I suspect the issue has to do with grub's loopback compatibility with FreeBSD's memory device handling.


is that FreeBSD does not use loopback, it is essentially a linux command.  The comparable process in FreeBSD, if you wanted to mount an iso would be:
[CMD=""]1. mdconfig -a -t vnode -f /path/to-image.iso -u 1[/CMD]
[CMD=""]2. mount -t cd9660 /dev/md0 /mountpoint[/CMD]

Granted, *grub* already has mounted the iso as a memory device, hence no need for *mdconfig* etc.  However, I just wonder whether FreeBSD recognizes the loopback'ed device in the manner necessary to continue with the boot process, and whether there is a mis-communication because of how the memory devices are specified/handled?


----------



## tanzhaus (Mar 23, 2011)

I tried to do the same as Beeblebrox. Got the same problem. The boot process stops with a rootmount prompt. I have absolutely no idea how Grub2 works and what's behind it. So it would be nice if someone could post a working grub2 FreeBSD entry to boot from usb.


----------



## Beeblebrox (Mar 28, 2011)

@wblock:
I think (and I am just speculating here) the key to your post is the type of iso being used:  *bootonly.iso* as opposed to a regular kernel / iso.  Again speculation, the *bootonly* is somehow different than the "regular" iso


----------



## wblock@ (Mar 28, 2011)

Beeblebrox said:
			
		

> @wblock:
> I think (and I am just speculating here) the key to your post is the type of iso being used:  *bootonly.iso* as opposed to a regular kernel / iso.  Again speculation, the *bootonly* is somehow different than the "regular" iso



A bootonly ISO boots the same as a "disc1" ISO, it just doesn't have all the install distributions on it.  Many of the systems I use don't have even 512M of RAM, so there's not enough to store a 653M disc1 image.  bootonly.iso is only 47M.

Just tested, and a full disc1 install CD also boots fine, provided the target system has enough RAM to hold it with some left over to run.


----------



## jola (Jun 5, 2011)

*install FreeBSD ISO from grub  via USB*

To install FreeBSD from grub or grub2, add the following to grub.cfg:

```
###  BEGIN of grub.cfg ###
# Boot automatically after 30 secs.
set timeout=30

# By default, boot the first entry.
set default=0

menuentry "1 FreeBSD 8.2 i386 boot only" {
 	set isofile=/FreeBSD-8.2-RELEASE-i386-bootonly.iso
	loopback loop $isofile
	kfreebsd (loop)/boot/kernel/kernel iso-scan/filename=$isofile noeject noprompt splash --
	kfreebsd_module (loop)/boot/mfsroot.gz type=mfs_root
}

menuentry "2 FreeBSD 8.2 amd64 boot only" {
 	set isofile=/FreeBSD-8.2-RELEASE-amd64-bootonly.iso
	loopback loop $isofile
	kfreebsd (loop)/boot/kernel/kernel boot=casper iso-scan/filename=$isofile noeject noprompt splash --
	kfreebsd_module (loop)/boot/mfsroot.gz type=mfs_root
}
###  END of grub.cfg ###
```


----------



## macronsfx (Aug 9, 2011)

I wonder if it's possible to put FreeBSD into a separate partition on the USB stick, install the FreeBSD bootloader on that partition, then use chainloader +1 option in /etc/grub.d/40_custom before running update-grub, all right on the USB stick. The USB stick would have to be rather big but maybe it will work?

Thanks.


----------



## bobpaul (Oct 24, 2011)

jola said:
			
		

> To install FreeBSD from grub or grub2, add the following to grub.cfg:
> 
> ```
> ###  BEGIN of grub.cfg ###
> ...



It looks like you've copy/pasted the required Ubuntu parameters. Have you tested? Does this work? Obviously "boot=casper" and "iso-scan/filename" have no meaning as BSD kernel arguments. Actually, I think "noeject", "noprompt", and "splash" are also meaningless for FreeBSD. You should be able to leave off everything after "kernel". Also, this won't work on Grub, only Grub2... Grub has a different format and doesn't support loopback.

There seems to be a lot of confusion in this thread about what Grub2 is doing. It's not magic. It's not creating a device that either Linux or FreeBSD kernels can see. It can't. It is opening the iso file you provide it and allowing grub2 (and only grub2) to access files inside of it. The only purpose of loopback on grub2 is to allow one to extract the kernel and init image (initrd or freebsd_module). After that, it is entirely up to the booted kernel and init scripts to find the ISO if necessary and continue booting. Once the kernel has taken over, the loopback "device" created by grub vanishes. It doesn't matter if you're booting Linux, FreeBSD, or Windows, the ISO you're booting is not able to see anything grub did and needs to support this method of booting.

For many small ISOs (TinyCore Linux, and I suspect the FreeBSD netinstaller), everything of value exists in the Kernel and Init image. For bigger live CDs, such as Ubuntu, the Init scripts need to support this method of booting. The way it works on Ubuntu is the init script (casper on Ubuntu... they like naming things goofy) recognizes the iso-scan/filename parameter. If that parameter is non-empty, casper mounts every partition on the system one after another until it finds the ISO. Then it mounts the ISO as a loopback device and continues as if that loopback device were the CD-ROM.

Only if FreeBSDs init scripts included the ability to parse a kernel parameter such as Ubuntu's poorly named "iso-scan/filename" could we boot a full image. But it sounds like the bootonly.iso works, so there you go...


----------



## Beeblebrox (Oct 28, 2011)

All the excitement aside, jola's proposed solution does not work. Tried and Tired, so did not respond.

The main problem with iso boots (and grub does a great job of it) is the *mount root* stage of the process.  So you can get the loop working, you can get kernel and init to boot, but it hits its head against a wall at the mount root from stage.

Each group of distro (debian/rhl/etc) has different mount root options and this info is not easily found on the intertubes. xBSD releases are no exception, mainly for the reason that they do not use grub as the main bootloader.

Another, but related problem, is that you may manage to boot from the iso but not manage to install it on your drive. The error in this case is usually a message that the filesystem or some package did not like the setup. These errors are really a result of incorrect mount options for root in the grub.cfg line.

RESULT: How do you mount root from loopback as posted in #1 post:


> Boot process moves along fine until it gets to mounting root (/). Here it drops to the  mountroot>


Any result which claims success without being able to fully install the iso is null and void. For clues, I have studied mfsbsd's boot options without result as how-to's are not for the iso but for the un-compressed format.


----------



## bobpaul (Oct 29, 2011)

*You need to use the Boot Only FreeBSD ISO*



			
				Beeblebrox said:
			
		

> The main problem with iso boots (and grub does a great job of it) is the *mount root* stage of the process.  So you can get the loop working, you can get kernel and init to boot, but it hits its head against a wall at the mount root from stage.



Please see my explanation. The FreeBSD CDs do not recognize the iso-scan/filename kernel parameter (as a FreeBSD n00b, I don't even know if FreeBSD accepts kernel parameters in that syntax). However, after posting my explanation, I did try Jola's method using the modifications I proposed (because FreeBSD is not Ubuntu).

If your CD is failing to mount root, then you are not using the Boot Only CD. The Boot Only CD does not contain any packages on the CD-Rom itself; everything is in the mfsroot.gz, which gets extracted into ram.

The proper configuration, which I am using without problems, is:


```
menuentry "FreeBSD 8.2 i386 boot only" {
  set isofile=/boot/isos/FreeBSD-8.2-RELEASE-i386-bootonly.iso
  loopback loop $isofile
  kfreebsd (loop)/boot/kernel/kernel
  kfreebsd_module (loop)/boot/mfsroot.gz type=mfs_root
}

menuentry "FreeBSD 8.2 amd64 boot only" {
  set isofile=/boot/isos/FreeBSD-8.2-RELEASE-amd64-bootonly.iso
  loopback loop $isofile
  kfreebsd (loop)/boot/kernel/kernel
  kfreebsd_module (loop)/boot/mfsroot.gz type=mfs_root
}
```

Again, you cannot use any install ISO other than boot only, as FreeBSD's mfs_root does not contain init scripts with the ability to find and mount an ISO. I've explained how the process works for Ubuntu (RedHat, etc are the same) and why it doesn't work for the normal FreeBSD CDs (and Damn Small Linux and many other distros) up above. You need to the boot only ISO and you can't give FreeBSD any instructions to mount any part of the ISO as the root partition.

I just completed an i386 install of FreeBSD using this method. It works, but only as described.


----------



## Beeblebrox (Nov 4, 2011)

The funny thing is, I have booted ISO files off USB for the likes of DSL and Pentoo before. Unfortunately I lost my notes when moving, so no help on that issue fromme at the moment.  Whether the booted ISO is able to do an install or correctly run the installation script/program is another issue and the reason why I started the thread (but your comment on the problem are certainly welcome).
Re the same problem for FreeBSD, I do think it is possible to boot the liveCD as ISO, and as you stated, sysinstall will probably have problems (exact similar issue recently with Backtrack-5).
To explain my explanation, what I meant by:


> FreeBSD's memory device handling


Is that (as I understand it) FreeBSD needs the extra step of mdconfig to create a loopback thingy (whatever you want to call it at this point is fine by me) wile linux does not seem to need that step.


----------



## bobpaul (Nov 5, 2011)

Beeblebrox said:
			
		

> Is that (as I understand it) FreeBSD needs the extra step of mdconfig to create a loopback thingy (whatever you want to call it at this point is fine by me) wile linux does not seem to need that step.



I'm sure that's true. If you use the bootonly ISO, it doesn't matter, though, because the ISO is never mounted as a loopback device by FreeBSD (or grub, for that matter). "loopback" in grub is more like unzipping a specific file from a tar.gz than actually mounting an ISO as a loopback device, and it disappears as soon as the kernel loads. The 'bootonly' ISO has nothing of value on the ISO filesystem, however, and downloads everything it needs from the internet.

I'm not familiar with Pentoo, but DSL certainly might have added support for Grub2 ISO style booting since I last attempted, either by shoving everything into the init ram image (ala FreeBSD boot only, the 20MB Ubuntu Mini ISOs, etc ) or by making the init scripts on the init ram image support searching and mounting the ISO. I guess this is off topic.

It really doesn't matter at all what method the OS kernel uses to mount ISOs as loopback devices. There's 0 communication between Grub2 and the OS kernel about the loopback "device" that Grub2 creates; that vanishes as soon as the kernel starts executing (whether it's Linux or FreeBSD). All you can do is pass a "kernel parameter" (which on Linux shows up in /proc/cmdline; not sure what FreeBSD does) and needs to be specifically detected by the init scripts.

If you want to boot the FreeBSD 'Disk1', 'DVD1', or 'LiveFS' ISO files, the init scripts on the 'mfsroot.gz' will need to be edited. If you want to boot the 'bootonly' ISO, you already can; I finished an AMD64 install with this method this morning, so now I know install works on both architectures in this way.

Have you had issues with the 'bootonly' ISO image? Or do you really need one of the other ISOs for some reason?


----------



## jk121960 (Nov 5, 2011)

*[Solved] boot FreeBSD iso from grub2 USB stick*

I understand this was marked solved but I hope you guys get this, you started me in the right direction, I am interested in BSD, currently I am Linux Debian. I wanted to check out the Debian BSD distro but it wouldnt install from a USB DVD Rom. I tried all of the Debian solutions with USB's etc and failed then I searched for booting from grub info as that was also one of the debian solutions more or less. I ran across your posts and it gave me sone information I was lacking. I modified your examples a little and came up with a working solution. 

```
menuentry "Debian BSD 1" {
	echo "Loading ..."
	set isofile=(hd0,7)/debian-6.0.3-kfreebsd-amd64-CD-1.iso
	loopback loop $isofile 
	set root=(loop)
	kfreebsd (loop)/boot/kernel/kfreebsd.gz
	kfreebsd_module (loop)/boot/mfsroot.gz type=mfs_root
	set kFreeBSD.vfs.root.mountfrom=ufs:/dev/md0
}
```
In the last examples the one thing I noticed was that the /boot reference had no "machine style reference" to anchor the process, this is really all I changed was adding the reference to loop for the paths and loop was based on a hard machine reference that is universal to the OS continuum. This allowed the installation to happen. As seen I put the ISO in a root directory of a hard drive (ext4 fs by the way) and loaded it from there with the grub hard drive reference. 

Hope one of you finds this! 

And thanks for the needed direction.

--jerry


----------



## Beeblebrox (Nov 6, 2011)

@jk*:
I must say good job! I'll have to try it myself as well.  The 
	
	



```
mountfrom=ufs:/dev/md0
```
 was exactly what I had in mind as a concept but could not describe. No need to fret about whether it was seen, the discussion was on-going (look at the posted dates)


----------



## jk121960 (Nov 6, 2011)

Hi and thanks happy you found it, The 
	
	



```
mountfrom=ufs:/dev/md0
```
 actually came from the grub.cfg on the dvd, I took info from you guys and what was in there and added the two together, along with the machine reference to anchor the filesystem.

--jerry


----------



## Nukama (Nov 17, 2011)

It seems that the FreeBSD-9.0-RC2-${ARCH}-bootonly.iso isn't built with a boot/mfsroot.gz.

Without this there seems no way to boot FreeBSD-9 with grub2.


----------



## frijsdijk (May 2, 2012)

Bump. Any ideas on this issue?


----------



## Beeblebrox (May 2, 2012)

> Bump. Any ideas on this issue?


Care to be more specific? What exactly is the part that's not working? Post #16 solves the issue by adding:

```
kfreebsd_module (loop)/boot/mfsroot.gz type=mfs_root
```
Use an ISO version that has mfsroot.gz included. You are not limited to the bootonly.iso.


----------



## frijsdijk (May 2, 2012)

This indeed works for FreeBSD 8.x and earlier, however the layout of the 9.0 ISO images has changed significantly - the mentioned mfsroot.gz does not exist anymore.

For earlier editions I was actually using the following config for Grub4dos:


```
title FreeBSD 8.3 64-bit
find --set-root /FreeBSD-8.3-RELEASE-amd64-bootonly.iso
map /FreeBSD-8.3-RELEASE-amd64-bootonly.iso (hd32)
map --hook
root (hd32)
chainloader (hd32)
```

Which works charmfully for basic installations. On our USB sticks we actually have a wide variety of Unix images, which can be booted by either Grub2 or Grub4dos (which in turn is chainloaded from Grub2). In neither Grub2 or Grub4dos I have been successful in booting the FreeBSD 9.0 ISO up to the language selection menu for installations.


----------



## epopen (Jan 14, 2013)

Beeblebrox said:
			
		

> Care to be more specific? What exactly is the part that's not working? Post #16 solves the issue by adding:
> 
> ```
> kfreebsd_module (loop)/boot/mfsroot.gz type=mfs_root
> ...


Hi
The mfsroot.gz NOT included in FreeBSD-9.1-RELEASE-amd64-disc1.iso.
So can't boot by the method...


----------



## dr-mdr (Mar 20, 2015)

How to boot FreeBSD 10.1 ISO from Grub2 USB stick? Anyone help?


----------



## thermionix (Aug 5, 2015)

Also trying this without success, any tips?


```
kfreebsd (loop)/boot/kernel/kernel -v
   kfreebsd_loadenv (loop)/boot/device.hints
   kfreebsd_module (loop)/data/base.ufs.uzip type=mfs_root
   set kFreeBSD.vfs.root.mountfrom="ufs:/dev/md0"
   set kFreeBSD.xconsole=YES
```


----------



## free-and-bsd (Jun 12, 2016)

dr-mdr said:


> How to boot FreeBSD 10.1 ISO from Grub2 USB stick? Anyone help?


Yes you can boot that or any other FreeBSD ISO from a Grub2 USB stick. The way I do it is have 2 partitions of the USB stick. The first one is ~1G large (for the ISO) and the second is for Grub2 boot/grub dir and ISO images that ARE bootable with Grub2 via loopback feature.

Now the 1st partition is used to write a FreeBSD*disc1.iso to it using the dd method. Whenever you need a new ISO you just remove/recreate the partition using gpart commands, then write the needed ISO to it. Yea, because I'm not sure writing with dd over the old one created the same way will work... AFAIK writing image over partition with dd makes the rest of the partition unusable.

Only DON'T try to use any *memstick.img for that, it's completely useless! Use normal CD iso.
Then you boot it from Grub2 menu like this (don't need to change that for each new ISO):

```
menuentry "FreeBSD *RELEASE" {
set root=(hd0,msods1)
kfreebsd /boot/loader
}
```


----------



## free-and-bsd (Jun 12, 2016)

Now here's the advantage OpenBSD holds here: you just download bsd.rd of the next release, which is a kernel with initramfs and all you need for the (network) installation. You can at all times boot THAT from Grub2 

...So you see, the topic of booting FreeBSD off an ISO using the all-winning loopback technology of Grub2 has been subject of my study for quite some time now. But the only working solution I've found is described above: write the iso to a separate partition! Technically that's not a problem at all, since those *disc1.iso CDs are mostly around 1G in size, so you create your 2nd partition on USB stick leaving just enough space for the first one.
And you can always reinstall Grub2 like this:

```
#grub-install --boot-directory=$mountpoint_of_your_2nd_usb-partition/boot /dev/daN
```

This solution is still better than writing a *memstick.img to a whole USB drive, thus leaving the drive unusable for other goals. The 2-partitioned setup allows to always use the 2nd partition for whatever you want.


----------



## Marcelo Politzer Couto (Mar 8, 2017)

Just got this working in qemu with the following code:


```
# set iso= ...
   loopback loop=${iso}
   kfreebsd (loop)/boot/kernel/kernel -v
   kfreebsd_module ${iso} type=mfs_root
   set kFreeBSD.vfs.root.mountfrom="cd9660:/dev/md0"
```


----------



## balanga (Mar 9, 2017)

Marcelo Politzer Couto said:


> Just got this working in qemu with the following code:
> 
> 
> ```
> ...



Do you have a bootable FreeBSD ISO on a USB stick? If so can you provide instructions on how to make one?


----------



## balanga (Apr 5, 2017)

free-and-bsd said:


> Yes you can boot that or any other FreeBSD ISO from a Grub2 USB stick. The way I do it is have 2 partitions of the USB stick. The first one is ~1G large (for the ISO) and the second is for Grub2 boot/grub dir and ISO images that ARE bootable with Grub2 via loopback feature.
> 
> Now the 1st partition is used to write a FreeBSD*disc1.iso to it using the dd method. Whenever you need a new ISO you just remove/recreate the partition using gpart commands, then write the needed ISO to it. Yea, because I'm not sure writing with dd over the old one created the same way will work... AFAIK writing image over partition with dd makes the rest of the partition unusable.
> 
> ...




I'm trying to create a USB stick capable of booting a FreeBSD ISO and thought I'd try following the instructions here but am getting lost...

Assuming I have 16GB USB stick, I need to reserve a 1GB partition for a FreeBSD*disc1.iso. Using `gpart add -s 1G da0`I need to add a partition type. What should that be? freebsd?

Then I need to dd https://download.freebsd.org/ftp/re...AGES/11.0/FreeBSD-11.0-RELEASE-i386-disc1.iso
to /dev/da0s1

How to create the second partition and install Grub2 on it?


----------



## aragats (Apr 5, 2017)

free-and-bsd said:


> This solution is still better than writing a *memstick.img to a whole USB drive, thus leaving the drive unusable for other goals. The 2-partitioned setup allows to always use the 2nd partition for whatever you want.


That's not always true: MS Windows recognizes only the 1st partition of a USB flash drive.
Why not using the 2nd partition for an ISO image?


----------



## free-and-bsd (Apr 6, 2017)

balanga said:


> I'm trying to create a USB stick capable of booting a FreeBSD ISO and thought I'd try following the instructions here but am getting lost...
> 
> Assuming I have 16GB USB stick, I need to reserve a 1GB partition for a FreeBSD*disc1.iso. Using `gpart add -s 1G da0`I need to add a partition type. What should that be? freebsd?
> 
> ...


First of all, unless I'm mistaken the partition table on my USB is not GPT but MBR type. Though I dont' remember now. 

But I'm actually using this setup to boot every new RELEASE ISO from USB. Just want to emphasize it, it's NOT a USB *img file (that doesn't work!), but CD ISO. Don't think it matters much which partition type it is for it will be overwritten by dd anyway.


----------



## balanga (Apr 6, 2017)

Do you set up the USB drive completely using FreeBSD? How do you install and configure Grub2 on the second partition?


----------



## free-and-bsd (Apr 6, 2017)

balanga said:


> Do you set up the USB drive completely using FreeBSD? How do you install and configure Grub2 on the second partition?


First of all, I think I did it under Linux (sorry for the disappointment!) Sysresccd using gparted. Create MBR, create vfat partition on your drive. Leave ~1Gb space BEFORE the partition you're creating. Format this one to vfat (in FreeBSD it will be msdosfs). Google for FreeBSD instructions to the same effect...

Then you mount that vfat/msdosfs partition you just created to /$mountpoint and install grub like this:

```
#grub-install --boot-directory=/$mountpoint/boot /dev/sda (or whatever your device is called)
```
Then in your formatted partition there appears /boot folder with grub config files. Grub2 installs to the disk itself (e.g. /dev/sda, /dev/da0 etc) and NOT to any _partition _(as in /dev/sda2, /dev/da0s1 etc). Then in the free space before the partition you create your other partition up to 1Gb in size etc.

Upon inserting your USB into Windows or other machine it will only mount your msdosfs partition. If you boot from it you'll have grub2 boot menu or prompt. This is the idea.

...Then you can also use this same USB (given it's good size) to boot some ISOs. While the first 1Gb partition will always contain a FreeBSD install CD which you can update as often as you want. I actually even booted Solaris CD this way, so it's pretty safe. Of course, for Solaris CD I played with grub.cfg found on their CD. But for FreeBSD GRUB2 command line is pretty simple...

PS: Tried formatting USB drive with GPT (which would be my preference), but then GRUB2 needs a bios-boot type partition to install the boot images, but in the end it doesn't work. With MBR all works fine.
On the other hand, I haven't tried it with the last GIT version of GRUB2, maybe it works now?


----------



## balanga (Apr 7, 2017)

I'm trying to create the USB stick as you suggest, but only using FreeBSD... USB stick is /dev/da0


```
gpart destroy -F da0
gpart create -s mbr da0
gpart add -t freebsd -s 1g da0
gpart add -t fat32 -s 1g da0
newfs_msdos -F32 /dev/da0s2
```

The second partition is for GRUB. Unfortunately when I try formatting as fat32, I get



> newfs_msdos: trim 8 sectors to adjust to a multiple of 63
> newfs_msdos: 65502 clusters too few clusters for FAT32, need 65525



I haven't seen any examples showing what should be done to overcome this problem. Any advice would be appreciated.


----------



## free-and-bsd (Apr 7, 2017)

Yea... size is important, too. Still, you know why I used Linux for that.


----------



## balanga (Apr 7, 2017)

After some experimenting, I created a 1GB FAT16 partition, followed by a 1GB FAT32 partition, and that one formatted without the previous error, so I guess I can try installing GRUB on the third partition. Should I install the FreeBSD grub2 pkg?


----------



## free-and-bsd (Apr 7, 2017)

balanga said:


> After some experimenting, I created a 1GB FAT16 partition, followed by a 1GB FAT32 partition, and that one formatted without the previous error, so I guess I can try installing GRUB on the third partition. Should I install the FreeBSD grub2 pkg?


Well definitely you WON'T be able to install grub from that. Still, I'm not sure which version of GRUB that pkg supplies... I'm using the latest GIT version for my needs.


----------



## free-and-bsd (Apr 7, 2017)

I mean, with a recent enough GRUB you will be able to even boot your system, not only your USB. I do that when bootloader troubles happen (due to my constant experimenting, not due to that software's faultiness).


----------



## balanga (Apr 7, 2017)

free-and-bsd said:


> Well definitely you WON'T be able to install grub from that. Still, I'm not sure which version of GRUB that pkg supplies... I'm using the latest GIT version for my needs.



FreeBSD's pkg GRUB2 works ok, at least it installs, and I've managed to boot my USB stick to a grub prompt, although there is no grub menu, even though I copied your grub.conf from #33 in this thread (which contains a typo BTW). I did enter a few grub commands and managed to get the ISO image to boot, which I'm pleased about, but it would nice to get the menu to come up.


----------



## free-and-bsd (Apr 7, 2017)

Did you boot FreeBSD ISO???


----------



## balanga (Apr 8, 2017)

Yes, I said so above, and very much appreciate your help in getting this working. I even come up with a little script to automate the process of creating a USB stick which will boot FreeBSD (or mfsBSD, or pfsense)....


```
gpart destroy -F da0
gpart create -s mbr da0
gpart add -t fat32 -b 7g da0
newfs_msdos -F 32 -c 4 /dev/da0s1
gpart add -t freebsd -s 1g da0
gpart add -t freebsd -s 1g da0
gpart add -t freebsd -s 1g da0
fetch https://download.freebsd.org/ftp/releases/amd64/amd64/ISO-IMAGES/11.0/FreeBSD-11.0-RELEASE-amd64-disc1.iso
fetch http://mfsbsd.vx.sk/files/iso/11/amd64/mfsbsd-mini-11.0-RELEASE-amd64.iso
fetch https://nyifiles.pfsense.org/mirror/downloads/pfSense-CE-2.3.3-RELEASE-amd64.iso.gz
dd if=FreeBSD-11.0-RELEASE-amd64-disc1.iso of=/dev/da0s2 bs=64k
dd if=mfsbsd-mini-11.0-RELEASE-amd64.iso of=/dev/da0s3 bs=64k
gunzip pfSense-CE-2.3.3-RELEASE-amd64.iso.gz
dd if=pfSense-CE-2.3.3-RELEASE-amd64.iso of=/dev/da0s4 bs=64k
pkg install grub2
mkdir /mnt/usb
mount -t msdosfs /dev/da0s1 /mnt/usb
grub-install --boot-directory=/mnt/usb/boot /dev/da0
cat << 'EOF' >> /mnt/usb/boot/grub/grub.cfg
menuentry "FreeBSD 11.0-RELEASE" {
set root=(hd0,msdos2)
kfreebsd /boot/loader
}
menuentry "mfsBSD" {
set root=(hd0,msdos3)
kfreebsd /boot/loader
}
menuentry "pfSense" {
set root=(hd0,msdos4)
kfreebsd /boot/loader
}
EOF
umount /mnt/usb
```

In my case da0 is an 8GB USB stick so the script may need adjusting. Give it a try and see if it works for you...

I would have tried more ISO's but I guess four partitions are the limit with gpart and mbr. Not sure if gpart can create extended partitions...

I did try using gpt, but found I couldn't `dd` an ISO to a gpt partition.


----------



## free-and-bsd (Apr 8, 2017)

balanga said:


> I did enter a few grub commands and managed to get the ISO image to boot





balanga said:


> Yes, I said so above


No, you _didn't_ "get an ISO image to boot" -- you _did_ boot from a _partition_ on your USB stick where you'd written an ISO image using dd. It wasn't an "image" any more, but a _partition_, and one would expect a partition to boot, you know.

While I was misled  by your wording to think that you somehow managed to boot an image the way sought earlier in this thread, using the loopback GRUB command.


----------



## balanga (Apr 24, 2017)

Having become a little more familiar with Grub and ISO files I have got as far as initiating a FreeBSD boot using `loopback` using mfsBSD
My Grub entry consists of 

```
menuentry "mfsBSD" {
set isofile="/iso/mfsbsd-mini-11.0-RELEASE-amd64.iso
loopback loop (hd0,3)$isofile
kfreebsd (loop)/boot/loader
}
```

(hd0,3) is a FAT32 partition on a USB disk  which also has FreeBSD 11 installed on the first partition, so I can't be absolutely certain where the loader is being loaded from... Is there any way to put FreeBSD options for the loader into the Grub entry to verify where it is booting from?


----------



## free-and-bsd (Apr 24, 2017)

balanga said:


> Having become a little more familiar with Grub and ISO files I have got as far as initiating a FreeBSD boot using `loopback` using mfsBSD
> My Grub entry consists of
> 
> ```
> ...


Don't waste your time on this one, you may use the results of others who have already wasted enough... Read this thread carefully to find out WHY this will not work. And BTW, not all linux-based ISO's can boot this way, but only those having this capability in kernel initrd-included boot-time scripts.

Hint: cannot pass any boot options to GRUB other than what's supported by FreeBSD kernel. So, you CAN build your own customized miniroot fs (normally not used in FreeBSD) and even include some scripts to boot from an mdconfig created loopback device linked to the ISO on your real device... yea, even describing the procedure takes more time than using conventional ways of booting FreeBSD from ISO image...


----------



## balanga (Apr 24, 2017)

As an alternative to trying to boot FreeBSD from an ISO I created an additional UFS partition and extracted the ISO onto it using `tar zxf FreeBSD-11.0-RELEASE` (no dash to tar). A grub menuentry using kfreebsd worked fine as far as loading went, the only problem being fstab. Once that was changed FreeBSD booted up to `bsdinstall`.

My original idea was to have each BSD distro extracted to a separate partition, but since I haven't yet mastered Grub/EFI, I thought I would create separate  directories for each distro... Unfortunately I can't get

```
kfreebsd /11.0-RELEASE/boot/loader
```
to work.  '11.0-RELEASE' being a subdirectory of '/dev/da0s2a'


----------



## free-and-bsd (Apr 24, 2017)

And how did you happen to have a /dev/da0s2a on your USB flash?? It should have been
/dev/da0p2 or /dev/da0s2, but 2a means you have slices inside a physical partition. Why would you need that on a USB drive??


----------



## balanga (Apr 24, 2017)

I'm currently using a USB attached Hard Disk with a MBR partition type. I added a FreeBSD partition using 
`gpart add -t freebsd -s 5G da0`.


----------



## free-and-bsd (Apr 24, 2017)

balanga said:


> I'm currently using a USB attached Hard Disk with a MBR partition type. I added a FreeBSD partition using
> `gpart add -t freebsd -s 5G da0`.


Well you need to read loader man page about how it works. The short answer is you need it installed, and with MBR there are a couple of tricks how you can avoid installing it as a (only) bootloader into MBR. You install it into MBR, then copy the MBR using dd to your target partition, then you can use that copy of MBR with GRUB chainload command. Not sure though if it exists in GRUB2.


----------



## balanga (Apr 24, 2017)

I simply extracted the ISO to the additional UFS partition (/dev/da0s2a) and it booted up without any problem (once fstab had been changed) and used 

```
set root=(hd0,msdos2,bsd1)
kfreebsd /boot/loader
```


----------



## free-and-bsd (Apr 28, 2017)

Ha, that's interesting. But sure you need to "insmod" a corresponding module into GRUB before it can see your msdos2,bsd1 kind of partition?


----------



## balanga (Apr 29, 2017)

I didn't need to but maybe that depends on where you have GRUB installed. I installed GRUB as a FreeBSD pkg, and it shows (hd0,msdos1,bsd1) if you run `ls` from a *grub> *prompt.  That is, if you have GRUB installed on an mbr disk. I've now managed to get grub-efi installed on a gpt disk and use (hd0,gpt2) to boot FreeBSD.


----------



## free-and-bsd (Apr 29, 2017)

Well I tried grub-efi some time ago, but the problem was that its drive notation was totally different, then secondly after boot I couldn't use X mode for some reason. So since EFI wasn't critical for me (I do care about GPT, but not EFI) I forgot about it.


----------



## diaren (Sep 9, 2018)

With the current mfsbsd you can boot the iso from the grub command line with the following:
N.B. This does not create a menuentry, and is not persistent across reboots.

From your running system, download the iso to e.g. /root (in this case it was a debian stretch installation)
`cd /root
wget http://mfsbsd.vx.sk/files/iso/11/amd64/mfsbsd-mini-11.2-RELEASE-amd64.iso`

Reboot and enter the grub command line
_Change (hd0,1) to the disk with the downloaded iso. This can be checked with  ls [HIGHLIGHT](hd0,1)[I][/I][/HIGHLIGHT][I][/I]/root/*.iso_
`loopback mfs [HIGHLIGHT](hd0,1)[/HIGHLIGHT][FILE]/root/mfsbsd-mini-11.2-RELEASE-amd64.iso[/FILE]
set root=(mfs)
kfreebsd (mfs)[FILE]/boot/kernel/kernel.gz[/FILE]
kfreebsd_module (mfs)[FILE]/mfsroot.gz[/FILE] type=mfs_root
set kFreeBSD.vfs.root.mountfrom=ufs:/dev/md0
boot`

mfsBSD should now be starting up


----------



## Makasimu (Dec 17, 2021)

Please subscribe to the channel to help popularize the FreeBSD operating system.
Creating a multi-boot USB flash drive with the FreeBSD operating system, booting a FreeBSD iso image using GRUB.





Sorry my English, i`m Ukrainian 
Configure https://mabufs.com/blog/booting-freebsd-grub


----------



## liuzhaoyzz (Oct 9, 2022)

thermionix said:


> Also trying this without success, any tips?
> 
> 
> ```
> ...


menuentry "/linux/Freebsd/FreeBSD-13.1-RELEASE-amd64-bootonly.iso" "/linux/Freebsd/FreeBSD-13.1-RELEASE-amd64-bootonly.iso" {
set gfxpayload=keep
insmod bsd
search --no-floppy --set --file $2
loopback loop $2
kfreebsd (loop)/boot/kernel/kernel -v
kfreebsd_module $2 type=mfs_root
set kFreeBSD.vfs.root.mountfrom="cd9660:/dev/md0"
}

It worked in BIOS.But it failed in UEFI mode.What's the problem?


----------

