Friday, December 30, 2011

combine two or multiple NIC network cards ISP using network link aggregation and load balance round robin

Hard disk are getting bigger and bigger. Network interface card (NIC) and switch are getting cheaper and cheaper. It usually means more services can squeeze into a server. Combining multiple network interface into 1 big pipe would be a big plus for services that is bandwidth hungry. e.g. file server, email server & etc

Combining 2 or more network interface have a few advantages. One of them is load balancing. Load balancing enables the network interface card to share the load among the 2 or more network interface card defined. It serves as redundancy as well. But load balancing does not makes full use of the 2 or more network interface, it is meant to share network traffic load.

To fully utilize 2 or more network interface links, link aggregation should be used. Link aggregation is a method to combine 2 or more network interface to become a bigger pipe. It is also a way to combine 2 or more interface to use the same IP address. Using link aggregation can also create redundancy, if one of the link fails, the other links will take over.

This post is using FreeBSD as a server because FreeBSD have all the ports (software packages) you will need and server administration on it is made to ease systems administration daily task.Without further ado, below are the steps to configure 2 (or more) network interfaces to work as 1 big trunk or round robin :

Scenario

rl0 = 1st interface card device name
rl1 = 2nd interface card device name
192.168.0.78 = IP address used on both 1st & 2nd network interface card

Setup Link Aggregation on 2 (or more) network interface card

kldload /boot/kernel/ng_ether.ko
kldload /boot/kernel/ng_one2many.ko
ifconfig rl0 up
ifconfig rl1 up
ngctl mkpeer rl0: one2many upper one
ngctl connect rl0: rl0:upper lower many0
ngctl connect rl1: rl0:upper lower many1
ngctl msg rl1: setpromisc 1
ngctl msg rl1: setautosrc 0
ngctl msg rl0:upper \
setconfig "{ xmitAlg=1 failAlg=2 enabledLinks=[ 1 1 ] }"
ifconfig rl0 inet 192.168.0.78

Restore NIC to original status from the previous setup

ngctl rmhook em0: lower
(remove notes / services from inteface rl0)

Setup Round Robin on 2 (or more) network interface card

kldload /boot/kernel/ng_ether.ko
kldload /boot/kernel/ng_one2many.ko
ifconfig rl0 up
ifconfig rl1 up
ngctl mkpeer rl0: one2many upper one
ngctl connect rl0: rl0:upper lower many0
ngctl connect rl1: rl0:upper lower many1
ngctl msg rl1: setpromisc 1
ngctl msg rl1: setautosrc 0
ngctl msg rl0:upper \
setconfig "{ xmitAlg=2 failAlg=2 enabledLinks=[ 1 1 ] }"
ifconfig rl0 inet 192.168.0.78

Permanently make the changes take effect, put it in rc.local

e.g.


kldload /boot/kernel/ng_ether.ko
kldload /boot/kernel/ng_one2many.ko
ifconfig rl0 up
ifconfig rl1 up
ngctl mkpeer rl0: one2many upper one
ngctl connect rl0: rl0:upper lower many0
ngctl connect rl1: rl0:upper lower many1
ngctl msg rl1: setpromisc 1
ngctl msg rl1: setautosrc 0
ngctl msg rl0:upper \
setconfig "{ xmitAlg=1 failAlg=2 enabledLinks=[ 1 1 ] }"
ifconfig rl0 inet 192.168.0.78


ngctl can be run interactively for diagnostic purpose. Below list a few example :
list ### show list of nodes ###
show em0: ### show information of a node tied interface em0 ###

Au revoir !!!

Reference:
http://scratching.psybermonkey.net/2009/06/freebsd-combine-2-or-more-nic-using.html

Wednesday, December 28, 2011

Prevent default gateway route and resolv.conf being overwritten by DHCP

Prevent default gateway route and resolv.conf being overwritten by DHCP


  • /etc/dhclient.conf
    dhclient requires a configuration file, /etc/dhclient.conf. Typically the file contains only comments, the defaults being reasonably sane. This configuration file is described by the dhclient.conf(5) manual page.
  • /sbin/dhclient
    dhclient is statically linked and resides in /sbin. The dhclient(8) manual page gives more information about dhclient.
  • /sbin/dhclient-script
    dhclient-script is the FreeBSD-specific DHCP client configuration script. It is described in dhclient-script(8), but should not need any user modification to function properly.
  • /var/db/dhclient.leases
    The DHCP client keeps a database of valid leases in this file, which is written as a log. dhclient.leases(5) gives a slightly longer description.

# man dhclient

# man dhclient-script

# man dhclient.conf

# man dhclient.leases

# cat /var/db/dhclient.leases.fxp0

# less /sbin/dhclient-script

To connect to a DHCP server in the background while other startup continues (asynchronous mode), use the "DHCP" value in /etc/rc.conf:

# vim /etc/rc.conf
ifconfig_fxp0="DHCP"

To pause startup while DHCP completes, use synchronous mode with the "SYNCDHCP" value:

# vim /etc/rc.conf
ifconfig_fxp0="SYNCDHCP"

Note: preferred method.

Create a dhclient hook script:
# vim /etc/dhclient-enter-hooks
### these two functions below will prevent dhclient from deleting and adding routes.
### Have a look at /sbin/dhclient-script. You can redefine all the functions in dhclient-enter-hooks.
add_new_routes() {
return 0
}

delete_old_routes () {
return 0
}

### this will prevent /etc/resolv.conf being overwritten as it 'overloads' the function of dhclient.
add_new_resolv_conf() {
# We don't want /etc/resolv.conf changed
# So this is an empty function
return 0
}

Request dhcp lease renew:
# dhclient interface

Reference:
http://www.freebsd.org/doc/handbook/network-dhcp.html
http://forums.freebsd.org/showthread.php?t=15283
http://forums.freebsd.org/showthread.php?p=36089
http://forums.freebsd.org/showthread.php?t=6984

Sunday, December 25, 2011

How to check hard disk transfer speed or timing

Storage are getting cheaper and cheaper. Almost a year ago, a 250 GB 2.5" portable hard disk would cost 120 bucks. But now, it only cost 100 bucks for a 500 GB hard disk. Size are getting bigger and bigger with the same monetary value but does the hard disk spins fast enough to fetch the big "gigabytes" in time? Of cause, given "enough" time, any rate of spin could fetch any size of data. Files are getting larger and larger, especially audio & video files. Fetching the files in an reasonable time is crucial.

Before we proceed to check out the command on how to check hard disk transfer speed or read timings, we will need to familiarize our self with hard disk naming convention. Here is a simple guide on how to identify hard disk naming convention in /dev (where device node are store) :

In Linux
hd = PATA / (E)IDE hard disk
sd = SATA / SCSI hard disk
a = First hard disk (primary master)
b = Second hard disk (primary slave)
c = Third Hard disk (secondary master)
d = Forth Hard disk (secondary slave)
1, 2, 3 ... = 1st partition, 2nd partition, 3rd partition and so on

Example 1 :

/dev/hdc2 = PATA or IDE (hd), third hard disk (c) & 2nd partition (2)

Example 2 :

/dev/sda4 = SATA (sd), first hard disk (a) & forth partition (4)


In FreeBSD
ad = PATA / (E)IDE hard disk
da = SATA / SCSI hard disk
0, 1, 2 ... = 1st hard disk (0), 2nd hard disk (1), 3rd hard disk (2) and so on ... (hard disk number starts at 0 as first hard disk)
s1, s2, s3 ... = slice1, slice2, slice3 and so on ... (slice number starts at 1 as first slice)
Example 1 :

/dev/ad0s4 = 1st PATA or IDE hard disk (ad0), 4th slice (s4)


Example 2 :

/dev/da3s1 = 3rd SATA or SCSI hard disk (da3), 1st slice (s1)


Do take note that even though we have brief through on hard disk partition and slice, we won't be using it as we are testing the hard disk as a whole (physical) and not partition or slice.

Next, how do we know the hard disk is spinning or transfer fast enough? Here is a way to check :

In Linux
To test the spin speed of device and read timings
hdparm -tT
e.g.

hdparm -tT /dev/sda, hdparm -tT /dev/hdb

To check the firmware info
hdparm -I
e.g.

hdparm -I /dev/sda, hdparm -I /dev/hdb
(or with "hdparm -I /dev/sda | grep SATA" for simpler output)


In FreeBSD
To test the spin speed of device and read timings
diskinfo -ct /dev/
e.g.

diskinfo -ct /dev/ad4, diskinfo -ct /dev/da0


To check the firmware info
atacontrol cap
e.g.

