Tag Archives: freebsd

Recursively checking out ports tree

I’ve come across a problem posted on #bsdports channel few days ago, how to download a port (FreeBSD) and its dependencies without checking out complete ports tree. So, I hacked a solution for this in Haskell to celebrate my recently attained bliss moment:

λ ghci
GHCi, version 7.6.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :load download-portstree.hs
[1 of 1] Compiling Main             ( download-portstree.hs, interpreted )
Ok, modules loaded: Main.
*Main> downloadPortsTree "editors/emacs-nox11"
Loading package transformers-0.3.0.0 ... linking ... done.
Loading package mtl-2.1.2 ... linking ... done.
Loading package array-0.4.0.1 ... linking ... done.
Loading package deepseq-1.3.0.1 ... linking ... done.
Loading package containers-0.5.0.0 ... linking ... done.
Loading package bytestring-0.10.0.2 ... linking ... done.
Loading package old-locale-1.0.0.5 ... linking ... done.
Loading package time-1.4.0.1 ... linking ... done.
Loading package unix-2.6.0.1 ... linking ... done.
Loading package filepath-1.3.0.1 ... linking ... done.
Loading package directory-1.2.0.1 ... linking ... done.
Loading package process-1.1.0.2 ... linking ... done.
PortsList {portsSeen = fromList ["converters/libiconv","devel/gettext","devel/gmake","devel/libffi","devel/ncurses","devel/pkgconf","editors/emacs-nox11","lang/perl5.16","math/gmp","print/indexinfo","security/ca_root_nss","security/gnutls","security/libgpg-error","security/libtasn1","security/nettle","security/openssl","security/p11-kit","textproc/libxml2"], portsNew = fromList []}
Checking out port editors/emacs-nox11
Checking out port devel/gmake
Checking out port converters/libiconv
Checking out port devel/gettext
Checking out port devel/ncurses
Checking out port devel/pkgconf
Checking out port print/indexinfo
Checking out port security/gnutls
Checking out port lang/perl5.16
Checking out port security/libgpg-error
Checking out port security/libtasn1
Checking out port security/nettle
Checking out port math/gmp
Checking out port security/openssl
Checking out port security/p11-kit
Checking out port devel/libffi
Checking out port security/ca_root_nss
Checking out port textproc/libxml2

Happy hacking lambdas… 😉

Sagittarius Scheme — FreeBSD port

Created a FreeBSD port for Sagittarius Scheme for fun1, and have this:

SHA256 (sagittarius-scheme-0.5.5.tar.xz) = 1ddf2841af2d79ddf2a54346e97fe7c0e45b9c486df69b866d60e2e6c89f7e03

The only issue pending is, fixing building on 10-CURRENT, and 11-CURRENT (i386) platforms, which I need some time to look.

[UPDATE: There you go]

HTH


1In memory of a sagittarian, I once knew.

Listing FreeBSD packages in order of dependency

So, I upgraded GHC (via pkg) few minutes ago, and didn’t upgrade any of hs- packages, which means now their registration information is gone from GHC’s cache. So as a workaround I just decided to invoke their corresponding register.sh which would result in their package being registered:

λ pkg info |awk '/^hs-/ { print $1; }' |xargs -n1 pkg info -l |fgrep register.sh |xargs -n1 sudo /bin/sh

This was ideal command except it didn’t work because package list (pkg info) was not topologically sorted. And RTFM-ing pkg info didn’t result in any option which could output topologically sorted listing. But thanks to pkg info -d which generates the list of dependencies, I hacked this, which worked:

λ pkg info |awk '/^hs-/ { print $1; }' |xargs -n1 pkg info -d |awk '/^[[:space:]]+hs-/ { print $1; }'  |sort |uniq -c |sort -k1 -nr |awk '{ print $2; }' |sed -re 's/-[[:digit:]._,]+$//g' |xargs -n1 pkg info -l |fgrep register.sh |xargs -n1 sudo /bin/sh

🙂

Checkout specific patchset of FreeBSD sources

Following is a script to checkout FreeBSD sources (from its subversion repository) to specific patch-level as asked by a friend on IRC:

#!/bin/sh

if [ -z "$1" -o -z "$2" ]; then
        echo Usage: $0 release patchlevel
        echo e.g. To get 8.0-p5, $0 8.0 5

        exit 0
