# FreeBSD - quick install



## balanga (May 25, 2017)

I'm trying to come up with a means of doing a quick install of FreeBSD. To do this I am booting mfsBSD either via PXE or from some local medium, and then running the following to install FreeBSD 11.0-RELEASE on the first USB device...


```
gpart destroy -F da0
gpart create -s gpt da0
gpart add -t freebsd-boot -l gpboot -s 512k da0
gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 da0
gpart add -t freebsd-ufs -l gprootfs -s 2G da0
newfs /dev/da0p2
mount /dev/da0p2 /mnt
cd /mnt
fetch ftp://ftp.freebsd.org/pub/FreeBSD/releases/amd64/11.0-RELEASE/base.txz
fetch ftp://ftp.freebsd.org/pub/FreeBSD/releases/amd64/11.0-RELEASE/kernel.txz
tar xfp base.txz
tar xfp kernel.txz
cp /etc/resolv.conf /mnt/etc/resolv.conf
cat 'pkg install pkg mc grub2-efi' >/mnt/script
chroot /mnt
```

There are a couple of things missing (maybe more...) such as missing /etc/fstab and /etc/rc/conf, but it does install a working, bootable FreeBSD.

One thing I can't get to work is the cmd `cat 'pkg install pkg mc grub2-efi' >/mnt/script`
 where `script` is the cmd I want to run from the `chroot`environment. Anyone know why it doesn't work?


----------



## SirDice (May 26, 2017)

balanga said:


> One thing I can get to work is the cmd  cat 'pkg install pkg mc grub2-efi' >/mnt/script
> where  script is the cmd I want to run from the  chrootenvironment. Anyone know why it doesn't work?


It probably does work but pkg(8) might be waiting for the "Y" confirmation. You also need to run the 'bootstrap' first.


```
env ASSUME_ALWAYS_YES=yes pkg -y bootstrap
env ASSUME_ALWAYS_YES=yes pkg -y mc grub2-efi
```

Alternatively you might want to have a look at sysutils/firstboot-pkgs which can do all this automated.


----------



## balanga (May 27, 2017)

balanga said:


> One thing I can't get to work is the cmd `cat 'pkg install pkg mc grub2-efi' >/mnt/script`
> where `script` is the cmd I want to run from the `chroot`environment. Anyone know why it doesn't work?



That's because `cat` should be `echo`. You idiot!!!


----------



## balanga (May 27, 2017)

SirDice said:


> It probably does work but pkg(8) might be waiting for the "Y" confirmation. You also need to run the 'bootstrap' first.
> 
> 
> ```
> ...



How do you set the chroot environment?


----------



## ShelLuser (May 27, 2017)

Note that you don't have to use environment variables though, this one might be easier: `echo 'pkg install -y pkg mc grub2-efi' >/mnt/script`. Also see pkg-install(8).


----------



## balanga (May 27, 2017)

I already tried that, and got:-

```
Please set ASSUME_ALWAYS_YES=yes environment variable to be able to bootstrap in non-interactive (stdin not being a tty)
```


----------



## balanga (May 28, 2017)

Getting an environment variable set in a chroot environment is proving a little tricky...

Can anyone help?


----------



## krawall (May 28, 2017)

```
echo 'env ASSUME_ALWAYS_YES=yes pkg -y mc grub2-efi' >> /mnt/script
```
Or if you're writing a sh script:

```
echo 'ASSUME_ALWAYS_YES=yes pkg -y mc grub2-efi' >> /mnt/script
```

It's enough to set the environment variable somewhere in the script before calling pkg..


----------



## balanga (May 28, 2017)

I guess I didn't understand Sir Dice's post above...


----------



## krawall (May 28, 2017)

Also weird that nobody pointed out yet that pkg is perfectly happy to directly work in a chroot environment from the outside:

```
pkg -c /path/to/chroot/ install mc grub2-efi
```
(this also works for -j <jail name or id> and -r /path/to/alternate/root/, see pkg(8))


----------



## balanga (May 28, 2017)

Unfortunately `pkg`is not yet available in this environment.


----------



## krawall (May 28, 2017)

mfsBSD really has no pkg or pkg-static available?


----------



## balanga (May 28, 2017)

The mini edition which I'm using doesn't. Not sure about other versions. It is only 23MB in size so quite a bit of functionality is doubtless missing.


----------



## krawall (May 28, 2017)

I see. Never used mfsBSD before..


----------



## balanga (May 29, 2017)

Moving on.... I now have:-


```
gpart destroy -F da0
gpart create -s gpt da0
gpart add -t freebsd-boot -l gpboot -s 64k da0
gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 da0
gpart add -t freebsd-ufs -l gprootfs -s 2G da0
newfs /dev/da0p2
mount /dev/da0p2 /mnt
cd /mnt
fetch ftp://ftp.freebsd.org/pub/FreeBSD/releases/amd64/11.0-RELEASE/base.txz
fetch ftp://ftp.freebsd.org/pub/FreeBSD/releases/amd64/11.0-RELEASE/kernel.txz
tar xfp base.txz
tar xfp kernel.txz
cp /etc/resolv.conf /mnt/etc/resolv.conf
echo 'env ASSUME_ALWAYS_YES=yes pkg bootstrap -f' >/mnt/script
echo 'env ASSUME_ALWAYS_YES=yes pkg -y install mc grub2-efi' >>/mnt/script
chroot /mnt sh script
```

This creates a bootable device (da0), but lacks an /etc/fstab at the moment.

Can I create one using the label gprootfs used when I created the freebsd-ufs partition?

And should it look like this?

```
/dev/ufs/gprootfs     /     ufs     rw     1     1
```

I guess I can always suck it and see...


----------



## balanga (May 29, 2017)

That entry for /etc/fstab didn't work...

Anyway here is my latest effort...


```
gpart destroy -F da0
gpart create -s gpt da0
gpart add -t freebsd-boot -l gpboot -s 64k da0
gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 da0
gpart add -t freebsd-ufs -l gprootfs -s 2G da0
newfs /dev/da0p2
mount /dev/da0p2 /mnt
cd /mnt
fetch ftp://ftp.freebsd.org/pub/FreeBSD/releases/amd64/11.0-RELEASE/base.txz
fetch ftp://ftp.freebsd.org/pub/FreeBSD/releases/amd64/11.0-RELEASE/kernel.txz
tar xfp base.txz
tar xfp kernel.txz
cp /etc/resolv.conf /mnt/etc/resolv.conf
echo '/dev/da0p2   /   ufs   rw   1   1' >/mnt/etc/fstab
echo 'env ASSUME_ALWAYS_YES=yes pkg bootstrap -f' >/mnt/script
echo 'env ASSUME_ALWAYS_YES=yes pkg -y install mc grub2-efi' >>/mnt/script
echo 'grub-install /dev/da0' >>/mnt/script
chroot /mnt sh script
cat <<EOF >/mnt/boot/grub/grub.cfg

