leaner code is cleaner code is more efficient code

leaner code is cleaner code is more efficient code. a program with 100 lines of code is better than a program with 10,000 lines of code. less prone to bugs and (un)intentional security holes, easier to decode/maintain, cheaper to run... etc.

of course, comments are not code and documentation is to be encouraged. but documentation should be maintained separate from code; not maintained within the code. any documentation change should not result in code change requests.

i'm gradually migrating to the console, which is where it all started! :-) i strongly prefer working from the console, mainly because console apps are leaner and not dependant on unnecessary services like mouse or window manager. i fire up x, only only when i need to use gui apps. i work on the console within tmux (previously screen), which allows me to keep my environment when i transfer between console and x.

i am on a constant quest for more efficient apps. soon as i find one, i change over. efficiency means smaller code, focussed approach. for example, i changed over from screen to tmux a while back, and from ratpoison to dwm more recently.

openbsd 5.1

i had to have a go.. and i just did :) i've been reading & hearing much about bsd lately. looking at the various bsd flavours floating about, i narrowed down my focus to two - freebsd & openbsd - based on popularity. i chose openbsd, for it's emphasis on clean code and security.

i was pleasantly surprised to find that openbsd used only 455m diskspace for a full & complete installation, including x and a few default window managers. i chose not to install the compilers. and the whole install took only minutes! i'm going to spend more time with openbsd and really look at migrating over.

debian would use about that same space for a minimal base install. and that was after i really hack a minimal base install. much more space is then required for installing x/dependencies for a functional system. after cutting and pruning down my system, i'd still end up with 1g plus, after a lot of work. my brief flirtation with arch required about 700m plus for the just the base install. and about 2g for a functional system. though i really like the arch philosophy, i reluctantly returned back to debian, my on-off long-term relationship since the potato days.

i had a quick look at freebsd, to whet my curiosity for its popularity. install cd is 502m, more than twice the size of openbsd's install cd. diskspace required for minimal install is 1.1g, about 2.5x openbsd full install. i guess the popularity results from it being a free-for-all accepting closed-source binaries from anywhere.

my brief notes from openbsd install follows:

* read openbsd faq
i mean really read it, and keep it open during install.

* download install51.iso for offline standalone install
i had no idea, how many times i would need to do the install to end up with a stable system. so i chose to download the full-cd for an off-the-network install. otherwise, there are much smaller downloads for alternative install-media, like netinstalls.

* boot from install-cd
: boot prompt, hit enter

* dmesg shows white text on blue background
logged at /var/run/dmesg.boot

* (i)nstall to create new openbsd system with new partitions
(u)pgrade only for upgrading from just the previous version, ie 5.0 only.
for older versions, you need to do each intermediate version upgrade. in that case, a full install might be more appropriate.

* install prompts:
at any install prompt, ! creates a shell. exit the shell, to return back to the same install prompt.
^c to quit installation.

* network interfaces
openbsd creates new interfaces for each different network driver like fxp0 xl0, rather than generic device names like eth0 eth1 in linux. i can see the pros & cons to both. personally, i would ln -s eth0 to whatever netdev in openbsd, to ensure my scripts don't break. i'm still very new to openbsd. i'm pretty sure that there might be better options and i need to discover those.
virtio is not understood. so don't use it, if you are installing on a virtual machine.

* dhcp yes. ipv6 no. sshd no. ntpd no.

* x yes. xdm no. com0 no.

* new user yes.
this user will be a member of wheel group, and can su.
i found that this user cannot sudo, by default.

* timezone europe/london

* disks (e)dit mbr. (w)hole disk will remove existing partitions.
i prefer one swap and one root partition, as i don't know how much space i (don't) need. later, i can always resize these partitions for optimum use. add multiple swap partitions on separate disks for optimum performance.
virtio is not understood.

* location cd
http for netinstall

* filesets all, except comp51.set
-comp51* [enter]

* et voila!

first boot:

* login root and read mail. read afterboot.

* swap
i noticed that my system was not using the intended swap disk. there was no swap.
fdisk & disklabel to identify swap partition. populate /etc/fstab with duid. reboot to test.

* packages
pkg_info listed nothing
pkg_add firefox
pkg_delete firefox
pkg_info lists a lot of junk now
these commands simply execute silently & promptly. i didn't get any info and/or confirmation prompts. i wish there was some way to customise the pkg defaults, presumably using conf files or env variables.

* login as normal user
startx

my toolkit

i am on a constant quest for more efficient apps. soon as i find one, i change over. efficiency means smaller code, focussed approach. leaner code is cleaner code is more efficient code.

root passwd reset

edit your (grub/syslinux/lilo?) boot command line and append
... init=/bin/sh

remount root partition in read/write mode
# mount -o remount,rw /