fi

RVER=$1
PVER=$2

SVNBASE=svn://svn.freebsd.org/base/releng
NEWVERS=sys/conf/newvers.sh
LIMIT=40
RETVAL=0

SVNLOG=$(mktemp -t $(basename $0))

if svn log -l $LIMIT $SVNBASE/$RVER/$NEWVERS >$SVNLOG; then
        BASEREV=$(awk 'BEGIN { sec=1; i=0; } /^r[[:digit:]]+/ { sec=0; revs[i++]=rev=$1; } /^(Security|Errata):/ { sec=1; } /^-+$/ { if(sec == 0) { print revs[i-'$PVER'-1]; exit 0; } }' <$SVNLOG)
        echo svn co -$BASEREV $SVNBASE/$RVER
else
        echo Error executing svn log
        RETVAL=$1
fi

rm -f $SVNLOG
exit $RETVAL

Obligatory screenshot:

chateau.d.if!abbe [~/bin] % co-freebsd-sources 7.0 4
svn co -r182740 svn://svn.freebsd.org/base/releng/7.0
chateau.d.if!abbe [~/bin] % svn cat -r 182740 svn://svn.freebsd.org/base/releng/7.0/sys/conf/newvers.sh|grep RELEASE-p
BRANCH="RELEASE-p4"
Download link: http://www.lostca.se/~abbe/scripts/co-freebsd-sources
SHA256 sum: c9958c4fd7cae5a5e9ff3fa84ba3af6adf38d9c8494b7913ad5b3a2f265a3f48

An ugly diff

Recently, I came across a breakage in editors/emacs-devel port (FreeBSD, ofcourse) on SPARC64 architecture caused by having a struct frame defined in both FreeBSD header machine/frame.h (on Sparc) and Emacs header src/frame.h. To fix it, I renamed frame before it gets defined by FreeBSD header files:

++#ifdef __sparc__ 
++#define frame freebsd_sparc_frame
++#endif
 +#include <sys/user.h>
-+#include <sys/resource.h>
++#ifdef __sparc__ 
++#undef frame
++#endif

So ugly, wish we had namespaces in C 🙁

P.S. Thanks to tabthorpe (mentor) for his Sparc64 tinderbox 🙂

Booting into ZFS only FreeBSD from GRUB2

After being bit by FreeBSD loader half a decade ago, I started preferring GNU GRUB as my bootloader for booting all the operating systems, I ever wrote or decided to try out. Recently I started using ZFS extensively, initially for just my /home, and later for / (root). It is great except for one issue that I needed a UFS2 as / volume for booting. Not a big deal, just created a tiny UFS2 partition and got started…