atacontrol cap ad4, atacontrol cap da0

Adios !!!

Reference:
http://scratching.psybermonkey.net/2009/06/how-to-check-hard-disk-transfer-speed.html

How to install add sound card driver module

FreeBSD is getting more and more attention due to its stability and (well done) documentation. Geeks and nerds are getting their hands into installing FreeBSD onto their Desktop or Laptop. Some might ask "Eh ??? Why ain't ya mentioning the servers? Is BSD.". Sound card, sound card belongs to multimedia. It's mostly meant for entertainment so it's gonna stay on the "personal" thing. e.g. Personal Computer, laptop, notebook, rig, moo or whatever you called it. :p

Back to the topic. This post will try to demo on how to install sound card driver onto FreeBSD.

Some commands or file to take note of :
kldunload <-- unload a driver module kldstat <-- list module driver loaded onto the memory /boot/defaults/loader.conf <-- default config files which list all supported drivers lspci <-- similar to pciconf but it will need to be install from the ports /usr/ports/sysutils/pciutils The common steps 1. Check the present/existence of the hardware. Sound cards fall under the "multimedia" "class". pciconf -lv e.g. hdac0@pci0:0:27:0: card=0x02731028 chip=0x284b8086 rev=0x03 hdr=0x00 vendor = 'Intel Corporation' device = '82801H &SUBSYS_81EC1043&REV_02\3&11583659&0&D8' class = multimedia subclass = HDA 2. We will need to load all the common drivers into the memory so that when it matches, we will know what is the sound card driver name. kldload sound kldload snd_driver 3. Check whether is the sound card drivers loaded in step 2, matches any. cat /dev/sndstat 4. If it any of the drivers manages to match the hardware, the sound card driver will bind on it. Note down the driver name shown in step 3. e.g. FreeBSD Audio Driver (newpcm: 32bit 2007061600/i386) Installed devices: pcm0: at cad 0 nid 1 on hdac0 kld snd_hda [MPSAFE] (1p:1v/1r:1v channels duplex default) Where "snd_hda" is the driver name. 5. To load the driver every time (or permanently) FreeBSD boots, edit the file /boot/loader.conf with such pattern: _load="YES"

e.g.
snd_hda_load="YES"

Alternative

1. Some of the sound cards driver module are not available in the kernel, it will need to use the OSS (Open Sound System driver) ports in /usr/ports/audio/oss. Install it by :
cd /usr/ports/audio/oss
make install clean

2. Insert the line :
"oss_enable="YES"
into /etc/rc.conf

3. Start the OSS service by :
/usr/local/etc/rc.d/oss start

4. To check whether the device driver for the sound card works or not, use
cat /dev/sndstat

5. Cross your finger and hope that the sound card driver module from OSS works. :p

Adios !!!

Reference:
http://scratching.psybermonkey.net/2009/07/freebsd-how-to-install-or-add-sound.html

Network subnet mask IP address calculator

One of the task installing a new FreeBSD box was dealing with IP addresses. Usually, google a web based calculator would do the trick. But today's task is a bit different.

IPv6. A whole row of hexadecimal spinning in front of my eyes, just to figure out what is the range of IP that is usable. Web based IP calculator (based on java script) are not accessible using console based internet browser (links & lynx).

Spontaneously, searching the apt repository return some very interesting stuff.

Console based IP calculatorsipcalc.

Not only it is intelligent enough to figure out the input but also it is intelligent enough to generate useful output. Surprisingly, the output is scripting friendly (using grep and sed will parse the output).

These are the steps to install it under FreeBSD & Debian based distribution (e.g. Ubuntu) :

In FreeBSD :
cd /usr/ports/net-mgmt/sipcalc/;make install clean

In Debian based distribution :
apt-get install sipcalc


Usage example :
Just run the sipcalc follow by ip address and netmask
#sipcalc 192.168.0.0 255.255.252.0
-[ipv4 : 192.168.0.0 255.255.252.0] - 0

[CIDR]
Host address            - 192.168.0.0
Host address (decimal)  - 3232235520
Host address (hex)      - C0A80000
Network address         - 192.168.0.0
Network mask            - 255.255.252.0
Network mask (bits)     - 22
Network mask (hex)      - FFFFFC00
Broadcast address       - 192.168.3.255
Cisco wildcard          - 0.0.3.255
Addresses in network    - 1024
Network range           - 192.168.0.0 - 192.168.3.255
Usable range            - 192.168.0.1 - 192.168.3.254

Or CIDR (Classless Inter-Domain Routing) notation IP address
#sipcalc 192.168.0.0/22
-[ipv4 : 192.168.0.0/22] - 0

[CIDR]
Host address            - 192.168.0.0
Host address (decimal)  - 3232235520
Host address (hex)      - C0A80000
Network address         - 192.168.0.0
Network mask            - 255.255.252.0
Network mask (bits)     - 22
Network mask (hex)      - FFFFFC00
Broadcast address       - 192.168.3.255
Cisco wildcard          - 0.0.3.255
Addresses in network    - 1024
Network range           - 192.168.0.0 - 192.168.3.255
Usable range            - 192.168.0.1 - 192.168.3.254

For a more detail output, add the parameter "-a" to sipcalc. e.g.
#sipcalc -a 192.168.0.1/28
-[ipv4 : 192.168.0.1/28] - 0

[Classfull]
Host address            - 192.168.0.1
Host address (decimal)  - 3232235521
Host address (hex)      - C0A80001
Network address         - 192.168.0.0
Network class           - C
Network mask            - 255.255.255.0
Network mask (hex)      - FFFFFF00
Broadcast address       - 192.168.0.255

[CIDR]
Host address            - 192.168.0.1
Host address (decimal)  - 3232235521
Host address (hex)      - C0A80001
Network address         - 192.168.0.0
Network mask            - 255.255.255.240
Network mask (bits)     - 28
Network mask (hex)      - FFFFFFF0
Broadcast address       - 192.168.0.15
Cisco wildcard          - 0.0.0.15
Addresses in network    - 16
Network range           - 192.168.0.0 - 192.168.0.15
Usable range            - 192.168.0.1 - 192.168.0.14

[Classfull bitmaps]
Network address         - 11000000.10101000.00000000.00000000
Network mask            - 11111111.11111111.11111111.00000000

[CIDR bitmaps]
Host address            - 11000000.10101000.00000000.00000001
Network address         - 11000000.10101000.00000000.00000000
Network mask            - 11111111.11111111.11111111.11110000
Broadcast address       - 11000000.10101000.00000000.00001111
Cisco wildcard          - 00000000.00000000.00000000.00001111
Network range           - 11000000.10101000.00000000.00000000 -
11000000.10101000.00000000.00001111
Usable range            - 11000000.10101000.00000000.00000001 -
11000000.10101000.00000000.00001110

[Networks]
Network                 - 192.168.0.0     - 192.168.0.15 (current)
Network                 - 192.168.0.16    - 192.168.0.31
Network                 - 192.168.0.32    - 192.168.0.47
Network                 - 192.168.0.48    - 192.168.0.63
Network                 - 192.168.0.64    - 192.168.0.79
Network                 - 192.168.0.80    - 192.168.0.95
Network                 - 192.168.0.96    - 192.168.0.111
Network                 - 192.168.0.112   - 192.168.0.127
Network                 - 192.168.0.128   - 192.168.0.143
Network                 - 192.168.0.144   - 192.168.0.159
Network                 - 192.168.0.160   - 192.168.0.175
Network                 - 192.168.0.176   - 192.168.0.191
Network                 - 192.168.0.192   - 192.168.0.207
Network                 - 192.168.0.208   - 192.168.0.223
Network                 - 192.168.0.224   - 192.168.0.239
Network                 - 192.168.0.240   - 192.168.0.255


As for IPv6, below are the sample usage :
#sipcalc 1111:2222:3333:4444:5555:6666:7777:8888/112
-[ipv6 : 1111:2222:3333:4444:5555:6666:7777:8888/112] - 0

[IPV6 INFO]
Expanded Address        - 1111:2222:3333:4444:5555:6666:7777:8888
Compressed address      - 1111:2222:3333:4444:5555:6666:7777:8888
Subnet prefix (masked)  - 1111:2222:3333:4444:5555:6666:7777:0/112
Address ID (masked)     - 0:0:0:0:0:0:0:8888/112
Prefix address          - ffff:ffff:ffff:ffff:ffff:ffff:ffff:0
Prefix length           - 112
Address type            - Unassigned
Network range           - 1111:2222:3333:4444:5555:6666:7777:0000 -
1111:2222:3333:4444:5555:6666:7777:ffff

