HOWTO: create custom FreeBSD usb bootable flash

graudeejs

Son of Beastie

Reaction score: 698
Messages: 4,617

Let me explain how to create custom USB bootable flash :D

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:
Code:
# for i in 1 2 3; do gpart delete -i $i da0; done 
# gpart destroy da0
2) create new GPT partitions and install bootcode
Code:
# 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
Code:
# 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 :D
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 :D
If you have 2GB drive you can go on, just don't add 3rd partition :D

Add FreeBSD partition and install bootecode
Code:
# 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:
Code:
# 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 :D

3) create FreeBSD image for gpt partition 2 and format it
Code:
# 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
Code:
# 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
Code:
# 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
Code:
# 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:
Code:
include GENERIC
nocpu		I486_CPU
nocpu		I586_CPU
ident		ALMOSTGENERIC
Now install world and kernel
Code:
# 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]
Code:
# 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...

Code:
# 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:
Code:
loader_logo="beastie"
geom_label_load="YES"
to /jails/resc/etc/fstab add:
Code:
/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
Code:
# bpkg -B
7.3) create and start jails
add something similar to /etc/rc.conf
Code:
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:
Code:
# 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 :D

Now start jail
Code:
# /etc/rc.d/jails start resc
7.4) attach jail and install software
Code:
# 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
Code:
keyrate="fast"
hostname="rescue.pc"
ifconfig_rl0="DHCP"
9) Other system files
Configure other sys files as you want :D

10) Write image to flash
stop jail, unmount image, and detach it
Code:
# /etc/rc.d/jail stop resc
# umount /jails/resc
# mdconfig -du0
write image to flash:
Code:
# 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]
Code:
# 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

Member


Messages: 23

Excelent post brother :D

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 :D
 

LuizBiazus

Member


Messages: 23

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

Member


Messages: 23

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 :D
 
OP
graudeejs

graudeejs

Son of Beastie

Reaction score: 698
Messages: 4,617

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

Member


Messages: 23

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

New Member


Messages: 13

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

New Member


Messages: 18

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

Code:
# make installworld DESTDIR=/jails/resc
# make installkernel DESTDIR=/jails/resc KERNLCONF=ALMOSTGENERIC
# cd etc
# make distribution DESTDIR=/jails/resc
Code:
make installworld DESTDIR=/jails/resc
works well and no error.
But when I input
Code:
make installkernel DESTDIR=/jails/resc KERNLCONF=ALMOSTGENERIC
the system is panic with the following error:
Code:
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.
 
OP
graudeejs

graudeejs

Son of Beastie

Reaction score: 698
Messages: 4,617

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

EDIT:
fixed
 

pigling

New Member


Messages: 18

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

EDIT:
fixed
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.
 
OP
graudeejs

graudeejs

Son of Beastie

Reaction score: 698
Messages: 4,617

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

pigling

New Member


Messages: 18

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.
 
OP
graudeejs

graudeejs

Son of Beastie

Reaction score: 698
Messages: 4,617

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 :D
$ zfs list

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

pigling

New Member


Messages: 18

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

pigling

New Member


Messages: 18

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

graudeejs

Son of Beastie

Reaction score: 698
Messages: 4,617

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
 
Top