2011年11月7日星期一

sysctl 中 vm.overcommit_memory 的含义



vm.overcommit_memory 表示内核在分配内存时候做检查的方式。这个变量可以取到0,1,2三个值。对取不同的值时的处理方式都定义在内核源码 mm/mmap.c 的 __vm_enough_memory 函数中。

取 1 的时候 :
此时宏为 OVERCOMMIT_ALWAYS,函数直接 return 0,分配成功。

取 2 的时候:
此时宏为 OVERCOMMIT_NEVER,内核计算:内存总量×vm.overcommit_ratio/100+SWAP 的总量,如果申请空间超过此数值,则分配失败。vm.overcommit_ratio 的默认值为50。

取 0 的时候:
此时宏为 OVERCOMMIT_GUESS,内核计算:NR_FILE_PAGES 总量+SWAP总量+slab中可以释放的内存总量,如果申请空间超过此数值,则将此数值与空闲内存总量减掉 totalreserve_pages(?) 的总量相加。如果申请空间依然超过此数值,则分配失败。

以上为粗略描述,在实际计算时,如果非root进程,则在计算时候会保留3%的空间,而root进程则没有该限制。详细过程可看源码。

大概过程如上所述,但还有很多数据结构还没搞清楚。

2011年11月4日星期五

resolv.conf 的配置和dns解析的关系

本文为笔记,部分地方不准确。目的:resolv.conf 中配置了 search 选项和 domain 选项后,检查系统查询域名的步骤和行为。

0 若不存在 resolv.conf,则将本机做为dns服务器,然后取主机名的domain,然后附加到域名中进行查询。查询时首先查询AAAA记录,再查询A记录。以下均为查询nxyi-dns2.hst.xyi.cn.xdc.net时,得到的不同结果。

1 若没有search(最长为6个)和domain选项,则取主机名的domain,然后附加到域名中进行查询:

17-Oct-2011 23:35:17.536 queries: client 172.22.41.176#44119: query: nxyi-dns2.hst.xyi.cn.xdc.net IN AAAA +
17-Oct-2011 23:35:17.536 queries: client 172.22.41.176#56562: query: nxyi-dns2.hst.xyi.cn.xdc.net.hst.xyi.cn.xdc.net IN AAAA +
17-Oct-2011 23:35:17.536 queries: client 172.22.41.176#53616: query: nxyi-dns2.hst.xyi.cn.xdc.net IN A +

2 如有domain,(domain xdc.net)则,附加domain到域名中进行查询:

17-Oct-2011 23:38:02.031 queries: client 172.22.41.176#43411: query: nxyi-dns2.hst.xyi.cn.xdc.net IN AAAA +
17-Oct-2011 23:38:02.031 queries: client 172.22.41.176#41697: query: nxyi-dns2.hst.xyi.cn.xdc.net.xdc.net IN AAAA +
17-Oct-2011 23:38:02.031 queries: client 172.22.41.176#35978: query: nxyi-dns2.hst.xyi.cn.xdc.net IN A +

3 若有多个search (search hst.xyi.cn.xdc.net hst.dsl.crm.xdc.net hst.x.dw.xdc.net) 则依次附加,然后查询,顺序依然首先查询AAAA记录,再查A记录:

17-Oct-2011 23:39:25.272 queries: client 172.22.41.176#49875: query: nxyi-dns2.hst.xyi.cn.xdc.net IN AAAA +
17-Oct-2011 23:39:25.272 queries: client 172.22.41.176#36371: query: nxyi-dns2.hst.xyi.cn.xdc.net.hst.xyi.cn.xdc.net IN AAAA +
17-Oct-2011 23:39:25.272 queries: client 172.22.41.176#33367: query: nxyi-dns2.hst.xyi.cn.xdc.net.hst.dsl.crm.xdc.net IN AAAA +
17-Oct-2011 23:39:25.272 queries: client 172.22.41.176#42772: query: nxyi-dns2.hst.xyi.cn.xdc.net.hst.x.dw.xdc.net IN AAAA +
17-Oct-2011 23:39:25.273 queries: client 172.22.41.176#45355: query: nxyi-dns2.hst.xyi.cn.xdc.net.xdc.net IN AAAA +
17-Oct-2011 23:39:25.273 queries: client 172.22.41.176#35669: query: nxyi-dns2.hst.xyi.cn.xdc.net IN A +

4 若nxyi-dns5.hst.xyi.cn.xdc.net没有找到,则附加search进行查询A记录:

17-Oct-2011 23:52:21.527 queries: client 172.22.41.176#56298: query: nxyi-dns5.hst.xyi.cn.xdc.net IN AAAA +
17-Oct-2011 23:52:21.528 queries: client 172.22.41.176#40880: query: nxyi-dns5.hst.xyi.cn.xdc.net.xyi.cn.xdc.net IN AAAA +
17-Oct-2011 23:52:21.528 queries: client 172.22.41.176#40359: query: nxyi-dns5.hst.xyi.cn.xdc.net IN A +
17-Oct-2011 23:52:21.528 queries: client 172.22.41.176#36129: query: nxyi-dns5.hst.xyi.cn.xdc.net.xyi.cn.xdc.net IN A +

5 若存在ndots:6,则由于域名仅仅包含了5个点,则顺序改变,首先附加search查找,然后再查找域名本身。默认为ndots值为1,即域名只要包含一个点,就不会首先附加search:

17-Oct-2011 23:53:04.260 queries: client 172.22.41.176#50630: query: nxyi-dns5.hst.xyi.cn.xdc.net.xyi.cn.xdc.net IN AAAA +
17-Oct-2011 23:53:04.260 queries: client 172.22.41.176#38081: query: nxyi-dns5.hst.xyi.cn.xdc.net IN AAAA +
17-Oct-2011 23:53:04.260 queries: client 172.22.41.176#36043: query: nxyi-dns5.hst.xyi.cn.xdc.net.xyi.cn.xdc.net IN A +
17-Oct-2011 23:53:04.260 queries: client 172.22.41.176#58401: query: nxyi-dns5.hst.xyi.cn.xdc.net IN A +

总结:
系统查询域名主要使用 gethostbyname(过时) 和 getaddrinfo 两个函数,二者接收的参数和返回的数据类型都不相同。其中 gethostbyname 实际调用 gethostbyname2,根据传递的参数可以查询AAAA或者A记录。getaddrinfo 功能更强大,可以控制是查询AAAA,A,或者先AAAA后A记录。详细可见glibc中 resolv/gethnamaddr.c 和 sysdeps/posix/getaddrinfo.c 中的实现。

2011年10月20日星期四

Linux 字体的一些概念

google + baidu 得到的结果,不保证准确。

字体有矢量字体和位图字体两种类型。矢量字体中的字形由数学曲线描述,这种字体由于需要计算,因此显示速度慢,但是该字体可以任意缩放,主要包括Truetype,Type1,OpenType等;位图字体又叫点阵字体,字形由点组成,显示速度快,但是缩放后会有锯齿。显示字形时要将矢量字体转换为位图字体,这个过程称为光栅化。

FreeType 是字体函数库。应用程序通过FreeType可以访问字体文件,获取字体信息,字形数据,而无需关心字体文件的位置。
相关文档参见:http://www.unixresources.net/linux/clf/kylix/archive/00/00/59/21/592188.html

X包含两种字体系统:原始的核心X11字体系统和Xft系统。 Xft比核心X11字体系统对缩放字形支持要好,并且提供了更多的功能。另外,Xft和核心X11字体系统是不兼容的。Xft 是 FreeType 之上的库,编写 Xft 库是为了给X应用程序提供一个能访问 FreeType 字体光栅化引擎和X渲染的接口。对于不支持渲染的X服务器,还要提供一个访问原始核心X11字体系统的功能。
相关文档参见:http://www.x.org/archive/X11R6.8.2/doc/fonts.html
https://wiki.archlinux.org/index.php/Font_Configuration_%28%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87%29#X.E7.9A.84.E5.AD.97.E4.BD.93.E9.85.8D.E7.BD.AE.E5.92.8C.E7.BE.8E.E5.8C.96
http://it.china-b.com/olbf/468958.html

Fontconfig 配置了程序如何选取字形,主要配置文件为 /etc/fonts/fonts.conf。除了字体信息外,还定义了字体的cache目录以及用户的字体目录。
具体配置方法参见:http://www.xfree86.org/~dawes/4.3.0/fonts2.html#4
https://wiki.archlinux.org/index.php/Font_Configuration_%28%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87%29

最后记录两个命令:
fc-cache 添加新字体后刷新cache,否则新字体无法使用。
fc-list 查看所有配置过的字体。
xlsfont 查看所有的X字体。

快速配置字体的方案:http://www.ikde.org/news/linux_font_config_from_beginner_to_expert_1/

2011年10月17日星期一

vsftpd 配置虚拟用户

最近配置两遍VSFTPD的虚拟用户了,第一次去google,第二次ssh到第一次的机器上运行history。。。现在记录一下vsftpd使用虚拟用户需要的相关配置:

1 配置vsftpd使用的虚拟用户,以及虚拟用户配置文件的目录。
本示例中虚拟用户映射到系统真实的notes用户,虚拟用户的配置目录在 /etc/vsftpd/vsftpd_user_conf  

chroot_local_user=YES
guest_enable=YES
guest_username=notes
user_config_dir=/etc/vsftpd/vsftpd_user_conf
virtual_use_local_privs=YE

2 在一个文件中配置虚拟用户的账户名和密码,该文件奇数行为用户名,偶数行为密码。
本示例中该文件为 /etc/vsftpd/userlist,虚拟用户名为tom,密码为456b7016a91

#cat /etc/vsftpd/userlist
tom
456b7016a91

3 利用该文件生成数据库文件,可能需要安装 db4-utils

yum -y install db4-utils
db_load -T -t hash -f /etc/vsftpd/userlist /etc/vsftpd/vsftpd_login.db

4 配置虚拟用户 tom 的具体权限

#cat /etc/vsftpd/vsftpd_user_conf/tom
local_root=/home/video_files
write_enable=YES
anon_other_write_enable=YES
anon_umask=022
allow_writeable_chroot=YES

4 配置 vsftpd 使用 PAM 验证:

pam_service_name=vsftpd

5 PAM验证使用刚才生成的数据库文件:

#cat /etc/pam.d/vsftpd
auth required /lib64/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login
account required /lib64/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login

2011年6月14日星期二

rsync delete 参数

抄的抄的,原文见: http://superuser.com/questions/156664/rsync-delete-options
我好奇的是什么时候才需要考虑具体的delete方式。

--del/--delete_during Deletes files from the destination dir as they are copied (saves memory compared to --delete-before: --delete-before makes a separate scan to look for deleteables)
--delete Deletes files in the destination directory if they don't exist in the source directory.
--delete-before Delete files in the destination directory before coping file-with-same-name from source directory
--delete-during Delete files in the destination directory WHILE copying file-with-same-name from source directory
--delete-delay Mark deletes during transfer, but wait until transfer is complete
--delete-after Receiver deletes after transfer, not before...If some other part of the rsync moved extra files elsewhere, you'd want this instead of --delete-delay, because --delete-delay decides what its going to delete in the middle of transfer, whereas --delete-after checks the directory for files that should be deleted AFTER everything is finished.
--delete-excluded Deletes files from the destination directory that are explicitly excluded from transferring from the source directory.


The point of rsync is not copying, it is archiving. This is an important distinction. Processing deleted/changed files is critical, and in many cases nuanced.

Most of the other options are space or performance related. When you delete the files is important if you want to make sure the transfer is successful before you do anything, but if your device is too small to handle 2 copies of all the information, you need to delete as you go, etc. It's a little wacky because of it's long history across multiple platforms: some options have been added so that people who were used to certain behaviour wouldn't be confused.

2011年6月1日星期三

linux的系统时间与硬件时间

用了 opensuse 11.4 后发现系统时间一直比当前时间快8个小时,明显是时区有问题,但是/etc/localtime 又没有错误,今天查了查资料,搞清楚了几个概念。
1 GMT:格林威治时间。基本可以理解为一个标准时间,时区的原点,按照经线将地球分为24个时区。
2 UTC:世界协调时间。由于地球不是正圆的,自传速度也不一样,因此按照经线来均分地球时区有些不太精确。此时间相当于对GMT的修正,实际还是GMT时间。中国位于东8区,又称为CST(China Standard Time) CST时间 = UTC时间 + 8小时。
3 硬件时间:即通过电池在BIOS里记录的时间。Linux 对此时间的判断有两种可能:a 该时间表示本地时间;b 该时间表示 UTC 时间。Linux启动时会读取硬件时间,并根据 /etc/sysconfig/clock 中的 HWCLOCK 判断该时间记录的是本地时间还是 UTC 时间。
4 系统时间:就是 Linux 系统中显示的本地时间,该时间取决于系统开机后读取的时间以及当前的时区设置。时区配置文件为 /etc/localtime。/usr/share/zoneinfo 下保存了所有时区的配置文件,找个合适的,复制到 /etc/localtime 就可以了。

有了以上概念就不难理解了,在时区配置没有问题的情况下,一定是 Linux 在启动时读取硬件时间出了问题。只要将硬件时钟配置正确,并让 Linux 正确的读出来就问题就可以解决了。考虑到 windows 在启动时会将硬件时间设定为系统时间,因此 Linux 最好也采用此配置,否则启动 windows 后,windows 时钟可能会不正确。另外,系统时间是本地时间,而硬件时间则是 UTC 时间,看起来也不舒服。