This worked great for me until yesterday, when due to abnormal power cut which my box experienced, and caused my UFS2 / to literally wipe off after I fscked it :(. Fortunately there wasn’t much stuff on it, except /etc, /boot, /lib{,exec}, and /{s,}bin just enough to boostrap my FreeBSD box and mount my ZFS root which has /usr, /var, /tmp etc. volumes. I was able to reinstall my root, and got back to old setup in just an hour. But to avoid this for future, I decided to give a shot to ZFS-only setup again. The normal way to have a ZFS only system requires having gptzfsboot (for GPT drives, ofcourse) as boot loader, zfsloader as loader (which does some magic so you can set vfs.root.mountfrom as a ZFS volume without getting any root device not found error), but since I use GRUB2, I can’t use gptzfsboot, but I can use zfsloader although that requires a bit of configuration, like creating a loader.conf(5) etc. Anyways, I decided to go zfsloader way, and started /boot/zfsloader from GRUB2 command-line:

grub> kfreebsd (hd0,7)/boot/zfsloader

This presented me with a loader menu, so instead of continuing with loader, I came back to loader prompt. Then I did lsdev and it displayed ZFS volumes. And then I did lsmod to verify if it added zfs.ko, which it didn’t, but I found something else there. A module entry for /boot/zfs/zpool.cache, which revealed the whole magic zfsloader was doing.

So I rebooted and decided to replicate this with GRUB2 taking inspiration from a similar situation I was in few months ago when I was trying to boot dragonflybsd kernel to boot from encrypted HAMMER volume, and needed an initrd to mount root from. Following is the final GRUB2 configuration which worked for me, with changes in bold:

menuentry "FreeBSD 9.0-RC3" {
        set root=(hd0,7)
        kfreebsd /boot/kernel/kernel
        kfreebsd_loadenv /boot/device.hints
        kfreebsd_module /boot/zfs/zpool.cache type=/boot/zfs/zpool.cache
        kfreebsd_module_elf /boot/kernel/opensolaris.ko
        kfreebsd_module_elf /boot/kernel/zfs.ko
        kfreebsd_module_elf /boot/kernel/coretemp.ko
        kfreebsd_module_elf /boot/kernel/geom_journal.ko
        kfreebsd_module_elf /boot/kernel/sound.ko
        kfreebsd_module_elf /boot/kernel/snd_hda.ko
        kfreebsd_module_elf /boot/kernel/uhid.ko
        kfreebsd_module_elf /boot/kernel/ukbd.ko
        kfreebsd_module_elf /boot/kernel/ums.ko
        kfreebsd_module_elf /boot/modules/nvidia.ko
#       set kFreeBSD.vfs.root.mountfrom=ufs:ad14p7
        set kFreeBSD.vfs.root.mountfrom=zfs:zroot
	set kFreeBSD.vfs.zfs.prefetch_disable=1
	set kFreeBSD.vfs.zfs.txg.timeout=5
	set kFreeBSD.maxvnodes=250000
	set kFreeBSD.zfs.txg.write_limit_override=805306368
}

In the above configuration (hd0,7) is my UFS2 partition which hosts /boot, which I’ll soon rm -rf once I upgrade my GRUB to 1.99 (which has support for ZFS), ufs:ad14p7 was my old UFS2 root, and zroot is the my ZFS volume I’m using as /.

chateau.d.if!abbe:~ % mount |grep -v zfs
devfs on /dev (devfs, local, multilabel)
devfs on /var/named/dev (devfs, local, multilabel)
/dev/ad14p7 on /boot (ufs, local, read-only)
chateau.d.if!abbe:~ % zfs list
NAME                        USED  AVAIL  REFER  MOUNTPOINT
home                        147G   538G  79.6G  /home
home/abbe                  65.3G   538G  65.3G  /home/abbe
home/tscache                  2G   540G    16K  -
zroot                      18.0G   178G   351M  legacy
zroot/tmp                   124K   178G   124K  /tmp
zroot/usr                  15.7G   178G  6.66G  /usr
zroot/usr/ports            8.67G   178G   244M  /usr/ports
zroot/usr/ports/distfiles  6.55G   178G  6.55G  /usr/ports/distfiles
zroot/usr/ports/packages   1.88G   178G  1.88G  /usr/ports/packages
zroot/usr/src               351M   178G   351M  /usr/src
zroot/var                  1.60G   178G   502M  /var
zroot/var/crash            31.5K   178G  31.5K  /var/crash
zroot/var/db                895M   178G   846M  /var/db
zroot/var/db/pkg           49.6M   178G  49.6M  /var/db/pkg
zroot/var/empty              31K   178G    31K  /var/empty
zroot/var/log              11.2M   178G  11.2M  /var/log
zroot/var/mail               54K   178G    54K  /var/mail
zroot/var/run              89.5K   178G  89.5K  /var/run
zroot/var/squid             231M  11.8G   231M  /var/squid
zroot/var/tmp               663K   178G   663K  /var/tmp

I hope it doesn’t happen again. Fingers crossed….;)

Let there be dragonflies

After an inspiring post by Siju George on bsd-india list, I decided to use DragonFlyBSD 2.10.1 for NAS (a long-time pending project). The installation using their installer was super-simple, and went fine. The userland is same as FreeBSD userland, and I used HAMMER filesystem as root. The NAS is not finished yet, have to install/configure BackupPC yet.

After installing on NAS, I decided to install on my notebook side-by-side with GNU/Linux. My notebook’s HDD is a GPT disk, with GNU GRUB2 used as bootloader. To install DragonFlyBSD on it, I booted with DragonFlyBSD’s USB image, started shell, and created a large partition of type DragonFly HAMMER, and a small UFS2 (to be used as /boot) using gpt(8). Encrypted partitions using dm-crypt. After that I newfs_hammer(8)-ed it, mount(8)-ed it, cpdup(1)-ed USB filesystem onto it as per instructions, and created an initrd using mkinitrd(8). Following is GRUB2 configuration I used to boot into it.

