Sunday, February 1, 2009

Sphinx|简介

简介

原文地址:http://sphinx.pocoo.org/intro.html
翻译人员:CliffPeng
翻译日期:2009年01月30日

本文档介绍的是文档编制软件 Sphinx 。该工具可以将一系列 reStructuredText 源 文本转换成各种不同的输出格式,并自动制作交叉引用(cross-references)、索引 等。也就是说,如果某目录中有一系列的 reST 格式文档(可能子目录中也有), Sphinx 可以制作一份组织得非常完美的 HTML 文件(在其它目录中),便于浏览 和查找。但是从同一组源文件,它也可以制作一份 LaTex 文件,以便你将其转换为 PDF 格式的文档。

重点是针对手写文档,而不是自动生成的 API 文档。尽管对自动生成的文档也提供 了有限的支持(目的是为了与手写文档自由混合),但如果你需要纯 API 文档,可 以试下 Epydoc ,它也能够支持 reST 。

从其它系统转换

本节对那些想将其他文档系统转换为 reStructuredText 或 Sphinx 的人提供了一些 有用的提示。

  • Gerard Flanagan 写了一段脚本将纯 HTML 转换为 reST ;可在 Launchpad 找到相关脚本。
  • 要将旧的 Python 文档转换为 Sphinx,可以从 Python SVN 仓库 找到相关转换工具。它包 括将 Python 文档风格 标记转换为 Sphinx reST 的通用代码。

先决条件

Sphinx 需要 Python 2.4 以上才能运行。如果你喜欢源代码高亮显示支持,你还必须 安装 Pygments 类库――你可以通过安装工具 easy_install 。Sphinx 需要0.4版本的 docutils 或它的一些(可运行的) SVN 快速版本。

建立文档源

文档集合的根目录被称为 源目录 。通常,该目录中包含了 Sphinx 配置文件 conf.py ,但该 文件也可以存放在其他目录中,或称为 配置目录 (该功能自0.3版起支持)。

Sphinx 带有一段称作 sphinx-quickstart 的脚本,它可以设置 源目录 并通过问你一些问题创建 缺省的 conf.py 。只需运行:

$ sphinx-quickstart

并回答这些问题。

进行编译

编译工作由 sphinx-build 脚本启动。它的调用方式如下:

$ sphinx-build -b latex sourcedir builddir

其中 sourcedir源目录 ,而 builddir 为要存放 已编译文档的目录(必须是已经存在的目录)。选项 -b 选择编译器,本例中 Sphinx 将编译 LaTex 文件。

脚本 sphinx-build 还有几个选项:

-a
如果设定该选项,将总是输出所有的文件。缺省情况下将只对已修改的源文件 进行输入。(这一点并不对所有编译器适用。)
-E
不使用已保存的 环境 (该数据结构缓存所有的交叉引用), 而是完全重新编译。缺省情况下将只对新的源文件或者上次编译之后已经修改过的源 文件进行读取和解析。
-d path
由于 Sphinx 在输出文件之前必须读取和分析所有的源文件,已经解析的源文件被缓存 为 "doctree pickles"。通常,这些文件被放置在编译目录下的 doctrees 目录中,设定 该选项后,你可以另外选择一个目录(doctrees 可以在所有的编译之间共享)。
-c path
不在源目录中查找 conf.py ` ,而使用给定的配置目录。注意,配置内容的许多不同的 其他文件和路径和配置目录相对的,因此它们也必须放置在该位置。(自0.3版开始增加)
-C
不查找配置文件,只使用 -D 选项设定的配置。(自0.5版开始增加)
-D setting=value
覆盖 conf.py 文件中所设定的配置值。(值必须是字符串值。)
-A name=value
使 name 等于 HTML 模板中的 value
-N
不进行彩色输出(在 Windows 平台上,任何时候都不会使用彩色输出)。
-q
在标准输出设备上不作任何输出,只对普通错误输出警告和错误。
-Q
在标准输出设备上不作任何输出,同时也禁止警告。只对普通错误输出错误。
-P
(仅对调试有用)如果在编译时出现未处理例外,运行 Python 调试器―― pdb

在命令行中,你可在源目录和编译目录之后给定一个或多个文件名。Sphinx 将只试图 编译这些输出文件(及其附属文件)。

Latex|论文编译程式Latex 介绍

论文编译程式Latex 介绍
2006年08月30日 星期三 19:43
   ----Leibniz
      电子科技大学  2005年12月6日

(本文为自由发布的文档,你可以对其进行任何的拷贝,传播,修改。但原始文档版权仍归原作者所有。如有直接引用的情况,请注明文档出处和作者姓名)
 
(本篇介绍在写作过程中参照了多篇网络上的教程及帮助文档,在此对那些作者表示感谢,感谢他们为我们做了绝大部分前期的文档工作,由于太多,此处不一一列 举;另外也感谢王垠,是他带给我去坚定地使用Linux和Latex的意志��这意志帮助我克服了学习过程中的种种困难;最后感谢图灵奖得主Knuth教 授,感谢他做出伟大的TeX出来,供后人使用。)


第一章:我为什么用Linux

Latex是什么,说它是一个"排版软件",可能大家都会莫名其妙,我要一排版软件来干什么?但是如果我说它是一个"写论文用的软件",那你可能就会很有兴趣了。毕竟作为大学生,不可能不写论文的。
 其实Latex不仅可以写论文,还可以处理日常生活中的各种文档工作。如写信,写书,画表格,甚至做幻灯片(对,我没打错,它就是可以做幻灯片,而且很容易就能做出世界一流水平的幻灯片)等等。
 
本文纯粹是从使用者的角度来讲解Latex的使用方法的(当然,这样说的原因在于我本身尚未深入地研究TeX的底层体系),因此力求讲得通俗易懂。如果有什么不妥或不完善的地方请大家能时指出,欢迎热烈批评!

我们为什么要用Latex?

你一定会说日常生活中的各种文档工作不是用WORD就能进行处理了吗,何必那么麻烦再去学习一样新的东西呢?不是的。世界上还有很多更优秀的东西我们还没 有发现,如果能掌握这些更优秀的工具,那么工作效率会事半功倍。可以说当今中国的大学生们(包括中国的很多大学教授们)的计算机方面的思想已经被微软所固 化了。"言文字处理必谈WORD","言程序开发必学VB, VC","言操作系统必选WINDOWS"。我这儿并不是想排斥微软,打倒微软什么的,(诚心地说它毕竟还为推动中国的信息化的进步做出了很大贡献)我只 是想说明作为优秀大学的学生的我们,作为中国21世纪的主力军的我们,不能再被一种并不先进的思想所禁锢了!我们必须掌握最先进的最有前途的思想,知识和 能力。而TEX就是这种先进思想的典型代表。
 它是计算机科学家图灵奖得主Knuth教授设计的一款权威的科技论文排版软件!更重要的它是开源 (Open Source)的,Knuth教授无偿公开了它的所有源代码。正因为这个原因,无数的爱好者们大胆尝试TeX。并在其基础上开发了一个宏集 ��Latex。Latex方便好用,被广泛传播,成了当今世界科技界最权威的论文排版软件。

下面,我就来详细地比较一下Latex与WORD各自的优缺点。
 1:"所想即所得" vs. "所见即所得"
 WORD所遵循的思想是"所见即所得"。因此,用它写东西非常直观。而且打印的效果与在屏幕上看到的效果一模一样。另外,它还具有一定的智能化(尽管这个功能很多时候会帮倒忙)。
 而Latex所遵循的思想是"所想即所得"。意思就是你脑子里想到什么,就可以做出什么样的版式效果来。这说明它有非常强大的功能,但对这一种强大的掌握有一条相对来说陡峭一点的学习曲线。
 用WORD写文章直接往里面写就行了,然后用那些数不清的格式按钮对文档进行格式编排。写这些文档必须在WORD软件环境里面(你无法拿记事本来写.doc的文档)。
 而用Latex写文章可以使用任何一个文本编辑器。Latex有它自己专用的文件后缀名,但它的格式完全是文本文件格式。这意味着你可以使用vim,gedit,记事本等中的任何一款写Latex文档。只不过在保存文件时把后缀名改.txt为.tex就可以了。
  用Latex写文章需要像写程序那样来写(请不要为我的语言所吓倒,那不是像C语言那样的程序代码,要简单得多),写完之后再编译。初看起来这不是自找麻 烦吗?仅仅写一篇文章就要编个程,是不是有病了。不是的,你仔细想想:一篇论文不仅包括文字,还有图片,表格,公式(对我们来说很重要),还有很多各种各 样的细微的格式和版式。把这些因素加在一起,就不是仅用WORD就能很好地处理的了。而对Latex来说,这些都是小菜一碟。
 Latex文档中,是通过插入一些标记符来标记其逻辑结构的(这方面它和HTML, XML有点类似)。这些标记符都是一些常用的英文单词,十分好记。只要花点时间用熟悉了,就可以很快地写出一篇格式漂亮的文章出来。
 万事开头难嘛,最难的不是记忆那些命令,而是转变一种思维,就像使用LINUX一样。(Latex和LINUX搭配是天籁之合,我的讲解也是以LINUX上的Latex为主。建议大家读读王垠的《完全用LINUX工作,摈弃WINDOWS》)
 
 2:如果你经常编排带数学公式的文章,你可能会发现,WORD编排出来的段落不是那么令人满意,如果左对齐,那么每行的右边参差不齐,很不美观;而如果 选用两边对齐,那么WORD为了达到这点要求,可能就加大了某些词与词之间的间距,那样也达不到我们满意的文章格式。能不能在行末自动地把一个单词在音节 处拆开, 并加上连字符呢? Latex可以。
    你一定对MathType不陌生。公式编辑器用多了,你可能就发现,WORD把公式当作图片来处理。如果你要修改文章字体的大小,这时需要一个一个地调整 公式的大小,这简直是无法承受的工作量;而且,你经常会发现,WORD的公式大小并不是和正文那么适配,许多时候看起来要么大了,要么小了,尤其是碰到大 行的公式,页面编排都可能因此受到影响了。而LatexX美观的公式混排,统一的公式正文大小,会令你有耳目一新,相见恨晚的感觉。

 3:你是否有这样一种经验:当你用WORD写完一篇大一点的含有很多插图,表格和公式的论文交给导师审阅后,导师要求你在某个地方再详细一点,多列一些 数据,多插几幅图片;而在另外一些地方简略一点。这时你不得不插入新的图片(表格,公式),结果一插,把后面的原先排好的版式全打乱了。这样你又不得不重 新排一遍,排好后,检查时,又发现哪个地方公式不对,得重新写。于是用MathType写了一个"体积"大一点的公式,再插入文档中,结果又发现,后面的 版式又莫名其妙地被改动了。又得重新来一遍,气死人了。就这样反反复复,一个本来不复杂的工作耗费了你太多的时间,同时把心情也搞孬(我们念pie,四 声)了。真不爽!
 还有在论文中每个图(表,公式)都有编号,当你插入一个新的图时,就打乱了先前的编号顺序,所以你还得一个一个地将先前的位于这幅图片后的那些图片(表 格,公式)的编号手工改正过来。天哪,这又是一件多么恐怖的事情��我这可是科技论文啊?密密麻麻的图片,公式。你叫我如何改啊!而且这还是一遍,说不定 要使一篇论文最终定稿要改N次呢!
 现在你是否感觉到已经忍受不了呢?你需要一种解脱。不用怕,不用恼,救星到来啦!Latex,专门解决这个问题。
 Latex处理这个问题是基于如下一种机制:
  它完全用一种逻辑结构标记符来标记哪里是文章题目、作者,哪部分是摘要,哪里是小节标题,哪里是图片,哪里是表格,哪里是公式,哪里是脚注,哪里是页眉, 哪里需要两栏排版,哪里则只用单栏等等等等。它对每种不同的对像都设计了一个计数器,用来记录这一类型的对象到现在是第几个。比如图片计数器,编译程序在 编译时会按从前到后的顺序寻找论文中所有图片,并且找到一个便给它编一个号(从1号开始依次递增),一直到文章末尾。
 利用这种机制,即使在中途又插入了很多新的图片(表格,公式)的情况下,只要它们的逻辑顺序是对的(废话,逻辑顺序不对就是你写论文的水平不够了!), 那么Latex就会重新从头到尾再编一次号,这样前述的那种令人头痛的问题就迎刃而解了。思想就是把无谓的重复劳动拿给机器处理,机器处理这些小东西来速 度是不用担心的。于是,你被解放出来了!什么是工作效率,这就是!

 4:很重要的一个问题是:WORD不太稳定。当你编辑一份含大量公式的文档时,会发现WORD有时会crash,有时甚至会莫名其妙地消失。这时如果我们的工作还未保存下来的话,那就太不幸了,只有重新输入一遍。而且弄得自己人心惶惶的。
 Latex就不存在这个问题,它是一种纯文本格式,所占用的空间(硬盘,内存)极小。再加上极稳定的编辑器VIM,几乎不会出现crash的现象。

 5:从美学观点来看,我实在不敢敬同WORD中的公式�漂亮这一说法。你去图书馆随便找一本比较有名气一点的外文杂志或外文著作。它们那里面的公式多么 漂亮,整体布局多么赏心悦目。虽然我不敢说那些书全是用Latex编排的,但至少可以说明一点:WORD中的公式符号不好看。虽然我不敢说那些书全是用 Latex编排的,但我敢说那些书里面大部分都是用Latex及类似的(基于TeX的)软件包编排的。不信,你去比较一下那些书里面的符号和Latex教 材(外面书店有卖的)里面的符号形状是不是一样的。有点使用Latex经验的人一眼就可辨别其中奥秘。
 这是什么意思?
 对!它意味着,使用Latex,你能用很简单的方法排版出具有专业水准格式的论文和杂质甚至书籍出来!

 6:比较现实的是,现在很多国内外杂志,要求寄去的文章按Latex格式排版(可能有些杂志可以接收WORD文档,那你可能又要在茫茫软件海洋中寻找英 文WORD,因为国外根本就没有中文WORD,而你的中文WORD文档在英文WORD中什么都看不出来)。那么会用Latex就更是你迫在眉睫的需要。
 
    Latex就和vi一样,在你对他一点不懂的时候,你总觉得他很难相处,但是在你了解他之后,你会越来越发现他的好。然后会爱不释手,然后会五体投地,然后会��进步:-)

 

Latex讲解第二章:Latex发行版的版本和使用基本方法

Latex软件的版本
 Linux和Windows平台都有各自的Latex版本。Linux下的版本主要是teTex,Windows下的版本主要有mikTex。而中国有 一个比较著名的版本CTeX,它是CTeX中文套装的简称,属于二次开发版本。它是把 mikTex和一些常用的相关工具,如GSview,winEdt,yap等包含在一起制作的一个简易安装程序。并且它对其中的中文支持进行了配置,使得 安装后马上就能处理中文。
 GSview是一个浏览.ps(一种打印格式)的工具。
 winEdt是一个专为TeX用户设计的编辑器。(但是用过它的人都会感觉到,这个编辑器打开速度太慢,功能难用,字体,界面也不好看。难怪经常被网上的人骂为垃圾)
 yap是用查看.dvi格式的一个工具,支持"反向位置查找"功能。(这个用起来还不错)

 而Linux下的teTeX,我并没有直接使用。我用的是由CTAN组织制作的一款软件包TeXLive2004(以TE为核心),里面包含了几乎所有的宏包,文档及查看、转换工具。我们常用到的主要有如下几个:
 latex 用于编译.TeX文档(这个是核心)
 xdvi  用于查看.dvi文档(这个文档是由前面那个.TeX文档经编译后生成的)
 dvipdfm 用于将.dvi文件转换成.pdf文件。

 转换成.pdf文件后,就可以方便地与别人交流了。(要知道,pdf格式可是当今世界上的几乎所有较正式的组织的电子档案的首选保存格式呀。比如,AD公司(www.analog.com)的所有芯片的技术资料全是以pdf格式提供)

 而据我的使用经验,TeXLive2004安装程序并不太适合中国用户,因为它完全不含有任何一种中文字体(当然,每一款比较好的中文字体都是有版权的),尽管网上有怎样添加的方法,但是要添加起字体来特别特别麻烦��我前段时间一直为此大伤脑筋呢!
  于是我将其中的绝大部分东西抽取出来,与我搜集的十三种中文字体(转换后的。注:那些字体那是有版权的,请不要用于商业目的)合起来做成了一个软件包。同 时还对里面的一些配置文件作了修改,使Linux下的用户直接也能使用中文字体了。(我称其为Latex2004完全版,包里面有详细的安装说明文档)

 到现在,可能大家会有这样一个疑问:现在软件有了,那该怎样使用呢?而且前面介绍那么多工具。那么用LATEX写论文到底是一种什么流程呢?下面就来回答这个问题。
 下面就以一个实例来说明。
 第1步:编写好一个.tex文档。并保存。假设文件名取为example.tex,存放在~/work/目录下。
 第2步:打开一个终端。输入 cd work 进入到~/work/目录下。
 第3步:在终端中输入 latex example.tex 对example.tex这个文档进行编译。通过编译,会生成一个新文件:example.dvi(当然原来那个example.tex文件还在那里,没有消失)。
 .dvi文件是"与设备无关的文件",这里的设备指CPU、显示器,打印机等。这意味着这种格式的文件无论在哪台机器上,无论是怎样的显示器,无论在哪个操作系统上,看到的效果都完全是一样的。
 当然,这样一种格式的文件得用专门的工具来查看。Linux下,这个工具就是xdvi。
  第4步:在终端中输入 xdvi example.dvi 就可以查看刚才由.tex文档生成的.dvi 文件了。在这一步中,你可以检查你所输入的那些东西是不是都出应有的效果了(当然,这个应有的效果就是你头脑中的预计��所想即所得,就初步体现在这里 了),如果发现有错误的文字或不合适的版面,那么就可以再回到第1步去,修改后,再编译,再查看。直到自己满意为止。
 第5步:在终端中输入 dvipdfm example.dvi 就会将example.dvi文件转换成为 example.pdf 文件。这时就可以用pdf浏览器查看了。
 至此,一篇论文就基本完成了。归纳起来,就是如下步骤:
      latex  dvipdfm
 .tex------->.dvi------->.pdf
 
 下一章,我就将开始正式讲解怎样写latex文档。


Latex讲解第三章:Latex文档的基本结构及基本样式

首先建议大家如果真的有心学,最好是去书店里买本书仔细看看,因为细节知识是非常多的。我这里只能讲解一些概要,不可能面面俱到。

 Latex文档的基本结构如下:
 
 文档类型声明
 宏包包含区
 自定义命令区(全局设定区)
 标题区        
 正文区--------正文
             |
    -----参考文献
 
 将上述结构翻译成代码即为如下:

 %文档类型声明
 \documentclass[a4paper,11pt,onecolumn]{article}
 
 %宏包包含区
 \usepackage{CJK}

 %自定义命令区(全局设定区)
 \renewcommand{\baselinestretch}{1.5} %定义行间距

 %标题区
 \title{  }
 \author{ }
 
 %正文区
 \begin{document}
 ....
 \end{document}

 上述即为Latex文档的基本结构了。大家可能还不十分懂这些命令到底起什么作用,但是根据各个英文单词的含意,应该能够猜测到一些意思了吧。同时,这也展示了Latex一个特点:每个命令都使用完整的很容易懂的英文单词,而不是缩写,所以很明了,学习起来不难。
 
 下面我给出一个英文文档的基本例子。
 \documentclass[a4paper,11pt]{article}
 \begin{document}
 Hello, world!
 \end{document}

 就这样四行简短的代码,会得到图1的效果。
 \documentclass[a4paper,11pt]{article} 用来说明你这篇文章用多大的纸,默认多大的字号,并且文档类型是什么。\documentclass 是命令,是每一篇文章开头必须写的。a4paper 指定现在用A4的纸;11pt 指定默认字体的大小;article 用来说明这篇文档是篇论文(当然计算机是不会认得你要写论文的,只不过它预先定义了一些格式,以article 称呼的这种格式适用于论文,所以就叫它为论文格式了)。
 字体大小那个选项一般不行去改动它,不写也行,系统默认为11pt。不写的话就为:\documentclass[a4paper]{article}。写论文一般用A4的纸,当然,与这个选项对应的其它选项还有b5paper 等,可根据实际需要来改变。
 而article 这个位置一定不能空。与之对应的参数有 book(书), report(报告), letter(信件) 等。
  然后是\begin{document}和\end{document},这也是两个必须写的命令,它们是一对语句,不能只写一个而不写另一个,即必须匹 配。而且在一篇文档中只能写一次这对语句。从这对语句中又可反映出Latex的一个特点:很多语句是成对出现的,而且命令名都是\begin{}和\ end{}的形式。
 \begin{document}和\end{document}中间的那个 Hello, world! 就是文章的内容了。你在图1中看一下,是不是在左上角有个 Hello, world! :-)
 
 好了,就这样一个文档就写好了。简单吧。

 但──一个严重的问题是:这个文档只能处理英文。只要打一个汉字上去,在编译时就会出错!怎么办?
 下面我就来详细讲一讲 Latex 是如何处理中文的。
 
 Knuth 在发明 TeX 的时候,根本没有考虑到还要处理中文字符(以及其它许多亚洲字符)──它发明 TeX 的目的就是为了排版它的巨著《计算机算法艺术》。尽管这样,Knuth 却采用了一种先进的设计思想,从底层留下了扩展接口,并将其全面公开。这样,当时他本人没有实现的一些功能,就可以通过宏包的形式加以扩展实现。这就是为 什么到现在为止 30多年过去了,TeX 在底层还几乎没有改动(只发现两处小错误)。说到这里,确实不得不佩服Knuth,什么叫牛人,这就是。计算机科学家写出的软件与一般程序员的软件就是不 同!
 
 Latex中处理中文,需要用一个叫做 CJK 的宏包(宏包就是预先定义了一些命令及格式的一个文档,学过 C 语言的同学都应该了解的)。
    CJK是由Werner Lemberg开发的支持中、日、韩、英文字的宏包。CJK的特点是不需要象CCT那样预处理,支持PDFLatex和Type1字体,因此得到越来越多 中国TeXer的喜爱,逐渐成为中文LaTeX的主流。而我提供的那个包里面就已经含有了。(可以毫不夸张地说,几乎所有已经出现的宏包在那个软件包里面 都有自己的位置)
 这个宏包通过一个字符映射表,来实现汉字的处理。讲下去就比较深了,这里暂且说到这么多。

 下面我就给出一个处理中文的基本样例。

 \documentclass[a4paper]{article}
 \usepackage{CJK}
 \begin{document}
 \begin{CJK*}{GBK}{song}
 你好,世界!
 \end{CJK*}
 \end{document}

 生成的效果图如图2 。
 下面细细讲解一下新增的语句的作用。
 \usepackage{CJK}表示把CJK这个宏包包含进来。\usepackage{ }就是包含宏包的命令。
 \ begin{CJK*}{GBK}{song}和\end{CJK*}是中文字符的定界语句。意思就是要想显示中文,就得用这两句把所有中文包含起来。当 然,英文字符在这里面是不受影响的。\begin{CJK*}{GBK}{song}中的最后一项,就是指宋体的意思。(我的安装包说明里面有十三种字体 对应的符号名字。在Latex中使用时就用那些符号名字。)当然,你可以把它改为 kai(楷书), hei(黑体), xihei(细黑), li(隶书) 等等。
 
 比如我将song改为kai,那么将得到如图3的效果。

 看,用Latex写中文文档也不复杂吧。这个文档还太小,还体现不出Latex的优势,以后用熟了,写大文档了,就能深刻地体会到了。

 在这里,我要着重强调一下,Linux下的字符编码问题。
 现在的Linux基本上都通用UTF-8编码作为其默认编码,这是事实,我们最好不要去改变它,否则很多程序的字体会出理乱码。但Latex无法支持 UTF-8编码(其实可以支持,但是好像要下一个宏包,在我提供的那个里面没有,我也没用过,不会用)。于是我们后退一步,绕过这个问题。在把文档写好 后,只要保存时选定编码就行了。
 
 具体这样来实现:
 对gvim来说,把文档写好后。在命令模式下,输入
 set fileencoding=GB18030
 如果程序提示modifiable if off。那么,可以先输入
 set modifiable
 再输入
 set fileencoding=GB18030
 就可以保存为GB16030编码的文件了。这样在编译时,才不会出错。
 
 对gedit来说,把文档写好后,只需要点击保存按钮。就会出现一个保存对话框。如图4 。在此对话框的下面的字符编码中选择GB18030就可以了(没在列表中的话,可以添加)。

 由于保存时忘记理性编码而出现编译通不过的情况经常发生,而且还会感觉莫名奇妙。所以大家在发现编译通不过时,首先就查一下是不是编码问题。


 
Latex讲解第四章:Latex内容总体概观(最后一章)

 我写这四篇讲解的目的是因为我怀着这样一种心理:我学习Latex的过程中遇到了太多的困难,花了太多的时间,费了太多的精力(买了一本教材但是是很老 的,与当前版本不适合)。我不希望每一个想学习Latex的人因为计算机和教程的原因(网络上也有很多教程,但是大多是英文的,中文的教程都不是很完整, 适用于初学者的不多,而且过滤这些信息要花大量时间)而半途而废或代价太高。因此我觉得自己有义务要把自己的亲身使用经验告诉大家。
 由于本讲解只是入门性的介绍,目的是给大家介绍这么一款软件和思想,同时让大家了解基本的操作。故不打算再做更深入的讲解了。正所谓"师傅领进门,修行 靠个人",要想真正的掌握它,就得由大家自己去找书看,去上网问了。同时,学习这个软件,必须要学会思考。不思考的人永远学不会。

 这一章,我就来列举Latex里面主要包括哪些知识。

字符集

 文字字符集
 数字字符集

环境集

 居中
 \begin{center}
 \end{center}
 左对齐
 \begin{flushleft}
 \end{flushleft}
 右对齐
 \begin{flushright}
 \end{flushright}
 列举环境
 \begin{itemize}
 \end{itemize}
 
 \begin{enumerate}
 \end{enumerate}
 保持环境
 \verb|  |

 \begin{verbatim}
 \end{verbatim}
 小页环境
 \begin{minipage}{size}
 \end{minipage}
 诗歌环境
 \begin{verse}
 \end{verse}
 
 浮动表格环境
 \begin{table}
 \end{table}
 
 固定表格环境
 \begin{tabular}
 \end{tabular}

 浮动图片环境
 \begin{figure}
 \end{figure}

 数学环境
 $ $
 $$ $$

 \begin{equation}
 \end{equation}

 \begin{array}
 \begin{array}

 \begin{eqnarray}
 \end{equarray}

10种字号命令
 {\tiny  }
 {\scriptsize  }
 {\footnotesize  }
 {\small  }
 {\normalsize  }
 {\large  }
 {\Large  }
 {\LARGE  }
 {\huge  }
 {\Huge  }

英文字体命令
 {\rm  } 罗马字体
 {\bf  } 黑体
 {\it  } 意大利字体
 {\sc  } 小号大写字体
 {\sl  } 斜体
 {\tt  } 打字机字体
 {\cal } 花体
 {\em  } 强调型字体

几种数字的格式
 \arabic{}
 \roman{}
 \Roman{}
 \alph{}
 \Alph{}

命令

文献头命令
 标题命令
 \title{ }
 作者命令
 \author{ }
 日期命令
 \date{ }
 页注命令
 \thanks{}
 \footnote{}

章节命令
 部分命令
 \part{}
 章命令
 \chapter{}
 节命令
 \section{}
 \subsection{}
 \subsubsection{}
 \subsubsubsection{}
 附录命令
 \appendix{}

使用上面所说的命令,处理一般的文字文档应该没多大问题了。但是一篇文章中还有三个很重要的部分:公式,图片,表格现在我们还没法处理。

下面就来讲一讲。

数学格式命令
 很多,这里就不写出来了。我提供的资料中有很多是讲这个的。我这里就举一个例子,让大家见识一下Latex中处理公式的思想。

 薛定谔方程
 $$i \hbar \frac{\partial}{\partial t}\psi(r,t)
   = [-\frac{\hbar^2}{2m}\nabla^2+V(r)]\psi(r,t)$$
 此方程效果如图1,怎么样,感觉不错吧。
  上面式子中用$$ $$括起来的部分,就是公式的Latex写法。大家肯定吓一跳吧。其实思路很简单,它就是定义了一套规则(如^表示上标,_表示下标,\frac{}{} 表示分数,第一个括号中的是分子,第二个括号中的是分母)和一些特殊字符表示方法,通过这个规则Latex能够实现将一个二维的公式写法化为一维的表达式 写法。这样就便于用文本文件的方式进行存储,并用Latex编译程序进行编译再现。而且这种写法有个好处是很容易扩展。这种思想就需要自己慢慢去体会了。

插入图片
 这个需要详细说明一下。
 Latex中只支持*.eps格式的图片。因此在插入时,如果不是.eps格式的文件,则首先要用GIMP或ImageMagick工具包转换到. eps格式。然后把图片放到与.tex文件同一个目录下。(务必注意,我在初学时就是因为不知道把图片放在哪里而浪费了一个通宵)
 Latex文档中本身并不包含图片,它是在编译时加载图片。而且在加载时,还可以控制图片显示的长度和宽度。
 要在Latex文档中加载图片,必须要包括graphicx宏包。
 下面即是典型代码:

 \begin{figure}
 \begin{center}
 \includegraphics[width=10cm,height=6cm]{1.eps}%就在前面括号中写图片名
 \renewcommand{\figurename}{图}
 \caption{}
 \label{}
 \end{center}
 \end{figure}

 这段代码功能是把名为1.eps的图片调进来,放在浮动图片环境中,并居中放置。
 \includegraphics[width=10cm,height=6cm]{1.eps}中的\includegraphics 即为调用图片的命令。[]中的参数用于指定加载图片后图片显示的宽度和高度。
 \renewcommand{\figurename}{图}用于将其默认的标号(为figure)改为"图"
 \caption{}给这幅图片取一个名字或作一段说明用于显示出来。
 \label{}中也写一个名字用来标明这幅图片,方便前面或后面的交叉引用。

插入表格

 插入表格没有图片步骤多,但也要注意一些细节问题。

 \begin{table}
 \centering
 \renewcommand{\tablename}{表}
 \caption{}
 \begin{tabular}{|l|l|l|}
 \hline
  1 & 2 & 3\\
 \hline
  8.2 & 7.8 & 12.7\\
  75.7 & 152.8 & 249.9\\
 \hline
 \end{tabular}
 \end{table}

 这段代码用来生成一个3行3列的浮动表格,并左右置中放置。
 \begin{tabular}{|l|l|l|}中的|l|l|l|表示有三列,列与列之间用竖线隔开(共四根竖线)。
 \hline表示画一根横线。
 1 & 2 & 3\\中,&表示一个单元格与一个单元格的逻辑分隔符。而\\则表示换行。

好了,公式,图片,表格大家都见识过了。都有一个初步的印象了。下面就该自己去使用了,多练一练才会熟悉。

 好,Latex内容的讲解就到此为止。

 学习Latex,除了买本书来看外,还要注意以下几点:
 1:仔细看书,打好基础;
 2:要多练习,要多做笔记,善于总结;
 3:多思考,因为是"先想才有所得",才会实现"所想即所得";
 4:有不懂的问题要向周围会的同学多问,如果无人可请教。可以到论坛上去问,那里有非常多的热心人。并且要经常从论坛上收集或摘抄下很好的代码供自己使用。(积累啊)
 5:有时不懂的问题用Baidu或Google也可以搜索出来哟。
 6:不要怕看英文文档,那里面有最原汁原味的知识;
 7:非常重要的一点,要学会做模板,并且要以做一个完全适合自己的模板为一大任务。尽管网上有很多很好的模板,但是不一定适合你。做好模板以后,你才会 发觉, Latex真的是太好用了,这次论文用的这个模板,无需修改或只修改一小部分就可以供下次写论文时使用。这样既提高了速度,又提高了正确率,而且有一个明 显的累积进步过程──这会使你感觉到很爽很充实的。

 好了,废话就这么多了。

 下面将搜集的资料分类整理如下,我将把它们上传到学生之家或5Come5服务器上。

 整理中……

 

近日去学无涯书店(寓苑公寓下面)逛了逛,发现几本有关Latex的书,现介绍如下:

《排版软件LATEX简明手册》
罗振东 葛向阳 编著 电子工业出版社 2004

《TEX、AMS-TEX、LATEX使用简介》
李勇编 高等教育出版社 2000

《Latex2e 及常用宏包使用指南》
李平 清华大学出版社 2004

《Latex 入门与提高》
陈志杰、赵书钦、万福永编 高等教育出版社 2002

《Latex 实用教程》(英文影印版)
机械工业出版社 2005

 

下面是一些网站,丰富的信息在那里找吧。

Samba|Sharity-Light - connecting Microsoft to FreeBSD 3

Sharity-Light - connecting Microsoft to FreeBSD 3 November 1998
Need more help on this topic? Click here
This article has 4 comments
Show me similar articles
The main page for Sharity-Light is http://www.obdev.at/Products/shlight.html.

This is a way to connect to windows share drive from freebsd

You might also want to see the NFS article.

NOTE: If you want to access FreeBSD file systems from your Microsoft box, you want Samba.


3 November
Today I wanted to try accessing my NT box from my FreeBSD box.  So I installed the Sharity-Light port.

Installing Sharity-Light
If you need to know how to install a port, here is a section from the FreeBSD Handbook.

Here's what I did.  Note that I had CD#3 from FreeBSD 2.2.7 in my CD drive.

  • mount /cdrom
  • cd /usr/ports/net/sharity-light
  • make install
  • umount /cdrom

The Sharity-Light files are installed in /usr/local/sbin.


Sharing an MS File system
This section deals with sharing an MS file system.  I've done this from within Windows NT.  But the process for Windows 95/98 should be similar.  In this example, I'll be sharing c:\temp and allowing only the user Barny to access it.  The following steps are performed on the MS system.
  1. start Windows NT explorer
  2. right click on c:\temp
  3. click on Sharing
  4. click on Shared As
  5. click on Permissions
  6. click on Remove to remove Everyone's access
  7. click on Add
  8. click on Show Users
  9. click on the user you wish to add (in my example, I clicked on Barny)
  10. click on Add
  11. select the Type of Access you wish to provide.  I choose Read.
  12. click on OK
  13. click on OK
  14. click on OK

This should now allow the directory to be shared.  The next section describes how to access this share from FreeBSD.


Accessing an MS file system
In the previous section, we published, or created, a share.  In this section, we will access that share from our FreeBSD box.  The NT box from which we issued that share is named NT1.  So we will mount that share under /nt1.
mkdir /nt1
cd /usr/local/sbin
./shlight //NT1/TEMP /nt1 -U barny -P password

where...

is the mount point we are going to use
//NT1/TEMP
is the share issued on the NT1 box.
-U Barny
specified the user id
-P password
specified the password

Accessing an MS file system
To unmount the file system, issue the following command:
./unshlight /nt1

Documentation
The best online documentation I've found is the following:

ftp://ftp.obdev.at/pub/Products/Sharity-Light/Sharity-Light.README


Limitations
You will find that dates and times are not correct on the files from NT4 file systems.   For more information, please refer to the documentation mentioned in the previous section.

Tuesday, January 13, 2009

copy svn working dir without svn hidden dirs and files?

I'm using svn to work on a JSP app which must be compiled and zipped into a fairly large .war file before deployment. I would like to be able to do this without having all the .svn directories and hidden files put into the archive as well. How can I just make a copy of my svn working directory (/home/user/progname) that doesn't include all svn's hidden files?



You could use the old "copy with tar" trick and pass it the --exclude option. The command would go something like this:
Code:
tar --exclude='.svn' -c -f - /path/to/sourcedir/* | (cd /path/to/destdir ; tar xfp -)
This command builds a tar archive of the files in sourcedir, excluding anything that matches the pattern '.svn' (i.e. the hidden Subversion directories). It writes the command to standard output, changes directory to destdir, and then extracts that standard output from a tar archive back to the filesystem.

Or, if you're too lazy for a command that complicated, you can just copy the entire directory and then delete the Subversion stuff with a plain-old:
Code:
find /path/to/destdir -name '.svn' -exec rm -r {} \;

-----------------------------------------------------------------------------------------
rysnc is a very powerful alternative to cp. It provides basic things like you want here, progress meters, copying to remote sites, a diff algorithm to efficiently maintain mirrors, compression, etc.

I think you want something like this:

Code:
rsync -r --exclude=.svn /home/user/progname/ /home/user/progname.copy
I almost always use -a (archive) instead of -r (recursive) because it means -r and more (see the man page). The trailing / on progname means `contents of' (otherwise progname.copy will contain progname and everything will be one more level down). Put a trailing / on .svn to only exlude directories with that name, instead of both directories and files with that name, if for some reason you had files named .svn that you wanted to keep in the copy.