As like IPv4, adding the parameter "-a" would yield a more verbose output.

Namaste !!!

Reference:
http://scratching.psybermonkey.net/2009/09/network-ip-address-calculator-for.html

Download ports simultaneously with multiple connections

By default, ports uses 1 connection to download and thus unbearably slow when a server rate limit the connection. Alternatively, we can tell ports to use external utility in order to open multiple connections and download simultaneously.

Firstly, install the utility "axel" by :

# cd /usr/ports/ftp/axel ;make install clean

Next, tell ports to use the external utility to perform downloading task. Insert the following lines into /etc/make.conf :

# vim /etc/make.conf
FETCH_CMD=axel
FETCH_BEFORE_ARGS= -n 4 -a
DISABLE_SIZE=yes

The parameter "-n 4" is to tell "axel" break the file into 4 parts and download simultaneously; "-a" is to show an alternate download progress bar. Do take note that, DO NOT set the simultaneously connection ("-n") too many as it takes time for the server to tear down the connection after the downloads terminate. This could block other people from downloading it. After all, these software are the "Best" & "Freely" available to everyone.

Ciao !!!

Reference:
http://scratching.psybermonkey.net/2009/09/freebsd-download-ports-simultaneously.html

Friday, December 23, 2011

該如何學好 "寫程式" ??

會寫這篇是因為上禮拜,有個資深的同事問我個問題,如何把底下的 programmer 素質拉上來? 跟他講這問題害我那天拖到晚上十點才回家吃晚飯 @_@,不過我想這也是現在台灣軟體業普遍碰到的人才問題,就順手寫了這篇。這篇是打算要貼在公司的 BLOG 裡給同事看的,這裡先貼一下,到時整理好再搬過去...。


-----------------------------------------------------------------------------------------
其實在這之前,一直有人問我這類問題,我的回答總是一樣: "要從基本功做起。"   只不過大部份人都會皺皺眉頭,想說 "為什麼沒有速成一點的方法? "
先撇開軟體開發一定要有其它領域的 domain know-how 之類的東西,我就針對到底有沒有辦法把 CODE 寫好這件事來討論。如果真的存在速成的方法,那滿街就都是高手強者了。技術及工具的進步,是簡化你操作工具及開發過程的細節,但是其中累積的知識及理論,只會越來越多
現在要寫程式所必須俱備的技術門檻越來越低,但是要能專精所必要的知識及經驗則是越來越高。我常在想,我是一路學習摸索上來的,大學四年唸了電機學到了硬體的底子,大三大四本來想修資工當輔系,後來直接去考資工所... 這三四年下來也有點資訊的底子,這些知識讓我到現在都還不用辛苦的去 "追" 技術,我只要看看 overview,大概就能掌握這些技術能解決什麼問題,不能解決什麼問題等等。真正需要用的時後,文件翻一翻就能找到我要的段落,就能夠上手了。
不過現在的年輕人就沒那麼好運了,要從 CPU 是怎麼設計,怎麼執行指令,到瀏覽器為什麼點下網址就能看到網頁,這一連串的細節,大概現在的大學畢業生都回答不出來吧? 現在隨便買本書翻一翻,就能寫出一個漂亮的網站,誰還願意去唸那些基本功? 也因為這樣現在的人都少了那份 "內功",只剩漂亮的招式,出招很勵害,不過打沒兩下就後繼無力,或是對手出了沒看過的招,就不知怎麼接下去了。
如果你真的有心把底子練好,我是有幾個建議的方向,雖然看起來沒什麼用,但是看熟了你一定會發現,你寫什麼程式都逃不了這幾個基礎知識。
  1. 最基本的: 計算機概論 & 資料結構這些有助於你用正確的邏輯寫程式。要成為一個合格的 programmer 一定要有這樣的能力。
  2. 進階一點的系統層面,作業系統 & 系統程式
    這些有助於你瞭解系統層面如何運作,如果你開發的系統需要些基礎建設,像是元件等等,這些知識很有用。成為 software engineer 就應該要有這些基礎。
  3. 再來就專精一點了,我推薦 OOP 理論 / Design Patterns、或是軟體工程的方法論 ( XP, TDD ... 等 )
    這個層次的知識能幫助你設計正確的架構,或是用正確的方式開發軟體,是成為 ARCHITECT 的必要技能。
