工作,学习,生活,这里将会有一些记录. 备用域名:http://meisw.wdlinux.cn 注册 | 登陆
浏览模式: 标准 | 列表2010年12月的文章

刷新,按钮连接

<input name="reload" type="button" id="reload" onclick=location.reload() value="刷新" />
      &nbsp;&nbsp;
      <input name="release" type="button" id="release" onclick="location='<?=$PHP_SELF;?>?act=release'" value="跳转" />&nbsp;&nbsp;&nbsp;      </label>

Sed简明速查手册

Sed(a stream editor)
4个空间:input stream, pattern buffer, output stream和hold buffer
基本操作过程是:
(1).将input stream的当前行放入pattern buffer,然后input stream的指针指向下一行;
(2).对pattern buffer中的行进行处理;
(3).将2的处理结果放入output stream.然后循环这个过程.

hold buffer是另一个空间,可以通过命令和pattern buffer进行交互.

sed命令介绍

1. 基本命令---"替换": s
  1.1 基本用法
  e.g. sed 's/day/night/' <old >new
  该例子将文件old中的每一行第一次出现的day替换成night,将结果输出到文件new
  s            "替换"命令
  /../../      分割符(Delimiter)
  day          搜索字符串
  night        替换字符串

  其实,分割符"/"可以用别的符号代替,比如",", "|"等.
  e.g. sed 's/\/usr\/local\/bin/\/common\/bin/'<old >new
  等价于sed 's_/usr/local/bin_/common/bin_' <old >new
  显然,此时用"_"作分割符比"/"好得多.


  1.2 用&表示匹配的字符串

  有时可能会想在匹配到的字符串周围或附近加上一些字符.
  e.g. sed 's/abc/(abc)/' <old >new
  该例子在找到的abc前后加上括号.
  该例子还可以写成 sed 's/abc/(&)/' <old >new
  下面是更复杂的例子:
  sed 's/[a-z]*/(&)/' <old >new
  sed 's/[0-9]*/& &/' <old >new

  1.3 用\1, \2, ..., \9来表示匹配的字符串

  e.g. sed 's/\([a-z]*\)[0-9]*/\1/' <old >new
  本例中\1就是指前面的\([a-z]*\)

  sed 's/\([a-z]*\) \([0-9]*\)/\2 \1/' <old >new
  本例中\2和\1分别代表前面的\([0-9]*\)和\([a-z]*\)

  \1, \2, ..., \9也可以出现在搜索字符串中
  e.g. sed 's/\([a-z]*\) \1/\1/' <old >new
  本例可以去除重复的由字母组成的词

  1.4 "替换"选项
      1.4.1 /g 替换所有的
      sed默认只替换搜索字符串的第一次出现,利用/g可以替换搜索字符串所有
      出现的地方.例如,
      sed 's/\([^ ]*\)/(&)/g' <old >new

      1.4.2 用/1, /2, ...来表明替换哪一次出现
      e.g. sed 's/[^ ]*//2' <old >new
      可以从/1用到/512

      1.4.3 /p print选项
      当sed命令有-n选项时,该命令没用输出.
      -n配合/p选项后,如果该行确实发生了替换,则输出该行,否则不输出.

      1.4.4 /w filename 写到文件filename中
      e.g. sed 's/\([0-9]*\) \([a-z]*\)/\2/w new' <old
      该例子把输出放入文件new中

  1.5 替换和插入换行符号
  替换 (echo a;echo x;echo y) | sed '/x$/ {
       N
       s:x\n:x:
       }'

  插入
       (echo a;echo x;echo y) | sed 's:x:X\
       :'



2. 只对特定行的处理

  2.1 通过行号限定
  sed '3 s/[0-9][0-9]*//' <old >new 只处理第3行

  sed '1,100 s/A/a/' <old >new 只处理1到100行

  sed '101,$ s/A/a/' <old >new 处理101到文件的最后一行

  sed '101,$ !s/A/a/' <old >new 这里!表示只对1到100行进行替换,!的作用是取反


  2.2 通过正规表达式限定
  sed '/start/,/stop/ s/#.*//' <old >new
  本例中,sed先找到有start的行作为开始,找到最近的有stop的行作为结束,对之
  间的行进行操作.
  重复上述过程,直到文件结束

  下面这个例子是行号和正规表达式配合来限定
  sed '1,/start/ s/#.*//' <old >new 对第1行到含有start的行进行处理