rsync is awesome

Sunday, January 11, 2009

FreeBSD|shutdown -h now

shutdown now will not shutdown freebsd but enter into single user mode, to shutdown, use shutdown -h now

howzit

use cmd like this shutdown -h now h means halt. I guess it is a way to take into single user mode without restarting the box compltely.

chow

HOWT0: Go from single-user to multi-user
Miscellaneous Posted by Jason on Sunday May 12, @09:52AM
from the dept.
As many of you may already know, UNIX is separated into two main camps. The BSD camp and the system V camp. System V Unix OS's have many run levels which they can run in and each level starts up different services. On the other hand, BSD(FreeBSD included) has only two: single-user and multi-user mode. Also, you can switch between these two modes without a reboot. Here's how...

For this to work, you have to start FreeBSD in single-user mode, after that you can switch to multi-user mode and back to singe-user mode without rebooting.


[Boot in single user mode]

As the operating system is starting, it will display the following message:

Hit [Enter] to boot immediately, or any other key for command prompt.

Booting [kernel] in 10 seconds...


You should now press the space bar, and you will see the following message:

Type '?' for a list of commands, or 'help' for more detailed help.

ok


Now type boot -s and press the enter key to start FreeBSD in single user mode. After the system boots, you should see the statement:

Enter full pathname of shell or RETURN for /bin/sh:

Press the enter key and you will have a # prompt.


[Mount the filesystems]

Next, you will have to mount your filesystems manually. At the command prompt, issue the mount command. This command will mount all the filesystems listed in your /etc/fstab file. You may also wish to enable your swap.

# mount -t ufs -a
# swapon -a


[Switch to multi-user mode]

At the command prompt, issue the exit command. This command will run your normal startup scripts and start FreeBSD in multi-user mode.

# exit


[Switch back to single-user mode]

At the command prompt, issue the shutdown now command. This will not shutdown the computer, but instead will drop you back to single-user mode.

# shutdown now



[Shutting down]

To shutdown the OS in either single-user or multi-user mode, issue the halt command:

# halt



To learn more about the commands listed in this post please refer to the man pages.

man mount
man swapon
man shutdown
man halt


< | >



Re: HOWT0: Go from single-user to multi-user
by ATorres on Friday April 18, @10:36AM
Good day, Please excuse my ignorance. I'm a DOS/Windows guy. How can I setup BSD to execute an application program after boot (just like Autoexec.bat in DOS) automatically? In the same line, is there a way to setup BSD so that it doesn't ask me for a user name to login? Again, imitating my DOS computer, I'd like to turn on the computer and the system would boot and run an application. Thanks much.
[ Add a Reply to this ]
  • No user and password prompt
    by Doug on Wednesday July 13, @09:43AM
    You could install a "display manager" like xdm or kdm or gdm if you want to get to a pretty login screen. I believe that kdm will do auto-login if you set it up properly.
    [ Reply to this ]
Re: HOWT0: Go from single-user to multi-user
by Gary on Saturday June 14, @02:31PM
add a line to /etc/rc.local or a script to /usr/local/etc/rc.d

Saturday, January 10, 2009

Qemu|Useful hint

Qemu on FreeBSD as host

worked for me:
qemu -monitor stdio -boot c -m 2020 -hda '/apps/qemuroot/winxproot/winxpdisk' -net nic -net tap,macaddr=00:bd:64:d8dhw -soundhw es1370e s1370 -kernel-kqemu -vnc :2 -usbdevice tablet


Qemu is a very fast generic and open source processor emulator by Fabrice Bellard. It runs under Windows 2000, Windows XP, GNU/Linux (RedHat, Debian) and FreeBSD "host" systems. After reading this page you should know:

* How to install qemu from the Ports tree
* How to run Qemu in FreeBSD 7.X or later

Installation notes

The installation of Qemu is pretty fast and easy. You can just install it from the Ports. The two Qemu ports we have in the Ports tree as of this writing are "emulators/qemu" and "emulators/qemu-devel". Installing "qemu-devel" for example should be as easy as:

*

# cd /usr/ports/emulators/qemu-devel
# make install && make clean

*

Note: It may be a good idea to enable the KQEMU accelerator when you install one of the Qemu ports (otherwise qemu is much slower).

Running Qemu

To successfully run Qemu, you should make sure that you have:

* IDE bus-master DMA support
*

The aio, if_tap and if_bridge modules in the "host" system

If you forget to load aio, then Qemu may crash with an error message of:

*

Bad system call: 12 (core dumped)

The if_tap and if_bridge modules are mandatory only when you plan to connect the "hosted" FreeBSD system with your existing network. If you don't need networking inside Qemu, it's ok to fire it up without these two networking modules in the host kernel.

*

Note: if you use kqemu make sure your kqemu.ko module is always in sync with your kernel (like with any kld installed outside of base), i.e. rebuild its port whenever you update the kernel — especially if you are switching branches or are following a -STABLE or -CURRENT branch!

You can enable autoloading of kqemu (and aio) at boot by adding a line to your /etc/rc.conf file:

*

kqemu_enable="YES"
*

Note: it is impossible to continue to use existing Windows 2000/XP disk images, since driver has been changed by activating IDE Bus-master DMA support.

Networking on FreeBSD 7

Contributed by: Marten Vijn.

Default, network is configured inside of the emulator; not visible from outside. if you use fxp0, then:

* # ifconfig tap create
* # ifconfig bridge create
* # ifconfig bridge0 addm tap0 addm fxp0 up

Also, /usr/local/etc/qemu-ifup script is needed. Networking Default, network is configured inside of the emulator; not visible from outside. This is not absolutely confotable! There are pros and cons: you must be the root and your qemu virtual machine is visible from outside. Assume you know your network interface name. In this case it is fxp0. you can check this by:

* % dmesg | grep Ethernet

First, as root,

* # sysctl net.link.ether.bridge_cfg=fxp0,tap0
* # sysctl net.link.ether.bridge.enable=1

create /usr/local/etc/qemu-ifup script as

*

#!/bin/sh
/sbin/ifconfig ${1} up

and make this script runnable.

* # chmod 755 /usr/local/etc/qemu-ifup

To do this at every boot time, write /etc/sysctl.conf

*

net.link.ether.bridge_cfg=fxp0,tap0
net.link.ether.bridge.enable=1

and in /boot/loader.conf

*

bridge_load="YES"

Invoke qemu by:

* # qemu -net nic -net tap

This was originally taken from freebsd vde helpp Qemu as VNC server Display writing is very slow in QEMU, so run qemu as vnc server is very useful.

* % qemu -hda /work/qemu/win2000ja/win2000.img -localtime -m 384 -vnc :1 -usbdevice tablet

Run vncviewer like (you can install via /usr/ports/net/tightvnc)

% vncviewer :1

You should wait for while until Windows starts up. Full Screen mode (slow) QEmu uses SDL to output the screen. Modern video card supports VESA 2.0, so you can use SDL with VESA 2.0. While using SDL you need additional settings. First, reconfigure kernel with this configuration.

*

include GENERIC
ident MAHO
device atapicam
options VESA

save as /usr/src/sys/i386/conf/MAHO or something like that. Then,

* # cd /sys/i386/compile/MAHO

# made cleandepend ; make depend ; make ; make install

you need additional setting:

* # setenv SDL_VIDEODRIVER vgl

and you can run with full-screen in the console. Again, you must be the root. As far as I tested,

* # kldload vesa

DOESN'T WORK (FOR ME). Unfortunately this is very slow, slower than X11 mode... Summary (My setting and command line) As summary, I present my setting for qemu 0.8.0.

* # qemu -localtime -hda /work/qemu/win2000ja/win2000.img -m 256 -net nic -net tap

/boot/loader.conf

* kqemu_load="YES"

bridge_load="YES" snd_driver_load="YES" snd_pcm_load="YES"

/etc/sysctl.conf

* net.link.ether.bridge_cfg=fxp0,tap0

net.link.ether.bridge.enable=1

/etc/qemu-ifup (chmod 755 /etc/qemu-ifup)

*

#!/bin/sh
ifconfig ${1} 0.0.0.0

Reconfigure kernel with this configuration file

*

include GENERIC
ident MAHO
device atapicam
options VESA

and additional enviromnent variable:

* # setenv SDL_VIDEODRIVER vgl

I don't want to be the root

* Network bridge
* Full screen mode

Links

*

Qemu

Virtual Machines...

I really like vmware, but I'm not thrilled about the licensing, pay-for-server, or proprietary virtual-disk format. So instead, I use Qemu. Qemu can boot from a dd image of a disk. (Cool!) And Qemu includes tools for converting disk images to vmware's format and vice versa. (For these goodies, I put them in my book, Hacking Ubuntu.)

The problem is, the virtual machine from Qemu does not seem to like the mouse when using Synergy. What happens is that the VM window says it grabbed the mouse, but no mouse is present. And if you move the mouse far enough to the other side, then Synergy takes it to another monitor.

The solution I use has two steps and uses VNC. VNC is normally used to see and control a remote desktop.

1. Run Qemu with the VNC setting. "-vnc 2" will start the virtual machine's desktop in VNC display 2. You can then use vncviewer localhost:2 to access the system.
2. The second problem is with mouse alignment. If your virtual machine is running Windows, then the real pointer and mouse pointer are not lined up. Fix this by specifying a pointer device that uses absolute coordinates: "-usbdevice tablet". Poof -- the mouse is aligned.


My full command-line (for my Windows 2000 image) looks like: qemu -hda win2k.hd -m 256 -win2k-hack -vnc :2 -usbdevice tablet. Works like a champ. Granted, Qemu + VNC is probably not refreshing fast enough for streaming real-time video, it is fine for basic word processing (which is the only reason I still keep a Windows system around).

Friday, January 9, 2009

1.3. Changing IP Addresses and Routes
Prev Chapter 1. Basic IP Connectivity Next
1.3. Changing IP Addresses and Routes

This section introduces changing the IP address on an interface, changing the default gateway, and adding and removing a static route. With the knowledge of ifconfig and route output it's a small step to learn how to change IP configuration with these same tools.
1.3.1. Changing the IP on a machine

For a practical example, let's say that the branch office server, morgan, needs to visit the main office for some hardware maintenance. Since the services on the machine are not in use, it's a convenient time to fetch some software updates, after configuring the machine to join the LAN.

Once the machine is booted and connected to the Ethernet, it's ready for IP reconfiguration. In order to join an IP network, the following information is required. Refer to the network map and appendix to gather the required information below.

*

An unused IP address (Use 192.168.99.14.)
*