每一項都代表一個階段。上禮拜跟同事討論的,其實只有討論到 (1) 的部份。如果工程師都 "" 寫程式,但是用的邏輯看起來都 "怪怪的",那就是要加強 (1) 的部份了。我簡單的舉個例子,資料結構在學什麼? 跟實際寫程式能有什麼關聯?
想到資料結構,不外忽一堆排序 (SORT) 的演算法,或是各種 TREE / LIST 等怎麼 "放" 資料,及怎麼 "找" 資料的問題,如 LINKED LIST,HASH TABLE,BINARY TREE,HEAP,STACK 等等。再來就是什麼問題可以用什麼資料結構來處理? 像是走迷宮要靠 STACK,各種資料結構的特性為何? 它們的時間複雜度 (Time Complexity) 為何? 什麼時後該用那一種?
這些是很基礎的問題,不過你如果不是科班的,只是翻翻書就會寫程式的,那這些問題應該都回答不出來吧? 針對這部份,我強烈建議要學的人一定要先搞懂這些 "邏輯"。我不稱為理論,是因為他們還太淺,只是個作法而以。搞懂這些邏輯,你至少要有能力把程式寫出來。
因此第一課很簡單,挑幾種 SORT 的方式,比如 Bubble Sort, Quick Sort 等等,不用多,兩三個就好,步驟搞清楚了,還能用你熟悉的程式寫出來 ( 如: C# / JavaScript,當然你不能作弊用現成的 SORT 函式庫 ),你就過關了。
再來就是搞懂各種資料結構,我舉幾個 .NET 內建的,卻又常讓人搞混的幾個 Collection。List / LinkedList 用的方式都一樣,那麼兩者到底有什麼不一樣? 只塞一百筆,找出一筆要 10 ms 的話,塞一萬筆找出一筆要花多少時間? 是 100 倍嗎? 還是 10 倍? 還是都一樣?? 是 Microsoft 工程師太無聊,故意寫來讓你傷惱筋的嗎?
如果這部份你也搞懂了,接下來就是應用了。就拿導航系統來說就好,地圖要用什麼方式存才好? 使用者選定起點及終點,你該怎麼幫它找出最佳的路逕? 不管畫面等等問題,你有辦法寫出程式找到答案嗎? 這就是典型的資料結構的應用。你沒學好資料結構的話,看再多 C# / ASP.NET 的書,一點用都沒有啦,碰到這類問題,管你用 VB / C++ / C# 還是  Java, 只能坐在螢幕前發呆而以。

總結一下,你符合我講的 (1) 基本要求嗎? 很簡單,這些問題或程式你都寫的出來就符合了:
  • 丟一付洗過的撲客排給你 (不要多,黑桃1 ~ 13就好),你知道怎麼用 Bubble Sort / Quick Sort 的步驟把它排好嗎? 丟一個陣列,裡面隨便打幾個數字,你能寫程式把它由小到大排好印出來嗎?
  • 假設記憶體夠大的話,你有辦法把一百萬筆通訊錄資料讀到記憶體內 (用什麼物件都隨你),然後還能用很快的速度找到你要的資料嗎? 不同的搜尋方式,你知道該用什麼樣的方式找才有效率嗎?
  • 以台灣高速公路為題 (中山高、北二高、國道二號),你有辦法寫程式,讓使用者指定起點跟終點的交流道,然後替它找出建議的路線嗎? (把延路經過的交流到跟收費站列出來就好)
看起來就像是作業,沒錯。不過它是很實際的基本功夫,如果寫不出來,那就真的該好好唸個書了。其實這些問題,都跟熱門的技術 (如 DB / WEB / RIA 等等) 無關,就很單純的看你的邏輯能力而以。這個主題我會繼續寫下去,大概一兩個禮拜一篇吧。我的目的是希望大家底子打好再來學這些熱門技術,這樣你才有辦法更進一階,否則就只能隨著技術規格推陳出新,不斷的在追新技術而以。上面那三個問題,有興趣的話歡迎找我聊聊。在這留話或是 mail 給我都可以。

Reference:
http://columns.chicken-house.net/post/2008/09/27/GoodProgrammer1.aspx

Watch log file in real time

Watch log file in real time

# tail -F /var/log/messages

Thursday, December 22, 2011

check user's information and profile

check user's information and profile

# finger the_user_name

or

# grep 'the_user_name' /etc/passwd

mkdir automatically create missing directory

mkdir automatically create missing directory

# mkdir -p /a/path/does/not/exist/yet

# ls -d /a/path/does/not/exist/yet

How to make grep and less command highlight the keyword

How to make grep and less command highlight the keyword

# dmesg | grep --color -i memory
real memory = 2147483648 (2048 MB)
avail memory = 2090934272 (1994 MB)

or
# dmesg | less +/memory

How to clear or reset screen font

After accidentally cat a tar or zip file or left out some pipe and echo the whole file of random string, on to the screen; it can mess up the font of the screen and result in missing of characters or blinking characters or ever the cursor is missing from the screen. No worries, just a 5 character will make your screen back to original display :

Just type :
# reset

and press enter. It might hang there for a few seconds then the usual prompt would come back. In case it does not, just press once enter key.

Ciao !!!

Reference:
http://scratching.psybermonkey.net/2009/05/how-to-clear-or-reset-screen-font.html

check CPU processor information on FreeBSD

check CPU processor information on FreeBSD

# sysctl -a | egrep -i 'hw.machine|hw.model|hw.ncpu'
hw.machine: i386
hw.model: Intel(R) Xeon(TM) CPU 2.80GHz
hw.ncpu: 4
hw.machine_arch: i386

# dmesg | grep -i cpu
CPU: Intel(R) Xeon(TM) CPU 2.80GHz (2793.02-MHz 686-class CPU)
FreeBSD/SMP: Multiprocessor System Detected: 4 CPUs

# grep -i cpu /var/run/dmesg.boot
CPU: Intel(R) Xeon(TM) CPU 2.80GHz (2793.02-MHz 686-class CPU)
FreeBSD/SMP: Multiprocessor System Detected: 4 CPUs

FreeBSD - CPU code in dmesg meanings

Another Method:
# cd /usr/ports/sysutils/sysinfo ; make install clean distclean ; rehash
===> The following configuration options are available for sysinfo-1.0.1:
DMIDECODE=on "Include information from the dmidecode tool"
PORTAUDIT=on "Include information from the portaudit tool"
===> Use 'make config' to modify these settings

# dmidecode -t processor | less

http://gala4th.blogspot.com/2011/12/how-to-get-detail-hardware-information.html

Objective :

What are those CPU features flags means in "dmesg", e.g.

# less -S /var/run/dmesg.boot

Features=0xfebfbff<FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,
PAT,PSE36,CLFLUSH,DTS,ACPI,MMX,FXSR,SSE,SSE2,SS>
Features2=0x82982203<SSE3,PCLMULQDQ,SSSE3,CX16,SSE4.1,SSE4.2,POPCNT,
AESNI,<b31>>
AMD Features=0x28100000<NX,RDTSCP,LM>
AMD Features2=0x1<LAHF>

Flags meanings :
FLAG
-------------------
MEANINGS
-----------------------------------------------------------------------------------------------------------------------------------------------
3DNOW
A multimedia extension created by AMD for its processors, based on / almost equivalent to Intel’s MMX extensions
3DNOWEXT
3DNOW Extended. Also known as AMD’s 3DNow!Enhanced 3DNow!Extensions
APIC
Advanced Programmable Interrupt Controller
CLFSH/CLFlush
Cache Line Flush
CMOV
Conditional Move/Compare Instruction
CMP_Legacy
Register showing the CPU is not Hyper-Threading capable
Constant_TSC
on Intel P-4s, the TSC runs with constant frequency independent of cpu frequency when EST is used
CR8Legacy
-unknown-
CX8
CMPXCHG8B Instruction. (Compare and exchange 8 bytes. Also known as F00F, which is an abbreviation of the hexadecimal encoding of an instruction that exhibits a design flaw in the majority of older Intel Pentium CPU).
CX16
CMPXCHG16B Instruction. (CMPXCHG16B allows for atomic operations on 128-bit double quadword (or oword) data types. This is useful for high resolution counters that could be updated by multiple processors (or cores). Without CMPXCHG16B the only way to perform such an operation is by using a critical section.)
DE
Debugging Extensions
DS
Debug Store
DS_CPL
CPL qualified Debug Store (whatever CPL might mean in this context)
DTS
Could mean Debug Trace Store or Digital Thermal Sensor, depending on source
EIST/EST
Enhanced Intel SpeedsTep
FXSR
FXSAVE/FXRSTOR. (The FXSAVE instruction writes the current state of the x87 FPU, MMX technology, Streaming SIMD Extensions, and Streaming SIMD Extensions 2 data, control, and status registers to the destination operand. The destination is a 512-byte memory location. FXRSTOR will restore the state saves).
FXSR_OPT
-unknown-
HT
Hyper-Transport. Note that the same abbreviation might is also used to indicate Hyper Threading (see below)
HTT/HT
Hyper-Threading. An Intel technology that allows quasi-parallel execution of different instructions on a single core. The single core is seen by applications as if it were two (or potentially more) cores. However, two true CPU cores are almost always faster than a single core with HyperThreading. This flag indicates support in the CPU when checking the flags in /proc/cpuinfo on Linux systems. For more info how you can detect active HyperThreading, see the first comment in my blog post about this page at [2]
HVM
Hardware support for virtual machines (Xen abbreviation for AMD SVM / Intel VMX)
LAHF_LM
Load Flags into AH Register, Long Mode.
LM
Long Mode. (64bit Extensions, AMD’s AMD64 or Intel’s EM64T).
MCA
Machine Check Architecture
MCE
Machine Check Exception
MMX
It is rumoured to stand for MultiMedia eXtension or Multiple Math or Matrix Math eXtension, but officially it is a meaningless acronym trademarked by Intel
MMXEXT
MMX Extensions – an enhanced set of instructions compared to MMX
MON/MONITOR
CPU Monitor
MSR
RDMSR and WRMSR Support
MTRR
Memory Type Range Register
NX
No eXecute, a flag that can be set on memory pages to disable execution of code in these pages
PAE
Physical Address Extensions. PAE is the added ability of the IA32 processor to address more than 4 GB of physical memory using Intel’s 36bit page addresses instead of the standard 32bit page addresses to access a total of 64GB of RAM. Also supported by many AMD chips
PAT
Page Attribute Table
PBE
Pending Break Encoding
PGE
PTE Global Bit
PNI
Prescott New Instruction. This was the codename for SSE3 before it was released on the Intel Prescott processor (which was later added to the Pentium 4 family name).
PSE
Page Size Extensions. (See PSE36)
PSE36
Page Size Extensions 36. IA-32 supports two methods to access memory above 4 GB (32 bits), PSE and PAE. PSE is the older and far less used version. For more information, take a look at [1].
SEP
SYSENTER and SYSEXIT
SS
Self-Snoop
SSE
Streaming SIMD Extensions. Developed by Intel for its Pentium III but also implemented by AMD processors from Athlon XP onwards
SSE2
Streaming SIMD Extensions 2. (An additional 144 SIMDs.) Introduced by Intel Pentium 4, on AMD since Athlon 64
SSE3
Streaming SIMD Extensions 3. (An additional 13 instructions) introduced with “Prescott” revision Intel Pentium 4 processors. AMD introduced SSE3 with the Athlon 64 “Venice” revision
SSSE3
Supplemental Streaming SIMD Extension 3. (SSSE3 contains 16 new discrete instructions over SSE3.) Introduced on Intel Core 2 Duo processors. No AMD chip supports SSSE3 yet.
SSE4
Streaming SIMD Extentions 4. Future Intel SSE revision adding 50 new instructions which will debut on Intel’s upcoming “Nehalem” processor in 2008. Also known as “Nehalem New Instructions (NNI)”
SVM
Secure Virtual Machine. (AMD’s virtualization extensions to the 64-bit x86 architecture, equivalent to Intel’s VMX, both also known as HVM in the Xen hypervisor.)
TM
Thermal Monitor
TM2
Thermal Monitor 2
TSC
Time Stamp Counter
VME
Virtual-8086 Mode Enhancement
VMX
Intel’s equivalent to AMD’s SVM
XTPR
TPR register chipset update control messenger. Part of the APIC code

This table of flag meanings is taken shamelessly from Sven's blog

Extra note, if the processor list the capability of "LM", this means it support 64 bit processing. Which also means, it can take advantage of 64 bit processing by installing FreeBSD AMD64 achitecture.

Arrivederci !!!

Reference:
http://scratching.psybermonkey.net/2011/04/freebsd-cpu-code-in-dmesg-meanings.html#more

How to get detail hardware information on FreeBSD

How to get detail hardware information on FreeBSD

# cd /usr/ports/sysutils/sysinfo ; make install clean distclean ; rehash
===> The following configuration options are available for sysinfo-1.0.1:
DMIDECODE=on "Include information from the dmidecode tool"
PORTAUDIT=on "Include information from the portaudit tool"
===> Use 'make config' to modify these settings

# sysinfo cpu
# sysinfo mem
# sysinfo bios
# sysinfo network
# sysinfo os
# sysinfo packages
# sysinfo services
# sysinfo storage
# sysinfo system
# sysinfo user
# sysinfo misc

# sysinfo -c network | less

Note: man sysinfo

Dumping a computer's DMI (some say SMBIOS) table contents in a human-readable format:
# dmidecode -t processor
# dmidecode -t memory
# dmidecode -t bios
# dmidecode -t system
# dmidecode -t baseboard

Note: man dmidecode
Note: man pciconf

Update portaudit database:
# portaudit -F

Run portaudit to check all installed packages with known security vulnerabilities:
# portaudit -av

FreeBSD - How to test newsyslog for syntax error

Platform :
FreeBSD

Purpose :
Syntax checking after creating a new logrotate in "newsyslog"

Command :
newsyslog


Installation :
The "newsyslog" log rotation program comes with the base installation.

Useful switches :
-n // DO NOT actually do the logrotate. Instead, show what will be perform
-vv // verbose on what newsyslog is doing

Example :


FreeBSD# newsyslog -nvv
/var/log/all.log <7j>: does not exist, skipped.
/var/log/amd.log <7j>: does not exist, skipped.
/var/log/auth.log <7j>: size (Kb): 6 [100] --> skipping
/var/log/console.log <5j>: does not exist, skipped.
/var/log/cron <3j>: size (Kb): 6 [100] --> skipping
/var/log/daily.log <7j>: does not exist, skipped.
/var/log/debug.log <7j>: size (Kb): 2 [100] --> skipping
/var/log/kerberos.log <7j>: does not exist, skipped.
/var/log/lpd-errs <7j>: size (Kb): 2 [100] --> skipping
/var/log/maillog <7j>: --> will trim at Sun Jan 2 00:00:00 2011
/var/log/messages <5j>: size (Kb): 92 [100] --> skipping
/var/log/monthly.log <12j>: does not exist, skipped.
/var/log/pflog <3j>: does not exist, skipped.
/var/log/ppp.log <3j>: size (Kb): 2 [100] --> skipping
/var/log/security <10j>: size (Kb): 2 [100] --> skipping
/var/log/sendmail.st <10>: age (hr): 1 [168] --> skipping
/var/log/weekly.log <5j>: does not exist, skipped.
/var/log/wtmp <3>: --> will trim at Tue Feb 1 05:00:00 2011
/var/log/xferlog <7j>: size (Kb): 2 [100] --> skipping

After adding or changing entries in newsyslog.conf, is good to run this test to check out any errors in the configuration file.

Das Vidanya !!!

Reference:
http://scratching.psybermonkey.net/2011/01/freebsd-how-to-test-newsyslog-for.html

show the full output line of ps process status command

show the full output line of ps process status command

# ps -auxww

The -w parameter tells ps to use 132 columns to display information. If the -w option is specified more than once, ps will use as many columns as necessary without regard for your window size, which will display the full output.

Another similar problem with less command:

# less -S /usr/ports/UPDATING

The -S options tells less command to not chop the long lines after the terminal width.

Sunday, December 18, 2011

Install X window, enlightenment window manager on FreeBSD

安裝 X-Window

Xorg 是預設的 X11 系統。 Xorg 是由 X.Org 機構發布的,根據 X11R6.7 所製作的 X11 伺服器。 X11R6.7 是基於 XFree86 4.4RC2 的程式碼和 X11R6.6。

1. 安裝 x11/xorg
# cd /usr/ports/x11/xorg ; make install clean
或是以 package 的方式安裝:
# pkg_add -r xorg

Note: 與 XFree86 的差異只有幾個設定方式改變。像是設定程式 XFree86(1) 變成 Xorg(1)、 xf86cfg(1) 變成 xorgcfg(1)、 xf86config(1) 變成 xorgconfig(1)。 設定檔 XF86Config 變成 xorg.conf。 紀錄檔 XFree86.0.log 變成 Xorg.0.log。

As of version 7.3, Xorg can often work without any configuration file by simply typing at prompt:

% startx
Starting with version 7.4, Xorg can use HAL to autodetect keyboards and mice. The sysutils/hal and devel/dbus ports are installed as dependencies of x11/xorg, but must be enabled by the following entries in the /etc/rc.conf file:

hald_enable="YES"
dbus_enable="YES"

Note: HAL (Hardware Abstraction Layer) is a daemon that allows desktop applications to readily access hardware information, to locate and use such hardware regardless of bus or device type. In this way a desktop GUI can present all resources to its user in a seamless and uniform manner.

Note: DBUS In computing, D-Bus (Desktop Bus) is a simple inter-process communication (IPC) open-source system for software applications to communicate with one another. Heavily influenced by KDE2–3's DCOP system, D-Bus has replaced DCOP in the KDE 4 release. An implementation of D-Bus supports most POSIX operating systems, and a port for Windows exists. It is used by Qt 4 and GNOME. In GNOME it has gradually replaced most parts of the earlier Bonobo mechanism.


Note: someone says if you enable hald_enable, you will get "Freebsd 8 kernel fault and automatic reboot"

Another possible solution might be remove the virtual CD-ROM from your virtual machine.

Feb 28 05:03:02 freebsd kernel: acd0: WARNING - unknown CMD (0x4a) read data overrun 8>0
Feb 28 05:05:08 freebsd syslogd: kernel boot file is /boot/kernel/kernel
Feb 28 05:05:08 freebsd kernel: acd0: WARNING - unknown CMD (0x4a) taskqueue timeout - completing request directly
Feb 28 05:05:08 freebsd kernel:
Feb 28 05:05:08 freebsd kernel:
Feb 28 05:05:08 freebsd kernel: Fatal trap 12: page fault while in kernel mode
Feb 28 05:05:08 freebsd kernel: cpuid = 0; apic id = 00
Feb 28 05:05:08 freebsd kernel: fault virtual address = 0x1a0
Feb 28 05:05:08 freebsd kernel: fault code = supervisor read, page not present
Feb 28 05:05:08 freebsd kernel: instruction pointer = 0x20:0xc060cdcf
Feb 28 05:05:08 freebsd kernel: stack pointer = 0x28:0xe537ab58
Feb 28 05:05:08 freebsd kernel: frame pointer = 0x28:0xe537ab70
Feb 28 05:05:08 freebsd kernel: code segment = base 0x0, limit 0xfffff, type 0x1b
Feb 28 05:05:08 freebsd kernel: = DPL 0, pres 1, def32 1, gran 1
Feb 28 05:05:08 freebsd kernel: processor eflags = interrupt enabled, resume, IOPL = 0
Feb 28 05:05:08 freebsd kernel: current process = 12 (swi6: task queue)
Feb 28 05:05:08 freebsd kernel: trap number = 12
Feb 28 05:05:08 freebsd kernel: panic: page fault
Feb 28 05:05:08 freebsd kernel: cpuid = 0
Feb 28 05:05:08 freebsd kernel: Uptime: 6h10m59s
Feb 28 05:05:08 freebsd kernel: Cannot dump. Device not defined or unavailable.
Feb 28 05:05:08 freebsd kernel: Automatic reboot in 15 seconds - press a key on the console to abort
Feb 28 05:05:08 freebsd kernel: Rebooting...

http://forums.freebsd.org/showthread.php?t=11782

> can you explain to me what is it : dbus_enable="YES" and
> hald_enable="YES" in file /etc/rc.conf? What is their function?

In explicit correspondence with their names, these settings enable to run dbus and hald. dbus is a D-Bus messaging daemon, hald is a HAL daemon. These programs are using in X environment to automount DVD's, flashcards and another, even mices and keyboards.

Since Xorg 1.7.5 running dbus and hald is required in some hardware equipment.
===
Those enable the startup of the DBUS and HAL services via their control files in /usr/local/etc/rc.d. Those services are often needed for X, and programs related to X (KDE, Gnome, Xfce, and many programs that have "Use DBUS for something" compile time options set).

Sadly, those don't provide "man dbus" or "man hal" in a very impolite manner. I'm sure you can find more documentation on the web, but it may already be outdated.

If you ask what HAL and DBUS actually *ARE*, I'm not sure what to answer - to me, they are both useless. :-)

Removing the entries in /etc/rc.conf may cause problems for some of your programs, or may even make your X stop working properly.
===
hald is an interface between devices and programs. It listens for attachment/detachment of input devices (keyboard, mouse, etc.) and informs listening programs of those events.

dbus is an interprocess communication system that allows programs to connect to and exchange messages with other programs. It allows the kernel, for example, to interact with your desktop. Rather than you interacting directly with the kernel, dbus acts as a middle man, controlling the communications between the two and only allowing you to perform actions which are considered "safe" or "reasonable".

The two work together, for example, to let your desktop know when you connect a new mouse or type on your keyboard.
===
http://en.wikipedia.org/wiki/HAL_%28software%29
http://en.wikipedia.org/wiki/D-Bus


These services should be started (either manually or by rebooting) before further Xorg configuration is attempted.

The automatic configuration may fail to work with some hardware, or may not set things up quite as desired. In these cases, manual configuration will be necessary.

Note: Desktop environments like GNOME, KDE or Xfce have tools allowing the user to easily set the screen parameters such as the resolution. So if the default configuration is not acceptable and you planned to install a desktop environment then just continue with the installation of the desktop environment and use the appropriate screen settings tool.

Starting with Xorg 7.4 and above, this test produces a black screen which may make it difficult to diagnose whether X11 is working properly. The older behavior is still available by using the retro option:

# Xorg -config xorg.conf.new -retro
If a black and grey grid and an X mouse cursor appear, the configuration was successful. To exit the test, switch to the virtual console used to start it by pressing Ctrl+Alt+Fn (F1 for the first virtual console) and press Ctrl+C.

http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/x-config.html

2. 產生設定檔 & 設定
先用 Xorg -configure 產生 xorg.conf.new 並且搬移到 /etc/X11/xorg.conf,或是用選單模式的 xorgcfg -textmode 產生 xorg.conf,然後用 Xorg -xf86config xorg.conf 來測試這個檔案能不能正常的運作。
# Xorg -configure

把檔案移到 /etc/X11
# mv xorg.conf.new /etc/X11/xorg.conf

由於筆者通常使用三鍵滑鼠,所以會設定一下, 接著編輯 /etc/X11/xorg.conf, 在 Section "InputDevice" 區段, 加入 Option "ZAxisMapping" "4 5"。並且用 /stand/sysinstall 來設定好 moused, 以便讓 /dev/sysmouse 有作用。 Configure → Mouse → Enable。

Section "InputDevice"
Identifier "Mouse0"
Driver "mouse"
Option "Protocol" "MouseSystems"
Option "Device" "/dev/sysmouse"
Option "ZAxisMapping" "4 5"
EndSection

通常都需要指定 HorizSync 和 VertSync 才能讓解析度設定的高一點, 像是使用 1024x768 的解析度, 在 Section "Monitor" 區段, 加入 HorizSync 31.5 - 57.0 與 VertRefresh 50.0 - 100.0。

Section "Monitor"
Identifier "Monitor0"
VendorName "Monitor Vendor"
ModelName "Monitor Model"
HorizSync 31.5 - 57.0
VertRefresh 50.0 - 100.0
EndSection

筆者通常都用 24bpp 和 1024x768 的螢幕, 在 Section "Screen" 區段, 加入 DefaultDepth 24,並在 SubSection "Display" 中, Depth 24 的地方加上 Modes "1024x768"。

Section "Screen"
Identifier "Screen0"
Device "Card0"
Monitor "Monitor0"
DefaultDepth 24
SubSection "Display"
Depth 24
Modes "1024x768"
EndSubSection
EndSection

Note: To test the server, run
# X -config /etc/X11/xorg.conf

3. 啟動 X-Window
# startx
如果看到全黑的畫面(正常應該要看到"格子背景" 和 "滑鼠游標 X") 那就把 HorizSync 和 VertRefresh 的值調小一點。

Note: 如果當掉的話,可以按 Ctrl + Alt + Backspace(<-) 來跳回 console

4. Install enlightenment
# cd /usr/ports/x11-wm/enlightenment ; make install

# echo "exec /usr/local/bin/ck-launch-session /usr/local/bin/enlightenment_start" > ~/.xinitrc

4. 安裝 Gnome # cd /usr/ports/x11/gnome2 ; make install clean

加了這句至 .xinitrc 以後,輸入 startx 即會執行 gnome
# echo "/usr/local/bin/gnome-session" > ~/.xinitrc

5. 安裝中文輸入法
個人建議 gcin 這個輸入PORT...如果你要用嘸蝦米輸入法安裝時請加 make WITH_BOSHIAMY=YES install clean這個指令去安裝...
# cd /usr/ports/chinese/gcin ; make WITH_BOSHIAMY=YES install clean

必須先執行一次 gcin,來建立相關目錄和檔案 (例如 ~/.gcin )
# /usr/X11R6/bin/gcin
進到 X-Window 以後,執行 gcin-setup 設定
# /usr/X11R6/bin/gcin-setup

想要在啟動 gnome 時,同時啟動 gcin,在 ~/.xinitrc 檔中加入
export XMODIFIERS=@im=gcin
gcin&

6. Install iBus

# cd /usr/ports/textproc/ibus ; make install

If you are using bash, please add following lines to your $HOME/.bashrc:

export XIM=ibus
export GTK_IM_MODULE=ibus
export QT_IM_MODULE=xim
export XMODIFIERS=@im=ibus
export XIM_PROGRAM="ibus-daemon"
export XIM_ARGS="--daemonize --xim"

If you are using tcsh, please add following lines to your $HOME/.cshrc:

setenv XIM ibus
setenv GTK_IM_MODULE ibus
setenv QT_IM_MODULE xim
setenv XMODIFIERS @im=ibus
setenv XIM_PROGRAM ibus-daemon
setenv XIM_ARGS "--daemonize --xim"

If you are using KDE4, you may create a shell script in $HOME/.kde4/env,
and add following lines:

#!/bin/sh
export XIM=ibus
export GTK_IM_MODULE=ibus
export QT_IM_MODULE=xim
export XMODIFIERS=@im=ibus
export XIM_PROGRAM="ibus-daemon"
export XIM_ARGS="--daemonize --xim"

Following input methods/engines are available in ports:

chinese/ibus-chewing Chewing engine for IBus
chinese/ibus-pinyin The PinYin input method
japanese/ibus-anthy Anthy engine for IBus
japanese/ibus-skk SKK engine for IBus
textproc/ibus-kmfl KMFL IMEngine for IBus framework
textproc/ibus-m17n The m17n IMEngine for IBus framework
textproc/ibus-table Table based IM framework for IBus

and QT4 input method module, textproc/ibus-qt.

If ibus cannot start or the panel does not appear, please ensure
that you are using up-to-date python.
There's a bug in python 2.5, which may prevent the panel from appearing.
-------------------------------------------------------------------
To display this message again, type /usr/sbin/pkg_info -D ibus-1.3.9_1

7. install 嘸蝦米 in iBus
# /usr/ports/textproc/ibus-table ; make install

# ls /usr/local/share/ibus-table
# tar zxvf boshiamy-ibus.tar.gz
# cd boshiamy-ibus
# cp *.png /usr/local/share/ibus-table/icons/
# cp *.db /usr/local/share/ibus-table/tables/

# ibus-setup
# ibus-daemon -rdx

Note: I use rxvt-unicode terminal to make ibus to work.

8. Install rxvt-unicode
# cd /usr/ports/x11/rxvt-unicode ; make install

# vi ~/.cshrc
setenv TERM rxvt-unicode

9. 安裝 chinese/fireflyttf 螢火飛中文字型。蠻漂亮的中文字型
# cd /usr/ports/chinese/fireflyttf ; make install clean

# pkg_add -r zh-fireflyttf

接著編輯 /etc/X11/xorg.conf, 在 Section "Module" 區段, 加入 Load "xtt"。

Section "Module"
:
Load "xtt"
EndSection

在 Section "Files" 區段, 加入 FontPath "/usr/X11R6/lib/X11/fonts/TrueType/" 與 FontPath "/usr/X11R6/lib/X11/fonts/local/"。

Section "Files"
:
FontPath "/usr/X11R6/lib/X11/fonts/TrueType/"
FontPath "/usr/X11R6/lib/X11/fonts/local/"
EndSection

請參考 輸出字型 http://chinsan2.twbbs.org/zh-tut/fonts.html 一節。XD

10. install google chrome
# /usr/ports/www/chromium ; make install
# /usr/local/share/chromium/chrome

11. install audio sound driver

# kldload snd_driver

# cat /dev/sndstat

# cat /boot/defaults/loader.conf | grep -i snd

# vim /boot/loader.conf
snd_driver_load="YES"

http://www.freebsd.org/doc/handbook/book.html#SOUND-SETUP

12. install mp3 player
# cd /usr/ports/multimedia/xmms ; make install

13. start x window
# startx
Add new application > /usr/local/bin/urxvt
Startup application


Reference:
http://gala4th.blogspot.com/2011/10/install-x-window-enlightenment-window.html

make install ncurses menu dialog pop up

make install ncurses menu dialog pop up

There are many times when a port installs another port and that port will have a screen that pops up asking for input so if you want to walk away while things are getting compiled you will want to use the recursive version of the command:

# make config-recursive
# make config-recursive
# make install
# make clean distclean

or to use default options to install:
# make -DBATCH install
or
# make BATCH=yes install

config-recursive is very handy, but don't forget to run it twice. You may enable a dependency the first time through that has its own configuration options.

I usually run this twice or until it produces no output. It's possible to enable a dependency on your first run through that has its own configuration.

set up logon script in Active Directory Domain Controller on Windows 2003 Server

set up logon script in Active Directory Domain Controller on Windows 2003 Server

\\(SERVERNAME)\sysvol\(DOMAIN NAME)\scripts

Active Directory Users and Computers > select a user > property > Profile > Logon script

Friday, December 16, 2011

Hello World Python GUI with Tkinter on CentOS and FreeBSD

Background


Tkinter is a Python binding to the Tk GUI toolkit. It is the standard Python interface to the Tk GUI toolkit[1] and is Python's de-facto standard GUI,[2] and is included with the standard Windows install of Python.

As with most other modern Tk bindings, Tkinter is implemented as a Python wrapper around a complete Tcl interpreter embedded in the Python interpreter. Tkinter calls are translated into Tcl commands which are fed to this embedded interpreter, thus making it possible to mix Python and Tcl in a single application.
Python 2.7 and Python 3.1 incorporate the "themed Tk" ("ttk") functionality of Tk 8.5.[3][4] This allows Tk widgets to be easily themed to look like the native desktop environment in which the application is running, thereby addressing a long-standing criticism of Tk (and hence of Tkinter).
There are several popular GUI library alternatives available, such as wxPython, PyQt and PyGTK.

From:
http://en.wikipedia.org/wiki/Tkinter

On CentOS 6.1
# python --version
Python 2.6.6

# yum list | grep lib-tk
python-matplotlib-tk.i686 0.99.1.2-1.el6 @base

# yum install python-matplotlib-tk.i68

# find /usr/lib/python2.6/ -name Tkinter.py
/usr/lib/python2.6/lib-tk/Tkinter.py

On FreeBSD 8.2
# cd /usr/ports/x11-toolkits/py-tkinter/ ; make install clean

# python --version
Python 2.7.2

# find /usr/local/lib/python2.7/ -name Tkinter.py
/usr/local/lib/python2.7/lib-tk/Tkinter.py

Hello World
# vim ~/tmp/hello_world.py
#!/usr/bin/python
# -*- coding: iso-8859-1 -*-

from Tkinter import *

root = Tk()

w = Label(root, text="Hello, world!")
w.pack()

root.mainloop()

# python ~/tmp/hello_world.py

Reference:
http://sebsauvage.net/python/gui/
http://www.tkdocs.com/tutorial/index.html

convert python scripts to .exe executable file

convert python scripts to .exe executable file

py2exe -
http://www.py2exe.org/

py2app -
http://svn.pythonmac.org/py2app/py2app/trunk/doc/index.html

cx_Freeze - is a set of scripts and modules for freezing Python scripts into executables in much the same way that py2exe and py2app do. Unlike these two tools, cx_Freeze is cross platform and should work on any platform that Python itself works on. It requires Python 2.3 or higher since it makes use of the zip import facility which was introduced in that version.

// supports Python 3.2

http://cx-freeze.sourceforge.net/

===
scru: If you know C, you can make a bootstrap app that embeds the interpreter and then runs your script. I think it's just a few lines of code, but you would have to distribute python30.dll alongside the parts of the standard library that your program uses.
===
Nicholas Knight: There's nothing stopping you from including a prebuilt Python environment in your package/installer and either creating an appropriate shortcut to launch the Python app, or creating a separate executable that launches the Python app (or even linking the Python interpreter into the executable and using it like that...). It just takes some more effort making sure you've got all the right pieces (like .dlls) where they need to be.

Remember, Python doesn't have to be "installed" per se, you just need to make sure it knows where to find everything (e.g. set the appropriate environment variables like PYTHONPATH in whatever launcher you're using).
===
Reference:
http://www.daniweb.com/software-development/python/threads/192343
http://stackoverflow.com/questions/3447093/what-technologies-exist-to-create-stand-alone-executables-for-python-3