set timeout=5

set default 0

set menu_color_normal=white/blue
set menu_color_highlight=yellow/green

menuentry "FreeBSD from new disk" {
    set root=(hd0,gpt2)
    kfreebsd /boot/loader
}
EOF
```

The `grub-install` part doesn't work, I get the following error:-

```
grub-install: info: changing current directory to /dev.
grub-install: error: cannot find a device for /boot/grub (is /dev mounted?).
```
However when in the chroot environment the cmd `grub-install /dev/da0` works OK. 

Anyone know what I'm missing?


----------



## krawall (May 30, 2017)

Is /dev mounted in the chroot environment (and does /dev/ad0 exists there)? There is nothing in your script to indicate that and without access to your devices `grub-install` shouldn't work at all.
Also why do you need grub if you only put a FreeBSD entry in it? You should be able to just install boot0 (the default FreeBSD boot manager) from mfsBSD: `fdisk -B -b /boot/boot0 /dev/da0`

see https://www.freebsd.org/doc/handbook/boot-introduction.html


----------



## balanga (May 30, 2017)

This 'quick install' of mine is work in progress with the idea of creating a multiboot disk which will utilise grub as the boot loader.

Pardon my ignorance but I'm not really sure what you mean by 'Is /dev mounted in the chroot environment (and does /dev/ad0 exists there)?' 

What do I need to display? The output of `mount`?


----------



## krawall (May 30, 2017)

You need to be able to access the block device /dev/da0 from within the chroot environment or grub wont be able to write something to the mbr of your harddrive. You enter your chroot environment and check if the block device you specified (/dev/ad0) exists there. If not do a `mount -t devfs devfs /mnt/dev` after you mounted your root filesystem.

See devfs(5)


----------



## balanga (May 30, 2017)

Many thanks. I would not have stumbled on this by myself.


----------



## debguy (Jul 17, 2017)

hmmm.  that might not always work but i wrote a lengthier script doing similar for debian once.

the issue is your running unix (freebsd infact) and installing unix which is an advantage.  your using chroot which is another advantage.  but you never reboot the new system.

my warning is: things in the new chroot may depend on (or conflict with) a newer kernel version - possibly in nasty ways.  you need to do your homework before deciding if "chroot /mnt" can use libraries that (access disk, like gparted) or do anything nasty that expects/assumes features of the new kernel are in the same distro.

another warning: you can't just copy /mnt to /, because that may cause things to go haywire ESPECIALLY if "bsd moderators" forced in used of RPATH.  if they did you have allot more work to do before booting, if not - some simple cp commands will do the trick   but they surely did use RPATH.  your trapped and need to use "an installer disk" or 2nd bootable partition or what.

it's a nice nifty trick you showed for advanced users, but not something novices should be told "works no problem"


----------

