Bowen's Blog

Respect My Authorita.

Change System Library Path

| Comments

找不到共享文件是我们在native build的时候经常遇到的问题,有一种情况是操作系统上确实没有该共享文件,还有一种是含有该共享文件的目录没有被加入到library 查询的路径中。

假设现在我们需要把 /opt/lib/lib.test.so 加入到library path中. 在不想影响到系统配置的情况下,我们可以修改环境变量:

1
export LD_LIBRARY_PATH=/opt/lib:$LD_LIBRARY_PATH

easy right? 如果你确信你是想一劳永逸的把/opt/lib/加入到library path中,那么可以直接修改ldconfig的配置:

1
2
[vagrant@localhost ~]$ sudo ldconfig | grep opt
[vagrant@localhost ~]$

目前尚未出现/opt/。 怎么办呢?直接去修改/etc/ld.so.conf.d/ 下的配置文件。

1
2
3
4
5
6
7
8
9
[vagrant@localhost ~]$ ls -al /etc/ld.so.conf.d/
total 16
drwxr-xr-x.  2 root root 4096 Mar  9  2013 .
drwxr-xr-x. 65 root root 4096 Nov 18 10:04 ..
-r--r--r--.  1 root root  324 Feb 22  2013 kernel-2.6.32-358.el6.x86_64.conf
-rw-r--r--.  1 root root   17 Dec  7  2012 mysql-x86_64.conf

[vagrant@localhost ~]$ less /etc/ld.so.conf.d/mysql-x86_64.conf
/usr/lib64/mysql

看上去规则很简单,第一步为app添加配置文件,第二步在文件中写入library的路径。

1
sudo echo “/opt/lib” > /etc/ld.so.conf.d/opt.conf

make it happen

1
2
3
4
5
[vagrant@localhost ld.so.conf.d]$ sudo ldconfig

[vagrant@localhost ld.so.conf.d]$ ldconfig -v | grep opt
/opt/lib:
ldconfig: File /opt/lib/lib.test.so is empty, not checked.    #测试文件

So, are you happy now?

Automately Install Dmg Package Under OSX

| Comments

The common way install a new app in osx is like:

  1. Install from AppStore;
  2. Download *.dmg file and open it then click install script;

For the App that can only be installed from *.dmg file, there’s a better way to automate the install process. The second way includes following steps:

  1. Download the package-*.dmg file;
  2. Mount *.dmg file to file system- e.g /Volumes/app_name
  3. Install app with the installer or *.pkg file

Given we want to install vagrant: For the first step, say curl -O download_link for people don’t have tools like wget. If you have wget, say installed through brew brew install wget, you can just use wget.

For the second step, we can use hdiutil command to mount the dmg file to /Volumes.

For the last step, the installer in osx perfectly solve the issue. Command like:

1
sudo installer -store -pkg /Volumes/Vagrant/Vagrant.pkg -target /

And it would use the administrator previlidge to install the package.

For the whole install process, the only thing you need to do is type in the password. And this is an example for fulfilling the automation. Surely it can be improved more genericly, for I only use this for vagrant session.^_^

Record and Play Back Terminal Session

| Comments

Nowadays, lots of people would use video to record a tutorial for demonstrating. It’s cool for vividly showing everything to people. The only issue is that file size of video is relatively large. From this point of view, the old way of recording actions from console might be a better idea.

script and scriptreplay are console recording/playing tools. script would record time and actions of current session in two different files. And scriptreplay would play those actions again in the console。 It’s quite easy to use these tools, just like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[vagrant@bogon shell]$ script -t 2>timing.log -a output.session
Script started, file is output.session
[vagrant@bogon shell]$ ls -al .
total 24
drwxrwxr-x 2 vagrant vagrant 4096 Nov  5 09:37 .
drwx------ 4 vagrant vagrant 4096 Nov  4 12:15 ..
-rw-rw-r-- 1 vagrant vagrant 4400 Nov  5 09:42 output.session
-rw-rw-r-- 1 vagrant vagrant   55 Nov  4 12:16 test.txt
-rw-rw-r-- 1 vagrant vagrant  111 Nov  5 16:21 timing.log
[vagrant@bogon shell]$ cd /tmp/
[vagrant@bogon tmp]$ ls
input.txt  ks-script-iuQ695  ks-script-iuQ695.log  output.txt  stderr  try.txt  vboxguest-Module.symvers  yum.log
[vagrant@bogon tmp]$ touch something.txt
[vagrant@bogon tmp]$ exit
exit
Script done, file is output.session

So the -t would record the time and -a would record actions to different files. In the command below, we also redirect the error message to timing.log file. And if we want to replay it, just use the scriptreplay command, like this:

1
scriptreplay timing.log output.session

Then it would play what you have done before. The thing you need to pay attention to is that such recording just happen in a session, so actions like switching user to do something would break that, to my understanding. Personally, I would use su user -c 'commands' to solve this issue.