修正方式如下:

方法1,读取硬件时间作为本地时间
1 hwclock --localtime 读取硬件时间。
2 如果要将该时间设置为系统时间,则将该时间写回系统 hwclock --localtime --hctosys,这时系统时间应该就变正确了
3 对于openSUSE12.2之前的版本:修改/etc/sysconfig/clock 中的HWCLOCK,由--utc 为 --localtime 否则下次重启还会出现问题。对于openSUSE 12.2:修改/etc/adjtime 将第三行从UTC更改为LOCAL。

办法 2,直接修改系统时间
1 date -s 设置系统时间
2 将系统时间写回硬件时钟 hwclock --localtime --systohc
3 修改/etc/sysconfig/clock


查看和设置时间的几个例子:

1 显示本地时间
# date
Thu Jun 2 21:48:44 CST 2011

2 查看硬件时间(指定硬件时间即为本地时间)
# hwclock --localtime
Thu Jun 2 21:48:47 2011 -0.239341 seconds

3 查看硬件时间 (指定硬件时间为 UTC 时间,则会对该时间+8,转换为本地时间后再显示出来)
# hwclock --utc
Fri Jun 3 05:48:49 2011 -0.125543 seconds

4 指明硬件时间为本地时间,并将该时间设置为系统时间
# hwclock --localtime --hctosys

5 指明硬件时间为本地时间,并将系统时间写回到 BIOS 中
# hwclock --localtime --systohc

2011年4月25日星期一

Mail Exchangers 避免循环转发

比如 abc.com 有以下3条 mx 记录:

abc.com. IN MX 0 0.abc.com.
abc.com. IN MX 10 11.abc.com.
abc.com. IN MX 10 12.abc.com.
abc.com. IN MX 20 21.abc.com.


向 0.abc.com 投递失败后,发送方会向 11 或者 12 进行投递,若 11 和 12 只有转发功能,则依然需要查询 mx 记录,然后再次进行投递。11 收到邮件后可能会投递给 12,12 收到邮件后会投递给 11,循环由此产生。
为解决此问题,转发服务器会自动 disable 优先级不高于自己的 mx 记录(即只保留优先级比自己高的记录),从而达到此目的。

比如对于 abc.com,11 在收到邮件后,根据自己的优先级10,会仅仅向 0 转发邮件,而停止向 12 和 21 的转发,这样就避免了循环投递的情况。

不过可以假设一个存在view情况,在 view1 中 11 的优先级高于 12,但是在 view2中 12 的优先级高于 11,这样是不是依然可能存在循环投递的情况。

nslookup 和 dig 的差别

One major difference between nslookup and dig is that dig doesn't apply the search list

nslookup 和 name server 的差别

To query like a resolver, you use nslookup's default settings. To query like a name server, use set norecurse and set nosearch. On the command line, that's nslookup -norecurse -nosearch.

简单的说,nslookup默认采用searchlist并在查询时设置递归位。经过set -norecurse 和 set -nosearch 后,nslookup的行为和name server的行为一致。

resolver 和 nslookup 的不同

resolv.conf中若配置一条name server记录,二者表现相同。若配置多条记录:
The resolver tries the first nameserver, then the second, then the first, then the second, until it receives a response or gives up. The resolver does this for every query. On the other hand, nslookup tries the first nameserver in resolv.conf and keeps retrying until it finally gives up on the first nameserver and tries the second. Once it gets a response, it locks onto that server and doesn't try the next.

2011年4月22日星期五

resolver 和 name server 的不同

resolver 一般是指系统提供解析服务的库文件,是它去向 name server 进行查询。简单的解释如下:
The resolver is the client half of the Domain Name System. It's responsible for translating a program's request for host information into a query to a name server and for translating the response into an answer for the program.


Name servers' query messages aren't that much different from resolvers' query messages in the first place. The primary difference in the query messages is that resolvers request recursive resolution and name servers seldom do. Requesting recursion is the default with nslookup, so you have to explicitly turn it off. The difference in operation between a resolver and a name server is that the resolver applies the search list, and the name server doesn't. By default, nslookup applies the search list, so that must be explicitly turned off as well. Of course, judicious use of the trailing dot will have the same effect.

2011年4月18日星期一

activity 和 desktop的差别

Plasma 中引入了 activity 的概念,虽然多个activity 和 desktop 从表面看起来差不多,但还是有很大的差别的。以下摘自 suse 的帮助文档,不翻译了。

