2009年11月23日 星期一

configure bridge


最近在玩kvm/qemu,因為要設定自己的網路,所以就順便寫一下紀錄。
網路設定在Linux上都是使用/etc/network/interfaces,這邊大概沒有什麼網路設定不能設的了。 我的設定是要把TUN/TAP(tap0)加入bridge(br0)中,並且設定br0的IP,如此簡單而已,我的/etc/network/interfaces設定如下:
auto br0
iface br0 inet static
auto br0
iface br0 inet static
    pre-up brctl addbr br0
    pre-up tunctl -b -u brook -t tap0
    pre-up brctl addif br0 tap0
    pre-up ifconfig tap0 up
    post-dwon brctl delif br0 tap0
    post-down tunctl -d tap0
    post-down brctl delbr br0
    address 192.168.12.1
    netmask 255.255.255.0
    bridge_port qtap0
    bridge_fd 9
    bridge_hello 2
    bridge_maxage 12
    bridge_stp off
iface br0 inet static是說br0是static IP。
pre-up 是說在up該interface之前,先執行
post-down 是說在down該interface之後,執行
address / netmask 是設定IP資訊。
bridge_xxx 是設定bridge參數。

這樣每次開機後,就會產生tap0並且把他加入br0,以及設定好br0。



2009年11月22日 星期日

好態度能改變一切


每天都可以看到一個公益廣告,"這題你不是練好幾遍 笨得喔",換個方式說"你不笨 是這題得練好幾遍喔",其實很多人需要鼓勵,讓他能在鼓勵中成長茁壯,真得是時代在變,回想我小時候,父母用的都是打罵教育,我們也沒因此倒地不起,而且常常被教育是要越挫越勇,就像七龍珠裡面的孫悟空。
不過幾天前,某人告訴我,打罵也是教育,鼓勵也是教育,何不讓小孩在快樂的環境中長大,其實我也認同,讓小孩有正面的態度,其實並不需要打罵。


Linux Kernel(1)- Linux Module簡介


Linux module練習手札I紀錄如何撰寫一個簡單的module,並且編輯它,以及load和unload一個module。

write a module

#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");

static int __init init_modules(void)
{
    printk("hello world\n");
    return 0;
}

static void __exit exit_modules(void)
{
    printk("goodbye\n");
}

module_init(init_modules);
module_exit(exit_modules);
<linux/init.h>和#include <linux/module.h>是Linux 任何的module都會用到的header file,init.h主要定義module的init和cleanup,如module_init()和module_exit()。而module.h定義了module所需要的資料結構與macro。
對於__init的解釋在init.h有非常好的解釋:
The kernel can take this as hint that the function is used only during the initialization phase and free up used memory resources after.
簡單的說就是這個function在初始化後(執行完)就被free了。

而__exit的解釋是:
__exit is used to declare a function which is only required on exit: the function will be dropped if this file is not compiled as a module.

module_init()的解釋是:
The module_init() macro defines which function is to be called at module insertion time (if the file is compiled as a module), or at boot time: if the file is not compiled as a module the module_init() macro becomes equivalent to __initcall(), which through linker magic ensures that the function is called on boot.
主要是用來設定當insert該module後,應該要被執行的進入點(enrty point)。

module_exit()的解釋是:
This macro defines the function to be called at module removal time (or never, in the case of the file compiled into the kernel). It will only be called if the module usage count has reached zero. This function can also sleep, but cannot fail: everything must be cleaned up by the time it returns.
Note that this macro is optional: if it is not present, your module will not be removable (except for 'rmmod -f').

簡言之,就是當user執行rmmod時,會被執行到的function。沒有module_exit(),module就不能被rmmod。


write a Makefile to manage the module

mname := brook_modules
$(mname)-objs := main.o
obj-m := $(mname).o

KERNELDIR := /lib/modules/`uname -r`/build

all:
        $(MAKE) -C $(KERNELDIR) M=`pwd` modules

clean:
        $(MAKE) -C $(KERNELDIR) M=`pwd` clean
$(mname)-objs是告訴make這個module有哪些object files。
obj-m是告訴make這個module的name是什麼。
KERNELDIR是告訴make這個module的kernel所在的位置。
後面就接兩個target(all/clean),用於處理產生和清除module用。


load/unload a module




熱門文章