# HOWTO: create custom FreeBSD usb bootable flash



## graudeejs (Dec 27, 2009)

Let me explain how to create custom USB bootable flash 

My goal is to create USB image that will be 2GB size.... but I want to be able to install this image on 16GB uzb flash, and use rest of disk space for backups.

This is definitely not the only way to do this...

So here I go:
Since I don't know how much exactly 2GB usb flash had... I'll plug in my 2GB flash and create gpt partitions. 

All commands here will be done on sh compatible shell shells/mksh, some commands won't execute on [t]csh (which is default shell), without modifications

1) Remove existing partitions etc...
I had 3 gpt partitions on flash:

```
# for i in 1 2 3; do gpart delete -i $i da0; done 
# gpart destroy da0
```

2) create new GPT partitions and install bootcode

```
# gpart create -s gpt da0
# gpart add -b 34 -s 128 -t freebsd-boot da0
```

Since I want to be 100% sure, that my image can fit in 2GB drive I do

```
# gpart show da0
=>     34  3841981  da0  GPT  (1.8G)
       34      128    1  freebsd-boot  (64K)
      162  [Red]3841853[/red]       - free -  (1.8G)
```
Number in red is size in sectors of image that we can create, that will fit 2GB flash 
Before we can use this number we need to convert it in bytes:
[red]3841853[/red]*512=*1967028736*

Now to make things shorter, from now on forget that I used 2GB drive and pretend that I said I'm using 16GB drive.... I needed to use 2GB drive to only get the number in bold and red 
If you have 2GB drive you can go on, just don't add 3rd partition 

Add FreeBSD partition and install bootecode

```
# gpart add -b 162 -s 3841853 -t freebsd-ufs da0
# gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 da0
```

now if you have more than 2GB on drive:

```
# gpart add -t freebsd-ufs da0
# newfs -nUL bak /dev/da0p3
```
This will create 3rd gpt partition with all remaining space on flash. Then we format it with Soft-Updates enable and label it bak 

3) create FreeBSD image for gpt partition 2 and format it

```
# cd /jails
# dd if=/dev/zero of=rescue.img bs=1 count=1 seek=[b]1967028736[/b]
# mdconfig -at vnode -f rescue.img
# newfs -nUL rescue /dev/md0
```

4) mount rescue.img

```
# mkdir /jails/resc
# mount -o noatime /dev/md0 /jails/resc
```
We use -o noatime, to make writing to rescue.img faster

5.a) Install custom world and kernel

```
# cd /usr/src
```

If you have already build world and kernel and haven't cleaned after installing, you can skip building world and kernel now

```
# make buildworld buildkernel KERNCONF=ALMOSTGENERIC
```
replace ALMOSTGENERIC with kernel you want to build and install on usb flash
I wanted to create almost GENERIC kernel:

```
include GENERIC
nocpu		I486_CPU
nocpu		I586_CPU
ident		ALMOSTGENERIC
```

Now install world and kernel

```
# make installworld DESTDIR=/jails/resc
# make installkernel DESTDIR=/jails/resc KERNCONF=ALMOSTGENERIC
# cd etc
# make distribution DESTDIR=/jails/resc
```

5.b) install generic world and kernel
insert FreeBSD disk1 or dvd in cdrom
or insert FreeBSD usb flash
or download any of these images and use mdconfig to attach them
mount image... [I'll assume we're using cdrom]

```
# mount -t cd9660 /dev/acd0 /mnt
# cd /mnt/8.0-RELEASE/base
# export DESTDIR=/jails/resc
# ./install.sh
You are about to extract the base distribution into /jails/resc - are you SURE
you want to do this over your installed system (y/n)?
```
press *y* if directory is right...


```
# cd ../manpages
# ./install.sh
# cd ../kernels
# ./install.sh generic
# cd /jails/resc/boot
# rm -Rf kernel
# mv GENERIC kernel
```

6) configure loader.conf and fstab
to /jails/resc/boot/loader.conf add:

```
loader_logo="beastie"
geom_label_load="YES"
```

to /jails/resc/etc/fstab add:

```
/dev/ufs/rescue / ufs rw,noatime 1 1
```

7) Install custom software
So far everything was quite generic... now we shall install custom software....
and again, even here, there are many ways to do this... But I want to do this fast.
7.1) install ports-mgmt/bpkg
7.2) create backup packages for all installed ports

```
# bpkg -B
```
7.3) create and start jails
add something similar to /etc/rc.conf

```
jail_enable="YES"
jail_resc_hostname="rescue.jail"
jail_resc_rootdir="/jails/resc"
jail_resc_fstab="/jails/fstab"
jail_resc_interface="rl0"
jail_resc_exec="/bin/sh /etc/rc"
jail_resc_devfs_enable="YES"
jail_resc_mount_enable="YES"
jail_resc_ip="[color="SeaGreen"]192.168.128.99[/color]"
```

run:

```
# ifconfig rl0 alias [color="SeaGreen"]192.168.128.99[/color] netmask 0xffffffff
# touch /jails/resc/etc/fstab
# echo "/var/tmp/bpkg /jails/resc/mnt nullfs noatime,rw 0 0" > /jails/fstab
# echo 'default_router="[red]192.168.128.1[/red]"' >> /jail/resc/etc/rc.conf
# echo 'nameserver [red]192.168.128.1[/red]' >> /jails/resc/etc/resolv.conf
```

replace IP's in red with appropriate numbers 

Now start jail

```
# /etc/rc.d/jails start resc
```

7.4) attach jail and install software

```
# jexec 1 csh
resc# cd /mnt
resc# pkg_add tmux-1.1.tbz
```
Use pkg_add to install all soft that you want

8) configure rc.conf
add to /jails/resc/etc/rc.conf

```
keyrate="fast"
hostname="rescue.pc"
ifconfig_rl0="DHCP"
```

9) Other system files
Configure other sys files as you want 

10) Write image to flash
stop jail, unmount image, and detach it

```
# /etc/rc.d/jail stop resc
# umount /jails/resc
# mdconfig -du0
```

write image to flash:

```
# dd if=rescue.img of=/dev/da0p2 bs=1m
```

11) Disable soft updates
Soft updates are good when you write a lot to FS.
On our flash we don't need that [after it's created]
so let's turn it off {it's kinda better for root fs to have Soft Updates turned off]

```
# tunefs -n disable /dev/da0p2
```

12) try it
Reboot, if it works... then you did everything right {and I probably wrote everything right as well}


Hope this will be useful for someone.

UPDATE:
btw, if you don't want to create jail, you can simply use chroot, and mount necessary fs with nullfs


----------



## LuizBiazus (Dec 28, 2009)

Excelent post brother  

exist some way to do something similar following ur method....
something like this...
i have a full functional freebsd 8 server installed... with all the softwares that i need (php,apache,postgresql,users,databases...) modifications on loader.conf and sysctl.conf, etc...

and MOVE all of this to the pendrive or CD? 

the intention is make an Easy install


----------



## graudeejs (Dec 29, 2009)

Well, 
http://forums.freebsd.org/showthread.php?t=185
or use zfs snapshots, and `$ zfs send zfs/fs@snapshot | gzip > /path/where/to/save.gz`

you may use tar..... or cp -Ra {I think}

But I'd go with zfs snapshots or dump/restore for this


----------



## LuizBiazus (Dec 29, 2009)

this other post of full backup are amazing!!!!
exist some way to use this tar.gz file without installing the freebsd and start in a single mode?

I mean... something like boot and put the location to restore, like that install /dev/ad0?


is it possible?


this tar.gz i think is perfect!!!! if the boot restore is possible dont exist best way to do that!!!


----------



## LuizBiazus (Dec 29, 2009)

is it possible transfor this portable usb distro in a liveCD?
if can i do that i can make one tiny distro to run the tar.gz


----------



## graudeejs (Dec 29, 2009)

Not the way I understand you want it.....
I also don't see point in this

you can use dvd/livefs cd or FreeBSD usb, in fixit mode to extract backup packages, but that already off-thread


----------