3. 其他的简单命令
  3.1 删除命令 d
  sed '11,$ d' <old >new 删除从11行到文件末尾
  sed '/^#/ d' <old >new 删除所有以#开始的行

  3.2 print命令 p (注意与s命令的/p选项的区别)
  sed 'p' <old    每一行将会被输出两次
  sed -n 'p' <old 每一行将会输出一次(-n屏蔽掉一次)
  sed '/^$/ p' <old 只对空行输出两次,其他只输出一次
  sed -n '1,10 p' <old 输出前10行
  sed -n '/match/ p' <old 输出含有match的行

  3.3 quit命令 q
  sed '11 q'<old    输出前10行(在第11行退出)
  注意:q命令不能接收多行,例如
  sed '2,5 q'<old 是不正确的

  3.4 写入文件命令 w filename(注意与s命令的/w选项的区别)
  把某些行写入文件filename
  sed -n '/^[0-9]*[02468]/ w even' <old    将以偶数开始的行写入文件even

  3.5 输出行号命令 =
  sed -n '/PATTERN/ =' <old 遇到含有PATTERN的行时,同时输出行号

  3.6 追加,改变,插入新行
  追加命令 a
  #!/bin/sh
  sed '
  /WORD/ a\
  Add this line after every line with WORD
  '

  改变命令 c
  #!/bin/sh
  sed '
  /WORD/ c\
  Replace the current line with the line
  '

  插入命令 i
  #!/bin/sh
  sed '
  /WORD/ i\
  Add this line before every line with WORD
  '

  3.7 变换命令 y
  sed 'y/abcdef/ABCDEF/' <old  该例将字符abcdef分别变成大写

  3.8 将本行的控制符也显示出来的命令 l
  sed '1,10 l' <old

  3.9 d命令和D命令
  d命令删除pattern buffer中的内容进入下一次操作循环
  D命令删除pattern buffer中第一个换行符之前的内容进入下一次操作循环,如
  果pattern buffer中还有内容,则不用从input stream中读入

  3.10 p命令和P命令
  p命令输出pattern buffer中的内容
  P命令输出pattern buffer中第一个换行符之前的内容

  3.11 n命令和N命令
  n命令把下一行读入pattern buffer中(如果没用-n选项,将原来行输出)
  N命令把下一行追加到pattern buffer中

 3.12 流程控制命令
  b label命令:在指定行跳到label
  t label命令:如果在某行发生了替换,跳到label
  T label命令:如果在某行没有发生了替换,跳到label

4. 调用sed时的参数

  4.1 -e script 执行script这个脚本
  e.g. sed -e 's/a/A/' -e 's/b/B/' <old >new
  对每一行分别执行's/a/A/'和 's/b/B/'

  4.2 -n     禁止输出
  这里的-n与前面的/p配合,可以只输出被修改了的行.

  4.3 -f scriptname 把scriptname文件中的sed命令加入本次sed的调用中
  e.g. sed -f sedscript <old >new

  sedscript的内容可能是这样的:
  # sed comment - This script changes lower case vowels to upper case
  s/a/A/g
  s/e/E/g
  s/i/I/g
  s/o/O/g
  s/u/U/g

5. Hold Buffer

  x命令:将pattern buffer放入hold buffer,而将hold buffer的内容输出,pattern
  buffer的内容变成下一行
  h命令:将pattern buffer放入hold buffer,并将pattern buffer的内容输出,
  pattern buffer的内容变成下一行
  H命令:将pattern buffer追加到hold buffer
  g和G命令:g用hold buffer的内容替换pattern buffer的内容,而G将hold buffer内
  容追加到pattern buffer

参考: http://www.grymoire.com/Unix/Sed.html

sed 11

sed -i '1,/PermitRootLogin/{/PermitRootLogin/c\
PermitRootLogin yes
}' /etc/ssh/sshd_config

只替换一次

 

sed -i '/^str/s/^/add/' t.sh

查找以str开头的行,并在行首插入add

 

sed -i '/libphp5/s/^/#/' /www/wdlinux/apache/conf/httpd.conf

squid.conf

cache_effective_user squid
cache_effective_group squid
tcp_recv_bufsize 65535 bytes

icp_port 0
visible_hostname localhost
http_port 80 vhost vport

acl local src 127.0.0.1
acl Manager proto cache_object
http_access allow Manager local
http_access deny Manager
acl alls src all
http_access allow alls

maximum_object_size 1024000 KB
maximum_object_size_in_memory 1024 KB
cache_mem 20480 MB
cache_swap_low  80
cache_swap_high 90

ipcache_size 1024
ipcache_low 90
ipcache_high 95
fqdncache_size 1024

connect_timeout 1 minute
peer_connect_timeout 30 seconds
request_timeout 2 minutes
persistent_request_timeout 30 seconds