Plasma 的组成:
The essence of Plasma revolves around two basic concepts: Plasma Widgets and Containments. Widgets is Applets, or small applications that live on the desktop. Containments is Applets that act as the container for the Plasma widgets
On a default desktop, there are two main elements: the Panel and the Desktop itself. Both are containments in the Plasma sense.

Plasma 和 desktop 的差别:
The desktop toolbox, accessed via the top right corner has a button for displaying your activities, of which Plasma allows you to have more than one. Basically, that is multiple desktop containments hosting multiple sets of Plasma widgets. Display the “Activities” bar, choose Add activity to create a new containment, select your new containment and customize suiting your taste. Plasma’s activities and KWin’s desktop grid are similar in that respect, but there is a fundamental difference. While virtual desktop are used to group and organize windows, Plasma’s activities are used to group and organize Plasma widgets. This way, you can switch between activities and have relevant Plasma widgets supporting the task you're currently trying to accomplish.

2011年2月26日星期六

sed 高级命令

sed 在处理数据的过程中使用到了“模式空间”和“保持空间”这两个概念。“模式空间”的作用是缓冲区,即sed每次都将一行文本读入到模式空间中,然后进行处理。“保持空间”则起到了剪贴板的作用,可以利用相关命令将模式空间的部分内容保存到保持空间里,然后再进行处理。

sed 处理文本的过程相对简单:
1 读取一行文本到模式空间
2 将命令依次应用到模式空间的文本
3 输出模式空间内容(-n参数可以取消)

为了改变sed默认的工作流程,可以采用以下的高级命令改变sed读取文本的行为:
d 删除模式空间的内容,后续命令不再执行,读取新行
p 打印模式空间的内容
n 输出模式空间的内容(-n参数取消此行为),读取新行,覆盖模式空间内容,后续命令用于新的模式空间
q 输出模式空间内容(-n参数取消此行为),立即退出脚本
N 读取新行,追加到模式空间,后续命令用于新的模式空间
P 打印模式空间的第一行
D 删除模式空间中的第一行,回到脚本开始,将命令应用于新的模式空间
其中 NPD 命令可以构成处理多行模式的循环。

sed 还提供了5个命令用以操作“保持空间”:
h 模式空间 > 保持空间
H 模式空间 >> 保持空间
g 保持空间 > 模式空间
G 保持空间 >> 模式空间
x 模式空间 <> 保持空间

sed 还提供了label的概念,通过跳转到 label 处执行命令,改变了命令的执行顺序:
: —— 加标签
b —— b label,如果 label 为空,则跳转到结尾
t —— t label,如果 label 为空,则跳转到结尾。此命令与s替换命令连用,即 test 最后一个s命令的替换是否成功,若成功,则跳转,否则不做处理。

2011年1月5日星期三

grub 中 chainloader 命令的使用

chainloader一般只在多系统并存的时候才会出现,典型的如linux和windows双系统并存。但在要求多个linux系统并存的环境中,也可以通过正确配置 chainloader 减少工作量。chainloader 的主要用途就是加载安装在PBR(Partation Boot Record)位置的bootloader,比如在一台机器上同时需要安装Fedora和opensuse,可以有以下两种方法。

第一种方法:首先安装Fedora,然后将 grub 安装在 MBR 上,然后再安装opensuse,但这时选择不安装 grub。在opensuse 安装完毕后,进入 Fedora,然后修改 grub.conf 文件来启动 opensuse。这种办法的缺点很明显,如果opensuse存在两个kernel,则需要在 grub.conf 中添加两条引导记录。内容大概如下:
title OpenSUSE-desktop
root (hd0,1)
kernel /vmlinuz-desktop root=/dev/sda2 ro
initrd /initrd-desktop.img

title OpenSUSE-default
root (hd0,1)
kernel /vmlinuz-default root=/dev/sda2 ro
initrd /initrd-default.img

一旦某天打算将 opensuse 更换成 ubuntu,则需要再次修改 grub.conf 。

第二种方法:首先安装Fedora,然后将 grub 安装在 MBR 上,然后再安装opensuse,但这时选择将 grub 安装在 PBR上,在 opensuse 安装完毕后,进入 Fedora,然后修改 grub.conf 文件,添加如下内容:
title Other Linux
root (hd0,2)
chainloader +1
即可进入opensuse的启动界面。
这样无论将 (hd0,2) 的 opensuse 更换为何种系统或者新加多个内核,都无须再次修改 Fedora 中的引导记录。