change password with the command
# passwd

binary to decimal tip

how to convert binary to decimal in your mind very easily

You only need to remember 2 things:

(1) the first 8 numbers:
000 0
001 1
010 2
011 3
100 4
101 5
110 6
111 7

(2) shifting to left by one digit multiplies the number by 2 (10b):
1010 = 101 x 10 = 5 x 2 = 10
10100 = 101 x 100 = 5 x 4 = 20
101000 = 101 x 1000 = 5 x 8 = 40

When you see something like 101011, you can process it this way:
101011 = 101000 + 11 = 5 * 8 + 3 = 43

1110101 = 7 * 16 + 5 = 117
and so on.

It is easier than it looks at first glance :)

source: vik korneev at http://catonmat.net/blog/low-level-bit-hacks-you-absolutely-must-know/

mtrr cleanup

having just done an upgrade, i went through the logs and found some mtrr #fail issues.

$ dmesg | grep -i mtrr
[    0.000000] MTRR default type: uncachable
[    0.000000] MTRR fixed ranges enabled:
[    0.000000] MTRR variable ranges enabled:
[    5.811624] mtrr: type mismatch for d0000000,10000000 old: write-back new: write-combining
[    5.811628] [drm] MTRR allocation failed.  Graphics performance may suffer.

$ cat /proc/mtrr
reg00: base=0x0c0000000 ( 3072MB), size= 1024MB, count=1: uncachable
reg01: base=0x000000000 (    0MB), size= 4096MB, count=1: write-back
reg02: base=0x100000000 ( 4096MB), size= 1024MB, count=1: write-back
reg03: base=0x0bf700000 ( 3063MB), size=    1MB, count=1: uncachable
reg04: base=0x0bf800000 ( 3064MB), size=    8MB, count=1: uncachable

the above doesn't seem to add up right. note that i have 4gb of memory on this machine.
debian testing 3.2.0-2-amd64 #1 SMP Fri Jun 1 17:49:08 UTC 2012 x86_64 GNU/Linux

i found many recommendations to enable_mtrr_cleanup, including this: http://forums.gentoo.org/viewtopic-t-857061-start-0-postdays-0-postorder-asc-highlight-.html. so i added the kernel bootoptions
enable_mtrr_cleanup mtrr_spare_reg_nr=1
to the grub commandline, and here is the result.

$ dmesg | grep -i mtrr
[    0.000000] MTRR default type: uncachable
[    0.000000] MTRR fixed ranges enabled:
[    0.000000] MTRR variable ranges enabled:
[    0.000000] original variable MTRRs
[    0.000000] Found optimal setting for mtrr clean up
[    0.000000] New variable MTRRs

$ cat /proc/mtrr
reg00: base=0x000000000 (    0MB), size= 2048MB, count=1: write-back
reg01: base=0x080000000 ( 2048MB), size= 1024MB, count=1: write-back
reg02: base=0x0bf700000 ( 3063MB), size=    1MB, count=1: uncachable
reg03: base=0x0bf800000 ( 3064MB), size=    8MB, count=1: uncachable
reg04: base=0x100000000 ( 4096MB), size= 1024MB, count=1: write-back
reg05: base=0x0d0000000 ( 3328MB), size=  256MB, count=1: write-combining

now this look much better! i'm keeping this kernel bootoption. have a go yourself, and post your (before/after) results below.

update: i should use mtrr_spare_reg_nr=0, as the first reg is 00.

net monitor

$ lsof -i -P

linux ip

the linux world might be moving towards the ip command suite of iproute2, leaving behind some familiar unix tools, thereby potentially deprecating ifconfig, route, arp, netstat, etc. i list the traditional commands and their equivalent ip commands, for reference.

$ ifconfig -a
$ ip addr
$ ip link

# ifconfig eth0 {up/down}
# ip link set eth0 {up/down}

# ifconfig eth0 192.168.1.2 netmask 255.255.255.0
# ip addr add 192.168.1.2/24 dev eth0

# ip addr show dev eth0
# ip addr add 192.168.1.2/24 dev eth0
# ip addr add 192.168.2.3/24 dev eth0
# ip addr del 192.168.2.3/24 dev eth0

# ifconfig eth0:1 192.168.2.3/24
# ip addr add 192.168.2.3/24 dev eth0 label eth0:1

$ route
$ netstat -rn
$ ip route

# ip route add 192.168.10.0/24 via 192.168.1.1
# ip route del 192.168.10.0/24 via 192.168.1.1

$ arp
$ ip neigh

# ifconfig -arp eth0
# ip link set dev eth0 arp off


working with ipv6 is easy. use the -6 option.

ip commands can be shortened to their initial characters, if you feel lazy :)
$ ip a
$ ip r

most viewed