Simple implementation of creating centos rootfs

When using docker, we usually use base image. Here we take centos as an example. We usually download official centos through: docker search centos and docker pull centos, and then use it as base image to build our own docker image.

Maybe you want to build your own centos base image like me, but you may suffer from not knowing how to extract the relevant files needed by a base image to generate rootfs, so you can’t start, here is a way to generate rootfs (** from the network Find the method, and then add personal understanding and summary**):

1.

First create a directory to be used as the root directory of rootfs, and set the root directory of rpm operation to the directory of rootfs

[ root@localhost ~]# mkdir my_rootfs
[ root@localhost ~]# cd my_rootfs/[root@localhost my_rootfs]# cd ..[root@localhost ~]# rpm --root  /root/my_rootfs/--initdb     #Set the root directory for rpm operations

2.

Go to the website: http://vault.centos.org/, according to the centos version you need, locate the corresponding package directory, find the centos-release package, and download it locally; then install this package to the specified above The rootfs directory, the example used here is as follows:

[ root@localhost ~]# wget http://vault.centos.org/7.5.1804/os/x86_64/Packages/centos-release-7-5.1804.el7.centos.x86_64.rpm
- - 2019- 09- 0122:22:57- - http://vault.centos.org/7.5.1804/os/x86_64/Packages/centos-release-7-5.1804.el7.centos.x86_64.rpm
Resolving vault.centos.org(vault.centos.org)...108.61.16.227
Connecting to vault.centos.org(vault.centos.org)|108.61.16.227|:80... connected.
HTTP request sent, awaiting response...200 OK
Length:24400(24K)[application/x-rpm]
Saving to: ‘centos-release-7-5.1804.el7.centos.x86_64.rpm’

100 %[=============================================================================================>]24,40033.4KB/s   in0.7s

2019- 09- 0122:22:58(33.4 KB/s)- ‘centos-release-7-5.1804.el7.centos.x86_64.rpm’ saved [24400/24400][root@localhost ~]#
[ root@localhost ~]# rpm -ivh --nodeps --root /root/my_rootfs/--package./centos-release-7-5.1804.el7.centos.x86_64.rpm   #You need to ignore dependency when installing here.
warning:./centos-release-7-5.1804.el7.centos.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...1:centos-release-7-5.1804.el7.cento################################# [100%]
warning:%post(centos-release-7-5.1804.el7.centos.x86_64) scriptlet failed, exit status 127[root@localhost ~]#

3.

Configure the local yum source to ensure that the corresponding repository can be used, and then install the yum package to the above rootfs directory: Note that the dependency cannot be ignored when installing the yum package here.

# The configuration process of yum source is ignored. Centos comes with yum source and can be used without configuration;
[ root@localhost my_rootfs]# yum --installroot=/root/my_rootfs/  install yum
...... # Too much content, the installation process is omitted here;
Total                                                                                                  1.6 MB/s |51 MB  00:00:30
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     :"CentOS-7 Key (CentOS 7 Official Signing Key) <[email protected]>"
 Fingerprint:6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-5.1804.el7.centos.x86_64(installed)
 From       :/etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Is this ok [y/N]: y
Running transaction check
......
Complete![root@localhost my_rootfs]#ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

4.

After the yum package installation is complete, we found the directory size is as follows, where the size under /root/my_rootfs/var/cache is 86M, these contents are caches when yum is installed, these caches can be cleaned up, but I don’t recommend deleting them directly Corresponding to the violent way to clean up the directory; we will clean up in the next step.

[ root@localhost my_rootfs]# du -hs ./* | sort -k1h
0  . /bin
0  . /boot
0  . /dev
0  . /home
0  . /lib
0  . /lib64
0  . /media
0  . /mnt
0  . /opt
0  . /proc
0  . /root
0  . /run
0  . /sbin
0  . /srv
0  . /sys
0  . /tmp
1.9 M    ./etc
94 M     ./var
265 M    ./usr
[ root@localhost my_rootfs]#
[ root@localhost var]# du -hxa . | sort -k1h | tail
31 M     ./cache/yum/x86_64/7/base/gen
37 M     ./cache/yum/x86_64/7/base
40 M     ./cache/yum/x86_64/7/updates/gen
40 M     ./cache/yum/x86_64/7/updates/gen/primary_db.sqlite
48 M     ./cache/yum/x86_64/7/updates
86 M     ./cache
86 M     ./cache/yum
86 M     ./cache/yum/x86_64
86 M     ./cache/yum/x86_64/7
94 M     .
[ root@localhost var]#