netmask (What's your guess?)
*

IP address of the default gateway (What's your guess?)
*

network address [5] (What's your guess?)
*

The IP address of a name resolver. (Use the IP of the default gateway here [6]. )

Example 1.5. ifconfig and route output before the change

[root@morgan]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:80:C8:F8:4A:53
inet addr:192.168.98.82 Bcast:192.168.98.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
Interrupt:9 Base address:0x5000

[root@morgan]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.98.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo
0.0.0.0 192.168.98.254 0.0.0.0 UG 0 0 0 eth0


The process of readdressing for the new network involves three steps. It is clear in Example 1.5, “ifconfig and route output before the change”, that morgan is configured for a different network than the main office desktop network. First, the active interface must be brought down, then a new address must be configured on the interface and brought up, and finally a new default route must be added. If the networking configuration is correct and the process is successful, the machine should be able to connect to local and non-local destinations.

Example 1.6. Bringing down a network interface with ifconfig

[root@morgan]# ifconfig eth0 down


This is a fast way to stop networking on a single-homed machine such as a server or workstation. On multi-homed hosts, other interfaces on the machine would be unaffected by this command. This method of bringing down an interface has some serious side effects, which should be understood. Here is a summary of the side effects of bringing down an interface.

Side effects of bringing down an interface with ifconfig

*

all IP addresses on the specified interface are deactivated and removed
*

any connections established to or from IPs on the specified interface are broken [7]
*

all routes to any destinations through the specified interface are removed from the routing tables
*

the link layer device is deactivated

The next step, bringing up the interface, requires the new networking configuration information. It's a good habit to check the interface after configuration to verify settings.

Example 1.7. Bringing up an Ethernet interface with ifconfig

[root@morgan]# ifconfig eth0 192.168.99.14 netmask 255.255.255.0 up
[root@morgan]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:80:C8:F8:4A:53
inet addr:192.168.99.14 Bcast:192.168.99.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
Interrupt:9 Base address:0x5000



The second call to ifconfig allows verification of the IP addressing information. The currently configured IP address on eth0 is 192.168.99.14. Bringing up an interface also has a small set of side effects.

Side effects of bringing up an interface

*

the link layer device is activated
*

the requested IP address is assigned to the specified interface
*

all local, network, and broadcast routes implied by the IP configuration are added to the routing tables

Use ping to verify the reachability of other locally connected hosts or skip directly to setting the default gateway.
1.3.2. Setting the Default Route

It should come as no surprise to a close reader (hint), that the default route was removed at the execution of ifconfig eth0 down. The crucial final step is configuring the default route.

Example 1.8. Adding a default route with route

[root@morgan]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.99.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo
[root@morgan]# route add default gw 192.168.99.254
[root@morgan]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.99.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo
0.0.0.0 192.168.99.254 0.0.0.0 UG 0 0 0 eth0


The routing table on morgan should look exactly like the initial routing table on tristan. Compare the routing tables in Example 1.1, “Sample ifconfig output” and Example 1.8, “Adding a default route with route”.

These changes to the routing table on morgan will stay in effect until they are manually changed, the network is restarted, or the machine reboots. With knowledge of the addressing scheme of a network, and the use of ifconfig and route it's simple to readdress a machine on just about any Ethernet you can attach to. The benefits of familiarity with these commands extend to non-Ethernet IP networks as well, because these commands operate on the IP layer, independent of the link layer.
1.3.3. Adding and removing a static route

Now that morgan has joined the LAN at the main office and can reach the Internet, a static route to the branch office would be convenient for accessing resources on that network.

A static route is any route entered into a routing table which specifies at least a destination address and a gateway or device. Static routes are special instructions regarding the path a packet should take to reach a destination and are usually used to specify reachability of a destination through a router other than the default gateway.

As we saw above, in Section 1.2.3, “Static Routes to Networks”, a static route provides a specific route to a known destination. There are several pieces of information we need to know in order to be able to add a static route.

*

the address of the destination (192.168.98.0)
*

the netmask of the destination (255.255.255.0)
o

EITHER the IP address of the router through which the destination (192.168.99.1) is reachable
o

OR the name of the link layer device to which the destination is directly connected

Example 1.9. Adding a static route with route

[root@morgan]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.99.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo
0.0.0.0 192.168.99.254 0.0.0.0 UG 0 0 0 eth0
[root@morgan]# route add -net 192.168.98.0 netmask 255.255.255.0 gw 192.168.99.1
[root@morgan]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.99.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.98.0 192.168.99.1 255.255.255.0 UG 0 0 0 eth0
127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo
0.0.0.0 192.168.99.254 0.0.0.0 UG 0 0 0 eth0


Example 1.9, “Adding a static route with route” shows how to add a static route to the 192.168.98.0/24 network. In order to test the reachability of the remote network, ping any machine on the 192.168.98.0/24 network. Routers are usually a good choice, since they rarely have packet filters and are usually alive.

Because a more specific route is always chosen over a less specific route, it is even possible to support host routes. These are routes for destinations which are single IP addresses. This can be accomplished with a manually added static route as below.

Example 1.10. Removing a static network route and adding a static host route

[root@morgan]# route del -net 192.168.98.0 netmask 255.255.255.0 gw 192.168.99.1
[root@morgan]# route add -net 192.168.98.42 netmask 255.255.255.255 gw 192.168.99.1
[root@morgan]# route add -host 192.168.98.42 gw 192.168.99.1
SIOCADDRT: File exists
[root@morgan]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.99.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.98.42 192.168.99.1 255.255.255.255 UGH 0 0 0 eth0
127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo
0.0.0.0 192.168.99.254 0.0.0.0 UG 0 0 0 eth0


This should serve as an illustration that there is no difference to the kernel in selecting a route between a host route and a network route with a host netmask. If this is a surprise or is at all confusing, review the use of netmasks in IP networking. Some collected links on general IP networking are available in Section I.1.3, “General IP Networking Resources”.

[5] The network address can be calculated from the IP address and netmask. Refer to Section H.1, “ipcalc and other IP addressing calculators”. Especially handy is the variable length subnet mask RFC, RFC 1878.

[6] Many networks are configured with the name resolution services on a publicly connected host. See Section 12.6, “DNS Troubleshooting”.

[7] It is possible for a linux box which meets the following three criteria to maintain connections and provide services without having the service IP configured on an interface. It must be functioning as a router, be configured to support non-local binding and be in the route path of the client machine. This is an uncommon need, frequently accomplished by the use of transparent proxying software.

Network|11.8.2 Configuring the Network Card

11.8.2 Configuring the Network Card

Once the right driver is loaded for the network card, the card needs to be configured. As with many other things, the network card may have been configured at installation time by sysinstall.

To display the configuration for the network interfaces on your system, enter the following command:

% ifconfig
dc0: flags=8843 mtu 1500
inet 192.168.1.3 netmask 0xffffff00 broadcast 192.168.1.255
ether 00:a0:cc:da:da:da
media: Ethernet autoselect (100baseTX )
status: active
dc1: flags=8843 mtu 1500
inet 10.0.0.1 netmask 0xffffff00 broadcast 10.0.0.255
ether 00:a0:cc:da:da:db
media: Ethernet 10baseT/UTP
status: no carrier
lp0: flags=8810 mtu 1500
lo0: flags=8049 mtu 16384
inet 127.0.0.1 netmask 0xff000000
tun0: flags=8010 mtu 1500

Note: Old versions of FreeBSD may require the -a option following ifconfig(8), for more details about the correct syntax of ifconfig(8), please refer to the manual page. Note also that entries concerning IPv6 (inet6 etc.) were omitted in this example.

In this example, the following devices were displayed:

*

dc0: The first Ethernet interface
*

dc1: The second Ethernet interface
*

lp0: The parallel port interface
*

lo0: The loopback device
*

tun0: The tunnel device used by ppp

FreeBSD uses the driver name followed by the order in which one the card is detected at the kernel boot to name the network card. For example sis2 would be the third network card on the system using the sis(4) driver.

In this example, the dc0 device is up and running. The key indicators are:

1.

UP means that the card is configured and ready.
2.

The card has an Internet (inet) address (in this case 192.168.1.3).
3.

It has a valid subnet mask (netmask; 0xffffff00 is the same as 255.255.255.0).
4.

It has a valid broadcast address (in this case, 192.168.1.255).
5.

The MAC address of the card (ether) is 00:a0:cc:da:da:da
6.

The physical media selection is on autoselection mode (media: Ethernet autoselect (100baseTX )). We see that dc1 was configured to run with 10baseT/UTP media. For more information on available media types for a driver, please refer to its manual page.
7.

The status of the link (status) is active, i.e. the carrier is detected. For dc1, we see status: no carrier. This is normal when an Ethernet cable is not plugged into the card.

If the ifconfig(8) output had shown something similar to:

dc0: flags=8843 mtu 1500
ether 00:a0:cc:da:da:da

it would indicate the card has not been configured.

To configure your card, you need root privileges. The network card configuration can be done from the command line with ifconfig(8) but you would have to do it after each reboot of the system. The file /etc/rc.conf is where to add the network card's configuration.

Open /etc/rc.conf in your favorite editor. You need to add a line for each network card present on the system, for example in our case, we added these lines:

ifconfig_dc0="inet 192.168.1.3 netmask 255.255.255.0"
ifconfig_dc1="inet 10.0.0.1 netmask 255.255.255.0 media 10baseT/UTP"

You have to replace dc0, dc1, and so on, with the correct device for your cards, and the addresses with the proper ones. You should read the card driver and ifconfig(8) manual pages for more details about the allowed options and also rc.conf(5) manual page for more information on the syntax of /etc/rc.conf.

If you configured the network during installation, some lines about the network card(s) may be already present. Double check /etc/rc.conf before adding any lines.

You will also have to edit the file /etc/hosts to add the names and the IP addresses of various machines of the LAN, if they are not already there. For more information please refer to hosts(5) and to /usr/share/examples/etc/hosts.
11.8.3 Testing and Troubleshooting

Once you have made the necessary changes in /etc/rc.conf, you should reboot your system. This will allow the change(s) to the interface(s) to be applied, and verify that the system restarts without any configuration errors.

Once the system has been rebooted, you should test the network interfaces.
11.8.3.1 Testing the Ethernet Card

To verify that an Ethernet card is configured correctly, you have to try two things. First, ping the interface itself, and then ping another machine on the LAN.

First test the local interface:

% ping -c5 192.168.1.3
PING 192.168.1.3 (192.168.1.3): 56 data bytes
64 bytes from 192.168.1.3: icmp_seq=0 ttl=64 time=0.082 ms
64 bytes from 192.168.1.3: icmp_seq=1 ttl=64 time=0.074 ms
64 bytes from 192.168.1.3: icmp_seq=2 ttl=64 time=0.076 ms
64 bytes from 192.168.1.3: icmp_seq=3 ttl=64 time=0.108 ms
64 bytes from 192.168.1.3: icmp_seq=4 ttl=64 time=0.076 ms

--- 192.168.1.3 ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.074/0.083/0.108/0.013 ms

Now we have to ping another machine on the LAN:

% ping -c5 192.168.1.2
PING 192.168.1.2 (192.168.1.2): 56 data bytes
64 bytes from 192.168.1.2: icmp_seq=0 ttl=64 time=0.726 ms
64 bytes from 192.168.1.2: icmp_seq=1 ttl=64 time=0.766 ms
64 bytes from 192.168.1.2: icmp_seq=2 ttl=64 time=0.700 ms
64 bytes from 192.168.1.2: icmp_seq=3 ttl=64 time=0.747 ms
64 bytes from 192.168.1.2: icmp_seq=4 ttl=64 time=0.704 ms

--- 192.168.1.2 ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.700/0.729/0.766/0.025 ms

You could also use the machine name instead of 192.168.1.2 if you have set up the /etc/hosts file.
11.8.3.2 Troubleshooting

Troubleshooting hardware and software configurations is always a pain, and a pain which can be alleviated by checking the simple things first. Is your network cable plugged in? Have you properly configured the network services? Did you configure the firewall correctly? Is the card you are using supported by FreeBSD? Always check the hardware notes before sending off a bug report. Update your version of FreeBSD to the latest STABLE version. Check the mailing list archives, or perhaps search the Internet.

If the card works, yet performance is poor, it would be worthwhile to read over the tuning(7) manual page. You can also check the network configuration as incorrect network settings can cause slow connections.

Some users experience one or two “device timeout” messages, which is normal for some cards. If they continue, or are bothersome, you may wish to be sure the device is not conflicting with another device. Double check the cable connections. Perhaps you may just need to get another card.

At times, users see a few “watchdog timeout” errors. The first thing to do here is to check your network cable. Many cards require a PCI slot which supports Bus Mastering. On some old motherboards, only one PCI slot allows it (usually slot 0). Check the network card and the motherboard documentation to determine if that may be the problem.

“No route to host” messages occur if the system is unable to route a packet to the destination host. This can happen if no default route is specified, or if a cable is unplugged. Check the output of netstat -rn and make sure there is a valid route to the host you are trying to reach. If there is not, read on to Chapter 31.

“ping: sendto: Permission denied” error messages are often caused by a misconfigured firewall. If ipfw is enabled in the kernel but no rules have been defined, then the default policy is to deny all traffic, even ping requests! Read on to Chapter 30 for more information.

Sometimes performance of the card is poor, or below average. In these cases it is best to set the media selection mode from autoselect to the correct media selection. While this usually works for most hardware, it may not resolve this issue for everyone. Again, check all the network settings, and read over the tuning(7) manual page.

Tuesday, January 6, 2009

Qemu|在BSD下使用qemu开源的虚拟机

在BSD下使用qemu开源的虚拟机

大家在windows 下都用什么虚拟机?可能大多数都是用VM ware 吧,那在BSD下呢?在我还用4.10的时候我也是用VM ware,但是VM ware是要授权文件的,不过这个文件是可以下载到的。但是现在,在BSD下,有了更好的选择,那就是qemu,一个开源的虚拟机,小巧而快速,好了不多说,我现在 是在BSD下打算虚拟出一个win来,并且让 win能上网


1 首先,用ports安装

CODE:
[Copy to clipboard]
#cd /usr/ports/emulators/qemu
#路径不怎么记得了,不好意思

CODE:
[Copy to clipboard]
#make WITH_KQEMU=yes install clean
#这里有个选项,with_kqemu,当初我就是没装个,导致出错,用了这个选项后 会在BSD启动时加入 kqemu.ko
然后就是几分钟的时间,很快的就可以装好qemu了(呵呵,比VM 时间短)


2 接下来就是使用qemu了,
首先建议您重启BSD,当然,不重启也可以,用下面的步骤就可以了:

CODE:
[Copy to clipboard]
#rehash
#kldload kqemu.ko
3 要用虚拟机,当然先要为虚拟机准备一块磁盘,这里推荐使用img格式的文件做为虚拟磁盘,当然,qemu兼容vm的文件,有兴趣你也可以试试。个人感觉似乎img的磁盘文件会更快点。往下看

CODE:
[Copy to clipboard]
#mkdir /tmp/qemu #找个地方安装你的虚拟机,要注意下自己的空间哦,不要像我,直接放在了 /下,造成空间不足。强烈建议你先 df -h下


CODE:
[Copy to clipboard]
#cd /tmp/qemu
#qemu-img create windows.img 2G #这个就是创建虚拟机磁盘,后面的2G意思是创建一个2G的磁盘
4 接下就是要安装虚拟机了,我有两手准备,一是光驱,二是ISO文件。下面就开始安装

CODE:
[Copy to clipboard]
#qemu -hda windows.img -cdrom /dev/acd0 -boot d -m 512 -enable-audio -localtime
解释下上面的语法:
-hda 是指定硬盘,在本文里我们使用的是刚才创建的/tmp/qemu/windows.img

-cdrom 指定使用光驱,这个参数后面必须要跟个文件名,在上面的例子里我用的是我的物理光驱/dev/acd0,如果你要使用ISO文件,那就写上文件的路径,比如我第二安装时用的就是/mtn/e/xp.iso


-m 是指定虚拟机使用的内存大小,后面的数字即是你要指定的内存数,我的是512M

-boot 当然就是指定虚拟机启动用的设备,-boot后可以选择的参数是 a ,c ,d .其中a 是指从软盘启动,c 就是用硬盘启动,d 当然就是光驱启动了。

-enable-audio 这个意思我就不说了。

-localtime 就是说要使用你的实际主板bios上的时间。

好了,按以上的步骤做了以后,就可以顺利的进入虚拟机了,并开始安装了。

*注意:如果你在安装QEMU没有选择 WITH_KQEMU,那么你将不能使用ISO文件来安装,这一点要注意。要想自己是否安装了kqemu.ko就自己看下:

CODE:
[Copy to clipboard]
#kldstat
下面是引用Linux下的Qemu的使用,由天Qemu是跨平台的,所以应该通用,当然,我只是为可能出现的问题提供一个解决参考:

CODE:
[Copy to clipboard]
安装过程中,要求换盘:

在qemu中按ctrl+alt+2切换到qemu monitor模式 输入?或help可以查看可用命令及使用说明。

(在其他版本的qemu中,运行qemu加载OS后,这个shell就会自动变成qemu monitor模式)

change device filename -- change a removable media

看来它就是用来换盘的了 : change cdrom /rhel4/EL_disc2.iso
5 虚拟机装好以后就可以用了,这里要给大家说几个常用的快捷键:

CODE:
[Copy to clipboard]
ctrl+alt+F 全屏切换
ctrl+alt 鼠标在虚拟机和主机间切换
6 虚拟XP上网的问题:
其实用上面的方法安装完后,你的XP已经能上网了,当然你要用DHCP方式。你也可以在启动时加上 -user-net 这个参数。我觉得qemu可能是默认就带了这个参数了,因为我没用任何参数就可以上网。

不过要说的是,采用这种默认的参数你只能 ping 你的BSD主机,不能ping 外网的,而且你得到的IP是10.0.2.X,Gateway 也一定是10.0.2.2,所以你不要奇怪。这种上网方式主机跟虚拟机的通讯不方便,至少我的samba就不行,也没有解决掉。

qemu也提供了第二种上网方案:tun方式,这个方式其实就是在你的BSD下虚拟一张网卡tun0,这样你的BSD就是双网卡上网,这种方式最大好处就是虚拟机跟主机还有外网通讯比较方便。不过首先你的内核要支持tun设备才行。我的是5.4 release-p6

针对5.0以前的应该先这样做:

CODE:
[Copy to clipboard]
#cd /dev
# sh MAKEDEV tun0
如果你跟我一样,是5。X那就先:

CODE:
[Copy to clipboard]
#mknod tun0 c 20 0
#ls /dev
接下来:

#ifconfig tun0 XXX.XXX.XXX.XXX #设定tun0的IP,一般用192.168开头的,也有人喜欢10开头的,随便啦,我的是192.168.0.1

然后进入虚拟机XP里,设定你的IP为:192.168.0.XXX,网关为 192.168.0.1(*也就是tun0)dns 就是BSD主机的dns。

这样设定之后,虚拟机还不能上网,但已经能用samba 了。要想用虚拟机上网,这就要用到nat 了,设置方法完全跟NAT是一样的,我就不多说了,下面的东西就只有请你参考FreeBSD 的NAT来完成了。


Article submitted by Michael Williamson. We are running out of articles! Please help DPOTD and submit good articles about software you like!

QEMU lets you emulate a machine —in other words, you can run a virtual computer on top of your real computer. This makes it perfect for trying and testing the latest release of a distribution, running older operating systems, or just testing.

So, let’s say you wanted to run a LiveCD, which you have stored on your hard drive. Easy enough! We just type:

qemu -cdrom path/to/livecd.iso -boot d

The -cdrom option tells QEMU the path to the CD you want to use, while -boot dictates what device we’re booting from —in this case, we want to boot from the CD, which is always device d.

You might get a message about kqemu —you can safely ignore this for now.

Alternatively, you may want to boot from a real CD in your computer —so, you simply use the path to your CD drive in /dev. For instance, if your CD drive is /dev/hdc, then we would use:

qemu -cdrom /dev/hdc -boot d

Of course, we don’t just want to use CDs all the time —we might want to actually install something! So, we’ve got to make a hard drive image first. This is achieved by using qemu-img, like so:

qemu-img create virtualharddrive.qcow 5G -f qcow

This creates an image called virtualharddrive.qcow, which is 5 gigabytes big. If you wanted it to be 5 megabytes, you could type 5M instead, for ten gigabytes, type 10G, and so on.
Finally, the -f option tells qemu-img what format you want to use. While there are a few to choose from, qcow is the norm - it works well enough, and only takes up the space on the hard drive that it needs. If the virtual hard drive had a capacity of 5 gigabytes, but only contained 2 gigabytes of data, then it would only take up about 2 gigabytes on your real hard disk.

So, now we want to get installing. Simply type:

qemu virtualharddrive.qcow -cdrom path/to/installcd.iso -boot d

Then, you can simply follow the instructions just as with an ordinary installation.

Now, not all distributions come on one CD —in some cases, you’ll need to swap CDs. To achieve this, we need to do two things. First of all, we make the QEMU monitor appear on the command line by adding the option -monitor stdio, so we end up with something like:

qemu virtualharddrive.qcow -cdrom path/to/installcd.iso -boot d -monitor stdio

When you run this command, QEMU should let you enter commands. To change CDs, simply type in the commands:

eject cdrom

change cdrom path/to/newcd.iso

Voila! The virtual machine should now have changed CDs so you can continue the installation. Naturally, you can change CDs at any time, not just during installation.

Once the installation has finished, you’ll want to boot from the hard drive. Since QEMU does this by default, simply remove the -boot d part of the command:

qemu virtualharddrive.qcow -cdrom path/to/installcd.iso -monitor stdio

If you have no intention of using the CD after the installation, then you can cut that out as well:

qemu virtualharddrive.qcow -monitor stdio

This should let you play around with your newly installed system at your heart’s content without endangering your own PC. If you want to fiddle with something, but don’t want the changes written to the image, then add the option -snapshot. If, after using this option, you decide that you actually want to save the changes made to the hard drive, then simply type commit into the QEMU monitor, and the changes will be written.

While these commands work, unless you’re using lightweight distributions, you might find things going a little slowly. This is due to QEMU, by default, only taking up 128MB of RAM. You can increase the amount available by using the -m option, followed by the amount of RAM in megabytes. For instance, if I wanted to allocate 256MB to QEMU for running a LiveCD, then I would type:

qemu -cdrom path/to/livecd.iso -boot d -m 256

That should speed things up nicely! But don’t give QEMU too much memory —you want some left for your other applications. Unfortunately, things are probably still fairly slow— to speed things up even more, you’ll probably want to use kqemu, otherwise known as the QEMU accelerator.

Installing kqemu from the repositories is reasonably straightforward. First of, grab the kqemu-source package —if you apt-get is your package manager of choice, then the command used is:

apt-get install kqemu-source

If you don’t have module assistant already, you’ll need that installed as well:

apt-get install module-assistant

Then, type in the following commands (as root):

m-a prepare

m-a auto-install kqemu

That should be it! Now, every time you want to use the kqemu module, you first need to become root, and then type:

modprobe kqemu major=0

Then, as an ordinary user, QEMU will automatically use kqemu, which should help speed things up. If QEMU complains that it still cannot use kqemu, then you might not have the necessary permissions —try typing the following as root:

chmod 666 /dev/kqemu

Hopefully, kqemu should now be usable by QEMU.

There is one final option: -kernel-kqemu. This, in theory, speeds up the emulation even further. Unfortunately, it isn’t as simple as that. Firstly, the version of QEMU in the Debian repositories cannot use -kernel-kqemu. Secondly, even if it could, not all guest operating systems would work with this enabled —for instance, a recent distribution of GNU/Linux would probably work faster with this option (if it worked!), but Windows 98 just crashes.

QEMU is available from Debian Sarge and Ubuntu Warty.


虚拟机qemu的使用

Redflag Workstation 下已经带了qemu

#qemu-img create windows.img 2G #这个就是创建虚拟机磁盘,2G大小

#qemu -hda windows.img -cdrom /dev/acd0 -boot d -m 512 -enable-audio -localtime

-hda 是指定硬盘

-cdrom 指定使用光驱,这个参数后面必须要跟个文件名,可以是物理光驱/dev/hda,可以使用ISO文件,为文件的路径

-m 是指定虚拟机使用的内存大小,如512M

-boot 指定虚拟机启动用的设备,-boot后可以选择的参数是 a ,c ,d .其中a 是指从软盘启动,c 就是用硬盘启动,d 是光驱启动。

-enable-audio

-localtime 使用实际主板bios上的时间。

应该可以顺利的进入虚拟机了,并开始安装了。

Redflag Workstation 下已经带了qemu

#qemu-img create windows.img 2G #这个就是创建虚拟机磁盘,2G大小

#qemu -hda windows.img -cdrom /dev/acd0 -boot d -m 512 -enable-audio -localtime

-hda 是指定硬盘

-cdrom 指定使用光驱,这个参数后面必须要跟个文件名,可以是物理光驱/dev/hda,可以使用ISO文件,为文件的路径

-m 是指定虚拟机使用的内存大小,如512M

-boot 指定虚拟机启动用的设备,-boot后可以选择的参数是 a ,c ,d .其中a 是指从软盘启动,c 就是用硬盘启动,d 当然就是光驱启动了。

-enable-audio

-localtime 使用实际主板bios上的时间。

应该可以顺利的进入虚拟机了,并开始安装了。

安装过程中,要求换盘: 在qemu中按ctrl+alt+2切换到qemu monitor模式 输入?或help可以查看可用命令及使用说明。 (在其他版本的qemu中,运行qemu加载OS后,这个shell就会自动变成qemu monitor模式)

change device filename — change a removable media

如change cdrom /tmp/xp1.iso

常用的快捷键:

ctrl+alt+F 全屏切换

ctrl+alt 鼠标在虚拟机和主机间切换
虚拟机上网设置两种方式:

1. user mode network :
这种方式实现虚拟机上网很简单,类似vmware里的nat,qemu启动时加入-user-net参数,虚拟机里使用dhcp方式,即可与互联网通信,但是这种方式虚拟机与主机的通信不方便。

2. tap/tun network :
这种方式要比user mode复杂一些,但是设置好后 虚拟机互联网 虚拟机主机 通信都很容易
这种方式设置上类似vmware的host-only,qemu使用tun/tap设备在主机上增加一块虚拟网络设备(tun0),然后就可以象真实网卡一样配置它.

前提:

首先内核中支持tap/tuns设备:
Device Drivers —>
Networking support —>
[M] Universal TUN/TAP device driver support

在Redflag Workstation中已经有了支持

如果没有编译内核模块:make modules

将它cp到当前内核的模块目录的相应位置:
cp /usr/src/linux/drivers/net/tun.ko /lib/modules/`uname -r`/kernel/drivers/net
重新建立模块依赖关系:
depmod

再加载就OK了

如果成功了应该存在设备文件 /dev/net/tun

若使用这种方式需要以下几步:
a.需要一个tun/tap的初始化脚本 :
/etc/qemu-ifup :
#!/bin/sh
/sbin/ifconfig $1 172.20.0.1

再给予qemu-ifup x执行权限放在/etc下就可以了,这是有qemu自动执行的
这个脚本只能root用户执行,如果需要普通用户使用qemu,就需要改成sudo /sbin/ifconfig …..再设置sudo相关权限。 启动qemu后,它会在主机上增加一块虚拟网络设备(tun0)

b.设置虚拟机网络参数

和vmware host-only一样:
ip与tun0相同网段,gateway为tun0 ip 其余参数与tun0相同,dns与主机相同:
tun0 : ip :172.20.0.1 broadcast:172.20.255.255 netmask :255.255.0.0
qemu : ip :172.20.0.100 broadcast:172.20.255.255 netmask :255.255.0.0 gateway:172.20.0.1

与host-only一样,这样只实现了虚拟机主机间通信,还需要设置router,nat才可以连上互联网
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o eth0 -s 172.20.0.0/24 -j MASQUERADE

图形界面的使用:

首先安装qemu
我使用Debian, 一句apt-get install qemu命令即可安装, 或者你使用其它的方法.

安装qemu的图形界面前端qemu-laucher
因为我也不是完全的命令行爱好者, 所以我安装qemu-laucher来启动运行qemu. 见截图:

设置qemu, 安装WinXP
从桌面菜单启动qemu-launcher后, 1. 取消Snapshot Mode, 因为这个选项表示只有你按Ctrl+a s后才将虚拟机的运行结果写入虚拟机的磁盘文件.

2.选中Use CD-ROM, 因为我们需要光驱安装操作系统.

3. 在Hard Disk 0 一栏里填入你的虚拟磁盘在Linux下的文件, 只需要填入, qemu自会创建.

4. 默认内存为128MB, 你可以设置为192MB. 太大了qemu会无法运行虚拟机.

5. CPU, 声卡, 显卡等设置

Sunday, January 4, 2009

svn|命令

svn命令 通常都有帮助,可通过如下方式查询:

$ svn help 

知道了子命令,但是不知道子命令的用法,还可以查询:

$ svn help add 

开发人员常用命令

(1) 导入项目

$ cd ~/project
$ mkdir -p svntest/{trunk,branches,tags}
$ svn import svntest https://localhost/test/svntest --message "Start project"
...
$ rm -rf svntest

我们新建一个项目svntest,在该项目下新建三个子目录:trunk,开发主干;branches,开发分支;tags,开发阶段性标签。然后导入到版本库test下,然后把svntest拿掉。

(2) 导出项目

$ svn checkout https://localhost/test/svntest/trunk

修订版本号的指定方式是每个开发人员必须了解的,以下是几个参考例子,说明可参考svn推荐书。

$ svn diff --revision PREV:COMMITTED foo.c
# shows the last change committed to foo.c

$ svn log --revision HEAD
# shows log message for the latest repository commit

$ svn diff --revision HEAD
# compares your working file (with local changes) to the latest version
# in the repository

$ svn diff --revision BASE:HEAD foo.c
# compares your “pristine” foo.c (no local changes) with the
# latest version in the repository

$ svn log --revision BASE:HEAD
# shows all commit logs since you last updated

$ svn update --revision PREV foo.c
# rewinds the last change on foo.c
# (foo.c's working revision is decreased)

$ svn checkout --revision 3
# specified with revision number

$ svn checkout --revision {2002-02-17}
$ svn checkout --revision {15:30}
$ svn checkout --revision {15:30:00.200000}
$ svn checkout --revision {"2002-02-17 15:30"}
$ svn checkout --revision {"2002-02-17 15:30 +0230"}
$ svn checkout --revision {2002-02-17T15:30}
$ svn checkout --revision {2002-02-17T15:30Z}
$ svn checkout --revision {2002-02-17T15:30-04:00}
$ svn checkout --revision {20020217T1530}
$ svn checkout --revision {20020217T1530Z}
$ svn checkout --revision {20020217T1530-0500}

(3) 日常指令

$ svn update

$ svn add foo.file
$ svn add foo1.dir
$ svn add foo2.dir --non-recursive
$ svn delete README
$ svn copy foo bar
$ svn move foo1 bar1

$ svn status
$ svn status --verbose
$ svn status --verbose --show-updates
$ svn status stuff/fox.c

$ svn diff
$ svn diff > patchfile

$ svn revert README
$ svn revert

修改冲突发生时,会生成三个文件:.mine, .rOLDREV, .rNEWREV。比如:

$ ls -l
sandwich.txt
sandwich.txt.mine
sandwich.txt.r1
sandwich.txt.r2

解决修改冲突方式之一:修改冲突的文件sandwich.txt,然后运行命令:

$ svn resolved sandwich.txt

方式之二:用库里的新版本覆盖你的修改:

$ cp sandwich.txt.r2 sandwich.txt
$ svn resolved sandwich.txt

方式之三:撤销你的修改,这种方式不需要运行resolved子命令:

$ svn revert sandwich.txt
Reverted 'sandwich.txt'
$ ls sandwich.*
sandwich.txt

确保没问题后,就可以提交了。

$ svn commit --message "Correct some fatal problems"
$ svn commit --file logmsg
$ svn commit

(4) 检验版本历史

$ svn log
$ svn log --revision 5:19
$ svn log foo.c
$ svn log -r 8 -v

$ svn diff
$ svn diff --revision 3 rules.txt
$ svn diff --revision 2:3 rules.txt
$ svn diff --revision 4:5 http://svn.red-bean.com/repos/example/trunk/text/rules.txt

$ svn cat --revision 2 rules.txt
$ svn cat --revision 2 rules.txt > rules.txt.v2

$ svn list http://svn.collab.net/repos/svn
$ svn list --verbose http://svn.collab.net/repos/svn

$ svn checkout --revision 1729 # Checks out a new working copy at r1729

$ svn update --revision 1729 # Updates an existing working copy to r1729

(5) 其他有用的命令

svn cleanup

为失败的事务清场。

(6) 分支和合并

建立分支方法一:先checkout然后做拷贝,最后提交拷贝。

$ svn checkout http://svn.example.com/repos/calc bigwc
A bigwc/trunk/
A bigwc/trunk/Makefile
A bigwc/trunk/integer.c
A bigwc/trunk/button.c
A bigwc/branches/
Checked out revision 340.

$ cd bigwc
$ svn copy trunk branches/my-calc-branch
$ svn status
A + branches/my-calc-branch

$ svn commit -m "Creating a private branch of /calc/trunk."
Adding branches/my-calc-branch
Committed revision 341.

建立分支方法二:直接远程拷贝。

$ svn copy http://svn.example.com/repos/calc/trunk \
http://svn.example.com/repos/calc/branches/my-calc-branch \
-m "Creating a private branch of /calc/trunk."

Committed revision 341.

建立分支后,你可以把分支checkout并继续你的开发。

$ svn checkout http://svn.example.com/repos/calc/branches/my-calc-branch

假设你已经checkout了主干,现在想切换到某个分支开发,可做如下的操作:

$ cd calc
$ svn info | grep URL
URL: http://svn.example.com/repos/calc/trunk
$ svn switch http://svn.example.com/repos/calc/branches/my-calc-branch
U integer.c
U button.c
U Makefile
Updated to revision 341.
$ svn info | grep URL
URL: http://svn.example.com/repos/calc/branches/my-calc-branch

合并文件的命令参考:

$ svn diff -r 343:344 http://svn.example.com/repos/calc/trunk
$ svn merge -r 343:344 http://svn.example.com/repos/calc/trunk
$ svn commit -m "integer.c: ported r344 (spelling fixes) from trunk."
$ svn merge -r 343:344 http://svn.example.com/repos/calc/trunk my-calc-branch
$ svn merge http://svn.example.com/repos/branch1@150 \
http://svn.example.com/repos/branch2@212 \
my-working-copy
$ svn merge -r 100:200 http://svn.example.com/repos/trunk my-working-copy
$ svn merge -r 100:200 http://svn.example.com/repos/trunk
$ svn merge --dry-run -r 343:344 http://svn.example.com/repos/calc/trunk

最后一条命令仅仅做合并测试,并不执行合并操作。

建立标签和建立分支没什么区别,不过是拷贝到不同的目录而已。

$ svn copy http://svn.example.com/repos/calc/trunk \
http://svn.example.com/repos/calc/tags/release-1.0 \
-m "Tagging the 1.0 release of the 'calc' project."

$ ls
my-working-copy/
$ svn copy my-working-copy http://svn.example.com/repos/calc/tags/mytag
Committed revision 352.

后一种方式直接把本地的工作拷贝复制为标签。

此外,你还可以删除某个分支。

$ svn delete http://svn.example.com/repos/calc/branches/my-calc-branch \
-m "Removing obsolete branch of calc project."

管理人员常用命令

(7) 版本库管理

$ svnadmin help
...
$ svnadmin help create
...
$ svnadmin create --fs-type bdb /usr/local/repository/svn/test
$ chown -R svn.svn /usr/local/repository/svn/test

建立版本库,库类型为bdb(使用Berkeley DB做仓库),库名称为test。
svn版本库有两种存储方式:基于Berkeley DB(bdb)或者基于文件系统(fsfs),通过 –fs-type可指定存储方式。

(8) 查询版本库信息

$ svnlook help
...
$ svnlook help tree
...
$ svnlook tree /usr/local/repository/svn/test --show-ids