List installed packages with yum in CentOS

List installed packages with yum in CentOS

# yum list installed | less

or

# rpm -qa | less

Thursday, December 15, 2011

script o add multiple a bunch of IP addresses to a network connection without having to type each one in individually

script o add multiple a bunch of IP addresses to a network connection without having to type each one in individually

Create a batch file like the below:

Add IP
netsh in ip add address "Local Area Connection" 10.0.0.2 255.0.0.0

Delete IP
netsh in ip delete address "Local Area Connection" 10.0.0.2

or just do a command like



for /L %a in (1,1,254) do netsh in ip add address "Local Area Connection" 10.0.0.%a 255.255.255.0

kernel: msk0: watchdog timeout

My Ethernet Card
# pciconf -lvc

mskc0@pci0:2:0:0: class=0x020000 card=0x34588086 chip=0x436111ab rev=0x18 hdr=0x00
vendor = 'Marvell Semiconductor (Was: Galileo Technology Ltd)'
device = 'Yukon 88E8050 PCI-E ASF Gigabit Ethernet Controller'
class = network
subclass = ethernet
cap 01[48] = powerspec 2 supports D0 D1 D2 D3 current D0
cap 03[50] = VPD
cap 05[5c] = MSI supports 2 messages, 64 bit enabled with 1 message
cap 10[e0] = PCI-Express 1 legacy endpoint max data 128(128) link x1(x1)
ecap 0001[100] = AER 1 0 fatal 1 non-fatal 0 corrected