It might not be a feature used often times, but it probably would help if you want to share or show actions you’ve done to others.

Math in Shell

| Comments

最近半年的所作的项目中,接触了些Ops的工作,自动化工具如Chef/Puppet都用过些。这些工具都很不错,但是过程当中会遇到一些问题,需要你基础的系统/shell知识,从而认识到了其重要性,开始系统学习,目前阅读的书为: 以及浏览china-unix论坛。

Shell的强大是毋庸置疑的,所以进行数学运算也就是理所应当具有的功能。Shell可以用declare/let/(( ))/[]进行一些简单的数学运算。 Shell 支持的运算:

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
ARITHMETIC EVALUATION
       The  shell  allows arithmetic expressions to be evaluated, under certain circumstances (see the let and declare builtin commands and Arithmetic Expansion).
       Evaluation is done in fixed-width integers with no check for overflow, though division by 0 is trapped and flagged as an error.  The  operators  and  their
       precedence, associativity, and values are the same as in the C language.  The following list of operators is grouped into levels of equal-precedence opera-
       tors.  The levels are listed in order of decreasing precedence.

       id++ id--
              variable post-increment and post-decrement
       ++id --id
              variable pre-increment and pre-decrement
       - +    unary minus and plus
       ! ~    logical and bitwise negation
       **     exponentiation
       * / %  multiplication, division, remainder
       + -    addition, subtraction
       << >>  left and right bitwise shifts
       <= >= < >
              comparison
       == !=  equality and inequality
       &      bitwise AND
       ^      bitwise exclusive OR
       |      bitwise OR
       &&     logical AND
       ||     logical OR
       expr?expr:expr
              conditional operator
       = *= /= %= += -= <<= >>= &= ^= |=
              assignment
       expr1 , expr2
              comma

可以看到涵盖了通用的编程语言中的数学运算方式,那么在shell中可以通过那些命令来进行计算呢?

declare: 声明变量同时定义其属性。

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
n=1*2*9/3
echo $n
declare -i n
n=1*2*9/3
echo $n

result:
1*2*9/3

6

从上面的例子可以看出,只有将变量声明为整数,shell才会认为你是在尝试进行数学运算而不是单纯的字符串变量。

expr: 数学表达式运算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
z=`expr 5 + 1`
echo $z
a=`expr 7 % 2`
echo $a
d=`expr 1 \& 0`
echo $d
#error
b=`expr 7*2`
echo $b

result:
6
1
0
7*2

expr后所带的表达式运算符必须以空格分开,不然会被当做字符串,不会进行运算,一些meta字符比如& %等,必须转义才可以执行。

let: 后面跟随数学表达式并进行运算

1
2
3
4
5
6
7
8
9
10
11
[vagrant@localhost shell]$ a=2
[vagrant@localhost shell]$ b=2
[vagrant@localhost shell]$ let result=a+b
[vagrant@localhost shell]$ echo $result
4
[vagrant@localhost shell]$ let result++
[vagrant@localhost shell]$ echo $result
5
[vagrant@localhost shell]$ let result--
[vagrant@localhost shell]$ echo $result
4

同样,在计算的时候需要用空格分隔。

(( ))[]也可以起到类似的作用。参加如下的例子:

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
[vagrant@localhost shell]$ a=1
[vagrant@localhost shell]$ b=2
[vagrant@localhost shell]$ result=$[ a + b ]
[vagrant@localhost shell]$ echo $result
3


[vagrant@localhost shell]$ result=$[ $a + b ]
[vagrant@localhost shell]$ echo $result
3


[vagrant@localhost shell]$ result=$(($a + 50))
[vagrant@localhost shell]$ echo $result
51

[vagrant@localhost shell]$ result=$(($a - 50))
[vagrant@localhost shell]$ echo $result
-49