5.

By chrooting, switch to base centos corresponding to rootfs, and then clean up unnecessary yum cache;

[ root@localhost my_rootfs]# mount --rbind /dev dev/[root@localhost my_rootfs]# mount --rbind /proc proc/[root@localhost my_rootfs]# mount --rbind /sys sys/[root@localhost my_rootfs]# chroot /root/my_rootfs/
bash-4.2#
bash-4.2# yum clean all        #Perform yum cache cleaning
Loaded plugins: fastestmirror
Cleaning repos: base extras updates
Cleaning up list of fastest mirrors
bash-4.2#
bash-4.2# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
bash-4.2# du -hsx ./* | sort -k1h | tail
du: cannot access ‘./proc/13246/task/13246/fd/3’: No such file or directory
du: cannot access ‘./proc/13246/task/13246/fdinfo/3’: No such file or directory
du: cannot access ‘./proc/13246/fd/3’: No such file or directory
du: cannot access ‘./proc/13246/fdinfo/3’: No such file or directory
0  . /proc
0  . /root
0  . /run
0  . /sbin
0  . /srv
0  . /sys
0  . /tmp
1.9 M    ./etc
8.0 M    ./var
265 M    ./usr
bash-4.2#
bash-4.2# exit
exit
[ root@localhost my_rootfs]#      

6.

We bind proc, sys, dev before umount, and then delete unnecessary man help files; finally compress and package /root/my_rootfs/*, and the generated my_rootfs.tar.gz is our target file;

[ root@localhost my_rootfs]# umount -l proc
[ root@localhost my_rootfs]# umount -l dev
[ root@localhost my_rootfs]# umount -l sys
[ root@localhost my_rootfs]# mount | grep my_rootfs
[ root@localhost my_rootfs]#
[ root@localhost my_rootfs]# find .-iname man -exec rm -rf {} \;[root@localhost my_rootfs]# find .-iname man
[ root@localhost my_rootfs]# tar -czvf my_rootfs.tar.gz   *  #When packaging, make sure to package only what you need, and don’t include a directory
......[ root@localhost my_rootfs]# tar -tvf my_rootfs.tar.gz | head      
# After packaging, you can use tar-tvf command to view content, the content needs to be similar to the following format
lrwxrwxrwx root/root         02019-09-0122:33 bin -> usr/bin
dr-xr-xr-x root/root         0 2018-04-11 12:59 boot/
drwxr-xr-x root/root         0 2019-09-01 22:34 dev/-rw-r--r-- root/root         02019-09-0122:34 dev/null
drwxr-xr-x root/root         0 2019-09-01 22:34 etc/
drwxr-xr-x root/root         02019-09-0122:34 etc/pki/
drwxr-xr-x root/root         02019-09-0122:26 etc/pki/rpm-gpg/-rw-r--r-- root/root      16902018-04-2900:35 etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7-rw-r--r-- root/root      10042018-04-2900:35 etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Debug-7-rw-r--r-- root/root      16902018-04-2900:35 etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Testing-7[root@localhost my_rootfs]# mv my_rootfs.tar.gz  ../[root@localhost my_rootfs]# cd ..[root@localhost ~]# ls -lht my_rootfs.tar.gz
- rw-r--r--.1 root root 81M Sep  421:47 my_rootfs.tar.gz
[ root@localhost ~]#

So can we use this my_rootfs.tar.gz to build our own centos base image?
Please see the next article: Create your own base image based on centos rootfs

This article is original, please indicate the source for reprinting

Recommended Posts

Simple implementation of creating centos rootfs
Implementation of CentOS8.0 Network Configuration
Simple practice of RHCS cluster in CentOS6
Explain the implementation of Centos8 static IP configuration
Graphical installation of CentOS8
Deployment of graphite on centos7
Centos7 silent installation of Oracle11g