Error messages from log:
# tail /var/log/messages
Dec 13 13:33:52 web0 kernel: msk0: watchdog timeout
Dec 13 13:33:52 web0 kernel: msk0: link state changed to DOWN
Dec 13 13:33:55 web0 kernel: msk0: link state changed to UP

If you see msk0 msk(4) watchdog timeouts, try disabling MSI (Message Signaled Interrupts) in /boot/loader.conf:

Note: MSI stands for Message Signaled Interrupts. MSI-X is an extension to MSI, for supporting more vectors. MSI can support at most 32 vectorsm while MSI-X can up to 2048.

# vim /boot/loader.conf
### disable MSI (Message Signaled Interrupts) support for msk ethernet card.
hw.msk.msi_disable=1

msk in manual:
# man 4 msk
hw.msk.msi_disable
             This tunable disables MSI support on the Ethernet hardware.  The
             default value is 0.

If it still doesn't work, try to disable TSO (net.inet.tcp.tso is set to "zero" in /etc/sysctl.conf). Active TSO causes many connection terminates, when network interfese was some load (~2 Mbit/s):

TSO (TCP segmentation offload)
---
The FreeBSD driver offers support for TSO (TCP Segmentation Offload).

You can enable/disable it in two ways/places:

- sysctl net.inet.tcp.tso=0 (or 1 to enable it)

