linux mock 使用
linux mock 构建
一、linux mock
mock是linux下创建chroot环境的工具,可以方便的进行RPM构建。
为什么要用mock?
- mock可以支持跨操作系统的RPM构建,因为只要配置文件相同,mock可以在不同的操作系统上构建相同的隔离OS环境。比如在suse和rhel上构建出相同的Centos环境;
- 便于环境管理(类似于docker)。因为RPM构建过程中会有很多的buildrequires,直接在OS上构建会安装这些依赖,导致当前的环境非常混乱;而在mock中不涉及这个问题,编译完成后直接删除该mock环境就行(但其实整个过程比docker流程要重)。
mock的地址:https://github.com/rpm-software-management/mock/wiki
二、mock安装
- 预备:centos7 OS
- 对接yum源:请参照网上相关介绍,可对接阿里的yum源
安装,执行命令:
yum install -y mock
yum install -y rpm-build
注意,可能出错:
Transaction check error:
file /etc/rpm/macros.ghc-srpm from install of redhat-rpm-config-9.1.0-72.el7.centos.noarch conflicts with file from package epel-release-6-8.noarch
解决办法:yum erase epel-release
,然后继续执行安装
安装完,查看mock版本:
[root@localhost Desktop]# mock --version
1.2.17
三、mock使用:
1、建议mock编译不要用root身份,这里为了方便,暂时将root用户加入mock组。
[root@test ~]# usermod -a -G mock root
2、mock的使用,需要准备相应的cfg文件,该文件会定义mock系统的规格。
在默认的路径/etc/mock下会存在众多的mock配置文件,如果需要在mock用户下执行的时候,需要将文件加入到mock组中。
[root@localhost mock]# chown root:mock /etc/mock/mock_leaning.cfg
3、生成配置文件/etc/mock/mock_learning.cfg(已对接阿里的yum镜像源):
[root@localhost mock]# cat /etc/mock/mock_leaning.cfg
config_opts['root'] = 'mock_learning_centos7-x86_64'
config_opts['target_arch'] = 'x86_64'
config_opts['legal_host_arches'] = ('x86_64',)
config_opts['chroot_setup_cmd'] = 'install basesystem rpm-build python2-devel gcc make python-sqlalchemy python-webob ghostscript graphviz python-sphinx python-eventlet python-six python-pbr python3-pbr openstack-macros git yum-plugin-priorities rubygems'
config_opts['dist'] = 'el7' # only useful for --resultdir variable subst
config_opts['releasever'] = '7'
config_opts['plugin_conf']['ccache_enable'] = False
config_opts['priorities.conf'] = """
[main]
enabled = 1
check_obsoletes = 1
"""
config_opts['yum.conf'] = """
[main]
keepcache=1
debuglevel=2
reposdir=/dev/null
logfile=/var/log/yum.log
retries=20
obsoletes=1
gpgcheck=0
assumeyes=1
plugins=1
syslog_ident=mock
syslog_device=
[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#released updates
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
"""
4、生成mock系统
[root@localhost mock]# cd /etc/mock/
[root@localhost mock]# ll | grep mock_leaning
-rw-r--r-- 1 root root 3399 Sep 15 09:15 mock_leaning.cfg
[root@localhost mock]# mock -r mock_leaning.cfg --init
INFO: mock.py version 1.2.17 starting (python version = 2.7.5)...
Start: init plugins
INFO: selinux disabled
Finish: init plugins
Start: run
Start: clean chroot
Finish: clean chroot
Start: chroot init
INFO: calling preinit hooks
... ... (中间略)
zip.x86_64 0:3.0-11.el7
zlib.x86_64 0:1.2.7-17.el7
Complete!
Finish: yum install
Start: creating cache
Finish: creating cache
Finish: chroot init
INFO: Installed packages:
Finish: run
查看生成的mock os:
[root@localhost root]# cd /var/lib/mock/
[root@localhost mock]# du * -sh | grep mock_learning
693M mock_learning_centos7-x86_64
5、chroot进去操作
[root@localhost root]# chroot .
bash-4.2# ls -al
total 36
dr-xr-xr-x 18 root root 4096 Sep 16 06:36 .
dr-xr-xr-x 18 root root 4096 Sep 16 06:36 ..
lrwxrwxrwx 1 root root 7 Sep 16 06:00 bin -> usr/bin
dr-xr-xr-x 2 root root 6 Nov 5 2016 boot
drwx------ 3 root mockbuild 88 Sep 16 06:03 builddir
drwxr-xr-x 4 root root 4096 Nov 5 2016 dev
drwxr-xr-x 52 root root 4096 Sep 16 06:03 etc
drwxr-xr-x 2 root root 6 Nov 5 2016 home
-rw-rw-r-- 1 root root 0 Sep 16 06:03 .initialized
lrwxrwxrwx 1 root root 7 Sep 16 06:00 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Sep 16 06:00 lib64 -> usr/lib64
drwxr-xr-x 2 root root 6 Nov 5 2016 media
drwxr-xr-x 2 root root 6 Nov 5 2016 mnt
drwxr-xr-x 2 root root 6 Nov 5 2016 opt
drwxrwsr-x 2 root mockbuild 8192 Sep 16 06:54 proc
dr-xr-x--- 2 root root 26 Sep 16 06:17 root
drwxr-xr-x 11 root root 122 Sep 16 06:02 run
lrwxrwxrwx 1 root root 8 Sep 16 06:00 sbin -> usr/sbin
drwxr-xr-x 2 root root 6 Nov 5 2016 srv
drwxrwsr-x 2 root mockbuild 6 Sep 16 05:59 sys
drwxrwxrwt 8 root root 101 Sep 16 06:02 tmp
drwxr-xr-x 13 root root 143 Sep 16 06:00 usr
drwxr-xr-x 18 root root 4096 Sep 16 06:01 var
bash-4.2# pwd
/
6、退出时使用exit,或者执行ctrl+d
三、mock构建RPM:
mock最常用的方式就是打RPM包,现在测试mock去打python RPM包。
过程是:python tar.gz包->生成*.src.rpm ->执行mock命令打包
1、下载一个tox python包,并解压,下面的命令请确保对接了pip源(你也可以下载tar包拷贝上去):
[root@localhost workspace]# pip download tox --no-binary :all:
... ... (中间打印略)
[root@localhost workspace]# tar -xf tox-2.8.2.tar.gz
[root@localhost workspace]# ll | grep tox
drwxr-xr-x 10 lanyyyy wheel 4096 Sep 16 07:45 tox-2.8.2
-rw-r--r-- 1 root root 471843 Sep 16 07:37 tox-2.8.2.tar.gz
2、生成src.rpm
(其实命令python setup.py bdist_rpm是可以直接生成最终的rpm,但是我们为了测试mock打包能力,取结果中的src.rpm即可)
[root@localhost workspace]# cd tox-2.8.2/
[root@localhost tox-2.8.2]# python setup.py bdist_rpm
running bdist_rpm
running egg_info
writing requirements to tox.egg-info/requires.txt
writing tox.egg-info/PKG-INFO
writing top-level names to tox.egg-info/top_level.txt
writing dependency_links to tox.egg-info/dependency_links.txt
writing entry points to tox.egg-info/entry_points.txt
reading manifest file 'tox.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'tox.egg-info/SOURCES.txt'
creating build
... ...(中间略)
+ exit 0
Executing(--clean): /bin/sh -e /var/tmp/rpm-tmp.5J0i9M
+ umask 022
+ cd /home/lanyyyy/workspace/tox-2.8.2/build/bdist.linux-x86_64/rpm/BUILD
+ rm -rf tox-2.8.2
+ exit 0
moving build/bdist.linux-x86_64/rpm/SRPMS/tox-2.8.2-1.src.rpm -> dist
moving build/bdist.linux-x86_64/rpm/RPMS/noarch/tox-2.8.2-1.noarch.rpm -> dist
最后,可以看到src.rpm和rpm拷贝到了当前目录下的dist目录中
[root@localhost tox-2.8.2]# tree dist/
dist/
├── tox-2.8.2-1.noarch.rpm # 我们是为了测试mock打rpm,故忽略该rpm
├── tox-2.8.2-1.src.rpm
└── tox-2.8.2.tar.gz
0 directories, 3 files
[root@localhost tox-2.8.2]# pwd
/home/lanyyyy/workspace/tox-2.8.2
[root@localhost tox-2.8.2]# cd dist/
[root@localhost dist]# mock -r /etc/mock/mock_leaning.cfg rebuild tox-2.8.2-1.src.rpm
INFO: mock.py version 1.2.17 starting (python version = 2.7.5)...
Start: init plugins
INFO: selinux disabled
Finish: init plugins
Start: run
INFO: Start(tox-2.8.2-1.src.rpm) Config(mock_learning_centos7-x86_64)
... ... (中间略)
+ exit 0
Finish: rpmbuild tox-2.8.2-1.src.rpm
Finish: build phase for tox-2.8.2-1.src.rpm
INFO: Done(tox-2.8.2-1.src.rpm) Config(mock_leaning) 1 minutes 16 seconds
INFO: Results and/or logs in: /var/lib/mock/mock_learning_centos7-x86_64/result
Finish: run
[root@localhost dist]# ls /var/lib/mock/mock_learning_centos7-x86_64/result
build.log root.log state.log tox-2.8.2-1.noarch.rpm tox-2.8.2-1.src.rpm
可以看到,在mock环境中的result目录下生成了tox的rpm包:
[root@localhost result]# cd /var/lib/mock/mock_learning_centos7-x86_64/result/
[root@localhost result]# rpm -qpl tox-2.8.2-1.noarch.rpm
/usr/bin/tox
/usr/bin/tox-quickstart
/usr/lib/python2.7/site-packages/tox-2.8.2-py2.7.egg-info
/usr/lib/python2.7/site-packages/tox-2.8.2-py2.7.egg-info/PKG-INFO
/usr/lib/python2.7/site-packages/tox-2.8.2-py2.7.egg-info/SOURCES.txt
/usr/lib/python2.7/site-packages/tox-2.8.2-py2.7.egg-info/dependency_links.txt
/usr/lib/python2.7/site-packages/tox-2.8.2-py2.7.egg-info/entry_points.txt
/usr/lib/python2.7/site-packages/tox-2.8.2-py2.7.egg-info/requires.txt
/usr/lib/python2.7/site-packages/tox-2.8.2-py2.7.egg-info/top_level.txt
/usr/lib/python2.7/site-packages/tox/__init__.py
/usr/lib/python2.7/site-packages/tox/__init__.pyc
/usr/lib/python2.7/site-packages/tox/__init__.pyo
/usr/lib/python2.7/site-packages/tox/__main__.py
/usr/lib/python2.7/site-packages/tox/__main__.pyc
/usr/lib/python2.7/site-packages/tox/__main__.pyo
/usr/lib/python2.7/site-packages/tox/_pytestplugin.py
/usr/lib/python2.7/site-packages/tox/_pytestplugin.pyc
/usr/lib/python2.7/site-packages/tox/_pytestplugin.pyo
/usr/lib/python2.7/site-packages/tox/_quickstart.py
/usr/lib/python2.7/site-packages/tox/_quickstart.pyc
/usr/lib/python2.7/site-packages/tox/_quickstart.pyo
/usr/lib/python2.7/site-packages/tox/_verlib.py
/usr/lib/python2.7/site-packages/tox/_verlib.pyc
/usr/lib/python2.7/site-packages/tox/_verlib.pyo
/usr/lib/python2.7/site-packages/tox/config.py
/usr/lib/python2.7/site-packages/tox/config.pyc
/usr/lib/python2.7/site-packages/tox/config.pyo
/usr/lib/python2.7/site-packages/tox/hookspecs.py
/usr/lib/python2.7/site-packages/tox/hookspecs.pyc
/usr/lib/python2.7/site-packages/tox/hookspecs.pyo
/usr/lib/python2.7/site-packages/tox/interpreters.py
/usr/lib/python2.7/site-packages/tox/interpreters.pyc
/usr/lib/python2.7/site-packages/tox/interpreters.pyo
/usr/lib/python2.7/site-packages/tox/result.py
/usr/lib/python2.7/site-packages/tox/result.pyc
/usr/lib/python2.7/site-packages/tox/result.pyo
/usr/lib/python2.7/site-packages/tox/session.py
/usr/lib/python2.7/site-packages/tox/session.pyc
/usr/lib/python2.7/site-packages/tox/session.pyo
/usr/lib/python2.7/site-packages/tox/venv.py
/usr/lib/python2.7/site-packages/tox/venv.pyc
/usr/lib/python2.7/site-packages/tox/venv.pyo
[root@localhost result]# tox
bash: tox: command not found...
[root@localhost result]# rpm -ivh tox-2.8.2-1.
tox-2.8.2-1.noarch.rpm tox-2.8.2-1.src.rpm
[root@localhost result]# rpm -ivh tox-2.8.2-1.noarch.rpm
Preparing... ################################# [100%]
Updating / installing...
1:tox-2.8.2-1 ################################# [100%]
[root@localhost result]# tox
ERROR: toxini file 'tox.ini' not found
并且经过mock打出来的tox rpm是可以正常安装部署的
四、linux mock与python mock
很多时候,我们会将python的mock和linux的mock相混淆。前者用于python的单元测试,模拟函数。后者用于在linux下隔离出一个环境,进行编译构建。因此请不要混淆了。
更多推荐
所有评论(0)