## LuizBiazus (Dec 30, 2009)

i solve it with clonezilla... it creates a ISO image 
the bad point is lost the freespace on the new bigger hardisk

thank you very much friend


----------



## yvonney (Jul 12, 2010)

I have gotten FBSD to run from a 1 gig stick some while ago.

I now would like to install something like the above tips here only onto the 3rd partition of an 8gig stick.(without a bootloader as I will use grub2 for this experiment)

So right now this stick is booting well with grub2 in mbr and partition 1 as 150meg.I have linux booting on partition 2 in about 3.5 gig and now I have a partition 3 waiting for freebsd.

Anyone know what the gottchas are for doing something like the above only have bsd on just the 3rd partition of the stick with no bootloader?

thanks.


----------



## pigling (Aug 11, 2010)

I follow the instructions and everything is ok until I come to the following:


```
# make installworld DESTDIR=/jails/resc
# make installkernel DESTDIR=/jails/resc KERNLCONF=ALMOSTGENERIC
# cd etc
# make distribution DESTDIR=/jails/resc
```


```
make installworld DESTDIR=/jails/resc
```
works well and no error.
But when I input 
	
	



```
make installkernel DESTDIR=/jails/resc KERNLCONF=ALMOSTGENERIC
```
the system is panic with the following error:

```
g_vfs_done():md0[WRITE(offset=4656005120, length=131072)]error = 28
/jails/resc: got error 28 while accessing filesystem
panic: softdep_deallocate_dependencies: unrecovered I/O error
cpuid = 1
Uptime:12m14s
Cannot dump. Device not defined or unavailable.
Automatic reboot in 15 seconds - press a key on the console to abort
```
However, the system does not automatically reboot in 15 seconds. And I can't abort the console with "any" key. The fan continuously runs without break. 

BTW, my kernel is FreeBSD 8.0-RELEASE.

Thanks for help.


----------



## graudeejs (Aug 11, 2010)

It's a typo, it should be
`# make [b]KERNCONF[/b]=ALMOSTGENERIC`
[not *KERNLCONF*]
I will fix howto

EDIT:
fixed


----------



## pigling (Aug 11, 2010)

killasmurf86 said:
			
		

> It's a typo, it should be
> `# make [b]KERNCONF[/b]=ALMOSTGENERIC`
> [not *KERNLCONF*]
> I will fix howto
> ...



I know that is a typo, but it has nothing wrong with my problem. In fact, I use "KERNCONF" but not "KERNLCONF" and the issue is still same. Thanks.


----------



## graudeejs (Aug 11, 2010)

well, in that case this is your systems configuration fault....
are you using zfs?


----------



## pigling (Aug 11, 2010)

killasmurf86 said:
			
		

> well, in that case this is your systems configuration fault....
> are you using zfs?



How do I know I am using zfs?
BTW, I just repeat the steps. This time, I installkernel first and it succeeds. Then I try installworld and it still prompts error 28. The whole procedure still fails.


----------



## graudeejs (Aug 11, 2010)

pigling said:
			
		

> How do I know I am using zfs?
> BTW, I just repeat the steps. This time, I installkernel first and it succeeds. Then I try installworld and it still prompts error 28. The whole procedure still fails.



If you don't know, then you're using UFS 
`$ zfs list`

and since you're not using zfs, I've no idea, what the problem is, maybe check free space


----------



## pigling (Aug 11, 2010)

```
zfs list
internal error: failed to initialize ZFS library
```
So does this mean I am not using ZFS?


----------



## graudeejs (Aug 11, 2010)

pigling said:
			
		

> ```
> zfs list
> internal error: failed to initialize ZFS library
> ```
> So does this mean I am not using ZFS?



yes


----------



## pigling (Aug 11, 2010)

Then you suspect the space not enough. Which part should I check free space? USB space or native FreeBSD /usr/src space?


----------



## graudeejs (Aug 11, 2010)

pigling said:
			
		

> Then you suspect the space not enough. Which part should I check free space? USB space or native FreeBSD /usr/src space?



you need space in /usr/obj


----------

