<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>dipplum.com &#187; linux</title>
	<atom:link href="http://dipplum.com/tag/linux/feed/" rel="self" type="application/rss+xml" />
	<link>http://dipplum.com</link>
	<description>Be  the change you wanna see in the world</description>
	<lastBuildDate>Sat, 12 Nov 2011 07:38:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Seccomp和系统调用过滤</title>
		<link>http://dipplum.com/2009/12/08/seccomp-syscall-filter/</link>
		<comments>http://dipplum.com/2009/12/08/seccomp-syscall-filter/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 02:13:43 +0000</pubDate>
		<dc:creator>li</dc:creator>
				<category><![CDATA[开源软件]]></category>
		<category><![CDATA[chrome]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[sandbox]]></category>
		<category><![CDATA[seccomp]]></category>

		<guid isPermaLink="false">http://dipplum.com/2009/12/08/seccomp-syscall-filter/</guid>
		<description><![CDATA[Seccomp v1 Seccomp是2005年Andrea Arcangeli在Linux内核加的一个功能，为了给*Grid计算中的应用*提供sandbox模型。运行在Seccomp模式下的进程，只能执行下面几个系统调用： &#160;&#160;&#160; read(), write(), exit(), sigreturn() 通过Seccomp沙箱，和4个api的限制，保证进程不会影响到其他进程。 Seccomp v2 2009年的时候，Ingo Molnar扩展了Seccomp的实现，让其支持更加全面的系统调用过滤(syscall filtering)的功能。Google Chrome Browser好像用的沙箱模型就是类似Seccomp的系统调用过滤。 Filter的规则是一个数组，每个元素包括两个字段，分别是系统调用名和过滤规则： &#123; &#123; &#34;sys_read&#34;, &#34;fd == 0&#34; &#125;, &#123; &#34;sys_write&#34;, &#34;fd == 1&#34; &#125;, &#123; &#34;sys_sigreturn&#34;, &#34;1&#34; &#125;, &#123; &#34;sys_gettimeofday&#34;, &#34;tz == NULL&#34; &#125; 上面的第一条规则要求进程只能以fd = 0这个参数，调用sys_read。&#160; 参考资料 [1] Seccomp Wiki http://en.wikipedia.org/wiki/Seccomp [2] Seccomp and sandboxing http://lwn.net/Articles/332974/ [3] [...]]]></description>
			<content:encoded><![CDATA[<h4>Seccomp v1</h4>
<p>Seccomp是2005年Andrea Arcangeli在Linux内核加的一个功能，为了给*Grid计算中的应用*提供sandbox模型。运行在Seccomp模式下的进程，只能执行下面几个系统调用：    <br />&#160;&#160;&#160; read(), write(), exit(), sigreturn() </p>
<p>  <span id="more-461"></span>
<p>通过Seccomp沙箱，和4个api的限制，保证进程不会影响到其他进程。</p>
<h4>Seccomp v2</h4>
<p>2009年的时候，Ingo Molnar扩展了Seccomp的实现，让其支持更加全面的系统调用过滤(syscall filtering)的功能。Google Chrome Browser好像用的沙箱模型就是类似Seccomp的系统调用过滤。</p>
<p>Filter的规则是一个数组，每个元素包括两个字段，分别是系统调用名和过滤规则： </p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#123;</span> <span style="color: #ff0000;">&quot;sys_read&quot;</span><span style="color: #339933;">,</span>            <span style="color: #ff0000;">&quot;fd == 0&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#123;</span> <span style="color: #ff0000;">&quot;sys_write&quot;</span><span style="color: #339933;">,</span>        <span style="color: #ff0000;">&quot;fd == 1&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#123;</span> <span style="color: #ff0000;">&quot;sys_sigreturn&quot;</span><span style="color: #339933;">,</span>        <span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#123;</span> <span style="color: #ff0000;">&quot;sys_gettimeofday&quot;</span><span style="color: #339933;">,</span>        <span style="color: #ff0000;">&quot;tz == NULL&quot;</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>上面的第一条规则要求进程只能以fd = 0这个参数，调用sys_read。&#160; </p>
<h4>参考资料</h4>
<p>[1] Seccomp Wiki <a href="http://en.wikipedia.org/wiki/Seccomp">http://en.wikipedia.org/wiki/Seccomp</a> </p>
<p>[2] Seccomp and sandboxing <a href="http://lwn.net/Articles/332974/">http://lwn.net/Articles/332974/</a> </p>
<p>[3] seccomp: Add bitmask of allowed system calls <a href="http://lwn.net/Articles/332987/">http://lwn.net/Articles/332987/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://dipplum.com/2009/12/08/seccomp-syscall-filter/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>用 syslinux 引导 Windows</title>
		<link>http://dipplum.com/2009/03/22/syslinux-load-windows/</link>
		<comments>http://dipplum.com/2009/03/22/syslinux-load-windows/#comments</comments>
		<pubDate>Sat, 21 Mar 2009 17:50:34 +0000</pubDate>
		<dc:creator>li</dc:creator>
				<category><![CDATA[开源软件]]></category>
		<category><![CDATA[bootloader]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[syslinux]]></category>

		<guid isPermaLink="false">http://dipplum.com/2009/03/22/syslinux-load-windows/</guid>
		<description><![CDATA[FAT 分区的引导扇区（Boot Sector）的功能是将 Windows XP 的 ntldr 文件，或 Windows Vista 的 bootmgr 文件加载到内存适当位置，由此启动 Windows。 Linux 下可以创建 Windows 的 FAT 文件系统，但是不能创建 FAT 文件系统中的 Boot Sector，也就是不能创建可以引导的 FAT 分区，因而 Linux 下无法创建可以引导的 Windows 磁盘。 不过可以曲线救国，通过 syslinux 来引导 Windows。 首先，将 syslinux 安装到 Windows 分区设备上。 syslinux /dev/sdXY 然后，将 syslinux 自带的 chain.c32 文件拷贝到 Windows 分区上。如果是 Debian，这一文件在 /usr/lib/syslinux/ 目录下。 接着，在 Windows 分区上创建 syslinux.cfg [...]]]></description>
			<content:encoded><![CDATA[<p>FAT 分区的引导扇区（Boot Sector）的功能是将 Windows XP 的 ntldr 文件，或 Windows Vista 的 bootmgr 文件加载到内存适当位置，由此启动 Windows。</p>
<p>Linux 下可以创建 Windows 的 FAT 文件系统，但是不能创建 FAT 文件系统中的 Boot Sector，也就是不能创建可以引导的 FAT 分区，因而 Linux 下无法创建可以引导的 Windows 磁盘。</p>
<p>不过可以曲线救国，通过 syslinux 来引导 Windows。 首先，将 syslinux 安装到 Windows 分区设备上。</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">syslinux <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sdXY</pre></div></div>

<p>然后，将 syslinux 自带的 chain.c32 文件拷贝到 Windows 分区上。如果是 Debian，这一文件在 /usr/lib/syslinux/ 目录下。</p>
<p>接着，在 Windows 分区上创建 syslinux.cfg 文件</p>

<div class="wp_syntax"><div class="code"><pre class="syslinux" style="font-family:monospace;">default 1
timeout 0
label 1
  kernel chain.c32
  append hd0,1 ntldr=ntldr</pre></div></div>

<p>chain.c32 接受的第一个参数代表启动第1块硬盘的第1个分区，并装载分区上的 ntldr 文件。用该配置文件可以引导 Windows XP。如果需要引导 Windows Vista，需要修改成下述内容：</p>

<div class="wp_syntax"><div class="code"><pre class="syslinux" style="font-family:monospace;">default 1
timeout 0
label 1
  kernel chain.c32
  append hd0,1 ntldr=bootmgr</pre></div></div>

<p>chain.32 接受的参数说明如下</p>
<ul>
<li>option &#8220;swap&#8221; to support swapping of BIOS drive numbers. This is necessary to boot certain operating systems (DOS, Windows) from a secondary drive.</li>
<li>option &#8220;file=&#8221; to support loading a boot file from the SYSLINUX filesystem instead of loading the boot sector from the drive.</li>
<li>option &#8220;seg=&#8221; to control the load location.</li>
<li>option &#8220;ntldr=&#8221; as a shorthand for &#8220;seg=0&#215;2000 file=&#8221;; use this to load one of WinNT&#8217;s loaders</li>
<li>options &#8220;freedos=&#8221; and &#8220;msdos=&#8221;/&#8221;pcdos=&#8221; as shorthands for &#8220;seg=0&#215;60 file=&#8221; and &#8220;seg=0&#215;70 file=&#8221; respectively; use this to load FreeDOS&#8217;s kernel.sys, MS-DOS&#8217;s io.sys or PC-DOS&#8217;s ibmbio.sys.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://dipplum.com/2009/03/22/syslinux-load-windows/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Linux 下读写 FAT 属性信息</title>
		<link>http://dipplum.com/2009/03/22/linux-fat-attributes/</link>
		<comments>http://dipplum.com/2009/03/22/linux-fat-attributes/#comments</comments>
		<pubDate>Sat, 21 Mar 2009 17:09:41 +0000</pubDate>
		<dc:creator>li</dc:creator>
				<category><![CDATA[开源软件]]></category>
		<category><![CDATA[fat]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://dipplum.com/2009/03/22/linux-fat-attributes/</guid>
		<description><![CDATA[FAT 是 Windows 下的文件系统格式。每个 FAT 下的文件都包含 ARHIVE，SYSTEM，HIDDEN，READ-ONLY 等属性。而 Linux 下面缺乏读写 FAT 属性信息的命令，难以读出，或者设置这些 FAT 属性。 而实际上 Linux 内核的 vfat 模块包含相应的 ioctl 命令，可以修改文件的属性。在 &#60;linux/msdos_fs.h&#62; 定义如下： #define FAT_IOCTL_GET_ATTRIBUTES _IOR('r', 0x10, __u32) #define FAT_IOCTL_SET_ATTRIBUTES _IOW('r', 0x11, __u32) 上述两个 ioctl 命令接受一个 int 类型，该 int 值的不同 bit 位，按照下面的定义表示不同的文件属性： #define ATTR_RO 1 /* read-only */ #define ATTR_HIDDEN 2 /* hidden */ #define [...]]]></description>
			<content:encoded><![CDATA[<p>FAT 是 Windows 下的文件系统格式。每个 FAT 下的文件都包含 ARHIVE，SYSTEM，HIDDEN，READ-ONLY 等属性。而 Linux 下面缺乏读写 FAT 属性信息的命令，难以读出，或者设置这些 FAT 属性。</p>
<p>而实际上 Linux 内核的 vfat 模块包含相应的 ioctl 命令，可以修改文件的属性。在 &lt;linux/msdos_fs.h&gt; 定义如下：</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define FAT_IOCTL_GET_ATTRIBUTES	_IOR('r', 0x10, __u32)</span>
<span style="color: #339933;">#define FAT_IOCTL_SET_ATTRIBUTES	_IOW('r', 0x11, __u32)</span></pre></div></div>

<p>上述两个 ioctl 命令接受一个 int 类型，该 int 值的不同 bit 位，按照下面的定义表示不同的文件属性：</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define ATTR_RO		1	/* read-only */</span>
<span style="color: #339933;">#define ATTR_HIDDEN	2	/* hidden */</span>
<span style="color: #339933;">#define ATTR_SYS	4	/* system */</span>
<span style="color: #339933;">#define ATTR_VOLUME	8	/* volume label */</span>
<span style="color: #339933;">#define ATTR_DIR	16	/* directory */</span>
<span style="color: #339933;">#define ATTR_ARCH	32	/* archived */</span></pre></div></div>

<p>由于没有发现有命令可以利用上述 ioctl 接口，操作 FAT 文件的属性，我自己写了一个 attrib.py 文件，代码在最后。该命令接收两个参数，说明如下</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Usage: .<span style="color: #000000; font-weight: bold;">/</span>attrib.py dir_or_file dump<span style="color: #000000; font-weight: bold;">|</span>load</pre></div></div>

<p>下面的命令将 /mnt/fat 目录下所有子目录和文件的属性输出到文件 attributes.dump 文件中</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>attrib.py <span style="color: #000000; font-weight: bold;">/</span>mnt<span style="color: #000000; font-weight: bold;">/</span>fat dump <span style="color: #000000; font-weight: bold;">&gt;</span> attributes.dump</pre></div></div>

<p>./attrib.py dump 输出的部分内容如下：</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">A..SHR     .<span style="color: #000000; font-weight: bold;">/</span>ntldr
A..SHR     .<span style="color: #000000; font-weight: bold;">/</span>NTDETECT.COM
A..SHR     .<span style="color: #000000; font-weight: bold;">/</span>boot.ini
A..SHR     .<span style="color: #000000; font-weight: bold;">/</span>IO.SYS
A..SHR     .<span style="color: #000000; font-weight: bold;">/</span>MSDOS.SYS
.D..H.     .<span style="color: #000000; font-weight: bold;">/</span>WINDOWS<span style="color: #000000; font-weight: bold;">/</span>inf
.D.S.R     .<span style="color: #000000; font-weight: bold;">/</span>WINDOWS<span style="color: #000000; font-weight: bold;">/</span>Fonts
.D.SH.     .<span style="color: #000000; font-weight: bold;">/</span>WINDOWS<span style="color: #000000; font-weight: bold;">/</span>Installer
.D.S..     .<span style="color: #000000; font-weight: bold;">/</span>WINDOWS<span style="color: #000000; font-weight: bold;">/</span>Tasks
.D.S..     .<span style="color: #000000; font-weight: bold;">/</span>WINDOWS<span style="color: #000000; font-weight: bold;">/</span>Downloaded Program Files
...SH.     .<span style="color: #000000; font-weight: bold;">/</span>WINDOWS<span style="color: #000000; font-weight: bold;">/</span>winnt.bmp
...SH.     .<span style="color: #000000; font-weight: bold;">/</span>WINDOWS<span style="color: #000000; font-weight: bold;">/</span>winnt256.bmp
A...HR     .<span style="color: #000000; font-weight: bold;">/</span>WINDOWS<span style="color: #000000; font-weight: bold;">/</span>WindowsShell.Manifest
A..S..     .<span style="color: #000000; font-weight: bold;">/</span>WINDOWS<span style="color: #000000; font-weight: bold;">/</span>bootstat.dat</pre></div></div>

<p>下面的命令将根据 attributes.dump 文件中包含的属性信息，给 /mnt/fat 目录下所有子目录和文件设置相应的属性</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>attrib.py <span style="color: #000000; font-weight: bold;">/</span>mnt<span style="color: #000000; font-weight: bold;">/</span>fat load <span style="color: #000000; font-weight: bold;">&lt;</span> attributes.dump</pre></div></div>

<p>下面是 attrib.py 的源码：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">fcntl</span>
<span style="color: #ff7700;font-weight:bold;">import</span> posixpath
&nbsp;
ATTR_ARCH = 0x20
ATTR_DIR = 0x10
ATTR_VOLUME = 0x08
ATTR_SYSTEM = 0x04
ATTR_HIDDEN = 0x02
ATTR_RO = 0x01
&nbsp;
AttrBitName = <span style="color: black;">&#123;</span>
    ATTR_ARCH: <span style="color: #483d8b;">'A'</span>,
    ATTR_DIR: <span style="color: #483d8b;">'D'</span>,
    ATTR_VOLUME: <span style="color: #483d8b;">'V'</span>,
    ATTR_SYSTEM: <span style="color: #483d8b;">'S'</span>,
    ATTR_HIDDEN: <span style="color: #483d8b;">'H'</span>,
    ATTR_RO: <span style="color: #483d8b;">'R'</span>,
<span style="color: black;">&#125;</span>
&nbsp;
AttrBits = AttrBitName.<span style="color: black;">keys</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
AttrBits.<span style="color: black;">sort</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
AttrBits.<span style="color: black;">reverse</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
FAT_GETATTR_IOCTL = 0x80047210
FAT_SETATTR_IOCTL = 0x40047211
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> fat_getattr<span style="color: black;">&#40;</span>filename<span style="color: black;">&#41;</span>:
  fd = <span style="color: #dc143c;">os</span>.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span>filename, <span style="color: #dc143c;">os</span>.<span style="color: black;">O_RDONLY</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">try</span>:
    word = <span style="color: #dc143c;">fcntl</span>.<span style="color: black;">ioctl</span><span style="color: black;">&#40;</span>fd, FAT_GETATTR_IOCTL, <span style="color: #483d8b;">'    '</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">except</span>:
    <span style="color: #dc143c;">os</span>.<span style="color: black;">close</span><span style="color: black;">&#40;</span>fd<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">raise</span>
  <span style="color: #dc143c;">os</span>.<span style="color: black;">close</span><span style="color: black;">&#40;</span>fd<span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">ord</span><span style="color: black;">&#40;</span>word<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> fat_setattr<span style="color: black;">&#40;</span>filename, attr<span style="color: black;">&#41;</span>:
  fd = <span style="color: #dc143c;">os</span>.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span>filename, <span style="color: #dc143c;">os</span>.<span style="color: black;">O_RDONLY</span><span style="color: black;">&#41;</span>
  word = <span style="color: #483d8b;">'%s<span style="color: #000099; font-weight: bold;">\0</span><span style="color: #000099; font-weight: bold;">\0</span><span style="color: #000099; font-weight: bold;">\0</span>'</span> <span style="color: #66cc66;">%</span> <span style="color: #008000;">chr</span><span style="color: black;">&#40;</span>attr<span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">try</span>:
    <span style="color: #dc143c;">fcntl</span>.<span style="color: black;">ioctl</span><span style="color: black;">&#40;</span>fd, FAT_SETATTR_IOCTL, word<span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">except</span>:
    <span style="color: #dc143c;">os</span>.<span style="color: black;">close</span><span style="color: black;">&#40;</span>fd<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">raise</span>
  <span style="color: #dc143c;">os</span>.<span style="color: black;">close</span><span style="color: black;">&#40;</span>fd<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> fat_attrstr<span style="color: black;">&#40;</span>attr<span style="color: black;">&#41;</span>:
  bit_to_name = <span style="color: #ff7700;font-weight:bold;">lambda</span> bit: bit <span style="color: #66cc66;">&amp;</span>amp<span style="color: #66cc66;">;</span> attr <span style="color: #ff7700;font-weight:bold;">and</span> AttrBitName<span style="color: black;">&#91;</span>bit<span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #483d8b;">'.'</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">''</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #008000;">map</span><span style="color: black;">&#40;</span>bit_to_name, AttrBits<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> fat_strattr<span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#41;</span>:
  str_to_bin = <span style="color: #ff7700;font-weight:bold;">lambda</span> x: x<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> == x<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #483d8b;">'1'</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #483d8b;">'0'</span>
  bin = <span style="color: #483d8b;">''</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #008000;">map</span><span style="color: black;">&#40;</span>str_to_bin, <span style="color: #008000;">zip</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'ADVSHR'</span>, <span style="color: #008000;">str</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>bin, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> fat_dumpattr<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">for</span> cur, dirs, files <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">walk</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span><span style="color: black;">&#41;</span>:
    items = dirs + files
    <span style="color: #ff7700;font-weight:bold;">for</span> item <span style="color: #ff7700;font-weight:bold;">in</span> items:
      <span style="color: #ff7700;font-weight:bold;">try</span>:
        filename = <span style="color: #483d8b;">'%s/%s'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>cur, item<span style="color: black;">&#41;</span>
        attr = fat_getattr<span style="color: black;">&#40;</span>filename<span style="color: black;">&#41;</span>
        <span style="color: #008000;">str</span> = fat_attrstr<span style="color: black;">&#40;</span>attr<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'%s     %s'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #008000;">str</span>, filename<span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">Exception</span>, e:
        <span style="color: #ff7700;font-weight:bold;">print</span> e
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> fat_loadattr<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">for</span> line <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdin</span>.<span style="color: black;">readlines</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #008000;">str</span> = line<span style="color: black;">&#91;</span>:<span style="color: #ff4500;">6</span><span style="color: black;">&#93;</span>
    <span style="color: #008000;">file</span> = line<span style="color: black;">&#91;</span><span style="color: #ff4500;">6</span>:<span style="color: black;">&#93;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> posixpath.<span style="color: black;">exists</span><span style="color: black;">&#40;</span><span style="color: #008000;">file</span><span style="color: black;">&#41;</span>:
      newattr = fat_strattr<span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#41;</span>
      oldattr = fat_getattr<span style="color: black;">&#40;</span><span style="color: #008000;">file</span><span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">if</span> newattr == oldattr:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'ignore same attribute %s[0x%02x]: %s'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #008000;">str</span>, newattr, <span style="color: #008000;">file</span><span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">else</span>:
        oldstr = fat_attrstr<span style="color: black;">&#40;</span>oldattr<span style="color: black;">&#41;</span>
        fat_setattr<span style="color: black;">&#40;</span><span style="color: #008000;">file</span>, newattr<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'-%s[0x%02X] +%s[0x%02X] %s'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>oldstr, oldattr, <span style="color: #008000;">str</span>, newattr, <span style="color: #008000;">file</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
      <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'ignore not existing file: %s'</span> <span style="color: #66cc66;">%</span> <span style="color: #008000;">file</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> usage<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Usage: %s dir_or_file dump|load'</span> <span style="color: #66cc66;">%</span> <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
  exit<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">3</span>:
  usage<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
target_dir = <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
old_dir = <span style="color: #dc143c;">os</span>.<span style="color: black;">getcwd</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">try</span>:
  <span style="color: #dc143c;">os</span>.<span style="color: black;">chdir</span><span style="color: black;">&#40;</span>target_dir<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">except</span>:
  <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Bad directory: %s'</span> <span style="color: #66cc66;">%</span> target_dir
  usage<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
op = <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">if</span> op == <span style="color: #483d8b;">'dump'</span>:
  fat_dumpattr<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">elif</span> op == <span style="color: #483d8b;">'load'</span>:
  fat_loadattr<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">else</span>:
  <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Bad option: %s'</span> <span style="color: #66cc66;">%</span> op
  usage<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #dc143c;">os</span>.<span style="color: black;">chdir</span><span style="color: black;">&#40;</span>old_dir<span style="color: black;">&#41;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://dipplum.com/2009/03/22/linux-fat-attributes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Debian下网卡对802.1Q VLAN的支持</title>
		<link>http://dipplum.com/2008/10/14/debian-vlan/</link>
		<comments>http://dipplum.com/2008/10/14/debian-vlan/#comments</comments>
		<pubDate>Tue, 14 Oct 2008 10:39:13 +0000</pubDate>
		<dc:creator>li</dc:creator>
				<category><![CDATA[开源软件]]></category>
		<category><![CDATA[802.1q]]></category>
		<category><![CDATA[Debian]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[vlan]]></category>

		<guid isPermaLink="false">http://dipplum.com/2008/10/14/debian-vlan/</guid>
		<description><![CDATA[现在的交换机大部分都支持802.1Q VLAN了, 通过VLAN可以将一个交换机上的不同端口分隔成隔离的链路, 也可以将多个交换机上的端口连接到同一个链路. 有时候, 我们可能希望一台机器同时连入多个VLAN, 最好是通过一块网卡. 也就是说, 让一个网络端口同时属于多个VLAN. 要实现这样的功能, 需要做两个方面的配置: 交换机配置 首先要把该主机对应的交换机端口设置为混合端口, 只有混合端口可以同时加入多个VLAN, 下面是Huawei交换机的配置片段: interface Ethernet0/1 port link-type hybrid port hybrid vlan 10 20 tagged port hybrid pvid vlan 10 上述配置让交换机的1号端口成为混合端口, 并同时加入id号为10和20的VLAN, 主VLAN号是10. Debian主机配置 首先需要在Debian上安装vlan配置工具. apt-get install vlan 然后, 我们可以在Debian的网络配置文件中, 配置每个VLAN对应的网卡. 下面是/etc/network/interfaces文件的片段: auto eth0.10 iface eth0.10 inet static address 192.168.1.10 netmask 255.255.255.0 gateway 192.168.1.254 [...]]]></description>
			<content:encoded><![CDATA[<p>现在的交换机大部分都支持802.1Q VLAN了, 通过VLAN可以将一个交换机上的不同端口分隔成隔离的链路, 也可以将多个交换机上的端口连接到同一个链路.</p>
<p>有时候, 我们可能希望一台机器同时连入多个VLAN, 最好是通过一块网卡. 也就是说, 让一个网络端口同时属于多个VLAN. 要实现这样的功能, 需要做两个方面的配置:</p>
<p><strong>交换机配置</strong></p>
<p>首先要把该主机对应的交换机端口设置为混合端口, 只有混合端口可以同时加入多个VLAN, 下面是Huawei交换机的配置片段:</p>
<p><code>interface Ethernet0/1      <br />port link-type hybrid       <br />port hybrid vlan 10 20 tagged       <br />port hybrid pvid vlan 10</code></p>
<p>上述配置让交换机的1号端口成为混合端口, 并同时加入id号为10和20的VLAN, 主VLAN号是10.</p>
<p><strong>Debian主机配置</strong></p>
<p>首先需要在Debian上安装vlan配置工具.</p>
<p><code>apt-get install vlan</code></p>
<p>然后, 我们可以在Debian的网络配置文件中, 配置每个VLAN对应的网卡. 下面是/etc/network/interfaces文件的片段:</p>
<p><code>auto eth0.10      <br />iface eth0.10 inet static       <br />address 192.168.1.10       <br />netmask 255.255.255.0       <br />gateway 192.168.1.254       </p>
<p>iface eth0.20 inet static       <br />address 192.168.2.10&#160; <br />netmask 255.255.255.0</code></p>
<p>上述配置文件配置的主机, 分别有两块虚拟网卡eth0.10, eth0.20, 对应VLAN 10和VLAN 20所在的网络.</p>
<p>除了配置文件之外, 管理员还可以通过vconfig命令, 增加或者删除VLAN.</p>
]]></content:encoded>
			<wfw:commentRss>http://dipplum.com/2008/10/14/debian-vlan/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linux下设置主机的FQDN</title>
		<link>http://dipplum.com/2008/10/13/linux-fqdn/</link>
		<comments>http://dipplum.com/2008/10/13/linux-fqdn/#comments</comments>
		<pubDate>Mon, 13 Oct 2008 13:00:42 +0000</pubDate>
		<dc:creator>li</dc:creator>
				<category><![CDATA[开源软件]]></category>
		<category><![CDATA[fqdn]]></category>
		<category><![CDATA[hostname]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://dipplum.com/2008/10/13/linux-fqdn/</guid>
		<description><![CDATA[FQDN是Fully Qualified Domain Name的缩写, 含义是完整的域名. 例如, 一台机器主机名(hostname)是www, 域后缀(domain)是example.com, 那么该主机的FQDN应该是www.example.com. 题外话, 其实FQDN最后是以&#34;.&#34;来结尾的, 但是大部分的应用和服务器都允许忽略最后这个点. Linux允许用户通过hostname命令查看并设置主机名. 用户也可以通过hostname -f命令得到该主机的FQDN. 但是, 却没有直接设置FQDN的命令. 实际上, 设置Linux的FQDN可以通过两种方法实现. 第一种, /etc/hostname + /etc/hosts文件组合 首先在/etc/hostname文件中设置主机名, 假设是 www 然后在/etc/hosts文件中增加一行主机记录, 第一个字段是该主机的IP地址, 第二个字段是你希望设置的FQDN, 最后是刚刚设置的主机名, 如下 A.B.C.D www.example.com www 设置好之后, 通过hostname -F /etc/hostname更新主机名. 这时, 通过hostname -f看到的FQDN就应该是: www.example.com 第二种, /etc/hostname + /etc/resolv.conf组合 如果Linux不能在/etc/hosts文件中找到hostname对应的记录, 就会试图从resolv.conf文件中得到主机的域名后缀(domain name). 例如, 这样的/etc/resolv.conf文件 domain example.com search example.com [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/FQDN">FQDN</a>是Fully Qualified Domain Name的缩写, 含义是<strong>完整的域名</strong>. 例如, 一台机器主机名(hostname)是www, 域后缀(domain)是example.com, 那么该主机的FQDN应该是<a href="http://www.example.com">www.example.com</a>. </p>
<p>题外话, 其实FQDN最后是以&quot;.&quot;来结尾的, 但是大部分的应用和服务器都允许忽略最后这个点.</p>
<p>Linux允许用户通过<a href="http://linux.die.net/man/1/hostname">hostname命令</a>查看并设置主机名. 用户也可以通过hostname -f命令得到该主机的FQDN. 但是, 却没有直接设置FQDN的命令.</p>
<p>实际上, 设置Linux的FQDN可以通过两种方法实现.</p>
<p><strong>第一种, /etc/hostname + </strong><a href="http://linux.die.net/man/5/hosts"><strong>/etc/hosts</strong></a><strong>文件组合</strong></p>
<p>首先在/etc/hostname文件中设置主机名, 假设是</p>
<blockquote><p><code>www</code> </p>
</blockquote>
<p>然后在/etc/hosts文件中增加一行主机记录, 第一个字段是该主机的IP地址, 第二个字段是你希望设置的FQDN, 最后是刚刚设置的主机名, 如下</p>
<blockquote><p><code>A.B.C.D www.example.com www</code> </p>
</blockquote>
<p>设置好之后, 通过hostname -F /etc/hostname更新主机名. 这时, 通过hostname -f看到的FQDN就应该是: www.example.com</p>
<p><strong>第二种, /etc/hostname + <a href="http://linux.die.net/man/5/resolv.conf">/etc/resolv.conf</a>组合</strong></p>
<p>如果Linux不能在/etc/hosts文件中找到hostname对应的记录, 就会试图从resolv.conf文件中得到主机的域名后缀(domain name). 例如, 这样的/etc/resolv.conf文件</p>
<blockquote><p><code>domain example.com       <br />search example.com       <br />nameserver W.X.Y.Z</code>   </p>
</blockquote>
<p>根据配置文件中的domain后缀example.com, 加上主机名www, Linux会试图自动拼接成一个候选FQDN: <a href="http://www.example.com">www.example.com</a>. 但这个候选FQDN还需要进一步验证.</p>
<p>Linux通过DNS服务器W.X.Y.Z解析候选FQDN, 如果解析失败, 生成FQDN过程就失败了. 如果解析成功, 则会返回www.example.com这个域名的正式名称. 也就是说, 如果在DNS服务器的记录中, www.example.com这个域名是指向server.example.com的CNAME记录的话, 而server.example.com才是A记录. 返回的FQDN就是server.example.com, 而不是<a href="http://www.example.com">www.example.com</a>.</p>
<p>以上就是Linux下设置FQDN的方法.</p>
]]></content:encoded>
			<wfw:commentRss>http://dipplum.com/2008/10/13/linux-fqdn/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