via off
reply_header_access Via deny all
reply_header_access Server deny all

-------------------------------------

cache_peer 192.168.0.10 parent 80 0 no-query originserver name=a
cache_peer 192.168.0.11 parent 80 0 no-query originserver name=b

cache_peer_domain a www.a.com
cache_peer_domain b www.b.com

acl all src 0.0.0.0/0.0.0.0
http_access allow all

cache_peer_access a allow all
cache_peer_access b allow all

vim 加密文本文件

参考vim中的介绍:

要开始编辑一个需要加密的新文件,可以用 “-x” 参数来启动 Vim。例如:

vim -x exam.txt

Vim 提示你确定一个密码,用于为文件加密和解密:

Enter encryption key:

现在仔细键入那个密码。你所键入的字符将为星号代替,因此你看不到。为了避免由于
打字错误引起麻烦,Vim 要求你再输入一次密码:

Enter same key again:

现在你可以像平时一样编辑这个文件并把你所有的秘密放进去。当你编完文件要退出 Vim
时,这个文件就被加密存盘了。
当你用 Vim 编辑这个文件时,它就会要求你再输入那同一个密码。你不需要用 “-x”
参数。你也可以用普通的 “:edit” 命令编辑加密的文件。Vim 给这个文件加上一句标记,
据以识别那是经过加密的文件。
如果你试图用另一个程序来阅读这个文件,你将读到一堆乱码。如果你用 Vim 来编辑
这个文件,但输入了错误的密码,你也只能得到乱码。Vim 并不具备检验密码正确性的机
理 (这一点使得破译密码更为困难)。

开 / 关 加 密

要给一个文件撤除加密,设定 ‘key’ 选项为空字符串:

:set key=

你下次把这个文件存盘时,存盘的文件就是未经加密的。
设定 ‘key’ 选项来启用加密是个坏主意。因为密码会被清清楚楚地显示在屏幕上。任
何人都可以偷看到你的密码。
为了避免这样的问题,创造了 “:X” 命令。它会像 “-x” 参数向你索取一个密码:

:X
Enter encryption key: ******
Enter same key again: ******

加 密 的 局 限 性

Vim 采用的加密算法有弱点的。它对于防止那种偷窥者是绰绰有余了,但不足以防止一
个手上有大量时间的密码专家。而且, 你应该知道交换文件是不加密的;所以当你在编辑
文件时,拥有超级用户权限的人能够读取未经加密的文件文本。
不让人们读到你的交换文件的一个方法就是不使用交换文件。如果在命令行上用了 -n
参数,交换文件就不会被创建了 (Vim 把所有的东西都存放在内存里)。例如,要编辑经
过加密的文件 “file.txt”,但不用交换文件,请用下面的命令:

vim -x -n file.txt

如果你已在经编辑这个文件了,那么交换文件 swapfile 可以用下面的命令禁止:

:setlocal noswapfile

由于没了交换文件,文件复原就不可能了。为了避免失去编辑的成果,要比平时更勤快地
存盘你的文件。

文件在内存中以明文形式存在。因此任何具备权限的人都能进入编辑者的内存浏览,从而,
发现这个文件的内容。
如果你使用信息文件 viminfo,别忘了文本寄存器的内容也是明明白白写在其中的。
如果你真的要保证一个文件内容的安全,那么,你必须永远只在一个不联网的可携式
计算机上编辑这个文件,使用优良的加密工具,并且在不用时,把你的计算机锁进一个
大保险箱。

============

#!/bin/bash
# Encrypt file with vim