Doing this disables TSO in the stack and will affect all adapters.

- ifconfig igbX -tso

Doing this will disable TSO only for this adapter.

To enable:

- ifconfig igbX tso

NOTE: By default only PCI-Express adapters are ENABLED to do TSO. Others
can be enabled by the user at their own risk
TSO is not supported on 82547 and 82544-based adapters, as well as older adapters.

# sysctl -a | grep tcp.tso
net.inet.tcp.tso: 1

Disable tso:
# vim /etc/sysctl.conf
net.inet.tcp.tso=0

Reference:
http://forums.freebsd.org/showthread.php?t=10183
http://wiki.freebsd.org/AppleMacbook
http://downloadmirror.intel.com/15815/eng/README.txt

> 請問 MIS 是哪幾個英文字的縮寫呀?

>請問 MIS 是哪幾個英文字的縮寫呀?

外行人認為的 MIS

Mission
Is
Simple

MIS = Man in Shit

IT is a dog. IT 是條狗。

中文簡稱屎人
台語叫專門拉D賽的人
運作正常沒你的功勞(因為那都是人家程式寫的好)
有問題時就換你背黑鍋
節省公司成本是上司用人正確(你做到死活該)與你無關
一切正常後,你就是公司養來吃閒飯的(這時候可以做做掃廁所的工作)
以上所言僅限於台灣

