Contents
RPM Package Manager (RPM) (Red Hat Package Manager) is a powerful command line driven package management system capable of installing, uninstalling, verifying, querying, and updating computer software packages and a core component of many Linux distributions, such as Red Hat Enterprise Linux, the Fedora Project, SUSE Linux Enterprise, openSUSE, CentOS, Tizen, Mageia and many others.
Overview
Simply put, RPM, similar to DEB in Ubuntu/Debian, set out to solve the software management problem by packaging metadata, which includes the info of package, the files, scripts and dependencies.
If you delete the rpm by mistake, following this tutorial to recover it.
There are two way to reinstall rpm. The one is compile and install rpm using gcc, that isn’t installed in new linux server commonly.
The other one is copy the related files from a normal machine with the same architecture and Linux distribution. As we known, in Linux System, everything is file. If the missing program is a standalone software or a static binary file, which can run independently without extra library, it will work well by directly copy and run. Otherwise, to coordinate with the related files, you may cost lots of time.
It’s hard to find out all the sparsely related files of rpm in a normal server. However, all the related files are contained in the package of rpm. A rpm package can be partly considered as compressed file, extracted by the rpm2cpio.
Operations
Check the architecture and version of target server
1 2 |
uname -a cat /etc/redhat-release |
Here, I will use Centos 6.8 x86_64 for a example. You should change them in the following steps.
Install a normal server
Running the container images will be faster than creating the traditional virtual machine in another server.
1 2 3 |
wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-18.06.1.ce-3.el7.x86_64.rpm sudo yum install docker-ce-18.06.1.ce-3.el7.x86_64.rpm sudo systemctl start docker |
pulling a base library image with the same architecture and version of target server from the mirror repositories for the higher speed.
1 2 |
docker pull registry.docker-cn.com/library/centos:6.8 docker tag registry.docker-cn.com/library/centos:6.8 library/centos:6.8 |
Create a new Dockerfile to run a simple container.
1 2 3 4 5 6 |
FROM centos:6.8 RUN yum install -y openssh-server bash EXPOSE 22 CMD ["/usr/sbin/sshd", "-D"] |
Build the image from Dockerfile and run in the backgound.
1 2 |
docker build -t centos . docker run -d -name centos centos |
Copy the rpm2cpio
Enter the inside of the running container.
1 |
docker exec it centos bash |
Transfer a rpm2cpio from the normal server to our target server through the Secure copy protocol.
1 |
scp -P 22 -v /usr/bin/rpm2cpio root@23.29.34.106:/usr/bin/rpm2cpio |
Ctrl+P+Q to return to the normal server. Change to the target server, make the copied rpm2cpio executable.
1 |
chmod +x /usr/bin/rpm2cpio |
Reinstall the RPM
Download the rpm package from http://vault.centos.org, choose the correct one for yours.
1 2 |
mkdir /home/install && cd /home/install wget -c http://vault.centos.org/6.8/os/x86_64/Packages/rpm-4.8.0-55.el6.x86_64.rpm |
Extract the files, change the permission and move to root directory.
1 2 3 |
rpm2cpio rpm-4.8.0-55.el6.x86_64.rpm | cpio -dim find . -type d -exec chmod 755 {} \; tar cf - ./usr ./etc ./bin | (cd /; tar xvf -) |
SHELL NOTE:
the symbol {} expands to the filename of each file or directory found with find. The find command means find the directory of the current path and give them 755 permission one by one.
the symbol – means stdout, and the tar command means compress three directory to stdout, move to root directory and decompress the three directory from stdout.
Rebuild the cache of package lists and install the rpm.
1 2 3 |
rpm --rebuilddb rpm -i rpm-4.8.0-55.el6.x86_64.rpm yum update |
Reference
- https://deadlockprocess.wordpress.com/2010/07/18/how-to-reinstall-rpm-and-yum-without-rpm-and-yum-in-centosrhel/amp/
- https://www.linuxquestions.org/questions/linux-general-1/tar-tar-cvf-%7C-cd-root-%3B-tar-xvf-353132/
- https://superuser.com/questions/794963/in-a-linux-shell-why-does-backslash-newline-not-introduce-whitespace
- https://en.wikipedia.org/wiki/Static_build
- https://en.wikipedia.org/wiki/Everything_is_a_file
- https://unix.stackexchange.com/questions/195939/what-is-meaning-of-in-finds-exec-command