if (test $# -lt 2) then
echo Usage: decrypt password filename
else
vim -e -s -c “:set key=$1″ -c ‘:wq’ $2
echo “$2 encrypted.”
fi

xxd使用手册


测试:
 
    以 I386/RedHat6.2 为测试环境
 
目录:
 
    ★ 命令行参数简介
    ★ xxd使用中的注意事项
    ★ xxd使用举例
       . 从偏移0x10开始显示,也就是缺省情况下的第一行不显示
       . 显示倒数0x30个字节(缺省情况下倒数3行)
       . 显示120字节,连续显示,每行20字节
       . 显示120字节,每行12字节
       . 显示0x6c偏移开始的12个字节
       . 创建一个65537字节大小的文件,除了最后一个字节是'A',其余都是0x00
       . 使用autoskip选项
       . 创建只包含一个'A'字节的文件
       . 复制输入文件到输出文件,且给输出文件前部增加100字节的0x00
       . 二进制文件打补丁
       . Read single characters from a serial line
 
★ 命令行参数简介
 
xxd -h
xxd [options] [infile [outfile]]
xxd -r [options] [infile [outfile]]
 
xxd可以创建给定文件或标准输入的hexdump,也可以还原一个hexdump成以前的二进
制文件。如果没有指定文件或指定文件名为-,则采用标准输入。如果没有指定输出
文件或指定文件名为-,则采用标准输出。
 
-a
    toggle autoskip: A single '*' replaces nul-lines. Default off.
 
-b
    采用2进制显示,而不是默认的16进制
 
xxd -l 32 -b scz.sh
0000000: 00100011 00100001 00100000 00101111 01100010 01101001  #! /bi
0000006: 01101110 00101111 01110011 01101000 00001010 00100011  n/sh.#
000000c: 00001010 00100011 00100000 11001000 10100001 11010110  .# ...
0000012: 11110111 11001001 11101000 10110001 10111000 10111010  ......
0000018: 11000101 00001010 00100011 00100000 01100011 01100001  ..# ca
000001e: 01110100 00100000                                      t 
 
-c cols
    默认16,-i指定时采用12,-p指定时采用30,-b指定时采用6,最大256
    -c8、-c 8、-c 010是等价的,其他选项类似。
 
xxd -c 8 -l 32 -g 1 scz.sh
0000000: 23 21 20 2f 62 69 6e 2f  #! /bin/
0000008: 73 68 0a 23 0a 23 20 c8  sh.#.# .
0000010: a1 d6 f7 c9 e8 b1 b8 ba  ........
0000018: c5 0a 23 20 63 61 74 20  ..# cat 
 
-E
    不用ASCII显示,而采用EBCDIC编码,不影响16进制显示
 
xxd -E -l 32 -g 1 scz.sh
0000000: 23 21 20 2f 62 69 6e 2f 73 68 0a 23 0a 23 20 c8  ......>........H
0000010: a1 d6 f7 c9 e8 b1 b8 ba c5 0a 23 20 63 61 74 20  .O7IY...E..../..
 
-g bytes
    比较下面三种显示方式,缺省bytes为2,指定-b时采用1。
 
xxd -l 32 -g 0 scz.sh
0000000: 2321202f62696e2f73680a230a2320c8  #! /bin/sh.#.# .
0000010: a1d6f7c9e8b1b8bac50a232063617420  ..........# cat 
 
xxd -l 32 -g 1 scz.sh
0000000: 23 21 20 2f 62 69 6e 2f 73 68 0a 23 0a 23 20 c8  #! /bin/sh.#.# .
0000010: a1 d6 f7 c9 e8 b1 b8 ba c5 0a 23 20 63 61 74 20  ..........# cat 
 
xxd -l 32 -g 2 scz.sh
0000000: 2321 202f 6269 6e2f 7368 0a23 0a23 20c8  #! /bin/sh.#.# .
0000010: a1d6 f7c9 e8b1 b8ba c50a 2320 6361 7420  ..........# cat 
 
-h 
    显示帮助信息
 
-i 
    以C语言数组形式显示,如果输入来自stdin,则有所区别
 
unsigned char scz_sh[] = {
  0x23, 0x21, 0x20, 0x2f, 0x62, 0x69, 0x6e, 0x2f, 0x73, 0x68, 0x0a, 0x23,
  0x0a, 0x23, 0x20, 0xc8, 0xa1, 0xd6, 0xf7, 0xc9, 0xe8, 0xb1, 0xb8, 0xba,
  0xc5, 0x0a, 0x23, 0x20, 0x63, 0x61, 0x74, 0x20
};
unsigned int scz_sh_len = 32;
 
注意区分下面两条命令执行效果的不同
 
xxd -l 12 -i < scz.sh
  0x23, 0x21, 0x20, 0x2f, 0x62, 0x69, 0x6e, 0x2f, 0x73, 0x68, 0x0a, 0x23
 
xxd -l 12 -i scz.sh
unsigned char scz_sh[] = {
  0x23, 0x21, 0x20, 0x2f, 0x62, 0x69, 0x6e, 0x2f, 0x73, 0x68, 0x0a, 0x23
};
unsigned int scz_sh_len = 12;
 
-l len
    只显示<len>这么多字节
 
-p
    以连续的16进制表示显示,比较下面的不同显示效果
 
xxd -u -l 32 scz.sh
0000000: 2321 202F 6269 6E2F 7368 0A23 0A23 20C8  #! /bin/sh.#.# .
0000010: A1D6 F7C9 E8B1 B8BA C50A 2320 6361 7420  ..........# cat 
 
xxd -u -l 32 -p scz.sh
2321202F62696E2F73680A230A2320C8A1D6F7C9E8B1B8BAC50A23206361
7420
 
-r
    reverse operation: convert (or patch) hexdump into binary. 
    If not writing to stdout, xxd writes into its output file without 
    truncating it. Use the combination -r -p to read plain hexadecimal 
    dumps without line number information and without a particular
    column layout. Additional Whitespace and line-breaks are allowed 
    anywhere.
 
    这个选项要用的话,一定要提前测试
 
-seek offset
    When used after -r : revert with <offset> added to file positions 
    found in hexdump.
 
-s [+][-]seek
    start at <seek> bytes abs. (or rel.) infile offset.  + indicates 
    that the seek is relative to the current stdin file position 
    (meaningless when not reading from stdin). - indicates that the seek 
    should be that many characters from the end of the input (or if 
    combined with +  :  before the current stdin file position). 
    Without -s option, xxd starts at the current file position.
 
-u
    采用大写16进制字母显示,缺省采用小写16进制字母
 
-v
    显示版本信息
 
★ xxd使用中的注意事项
 
xxd -r has some builtin magic while evaluating line number information.  If the  ouput  file
is  seekable,  then  the  linenumbers at the start of each hexdump line may be out of order,
lines may be missing, or overlapping. In these cases xxd will lseek(2) to the next position.
If  the  output  file  is not seekable, only gaps are allowed, which will be filled by null-
bytes.
 
xxd -r never generates parse errors. Garbage is silently skipped.
 
When editing hexdumps, please note that xxd -r skips everything  on  the  input  line  after
reading enough columns of hexadecimal data (see option -c). This also means, that changes to
the printable  ascii  (or  ebcdic)  columns  are  always  ignored.  Reverting  a  plain  (or
postscript)  style  hexdump with xxd -r -p does not depend on the correct number of columns.
Here an thing that looks like a pair of hex-digits is interpreted.
 
xxd -s +seek may be different from xxd -s seek , as lseek(2) is used to "rewind"  input.   A
'+'  makes a difference if the input source is stdin, and if stdin's file position is not at
the start of the file by the time xxd is started and given its input.  The  following  
examples may help to clarify (or further confuse!)...
 
Rewind  stdin before reading; needed because the `cat' has already read to the end of stdin.
% sh -c 'cat > plain_copy; xxd -s 0 > hex_copy' < file
 
Hexdump from file position 0x480 (=1024+128) onwards.  The `+' sign means "relative  to  the
current position", thus the `128' adds to the 1k where dd left off.
% sh -c 'dd of=plain_snippet bs=1k count=1; xxd -s +128 > hex_snippet' < file
 
Hexdump from file position 0x100 ( = 1024-768) on.
% sh -c 'dd of=plain_snippet bs=1k count=1; xxd -s +-768 > hex_snippet' < file
 
However,  this  is a rare situation and the use of `+' is rarely needed.  the author prefers
to monitor the effect of xxd with strace(1) or truss(1), whenever -s is used.
 
★ xxd使用举例
 
.. 从偏移0x10开始显示,也就是缺省情况下的第一行不显示
  xxd -s 0x10 scz.sh
 
.. 显示倒数0x30个字节(缺省情况下倒数3行)
  xxd -s -0x30 -g 1 scz.sh
 
.. 显示120字节,连续显示,每行20字节
  xxd -l 120 -p -c 20 scz.sh
 
.. 显示120字节,每行12字节
  xxd -l 120 -c 12 -g 1 scz.sh
 
.. 显示0x6c偏移开始的12个字节
  xxd -l 12 -c 12 -g 1 -s 0x6c scz.sh
 
.. 创建一个65537字节大小的文件,除了最后一个字节是'A',其余都是0x00
  echo 10000: 41 | xxd -r > scz.txt
 
  od -A x -t x1 scz.txt
  000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  *
  010000 41
  010001
 
.. 使用autoskip选项
 
  xxd -a scz.txt
  0000000: 0000 0000 0000 0000 0000 0000 0000 0000  ................
  *
  0010000: 41                                       A
 
  注意,缺省情况下autoskip功能是关闭的,但od看样子缺省就是打开的。
 
.. 创建只包含一个'A'字节的文件
  echo '010000: 41' | xxd -r -s -0x10000 > scz_1
 
  注意对比这些效果
  echo '010: 41 42 43 44 45 46' | xxd -r -s 0 > scz_1
 
  echo '010: 41 42 43 44 45 46' | xxd -r -s -0x10 > scz_1
  等价于
  echo '0: 41 42 43 44 45 46' | xxd -r -s 0 > scz_1
 
  echo '010: 41 42 43 44 45 46' | xxd -r -s -0x12 > scz_1
  执行的时候报错,xxd: sorry, cannot seek backwards.
 
  echo '010: 41 42 43 44 45 46' | xxd -r -s 0x10 > scz_1
  等价于
  echo '020: 41 42 43 44 45 46' | xxd -r -s 0 > scz_1
 
.. 复制输入文件到输出文件,且给输出文件前部增加100字节的0x00
  xxd scz_1 | xxd -r -s 100 > scz_2
 
  od -A x -t x1 scz_2
  000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  *
  000060 00 00 00 00 41
  000065
 
.. 二进制文件打补丁
  echo '0: 41 42 43 44 45 46' | xxd -r -s 0 > scz_1
  od -A x -t x1 scz_1
  echo '4: 47 48' | xxd -r - scz_1
  od -A x -t x1 scz_1
 
.. Use xxd as a filter within an editor such as vim(1) to hexdump a region 
  marked between `a' and `z'.
  :'a,'z!xxd
 
.. Use  xxd  as  a  filter  within  an editor such as vim(1) to recover a 
  binary hexdump marked between `a' and `z'.
  :'a,'z!xxd -r
 
.. Use xxd as a filter within an editor such as vim(1) to recover one line
  of a hexdump. Move the cursor over the line and type:
  !!xxd -r
 
.. Read single characters from a serial line
  xxd -c1 < /dev/term/b &
  stty < /dev/term/b -echo -opost -isig -icanon min 1
  echo -n foo > /dev/term/b

虚拟化之Openvz 配置初步

OpenVZ是基于Linux内核和系统的操作系统级虚拟化技术,OpenVZ允许物理服务器运行多个操作系统,被称虚拟专用服务器(VPS,Virtual Private Server)或虚拟环境(VE, Virtual Environment),更详细的信息可以参照:http://zh.wikipedia.org/zh/OpenVZ;下面来简要的介绍下openvz的安装和虚拟机的部署:

[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# wget http://download.openvz.org/openvz.repo
[root@localhost yum.repos.d]# yum -y install ovzkernel.x86_64  ovzkernel-devel.x86_64 vzctl.x86_64  vzquota.x86_64   //安装openvz内核和客户端工具

[root@localhost ~]# grep -v '^#' /etc/grub.conf   //确认下次启动时以openvz核启动
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux Server (2.6.18-194.3.1.el5.028stab069.5)
root (hd0,0)
kernel /vmlinuz-2.6.18-194.3.1.el5.028stab069.5 ro root=LABEL=/1 rhgb quiet
initrd /initrd-2.6.18-194.3.1.el5.028stab069.5.img
title Red Hat Enterprise Linux Server (2.6.18-164.el5)
root (hd0,0)
kernel /vmlinuz-2.6.18-164.el5 ro root=LABEL=/1 rhgb quiet
initrd /initrd-2.6.18-164.el5.img


[root@localhost ~]# uname  -r          //重启并确定当前内核为openvz内核
2.6.18-194.3.1.el5.028stab069.5


[root@localhost ~]# service vz status  //启动vz服务
OpenVZ is running...

[root@localhost ~]# ls /vz/ 
dump  lock  private  root  template

openvz默认的工作目录为/vz,如何修改这个目录还有待研究,dump目录应该是存放虚拟机备份文件的位置,private和root都是存放当前虚拟机系统文件的目录,template是存放虚拟机模板的目录,lock目录何用,也需要继续研究…

openvz支持的guest操作系统版本模板下载地址:
http://download.openvz.org/template/precreated/
[root@localhost ~]# ls /vz/template/cache/*.gz     //下载相应的模板并移动的这个目录
/vz/template/cache/centos-5-i386-afull.tar.gz
/vz/template/cache/centos-5-i386-hostinabox576.tar.gz

[root@localhost ~]# vzctl create 101 --ostemplate centos-5-i386-afull   //创建虚拟机,101为ID号,从100开始,需要注意的是只能写模板文件的名称去掉后缀(.tar.gz)的部分,否则会报错
Creating container private area (centos-5-i386-afull)
Performing postcreate actions
Container private area was created

[root@localhost ~]# vzctl set 101 --ipadd 192.168.50.249 --save       //设定虚拟机的ip参数,并保存
Adding IP address(es): 192.168.50.249
Saved parameters for CT 101

[root@localhost ~]# vzctl start 101     //启动虚拟机101
Starting container ...
Container is mounted
Adding IP address(es): 192.168.50.249
Setting CPU units: 1000
Configure meminfo: 49152
Container start in progress...

[root@localhost ~]# vzctl enter 101   //进入虚拟机
entered into CT 101

[root@localhost /]# ifconfig |grep 'inet addr'    //查看虚拟机网卡信息
inet addr:127.0.0.1  Mask:255.0.0.0
inet addr:127.0.0.1  P-t-P:127.0.0.1  Bcast:0.0.0.0  Mask:255.255.255.255
inet addr:192.168.50.249  P-t-P:192.168.50.249  Bcast:192.168.50.249  Mask:255.255.255.255

[root@localhost /]# df -h     //查看虚拟机磁盘信息,可以看到,其实所有的虚拟机都是共享/vz目录,因而在生产环境中使用openvz的时候,最好要独立划出/vz分区,并做lvm
Filesystem            Size  Used Avail Use% Mounted on
/dev/simfs             23G  501M   21G   3% /
none                   96M  4.0K   96M   1% /dev

[root@localhost /]# free      //同样,共享系统内存和cpu
total       used       free     shared    buffers     cached
Mem:        196608      15568     181040          0          0          0
-/+ buffers/cache:      15568     181040
Swap:            0          0          0

[root@localhost /]# cat /proc/cpuinfo  |less
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 15
model name      : Intel(R) Xeon(R) CPU            5110  @ 1.60GHz
stepping        : 6
………………………………………………………………………………………

[root@localhost /]# exit     //退出虚拟机101
exited from CT 101


[root@localhost ~]# cat /vz/root/101/etc/sysconfig/network-scripts/ifcfg-venet0:1  //也可以直接编辑/vz/root下的文件修改虚拟机相关参数
DEVICE=venet0:1
ONBOOT=yes
IPADDR=192.168.50.249
NETMASK=255.255.255.255

[root@localhost ~]# ls /vz/root/
101  102  103  104  105  106
[root@localhost ~]# ls /vz/private/
101  102  103  104  105  106


总结:
1:openvz是一款独树一帜的虚拟机,同传统的虚拟机,例如xen,kvm,MS hypev-v,VMWARE等相比,安装虚拟机操作系统变的相当的容易,且快速,这些传统的虚拟机安装都需要和真实机安装操作系统的方式一样(这里排除了使用克隆和dd的方式),openvz则使用模板这种方式,从安装上变的高效;

2:默认openvz有一张venet0网卡,虚拟机的网段和物理机的网段通过这个网卡桥接在一起,并没有提供其他的虚拟网卡,这个也有待进一步研究;

3:另外,openvz支持多种方式的管理,例如:hypervm,vtonf等...

4:对硬件没有特殊的要求,kvm需要cpu vt技术的支持,xen装windows也需要vt支持

openvz 安装配置

系统环境:ubuntu server 9.04

1、openvz简介
OpenVZ是基于Linux内核和作业系统的操作系统级虚拟化技术。OpenVZ允许物理服务器运行多个操作系统,被称虚拟专用服务器(VPS,Virtual Private Server)或虚拟环境(VE, Virtual Environment)。
与VMware这种虚拟机和Xen这种半虚拟化技术相比,OpenVZ的host OS和guest OS都必需是Linux(虽然在不同的虚拟环境里可以用不同的Linux发行版)。但是,OpenVZ声称这样做有性能上的优势。根据OpenVZ网站的说法,使用OpenVZ与使用独立的服务器相比,性能只会有1-3%的损失。
OpenVZ是SWsoft, Inc.公司开发的专有软件Virtuozzo的基础。OpenVZ的授权为GPLv2。

2、安装前准备
删除apparmor(apparmor是一个安全软件,但是与openvz内核配合不太好)

Java代码
sudo /etc/init.d/apparmor stop   
sudo update-rc.d -f apparmor remove   
sudo apt-get remove apparmor apparmor-utils 
sudo /etc/init.d/apparmor stop
sudo update-rc.d -f apparmor remove
sudo apt-get remove apparmor apparmor-utils
更新源列表sources.list,找到有linux-openvz的源。可以用命令查看是否有linux-openvz:

Java代码
sudo apt-get update   
sudo apt-cache search openvz 
sudo apt-get update
sudo apt-cache search openvz

3、安装openvz及基本配置
linux-openvz:有openvz的linux内核补丁
vztcl:openvz管理工具
vzquota:openvz限额管理工具

Java代码
sudo apt-get install linux-openvz vzctl vzquota 
sudo apt-get install linux-openvz vzctl vzquota
内核参数调整:

Java代码
sudo vi /etc/sysctl.conf   
#确认有如下几项   
net.ipv4.conf.all.rp_filter=1  
net.ipv4.icmp_echo_ignore_broadcasts=1  
net.ipv4.conf.default.forwarding=1  
net.ipv4.conf.default.proxy_arp = 0  
net.ipv4.ip_forward=1  
kernel.sysrq = 1  
net.ipv4.conf.default.send_redirects = 1  
net.ipv4.conf.all.send_redirects = 0  
net.ipv4.conf.eth0.proxy_arp=1  
#保存退出   
sudo sysctl -p 
sudo vi /etc/sysctl.conf
#确认有如下几项
net.ipv4.conf.all.rp_filter=1
net.ipv4.icmp_echo_ignore_broadcasts=1
net.ipv4.conf.default.forwarding=1
net.ipv4.conf.default.proxy_arp = 0
net.ipv4.ip_forward=1
kernel.sysrq = 1
net.ipv4.conf.default.send_redirects = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.eth0.proxy_arp=1
#保存退出
sudo sysctl -p
修改openvz全局配置文件/etc/vz/vz.conf。选项NEIGHBOUR_DEVS定义VE使用的网卡。

Java代码
sudo vi /etc/vz/vz.conf   
NEIGHBOUR_DEVS=all 
sudo vi /etc/vz/vz.conf
NEIGHBOUR_DEVS=all
修改vps.basic配置文件,默认OpenVz创建VE时会复制/etc/vz/conf/ve-vps.basic.conf-sample的内容为新VE配置文件。修改此文件可以避免每次创建VE时重复修改配置文件。大部分是限额调整,可以根据实际情况调整。这里在末尾加一行打开部分功能。

Java代码
sudo vi /etc/vz/conf/ve-vps.basic.conf-sample   
#加入以下   
CAPABILITY="CHOWN:on DAC_READ_SEARCH:on SETGID:on SETUID:on NET_BIND_SERVICE:on NET_ADMIN:on SYS_CHROOT:on SYS_NICE:on" 
sudo vi /etc/vz/conf/ve-vps.basic.conf-sample
#加入以下
CAPABILITY="CHOWN:on DAC_READ_SEARCH:on SETGID:on SETUID:on NET_BIND_SERVICE:on NET_ADMIN:on SYS_CHROOT:on SYS_NICE:on"
修改引导文件,默认启动进入openvz内核。然后重启,用 uname -a 查看是否进入openvz的内核。

Java代码
sudo vi /boot/grub/menu.lst   
#修改default 
sudo vi /boot/grub/menu.lst
#修改default

4、openvz基本操作
openvz创建ve十分简单,只需下载官方的模板放到/var/lib/vz/template/cache里,用vzctl工具创建后稍加修改即可。
模板下载地址:http://download.openvz.org/template/precreated/

Java代码
wget http://download.openvz.org/template/precreated/old/ubuntu-8.04-i386-minimal.tar.gz   
sudo mv ubuntu-8.04-i386-minimal.tar.gz /var/lib/vz/template/cache/   
sudo vzctl create 101 --ostemplate ubuntu-8.04-i386-minimal 
wget http://download.openvz.org/template/precreated/old/ubuntu-8.04-i386-minimal.tar.gz
sudo mv ubuntu-8.04-i386-minimal.tar.gz /var/lib/vz/template/cache/
sudo vzctl create 101 --ostemplate ubuntu-8.04-i386-minimal
运行vzctl creat命令后会生成101(VE的ID,简称VEID,必须设置成100以上)VE的配置文件/etc/vz/conf/101.conf。101VE的根目录被默认放到/var/lib/vz/private/101。
5、VE管理与vzctl的使用
常用命令:

Java代码
#启动VE   
sudo vzctl start 101  
#关闭VE   
sudo vzctl stop 101  
#进入VE与退出VE   
sudo vzctl enter 101  
exit   
#删除VE   
sudo vzctl destroy 101  
#设置主机名   
sudo vzctl set 101 --hostname test --save   
#在VE中执行命令   
sudo vzctl exec 101 ps -aux 
#启动VE
sudo vzctl start 101
#关闭VE
sudo vzctl stop 101
#进入VE与退出VE
sudo vzctl enter 101
exit
#删除VE
sudo vzctl destroy 101
#设置主机名
sudo vzctl set 101 --hostname test --save
#在VE中执行命令
sudo vzctl exec 101 ps -aux
更多关于vzctl的配置命令可以参考官方的openvvz文档。

6、其他
VE的限制资源使用情况可以在VE里运行命令查看:more /proc/user_beancounters
其中held是正在使用的资源数,maxheld是做大使用资源数。failcnt是超过限制的资源数。可以根据failcnt和maxheld调整VE的资源限制(UBC)。

Records:65123456789