Daily Archives: September 26, 2010

Upgrading FreeBSD (using source method)

From time-to-time you will want to upgrade your FreeBSD system.  My preferred method is the source method.

To start, we need to create a file to define our source update.  Part of this requires you to choose a release engineering tag.  This is quite simple to generate.  It is the word RELENG followed by an underscore and the version number.  For example, if your system is running FreeBSD 8.1-RELEASE then your release engineering tag is RELENG_8_1.  If your system is FreeBSD 7.2-RELEASE, your tag is RELENG_7_2.

You can also, if you’re slightly braver, choose the ‘stable’ branch for your version of FreeBSD.  This is the tag with only the major number.  e.g. RELENG_8 for FreeBSD 8 stable.  This is my preferred version so I will use this in my examples.

Edit/create a file called /root/bsdsrc containing:

*default tag=RELENG_8
*default host=cvsup2.us.freebsd.org
*default prefix=/usr
*default release=cvs delete use-rel-suffix compress

src-all

You can change the host to be your local mirror.  See my post on updating the ports tree for a list of mirror sites.

Now, to update your source tree (the files in /usr/src), make sure you’re connected to the internet and run the following command:

csup /root/bsdsrc

This will take a while as it updates any files required based on your released engineering tag.  Once completed, your FreeBSD kernel/OS sources are up-to-date.

Now, we need to build a new kernel and recompile the entire OS.  This sounds far worse than it is 😉

To rebuild the OS, type the following:

cd /usr/src
make buildworld

If you have multiple CPUs/cores in your computer, you can speed this process up by using:

make -j5 buildworld

The number after the ‘j’ should be the number of cores plus one for optimal usage.

This will take a while depending on your system specification.  Once it is complete, you can build a new kernel.  If you are running a custom kernel, you would replace the kernel configuration name with your custom one below.  To compile a new kernel, type:

make buildkernel KERNCONF=GENERIC

Again, you can use the “-j5” notation from above to speed things up.

Ok, now you have both the OS and kernel recompiled and ready to install.  At this point, you have not installed any updates, merely compiled them ready to install.

To install, type:

make installkernel KERNCONF=GENERIC
make installworld

This will install the new kernel and install the OS files.  Now, there’s just one step remaining.  The files in /etc/ are never replaced by the install process – but they may have changed between versions.

FreeBSD includes a handy little utility that compares RCS/CVS ids on these files and allows you to see the difference and install them.

To do this, type the following while in the /usr/src folder:

mergemaster -iFU

This will automatically install any files that the user has not modified, or new files.  Follow any instructions on the screen to handle other files.

Be weary about changes to /etc/group and /etc/master.passwd files.  If you’re unsure, pressing enter will make no changes to the file.

Once this process is complete, you just need to restart your computer and it will boot with the latest kernel and OS.

Formatting external disks for use with FreeBSD

A common question I get asked is how to reformat an external hard drive or USB penstick for use with FreeBSD.

NOTE: this will render the disk only usable in FreeBSD systems.  If you plug the disk into a windows computer, it will say it is not formatted.

First… plug the disk in, then check your console for details about the disk.  You can do this by typing:

tail /var/log/messages

You are looking for the disk name.  It will usually be something like da0 but it could be a different number at the end.

You may notice your disk has been automounted (if you’re running gnome for example) – check your current mounted disks with:

mount

If your disk is mounted, you will need to unmount it before you can format it.  You can unmount it using is path (the bit after ” on ” in the output above).  If your disk was mounted on /media/usbdisk you would unmount with:

umount /media/usbdisk

Once the disk is unmounted (or if it wasnt already mounted) we need to wipe the start of the disk to remove any existing partitions.  You will need the disk name from the console earlier.  I will assume it is da0.  Wipe the start of the drive with:

dd if=/dev/zero of=/dev/da0 bs=1m count=128

This command will write zero’d (blank) data to the first 128MB of the disk at da0.

Next we are ready to format the disk for FreeBSD’s use using UFS2 filesystem.  You will need to decide a name/label for the drive.  I will assume it is usbdisk here.  Format with the following command:

newfs -L usbdisk -O2 -U -m 6 /dev/da0

Once the format is complete, any automounter will auto-mount the disk for you.  Check with the mount command to find out.

If the disk is not mounted, you can mount it with the following command:

mount /dev/ufs/usbdisk /mnt

By default, FreeBSD filesystems have ownership by root only.  You will most likely want to change the ownership to your user on the system.  If your username is ‘dan’ you would do this like so:

chown dan /mnt

That should be everything 🙂

Installing Flash Plugin 10 for Firefox in FreeBSD 8

Flash is annoying.  Adobe seem to have forgotten that other operating systems exist outside of windows, mac and linux.  Because of this, we have to use the linux-based flash plugin for FreeBSD.

However, this is (mostly) easy enough to configure if you know how.  Here’s a guide to do it.

First of all, update your ports tree (see other posts on my blog on how to do this)

Next we need to install the linux compatibility base system (if you haven’t already) – to do this, type the following as root:

cd /usr/ports/emulators/linux_base-f10
make install distclean

This will take a while as it installs a few helpers (like rpm), and downloads many linux RPM packages.

Once it is complete, we can install the linux flash plugin by typing:

cd /usr/ports/www/linux-f10-flashplugin10
make install distclean

This will fetch the linux flash plugin.  If it complains about size mismatches or MD5 checksum failures, then you have likely not updated your ports tree like you were told!

Once this is installed, we need to install a plugin wrapper to make it work… To do this, we type:

cd /usr/ports/www/nspluginwrapper
make install distclean

Now we need to make a quick file link.  To do this, type the following:

cd /usr/local/lib/browser_plugins/
ln -fs /usr/local/lib/npapi/linux-f10-flashplugin/libflashplayer.so

Ok, just one final step now (honest!).  For each user that you want to use the plugin, open a terminal as that user and DO NOT “su root”.  Then type:

nspluginwrapper -v -a -i

If all is well, you’ll see some mention of flash.  Next time you open firefox, flash will be enabled and fully working – including sound.

Feel free to complain to Adobe about the existence of other Operating Systems 🙂

Updating the ports tree in FreeBSD 8

After you have installed your system, you will no doubt want to update your ports tree regularly.

To do this, edit/create a file called /root/bsdports which contains the following:

*default tag=.
*default host=cvsup2.us.freebsd.org
*default prefix=/usr
*default release=cvs delete use-rel-suffix compress

ports-all

You should change the host “cvsup2.us.freebsd.org” to be one of your local mirrors.  You can see a list of mirrors at http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/cvsup.html#CVSUP-MIRRORS

Now to update your ports tree, type:

csup /root/bsdports

This takes quite a while, and will report any files it is updating as it goes.  Once it completes, your ports tree will be up to date.

Loading soundcard drivers in FreeBSD

By default, FreeBSD does not load any sound card drivers for you.

However, almost all soundcards are supported.  First you need to identify your soundcard.  You can do this with:

pciconf -lv | more

This shows a list of PCI devices in your system.  You are looking for a device which has a “class” of “multimedia”.

Mine, for example, shows the following:

none0@pci0:0:27:0:      card=0x022e1028 chip=0x284b8086 rev=0x02 hdr=0x00
vendor     = ‘Intel Corporation’
device     = ‘Intel audio controller embedded with the 82801H chipset ( ICH8 chipset ) (82801H)’
class      = multimedia
subclass   = HDA

If “subclass” says “HDA” (most new soundcards do) then the driver you will need later is snd_hda.  If not, most likely you will want the snd_ich driver.  If your system is really old, you may need another driver – check the list of drivers available by typing:

ls -al /boot/kernel/snd_*.ko

and select the one that looks most promising.

To test the driver is correct, we need to manually load it.  Do this with the following command:

kldload snd_hda

(substitute snd_hda with your required driver)

If all is well, your console screen (or type tail /var/log/messages if you’re not using the console – e.g. if you’re using SSH) will show a few messages about “pcm”.  If the driver is incorrect, nothing will be shown.

If you’re happy with your selection, we need to tell FreeBSD to load the driver on boot every time.  Edit the file /boot/loader.conf and add:

snd_hda_load=”YES”

If you’re using another driver (e.g. snd_ich) then alter the above line to match the driver name followed by _load=”YES”

Everytime you reboot, your computer will load the driver automatically.

Using ‘ee’ to edit files in FreeBSD

When you install FreeBSD, you have a choice of two editors… vi or ee – vi is, for most people, over complicated and scary… so here’s a brief introduction to using ee.

To edit or create a file, you issue the command

ee /path/to/filename.txt

This will bring you into an interactive editor.  You can use the cursor keys to move around and type wherever the cursor is.

At the top of the window, you will see a list of commands.  These are all prefixed with a “^” symbol – this means the Control key.

To do a search, it says to use ^y – press Ctrl Y and type the text you want to search for.  To repeat the last search, press Ctrl X.

To save and exit a file, press ESC then enter twice.  To exit without saving, press ESC, enter then select no save and press enter.

It takes a little getting used to, but it’s a very simple editor and definitely the easiest choice for you in FreeBSD.

Installing gnome GUI on FreeBSD 8

Most people seem to think installing a GUI on FreeBSD is a difficult task.  Here’s how to do it, including little tweaks to make it work even better 😉

Login to your computer as root (or su to root from a regular user)

Use your favourite editor (check my blog for instructions on using ‘ee’ which is included in FreeBSD) to edit /boot/loader.conf – add the following lines:

linux_load=”YES”

linprocfs_load=”YES”

atapicam_load=”YES”

This will load the linux compatibility layer, and linux procfs compatibility modules (these will most likely be used later when you need to install flash plugins) and also the ATAPI scsi emulation driver.  This is used for cd/dvd recording tools within gnome.

Next make sure that the linux proc folder exists by typing:

mkdir -p /compat/linux/proc

And now we will edit the /etc/fstab file.  Add the following lines if they do not exist already:

proc /proc procfs rw 0 0

linproc /compat/linux/proc linprocfs rw 0 0

Next, we will tell FreeBSD to initialise the linux compatibility layer on boot-up.  Edit /etc/rc.conf and add:

linux_enable=”YES”

Ok, now you’re ready to install gnome.  Make sure you’re connected to the internet, and type:

pkg_add -r xorg gnome2

This will install xorg (if needed) and gnome.  It will fetch all dependencies it needs, so it may take a while.  You will be returned to a prompt when it’s finished.

Now to help things move along smoothly, you need to find out your computer’s hostname and add it to your hosts file…  Run the following command to get your hostname:

hostname

Next edit /etc/hosts and add the following line at the bottom (replace ‘hostname’ with the output of the above command):

127.0.0.1 hostname

Now just one thing left to do, and that is to tell FreeBSD to load the gnome components next reboot… Edit /etc/rc.conf and add:

gnome_enable=”YES”

hald_enable=”YES”

gdm_enable=”YES”

dbus_enable=”YES”

reboot, and you will be presented with the gnome login prompt 🙂