remove delete file with a special character filename

For example, if your filename contains a dash character:
-2

# rm -- -2

The paired double dash is the delimiter for the command,
telling it to expect a 'dashed' argument.
Glance through the man page.

or

# rm ./-2

or

# rm '-2'

or

# touch tmp ; rm tmp -2

Cisco Router Simulator (emulator) 模擬器

Cisco Router Simulator (emulator) 模擬器

Packet Tracer - is a Cisco router simulator that can be utilized in training and education, but also in research for simple computer network simulations.

http://www.cisco.com/web/learning/netacad/course_catalog/PacketTracer.html

Dynamips - is an emulator program that was written to emulate Cisco routers. Dynamips runs on Linux , Mac OS X or Windows and can emulate the hardware of the Cisco series routing platforms by directly booting an actual Cisco IOS software image into the emulator.

Dynagen - is a front-end for use with the Dynamips Cisco router emulator. It uses an INI-like configuration file to provision Dynamips emulator networks. It takes care of specifying the right port adapters, generating and matching up those pesky NIO descriptors, specifying bridges, frame-relay, ATM switches, etc. It also provides a management CLI for listing devices, suspending and reloading instances, determining and managing idle-pc values, performing packet captures, etc.

http://dynagen.org/

GNS3 - is a graphical network simulator that allows simulation of complex networks.

To provide complete and accurate simulations, GNS3 is strongly linked with:

- Dynamips, a Cisco IOS emulator.
- Qemu, a generic and open source machine emulator and virtualizer.
- VirtualBox, a free and powerful virtualization software.

http://www.gns3.net/

Saturday, December 10, 2011

FreeBSD Set Speed Duplex For My Network Card ( NIC )

How do I set / change the speed ( duplex settings ) on my FreeBSD network card? I'd like to change default 10Mbps to 100Mbps or vice versa. Linux user can use ethtool to set an Ethernet card speed and duplex, can I use the same command on FreeBSD?



A. ethtool is not required on FreeBSD. By default, the NIC auto-negotiates the speed and duplex of the connection. Make sure port speed set correctly on your switch. You need use the ifconfig command to configure the speed and duplex settings on the adapter. You also need to update your /etc/rc.conf file to set speed and duplex configuration.

Task: View Current Speed and Duplex Settings

Simply, use ifconfig command, enter:

# ifconfig em0

# ifconfig interface-name

# ifconfig | grep media


Output:

em0: flags=8843 mtu 1500
        options=b
        inet 10.10.1.2 netmask 0xffffffc0 broadcast 10.10.1.29
        ether 00:30:48:93:1e:9a
        media: Ethernet 10baseT/UTP 
        status: active

ifconfig command to change speed and duplex settings

General syntax is as follows to set 10Mbps full-duplex:

ifconfig <interface-name> <IP_address> media 10baseT mediaopt full-duplex
or
ifconfig <interface-name> <IP_address> media 10baseTX mediaopt full-duplex

Set 100Mbps full-duplex, enter:

ifconfig <interface-name> <IP_address> media 100baseT mediaopt full-duplex
or
ifconfig <interface-name> <IP_address> media 100baseTX mediaopt full-duplex

Set 1000Mbps full-duplex, enter:

ifconfig <interface-name> <IP_address> media 1000baseT mediaopt full-duplex
or
ifconfig <interface-name> <IP_address> media 1000baseTX mediaopt full-duplex

For example, set interface em0 with IP 10.10.1.2 to 100Mbps full duplex, enter:

# ifconfig em0 10.10.1.2 media 100baseTX mediaopt full-duplex

If the interface is currently forced to 100 full duplex, in order to change to half duplex you must type the following command:

# ifconfig em0 10.10.1.2 media 100baseTX -mediaopt full-duplex

The -mediaopt option disable the specified media options (full-duplex) on the interface i.e. go back to half duplex.

Make speed and duplex settings permanent

To make configuration changes persistence, update /etc/rc.conf file. Open config file using text editor, enter:

# vi /etc/rc.conf

Find line that read as follows:

ifconfig_interfacename="...."

To set em0 interface to 10Mbps full, enter:

ifconfig_em0="inet x.x.x.x netmask y.y.y.y media 10baseT mediaopt full-duplex"
or
ifconfig_em0="inet x.x.x.x netmask y.y.y.y media 10baseT/UTP mediaopt full-duplex"

To set em0 interface to 100Mbps full, enter:

ifconfig_em0="inet x.x.x.x netmask y.y.y.y media 100baseT mediaopt full-duplex"
or
ifconfig_em0="inet x.x.x.x netmask y.y.y.y media 100baseTX mediaopt full-duplex"

To set em0 interface to 1000Mbps full, enter:

ifconfig_em0="inet x.x.x.x netmask y.y.y.y media 1000baseT mediaopt full-duplex"
or
ifconfig_em0="inet x.x.x.x netmask y.y.y.y media 1000baseTX mediaopt full-duplex"

Make sure you replace x.x.x.x and y.y.y.y with actual IP and netmask address. Here is entry from my own personal FreeBSD workstation:

ifconfig_em0="inet 10.10.1.2 netmask 255.255.255.0 media 100baseT/UTP mediaopt full-duplex"

Save and close the file. Restart the networking:

# /etc/rc.d/netif restart


Note: There are five physical layer standards for gigabit Ethernet using:
- optical fiber (1000BASE-X)
- twisted pair cable (1000BASE-T) (Cat‑5, Cat‑5e, Cat‑6, or Cat‑7)
- twisted pair cable (1000BASE-TX) (Cat‑6, Cat‑7)
- balanced copper cable (1000BASE-CX).

Reference:
http://en.wikipedia.org/wiki/Gigabit_Ethernet
http://www.cyberciti.biz/faq/howto-configure-freebsd-full-half-duplex-speed/

pf firewall nested macros variables expansion

# pfctl -f /etc/pf.conf
No ALTQ support in kernel
ALTQ related functions disabled
/etc/pf.conf:260: syntax error
/etc/pf.conf:261: syntax error

pfctl: Syntax error in config file: pf rules not loaded

Solution:
You need extra single quotes to solve the problem.

net1 = '"172.16.0.0/12"'
net2 = '"192.168.0.0/16"'
net3 = '"10.0.0.0/8"'

allow_hosts = "{" $net1 $net2 $net3 "}"

FreeBSD: HowTo Start / Stop / Restart Network and Routing Service

FreeBSD is a Unix-like free operating system. You can install to choose third party software on FreeBSD from the Ports Collection. Many services such as POP3 server daemons, IMAP, etc. could be started using the inetd.

However, networking can be stop or started using special script located in /etc/rc.d/ directory. This directory includes script to stop or start network, and other services such as SSH server.
FreeBSD 5.x/6.x and above version - start or stop network

FreeBSD 5.x/6.x and above version comes with netif script to start, restart, stop network service. You need to login as a root user. Then you can use following commands:

To start FreeBSD network service:
# /etc/rc.d/netif start

To stop FreeBSD network service:
# /etc/rc.d/netif stop

To restart FreeBSD network service:
# /etc/rc.d/netif restart

Manual method using ifconfig

To stop network card (NIC) on-fly:
# ifconfig network-interface down

To start network card (NIC) on fly:
# ifconfig network-interface up

To list down network interface:
# ifconfig -d

To list up network interface:
# ifconfig -u

FreeBSD Update / restart routing tables / service

It is also necessary to update routing table after restating networking service, enter:
# /etc/rc.d/routing restart

How do I restart network service over ssh session?

You need to type the commands as follows in order to avoid any problems:
# /etc/rc.d/netif restart && /etc/rc.d/routing restart

FreeBSD 4.x and prior version only - start, restart, stop network service

1) To start network service under FreeBSD you need to copy special
script as follows:

# cp /usr/share/examples/etc/netstart /etc/network

2) Execute script:

. /etc/network

OR

sh /etc/network

Please note that script will do all necessary work for you i.e. it will start network, routing, firewall etc.

http://www.cyberciti.biz/tips/freebsd-how-to-start-restart-stop-network-service.html