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:
2) create new GPT partitions and install bootcode
Since I want to be 100% sure, that my image can fit in 2GB drive I do
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
now if you have more than 2GB on drive:
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
4) mount rescue.img
We use -o noatime, to make writing to rescue.img faster
5.a) Install custom world and kernel
If you have already build world and kernel and haven't cleaned after installing, you can skip building world and kernel now
replace ALMOSTGENERIC with kernel you want to build and install on usb flash
I wanted to create almost GENERIC kernel:
Now install world and kernel
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]
press y if directory is right...
6) configure loader.conf and fstab
to /jails/resc/boot/loader.conf add:
to /jails/resc/etc/fstab add:
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
7.3) create and start jails
add something similar to /etc/rc.conf
run:
replace IP's in red with appropriate numbers
Now start jail
7.4) attach jail and install software
Use pkg_add to install all soft that you want
8) configure rc.conf
add to /jails/resc/etc/rc.conf
9) Other system files
Configure other sys files as you want
10) Write image to flash
stop jail, unmount image, and detach it
write image to flash:
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]
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
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)
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
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
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
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
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)?
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
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
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
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
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