insmod bsd

menuentry "DragonFlyBSD 2.10-RELEASE" {
        set root='(hd0,6)'

        kfreebsd /kernel/kernel

        kfreebsd_module /kernel/initrd.img type=md_image

        kfreebsd_module_elf /kernel/acpi.ko
        kfreebsd_module_elf /kernel/ahci.ko
        kfreebsd_module_elf /kernel/ehci.ko
        kfreebsd_module_elf /kernel/dm.ko
        kfreebsd_module_elf /kernel/md.ko
        kfreebsd_module_elf /kernel/dm_target_crypt.ko
        kfreebsd_module_elf /kernel/wlan.ko
        kfreebsd_module_elf /kernel/wlan_ccmp.ko
        kfreebsd_module_elf /kernel/wlan_tkip.ko
        kfreebsd_module_elf /kernel/ath_hal.ko
        kfreebsd_module_elf /kernel/ath_rate.ko
        kfreebsd_module_elf /kernel/if_ath.ko
        kfreebsd_module_elf /kernel/coretemp.ko
        kfreebsd_module_elf /kernel/sound.ko
        kfreebsd_module_elf /kernel/snd_hda.ko

        set kFreeBSD.vfs.root.mountfrom=ufs:md0s0
        set kFreeBSD.vfs.root.realroot=crypt:hammer:/dev/da0s4:dragon
}
### END /etc/grub.d/01_bsd ###

Thanks to alexh on #dragonflybsd on EFnet for help with installation.

Default sound device in FreeBSD

I’ve an nVidia Graphics card in my FreeBSD box, and snd_hda.ko detects audio device on it as well. But the problem is, the audio interface is masked on the card, so there are no physical audio sockets on the card.

chateau.d.if!abbe:~ % cat /dev/sndstat
FreeBSD Audio Driver (newpcm: 64bit 2009061500/amd64)
Installed devices:
pcm0: <HDA NVidia (Unknown) PCM #0 DisplayPort> (play) default
pcm1: <HDA NVidia (Unknown) PCM #0 DisplayPort> (play)
pcm2: <HDA NVidia (Unknown) PCM #0 DisplayPort> (play)
pcm3: <HDA NVidia (Unknown) PCM #0 DisplayPort> (play)
pcm4: <HDA Realtek ALC889 PCM #0 Analog> (play/rec)
pcm5: <HDA Realtek ALC889 PCM #1 Analog> (play/rec)
pcm6: <HDA Realtek ALC889 PCM #2 Digital> (play)
pcm7: <HDA Realtek ALC889 PCM #3 Digital> (play)

The only problem with this setup is that all of my sound applications which try to use /dev/dsp end up using pcm0, which doesn’t exist physically. The tricky thing is that, there is no /dev/dsp on filesystem, so kernel internally manages accesses to that device node. To switch to on-board audio (i.e. pcm4) as default sound device, I did sysctl hw.snd.default_unit=4, which worked:

chateau.d.if!abbe:~ % cat /dev/sndstat
FreeBSD Audio Driver (newpcm: 64bit 2009061500/amd64)
Installed devices:
pcm0: <HDA NVidia (Unknown) PCM #0 DisplayPort> (play)
pcm1: <HDA NVidia (Unknown) PCM #0 DisplayPort> (play)
pcm2: <HDA NVidia (Unknown) PCM #0 DisplayPort> (play)
pcm3: <HDA NVidia (Unknown) PCM #0 DisplayPort> (play)
pcm4: <HDA Realtek ALC889 PCM #0 Analog> (play/rec) default
pcm5: <HDA Realtek ALC889 PCM #1 Analog> (play/rec)
pcm6: <HDA Realtek ALC889 PCM #2 Digital> (play)
pcm7: <HDA Realtek ALC889 PCM #3 Digital> (play)

To persist the changes, I added it to sysctl.conf(5). I’ve not rebooted it yet, but I’m sure it’ll work 😉

Setting up MTNL 3G internet connection in FreeBSD