[vagrant@localhost shell]$ echo $((16#2a))  #进制转换,将16进制的”2a”转换为十进制的数字, 相当酷帅吊炸天
42

[vagrant@localhost shell]$ echo $((2#1111))
15

(())或者[]中的变量也可以加或者不加$符号从表达式外获取。以上这些命令只能处理整数运算,如果想要进行浮点数运算,就得求助bc命令了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[vagrant@localhost shell]$ echo "5 * 0.86" | bc
4.30

[vagrant@localhost ~]$ n=40
[vagrant@localhost ~]$  echo "$n * 0.86" | bc
34.40

[vagrant@localhost ~]$ echo "scale=3;4/7" | bc   #指定十进制浮点运算的精度
.571

[vagrant@localhost ~]$ umask
0002
[vagrant@localhost ~]$ echo "obase=8;$(( 8#666 & (8#777 ^ 8#$(umask)) ))" | bc
664

[vagrant@localhost ~]$ no=100
[vagrant@localhost ~]$ echo "obase=2;$no" | bc   #进制转换, 10进制-> 2进制
1100100

[vagrant@localhost ~]$ echo "sqrt(100)" | bc #开方
10

[vagrant@localhost ~]$ echo "10^10" | bc   #次方计算
10000000000

bc的功能远超上面所列举的例子,想仔细研究的话就 man bc…., enjoy。

references:1, 2, 3

Learning Centos: Yum(4) Advanced Commands & Etc

| Comments

前一篇 YUM基本命令,介绍了工作中比较常用的一些命令。除此之外,YUM还有些不太常用的但是还算比较有趣的命令,在此我们稍微探索发现下。

yum shell: yum内建的交互式shell

在这个shell中,我们可以进行各种操作,比如罗列所有repo,搜索/查看软件信息等等。eg:

1
2
3
4
5
6
7
8
9
10
[vagrant@localhost ~]$ sudo yum shell
Loaded plugins: fastestmirror
Setting up Yum Shell
> search vim-common
Loading mirror speeds from cached hostfile
 * base: mirrors.btte.net
 * extras: mirrors.stuhome.net
 * updates: mirrors.btte.net
N/S Matched: vim-common ==============================================================================
vim-common.x86_64 : The common files needed by any version of the VIM editor

好处非常明显,就是免掉了每次去多输入”yum”。实际的测试发现,除了Install/Remove之外,大多数的命令都可以在这个shell下执行。其中的原因不太明白,也没有找到合理的解释。

yum history: 记录所有yum transaction的历史 yum 的transaction 记录都被保存在/var/lib/yum/history/ 下的sqlite db文件中,通过history命令,我们可以查看到软件安装的详细记录,执行者,具体操作,影响的package等。

1
2
3
4
5
6
7
8
9
10
11
 [vagrant@localhost ~]$ sudo yum history
Loaded plugins: fastestmirror
ID     | Login user               | Date and time    | Action(s)      | Altered
-------------------------------------------------------------------------------
 7 |  <vagrant>               | 2013-10-26 06:30 | Install        |   58
 6 |  <vagrant>               | 2013-10-26 05:08 | Install        |    3
 5 |  <vagrant>               | 2013-09-25 08:25 | Install        |    3
 4 |  <vagrant>               | 2013-09-24 07:04 | Install        |    1
 3 |  <veewee>                | 2013-03-09 15:50 | Install        |   16  <
 2 |  <veewee>                | 2013-03-09 15:49 | I, U           |   40 >
 1 | System <unset>           | 2013-03-09 15:37 | Install        |  199

通过ID可以查找Transaction发生时候的相关信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[vagrant@localhost ~]$ sudo yum history info 3
Loaded plugins: fastestmirror
Transaction ID : 3
Begin time     : Sat Mar  9 15:50:00 2013
Begin rpmdb    : 239:6f5a8ecd22e6f0a663940801ef56d66b2ad40228
End time       :            15:50:07 2013 (7 seconds)
End rpmdb      : 255:92e8267085fc364911baebaf646b14856fb20498
User           :  <veewee>
Return-Code    : Success
Command Line   : -y install puppet facter
Transaction performed with:
    Installed     rpm-4.8.0-32.el6.x86_64                       @anaconda-CentOS-201303020151.x86_64/6.4
    Installed     yum-3.2.29-40.el6.centos.noarch               @anaconda-CentOS-201303020151.x86_64/6.4
    Installed     yum-plugin-fastestmirror-1.1.30-14.el6.noarch @anaconda-CentOS-201303020151.x86_64/6.4
Packages Altered:
    Dep-Install augeas-libs-0.9.0-4.el6.x86_64        @base
    Dep-Install compat-readline5-5.2-17.1.el6.x86_64  @base
    Dep-Install dmidecode-1:2.11-2.el6.x86_64         @base
    Install     facter-1:1.6.17-1.el6.x86_64          @puppetlabs
    Dep-Install hiera-1.1.2-1.el6.noarch              @puppetlabs
    ...

既然可以记录Transaction,那么势必也会提供undo和redo的功能:

1
2
   sudo yum history undo 7  #将Transaction 7中安装的所有软件都删除
   sudo yum history redo 8  #将Transaction 7中删除的软件重新安装

对于系统管理员来说,这应该算得上是个比较有用的命令。前面提到yum的交易记录数据库文件保存在 /var/lib/yum/history/下面,如果想将Transction记录到新的db文件中,可以用yum history new,它会清空/var/lib/yum/history/目录,然后新建db文件。想打开某个history记录文件可以用yum load-transaction file_name

yum provides : 通过软件安装后的可执行文件,反查安装它的package

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
vagrant@localhost ~]$ yum provides /usr/bin/vim
1:vim-enhanced-7.2.411-1.8.el6.x86_64 : A version of the VIM editor which includes recent enhancements
Repo        : installed
Matched from:
Other       : Provides-match: /usr/bin/vim

[vagrant@localhost ~]$ yum provides wget
wget-1.12-1.8.el6.x86_64 : A utility for retrieving files using the HTTP or FTP protocols
Repo        : installed
Matched from:
Other       : Provides-match: wget

[vagrant@localhost ~]$ yum provides /*/vi
1:vim-minimal-7.2.411-1.8.el6.x86_64 : A minimal version of the VIM editor
Repo        : installed
Matched from:
Other       : Provides-match: /bin/vi

这个命令还是略有用处的。

yum downgrade: 版本回滚

在持续部署出错时候,使用downgrade可以顺利进行版本的回滚。

好吧,熟悉了这些个命令和相关配置,我相信,从使用的角度讲,已经相当的足够了。下一篇将尝试搭建自己的yum repo服务器。

Be Careful When You Playing With History Database

| Comments

When we talking about History database, we are generally saying the database that deposit the actions performed on other databases by application. It is freaking important for recording the critical action that performed by users in both business and development point of view. With the help of ChangeLog database, we can easily track that an purchased transaction has or has not happened at certain time, thus by querying this table, we will 100% sure if the user wants to confirm with the transaction.

A typical table, say ChangeLog in History database, would have such kind of schema:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
mysql> show create table ChangeLog\G
*************************** 1. row ***************************
       Table: ChangeLog
Create Table: CREATE TABLE `ChangeLog` (
  `ID` bigint(40) NOT NULL AUTO_INCREMENT,
  `TableName` varchar(40) DEFAULT NULL,
  `ForeignKey` varchar(40) DEFAULT NULL,
  `Who` varchar(255) DEFAULT NULL,
  `Field` varchar(40) DEFAULT NULL,
  `Mod` int(11) NOT NULL DEFAULT '0',
  `OldValue` text,
  `NewValue` text,
  `Comments` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`ID`,`Mod`),
  KEY `idx1` (`TableName`,`ForeignKey`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

The schema shows that it would record action done on some table by someone from certain value to other value on certain time. Two columns, TableName and ForeignKey, are indexed, which would be faster if you take those two in the where clause. The thing you need to aware is that index of ForeignKey is only working when TableName is also existed in the where clause.

History database is normally huge and rarely has replica, as we can imagine. The reason, I suspect, is the changing-write/read operation much more frequently than other tables. Normally, it has several following characters:

  1. Query/Scan/Join operation could cause performance on database for it has large bunch of data;
  2. It’s pretty pretty important that I don’t need to talk about this twice;
  3. Things in the history database is genuine, application or log can fake but the history table does not lie;
  4. Since it’s might be the only database, clearly it’s not

Due to the importance of History database, few people would have access. But there gonna be chance that you would talk to it. And the following are the tips I learned from a senior developer about interacting with History database:

  1. Pair with someone who are experienced and can make sure the query you write would not do harm to the database;
  2. Check the ChangeLog table schema first, attention whether it has some columns indexed;
  3. Use “–“ in the beginning to comment out your query in case if you hit enter before you complete the query;
  4. To “explain" the query you want to run firstly, to see whether the query would happen in a very large dataset, if it is, then you need to consider the consequent for running the query.
1
2
3
4
5
6
7
mysql> Explain select * from ChangeLog where TableName = '' and ForeignKey = '';
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
| id | select_type | table     | type | possible_keys | key  | key_len | ref         | rows | Extra       |
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
|  1 | SIMPLE      | ChangeLog | ref  | idx1          | idx1 | 86      | const,const |   61 | Using where |
+----+-------------+-----------+------+---------------+------+---------+-------------+------+-------------+
1 row in set (0.47 sec)

What we can see from the result of explain is that the query would happen only in 61 rows, which would barely cause performance issue on the database. Of course, good to go.

Set Locale in Debian Wheezy

| Comments

最近和一个客户聊天,说到一个很有意思的事情。他认为,在产品环境使用*inx的情况下,公司给开发人员普遍 配发Mac是不太合适的, 当然这个是见仁见智了。不过,如果对产品环境的系统熟悉的话,在遇到一些问题的时候,可以节省些时间。

想把Mac Book/Air操作系统更换成Linux代价还是略高,所以最好的办法是用虚拟机,必须就是Vagrant了。今天在使用Debian7 Wheezy的时候,就遇到了关于Locale的简单问题。

locale是关于系统语言和区域选择,目的是为了表示正确的时间和数字格式。悲剧的是,下载的debian 7的virtual box默认竟然是用法语,提示信息各种看不懂。只好自己查找资料修改了。

最初使用的方法是修改LANG/LC_ALL的环境变量:

1
2
  echo -e "export LANG=en_us.UTF-8 \nexport LC_ALL=en_us.UTF-8" >> ~/.bash_profile;
  source ~/.bash_profile

奇怪的是,并非是所有的输出消息都变成了英文,仍然有法语出现。于是,尝试从根上修改:

1
2
  sudo vi /etc/default/locale
  LANG=en_US.UTF-8

重启系统,按照常理应该已经OK,怎知还是出现警告,说设置locale失败。技穷只能求助文档,先查看了下当前 locale中已有的语言环境:

1
2
3
4
5
  vagrant@vagrant-debian-wheezy:~$ locale -a
  C
  C.UTF-8
  POSIX
  fr_FR.utf8

我去,只有这么几个选项,只能重头添加en_US.UTF-8了。所幸并不是很困难:

1
  sudo /usr/sbin/dpkg-reconfigure  locales

在弹出的选项框中,选中en_US.UTF-8确定即可。这个时候/etc/default/locale文件也被修改成支持en_US.UTF-8,运行下 locale -a :

1
2
3
4
5
6
vagrant@vagrant-debian-wheezy:~$ locale -a
C
C.UTF-8
POSIX
en_US.utf8
fr_FR.utf8

Nice. reference

Learning Yum(3): Basic Commands

| Comments

对于包管理工具来说,安装/删除/查看包信息/升级软件包/搜索查找/清除metadata, 都是必须提供的功能。yum的基本命令操作同样也可以概括为:

1
install/(remove|erase|downgrade)/(list|info)/(update|upgrade)/search/clean

安装package:
yum安装package有两种方式,一种是安装单个/多个的package,一种是安装软件组。

1
2
3
yum install package_names
yum install  -y(--ausumeyes) package_names
yum install --enablerepo=drivers nvidia-driver

install的时候可以指定或者不指定具体的版本号,正常安装的时候会提示是否确认安装。为了偷懒或者在自动化部署的时候,想在跳过提示,可以加上个-y的参数,就可以了。有些repo可能默认被disable了,只想在单次的yum install的时候enable,可以加上 --enablerepo=<repo name>.

组安装:
想知道当前repo都提供了哪些package group,可以用yum -v grouplist,可以知道组名及组id。

1
2
3
4
5
6
7
8
9
[vagrant@localhost ~]$ yum -v grouplist kde\*
Config time: 0.009
Yum Version: 3.2.29
Setting up Group Process
rpmdb time: 0.001
group time: 0.159
Available Groups:
   KDE Desktop (kde-desktop)   # KDE Desktop 组的id 就是(kde-desktop)
Done

组安装的有几种不同形式,但是结果都是相同的,如下的几个命令都会安装KDE Desktop的groupyum groupinstall “KDE Desktop”

1
2
yum groupinstall kde-desktop
yum install @kde-desktop

删除package:

1
2
3
4
yum remove package_name
yum remove glibc*
yum erase package_name/glob expression
yum groupremove  #删除软件组

开始我以为remove和erase是有区别的,但是后来在fedora社区论坛上发现其实它们没什么区别,很汗啊,上次分享的时候我还信誓旦旦的说了来着,都是被apt搞混了。

查看package信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
yum info package_name   等同于rpm -q --info package_name
vagrant@localhost ~]$ yum info vim-common
Installed Packages
Name        : vim-common
Arch        : x86_64
Epoch       : 2
Version     : 7.2.411
Release     : 1.8.el6
Size        : 17 M
Repo        : installed
From repo   : base
Summary     : The common files needed by any version of the VIM editor
URL         : http://www.vim.org/
License     : Vim and GPLv2+ and BSD and LGPLv2+ and Open Publication
Description : VIM (VIsual editor iMproved) is an updated and improved version of the
            : vi editor.  Vi was the first real screen-based editor for UNIX, and is........

可以看到package相关的所有信息,

罗列packages信息:

1
yum list [available|installed|extras|updates|obsoletes|all|recent] [pkgspec]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
yum list available 列出当前enable的repo中所有可用package
yum list installed 列出当前系统中已经安装的package
yum list extras 列出已安装的package中不在enable的repo中的package
yum list updates  列出当前enable的repo中可以提供更新的package
yum list obsoletes 列出enable的repo中或者已安装的package中被淘汰的package
yum list all 列出所有enable的repo中的package
yum list recent 列出所有enable的repo中最近一周添加的package
yum list installed "krb?-*" 以上所有的list命令都支持通配符,罗列相关的package信息
yum list package spec  为特定的package列出相关信息,比如列出glibc的信息
[vagrant@localhost yum.repos.d]$ yum list glibc
Installed Packages
glibc.x86_64                                                       2.12-1.107.el6                                                            @anaconda-CentOS-201303020151.x86_64/6.4
Available Packages
glibc.i686                                                         2.12-1.107.el6_4.2                                                        updates
glibc.x86_64                                                       2.12-1.107.el6_4.2                                                        updates

yum list  \*.i686  #glob expression

罗列组信息:

1
2
yum grouplist
yum -v grouplist  "KDE*"

查看repo信息:

1
yum repolist

更新package:

1
2
3
4
5
6
7
8
9
10
yum check-update  #检查有更新的软件,返回的值分别为可以更新的package所在repo已经最新的版本号
yum check-update glibc*   #检查单一的package,支持通配符
[vagrant@localhost yum.repos.d]$ yum check-update
glibc.x86_64                                                                            2.12-1.107.el6_4.2                                                                    updates

yum update   #升级所有已安装package有更新的package
yum update glibc #升级某一个package
yum update glibc*  #升级某一票package

yum upgrade  #和update类似,但是会考虑淘汰的package

搜索软件包:
yum search yum 提供了软件包搜索的功能,可以通过关键字(package描述|package名)搜索到你想要的软件包。

1
2
3
4
5
6
7
8
9
10
vagrant@localhost yum.repos.d]$ yum search vim
=================================================================================== Matched: vim ====================================================================================
vim-X11.x86_64 : The VIM version of the vi editor for the X Window System
vim-common.x86_64 : The common files needed by any version of the VIM editor
vim-enhanced.x86_64 : A version of the VIM editor which includes recent enhancements
vim-minimal.x86_64 : A minimal version of the VIM editor

[vagrant@localhost yum.repos.d]$ yum search "vi editor" #只需要描述软件提供的功能,便可以查找出提供该功能的软件包
============================================================================== N/S Matched: vi editor ===============================================================================
vim-X11.x86_64 : The VIM version of the vi editor for the X Window System

清理本地cached metadata 和package:
减少占用的磁盘空间,或者处理metadata出错的情况:

1
2
3
4
5
yum clean  #默认清理cached package和metadata,cache的目录是/var/cache/yum/
yum clean packages #只清理cached package
yum clean metadata #只清理metadata
yum clean dbcache   #清理yum的sqllite数据库文件
yum clean all     #清理

常用的yum 命令大概就有这么些,我们可以发现:
1. yum基本的命令使用模式比较接近, 而且都是正常人的使用逻辑;
2. yum的命令一般都会支持通配符的操作,用法比较统一;
下一次我们将介绍一些平常不太用yum但是还有点意思的命令。

Learning Yum(2): Utils and Plugins

| Comments

Yum 提供了一些列的utils以及plugins来增强自身的功能,比如yum-config-manager,fastestmirror等。

1
2
3
4
5
6
7
8
9
10
11
[vagrant@localhost ~]$ yum info yum-utils
.......
Description:
yum-utils is a collection of utilities and examples for the yum package manager. It includes utilities by different authors that make yum easier and more powerful to use. These tools include:
: debuginfo-install, find-repos-of-install, repodiff,
: needs-restarting, package-cleanup, repoclosure,
: repo-graph, repomanage, repoquery, repo-rss, reposync,
: repotrack, show-installed, show-changed-rco, verifytree,
: yum-builddep, yum-complete-transaction, yumdownloader,
: yum-config-manager, yum-debug-dump,
: yum-debug-restore and yum-groups-manager.

它们各自的作用可以很容易从自己的命名上看的出来, 我们挑几看上去有意思的命令研究下, 比如yum-config-manager,yumdownloader,find-repos-of-install等。

yum-config-manager: yum配置管理工具 显示base这个repo显示的所有的配置项以及其对应的值:

1
2
3
4
5
6
7
8
9
10
11
12
13
yum-config-manager base
[vagrant@localhost ~]$ yum-config-manager base
======== repo: base
[base]
base_persistdir = /var/lib/yum/repos/x86_64/6
baseurl = http://mirrors.163.com/centos/6/os/x86_64/
cache = 0
cachedir = /var/tmp/yum-vagrant-isJUZ8/x86_64/6/base
enabled = True
name = CentOS-6 - Base
retries = 10
timeout = 30.0
......

修改base这个repo的enabled选项为false同时保存:

1
2
3
4
5
6
[vagrant@localhost ~]$ sudo yum-config-manager base --setopt=“base.enabled=0” --save
[vagrant@localhost ~]$ yum-config-manager base
======== repo: base
[base]
enabled = False
......

yumdownloader: 从yum的repo下载rpm package

1
2
3
yumdownloader  --destdir /tmp/  ConsoleKit-devel-0.4.1
ConsoleKit-devel-0.4.1-3.el6.i686.rpm      |  15 kB     00:00
ConsoleKit-devel-0.4.1-3.el6.x86_64.rpm    |  15 kB     00:00

find-repos-of-install: 找到已安装package的repo源

1
2
[vagrant@localhost ~]$ find-repos-of-install vim-common
2:vim-common-7.2.411-1.8.el6.x86_64 from repo base

试用了一些个utils命令,觉得没有那些是特别有用的,只是能够稍微提高下效率,比如yum-config-manager,可以直接通过这个命令修改配置。

当你使用yum命令的时候,会提示你加载的plugins。相比之下,yum的plugins就显得有用多了。yum有很多plugins,aliases, axelget等都是我觉得比较有用的。配置yum plugins的入口有三个地方,我们以yum-aliases这个plugin作为示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[vagrant@localhost ~]$ sudo yum install yum-aliases #安装插件
/etc/yum.conf
  plugins=1/0                      #使用/不试用所有的插件
/etc/yum/pluginconf.d/aliases.conf
  [main]
  enabled=1/0                      #使用/不试用aliases插件
  # conffile - config. file to use
  # <default> = /etc/yum/aliases.conf
/etc/yum/aliases.conf
  FORCE --skip-broken --disableexcludes=all
  up   upgrade
  in   install
  rm   remove
  .........

/etc/yum/aliases.conf中定义了yum很多命令的alias,比如 in => install, 这样我就可以用yum in package_name,少敲几个字,节省体力。alias到option的用处会更大些,比如上面的FORCE。乐意的话,我们也可以按照个人喜好自己定制这个aliases的配置文件。

最近用过的一个比较有用的yum plugin是axelget,作用是从repo中下载package的时候支持多线程,加快下载速度。yum依赖于libcurl,默认应该是只能支持单线程的,如果网络条件一般的话,速度会比较慢。axelget 插件的用法:

1
2
3
4
5
 echo -e "[main]\nenabled=1\nonlyhttp=1\nenablesize=300000\ncleanOnException=1\maxconn=10" > /etc/yum/pluginconf.d/axelget.conf

 curl -o   /usr/lib/yum-plugins/axelget.py  http://yum-axelget.googlecode.com/svn/trunk/axelget.py

 rpm -i http://pkgs.repoforge.org/axel/axel-2.4-1.el5.rf.x86_64.rpm

axelget.conf 文件中的maxconn意为同时下载的线程数量。在我们项目中,从build pipeline打包出来的rpm package是保存在企业数据中心的repo中,但是测试环境是亚马逊的美国EC2节点,处于不同的网络中。如果是自动化部署,对于一个70Mb左右的package来说,安装更新消耗的时间大概是11分钟左右,使用axelget插件后,消耗的时间不到2分钟,下载速度提升很明显。虽然还是有点慢,但是已经处于可接受范围了。如此,每次在测试环境部署新的package的时间就大大降低了。

如果是想在某次的yum的transaction中禁止某个plugin的话,只需要加上 –disableplugin=plugin_name的option就好了,比如:

1
yum update --disableplugin=aliases, refresh-pack*

在开始下一篇关于yum的blog前,给大家介绍yum自动补齐的工具,懒人必备。

1
2
rpm -i http://pkgs.repoforge.org/bash-completion/bash-completion-20060301-1.el6.rf.noarch.rpm
source /etc/bash_completion

有了自动补齐后,就不用担心忘记命令或者费劲敲键盘了。

关于配置和插件等,基本就是这样了,下次介绍yum基本的命令用法。

Learning Yum(1): Config Files Explained

| Comments

Yum是RedHat系列中(如Centos, Fedora)等软件包管理工具,全称为Yellow Dog Updater, Modified, 在RPM的基础上,它可以自动解析安装软件的依赖。最早接触Redhat的时候时的版本是Redhat9, 实在是不堪忍受各种坑爹的依赖,所以后来接触Debian/Ubuntu之后才有大喜过望的感觉。最近想多熟悉几个系统,Centos/Arch Linux都在之列(没有Fedora的原因它发布比较快,不是特别稳定),在都使用相同内核的前提,这些发行版本的最大的不同也许就是包/软件管理器了,这就是为什么我要先开始熟悉yum了…….

这里下载Centos的virtual box镜像,添加到Vagrant中,你就可以拥有一个Centos的环境了,然后深度接触yum。

开始之前,得先知道 yum 相关的配置文件都在什么地方,

1
man yum.conf

在帮助文件的底部我们就可以看到几乎所有的yum相关的配置文件:

1
2
3
4
5
6
7
FILES
   /etc/yum.conf
   /etc/yum.repos.d/
   /etc/yum/pluginconf.d/
   /etc/yum/protected.d
   /etc/yum/vars
   /etc/yum/version-groups.conf

yum 主配置文件:

1
/etc/yum.conf

如下为yum.conf的示例文件:

1
2
3
4
5
6
7
8
9
10
11
12
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=1
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=16&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release

可以看到配置文件中有大量的选项,比如选择是否在本地保存cache,cache目录在那里,log文件的位置,是否enable yum的plugin等等,详细的配置项的解释可以查看man yum.conf. yum repos 配置文件:

1
2
3
4
5
6
[vagrant@localhost ~]$ tree /etc/yum.repos.d/
/etc/yum.repos.d/
|-- CentOS-Base.repo
|-- CentOS-Debuginfo.repo
|-- CentOS-Media.repo
`-- CentOS-Vault.repo

配置文件中有两个section,一个是[main],包含全局的yum配置, 一个是[repository],包含repository的配置,定义软件仓库。 通常在主配置文件中定义main,repository定义通常放置在/etc/yum.repos.d/*.repo 文件中,扩展性非常好,如果你想添加新的软件源或者镜像,在/etc/yum.repos.d/目录下新创建一个.repo文件就好了。 yum 依赖libcurl, 所以下载的时候,是单线程的,在不使用插件的情况下,寻找一个合适的软件源是很重要的。 从列表中找到中国的镜像,还是很多的,教育网公网服务器一半一半的样子,最奇怪的是台湾的镜像,竟然显示是台湾省,呵呵。我选择的是163的镜像,步骤如下:

1
2
3
4
sudo cp CentOS-Base.repo Centos-163.repo
sudo sed -i 's/^mirrorlist/\#mirrorlist/g' Centos-163.repo
sudo sed -i 's/^\#baseurl/baseurl/g' Centos-163.repo
sudo sed -i 's/mirror.centos.org/mirrors.163.com/g' Centos-163.repo

之后运行yum update metadata,就可以更新metadata/index文件了。 .repo文件中配置细节目录区别:

1
2
3
4
5
6
7
8
[base]
name=CentOS-$releasever - Base
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
baseurl=http://mirrors.163.com/centos/$releasever/os/$basearch/
               ftp://path/to/repo
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
enabled=1

其中base为repo的名字,baseurl支持多个url、协议,指向repo的地址。gpgcheck选项表示要不要对下载的package做校验。enabled选项表示默认情况下是否使用该repo。mirrorlist为后备的repo镜像url列表。相关的配置选项还很多,比如time_out, http_caching等,可以man yum.conf查看。

yum 的插件配置文件:

1
2
3
4
[vagrant@localhost ~]$ tree /etc/yum/pluginconf.d/
/etc/yum/pluginconf.d/
|-- aliases.conf
`-- fastestmirror.conf

拿fastestmirror.conf作为例子看看:

1
2
3
4
5
6
7
8
[main]
enabled=1
verbose=0
always_print_best_host = true
socket_timeout=3
hostfilepath=timedhosts.txt
maxhostfileage=10
maxthreads=15

其中enable选项可以打开或者关闭yum的插件。

/etc/yum/vars/*,自定义yum中可以引用的变量: 在yum.conf或者.repo中可以看到一些yum build-in变量,这些变量包括:

1
2
3
4
$basearch
$releasever
$arch
$YUM0-9

$basearch读取的是yum.conf中distroverpkg的值,$arch读取的是系统的CPU的architecture, $releasever是发行版的版本号,比如Centos-6。$YUM0-9读取shell中的变量,有则取之,无则为空。如果想从系统的环境变量中fetch一些值,可以在bash中设置$YUM0-9。自定义yum变量还有一种方法是在/etc/yum/vars/下新增变量文件,比如:

1
echo "It's not Ubuntu" > /etc/yum/vars/declare

在yum.conf或者.repo文件中就可以调用$declare了,

/etc/yum/version-groups.conf: 定义软件组, 用yum version查看

1
2
3
4
5
6
7
8
9
[yum]
#  These are the top level things to do with yum, we don't list Eg. libselinux
# even though that's require by rpm(-libs).
run_with_packages = true
pkglist = glibc, sqlite, libcurl, nss,
          yum-metadata-parser,
          rpm, rpm-libs, rpm-python,
          python,
          python-iniparse, python-urlgrabber, python-pycurl

/etc/yum/protected.d: 保护某些软件不被卸载,default只有yum,可以添加新的conf文件,比如:

1
[vagrant@localhost ~]$echo "vim-common" > /etc/yum/protected.d/vim.conf

此时如果remove vim-common这个package,就会得到失败的信息:

1
2
3
4
5
6
7
8
9
10
11
12
[vagrant@localhost ~]$ sudo yum remove vim-common
Setting up Remove Process
Resolving Dependencies
--> Running transaction check
---> Package vim-common.x86_64 2:7.2.411-1.8.el6 will be erased
--> Processing Dependency: vim-common = 2:7.2.411-1.8.el6 for package: 2:vim-enhanced-7.2.411-1.8.el6.x86_64
--> Running transaction check
---> Package vim-enhanced.x86_64 2:7.2.411-1.8.el6 will be erased
--> Finished Dependency Resolution
Error: Trying to remove "vim-common", which is protected
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest

关于yum config文件大概就是这些,我们下一篇介绍yum的utils以及一些有用的plugins。