Recently I bought an MTNL 3G internet connection to add some redundancy to my internet connectivity stack. As I use, Following is how I got it running in FreeBSD (8.2-RELEASE) with the help from a similar HOWTO for GNU/Linux.

1. Plugin the device. Following is the dmesg output after plugging in the device (Teracom LW272):

umass0: <6227--Storage> on usbus4
umass0:  SCSI over Bulk-Only; quirks = 0x0000
umass0:0:0:-1: Attached to scbus0
ugen4.2: <D-88n> at usbus4 (disconnected)
umass0: at uhub4, port 1, addr 2 (disconnected)
usb_alloc_device: Found possible auto-install disk (trying next config)
ugen7.2: <HSPADataCard> at usbus7
umass0: <HSPADataCard  > on usbus7
umass0:  SCSI over Bulk-Only; quirks = 0x0000
umass0:0:0:-1: Attached to scbus0
(probe0:umass-sim0:0:0:0): TEST UNIT READY. CDB: 0 0 0 0 0 0
(probe0:umass-sim0:0:0:0): CAM status: SCSI Status Error
(probe0:umass-sim0:0:0:0): SCSI status: Check Condition
(probe0:umass-sim0:0:0:0): SCSI sense: UNIT ATTENTION asc:28,0 (Not ready to ready change, medium may have changed)
cd0 at umass-sim0 bus 0 scbus0 target 0 lun 0
cd0: <HSPAData CD-ROM 0000> Removable CD-ROM SCSI-0 device
cd0: 40.000MB/s transfers
cd0: cd present [8982 x 2048 byte records]
(probe0:umass-sim0:0:0:1): TEST UNIT READY. CDB: 0 20 0 0 0 0
(probe0:umass-sim0:0:0:1): CAM status: SCSI Status Error
(probe0:umass-sim0:0:0:1): SCSI status: Check Condition
(probe0:umass-sim0:0:0:1): SCSI sense: NOT READY asc:3a,0 (Medium not present)
da0 at umass-sim0 bus 0 scbus0 target 0 lun 1
da0: <HSPAData SD Card 0000> Removable Direct Access SCSI-0 device
da0: 40.000MB/s transfers
da0: Attempt to query device size failed: NOT READY, Medium not present

2. Load umodem kernel module to recognize the modem in the device, no usb_modeswitch (or its equivalent) required:

chateau.d.if!abbe:~ % sudo kldload umodem
Password:
chateau.d.if!abbe:~ % dmesg|tail -4
umodem0: <HSPADataCard USB Modem1> on usbus7
umodem0: data interface 2, has CM over data, has break
umodem1: <HSPADataCard USB Modem2> on usbus7
umodem1: data interface 4, has CM over data, has break

3. Add following lines ppp.conf for dialing, in my case for prepaid connection (APN: gprsppsdel):

mtnl:
        set device /dev/cuaU0  # USB modem device node created after loading umodem
        set speed 460800
        set phone "*99#"
        set authname mtnl
        set authkey mtnl123
        accept PAP
        accept CHAP
        set dial "ABORT BUSY ABORT NO\\sCARRIER \
         \"\" AT OK-AT-OK \
         ATEQV1 OK \
         AT+CFUN=6 OK \
         AT+CGDCONT=1,\\\"ip\\\",\\\"gprsppsdel\\\" OK \\dATDT\\T CONNECT"
        set ifaddr 10.0.0.1/0 10.0.0.2/0 255.255.255.255 0.0.0.0
        add default HISADDR
        disable dns
        disable ipv6cp
        disable mppe
        enable lqr

4. Start dialer:

chateau.d.if!abbe:~ % sudo ppp -ddial mtnl
Working in ddial mode
Using interface: tun0

(wait for 5-10 seconds)

chateau.d.if!abbe:~ % ifconfig tun0
tun0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> metric 0 mtu 1500
        options=80000<LINKSTATE>
        inet 59.179.150.204 --> 10.0.0.2 netmask 0xffffffff
        Opened by PID 12379
chateau.d.if!abbe:~ % curl http://checkip.dyndns.org/
<html><head><title>Current IP Check</title></head><body>Current IP Address: 59.179.150.204</body></html>

I’m planning to use it with mpd5, will post the instructions when I’m done setting it up.