Querying and Packaging With rpm
Goal
You query and verify with rpm, and build a package yourself from a spec file and install it. In the final step, you sort the five "changed files" the integrity check reported by the position letters of the verification output, and restore only the ones that are incidents to the package's state.
Why it matters
rpm -V is an audit tool that compares the size, mode, digest, owner, and time recorded in the rpmdb at install time with the current file and tells you what has changed. But a line saying "changed" is not necessarily an incident. A configuration file (c) edited by an operator is normal, while if an executable's content (5) or mode (M) changed, you must investigate. If you cannot read the nine positions of the output, you either revert normal changes too and break the service, or pass over real tampering as "the configuration must have changed".
And if you build a package yourself, you know for certain what is inside it — the strictness of rpmbuild, where a build fails if a file is not listed in %files, keeps the package contents accurate.
Steps
- Create the
/root/rpmlabdirectory and write the number of installed packages as a single numeric line in/root/rpmlab/count.txt. - Save the list of documentation files of the
coreutils-commonpackage to/root/rpmlab/docs.txt. - Write only the name of the package that owns
/usr/bin/lson one line in/root/rpmlab/owner.txt. - Pick one configuration file of an installed package, append one line to its content, run the verification, and save the lines showing tampering to
/root/rpmlab/verify.txt. (Pick a safe file such as/etc/DIR_COLORS.) - Query the pre- and post-install scripts of
openssh-serverand save the whole output to/root/rpmlab/scripts.txt. - Create the
~/rpmbuildbuild tree, bundle/opt/fixtures/rpmbuild/labhub-tool-1.0intolabhub-tool-1.0.tar.gzand put it inSOURCES/, put/opt/fixtures/rpmbuild/labhub-tool.specinSPECS/, and build. An.rpmmust appear under~/rpmbuild/RPMS/noarch/. - Query the
.rpmyou built without installing it, and save the information and the file list to/root/rpmlab/pkg-info.txtand/root/rpmlab/pkg-files.txtrespectively. - Install and run that package, then make
/root/rpmlab/report.txtwith 4 lines (NAME,VERSION,ARCH,RUN). - Incident: raise
incident rh-config-drift, classify the five changed files of acme-portal, and restore only the ones that are incidents. The report is/root/incident/rh-config-drift/report.txt.
Notes
- This lab is about 50 minutes. It fits within a 60-minute session, but if you spend a long time on the incident, extend it with
+시간(add time). The lab Pod has no volume, so your work disappears when the session ends. - Query options choose "what" (all, configuration, documentation, owner, scripts) and "where" (an installed package or a file path) separately. When looking at an .rpm file that is not installed, one more marker saying it is a file path is added.
- Build in a build tree that has SPECS, SOURCES, BUILD, BUILDROOT, RPMS, and SRPMS. If the top-level directory name inside the tar differs from
%{name}-%{version},%setupfails. - Incident tools:
incident listshows the incidents you can raise and a one-line symptom for each, andincident show <이름>(name) shows the ticket again. Raisingincident <이름>again returns it to its original broken state. - The incident report goes in
/root/incident/<이름>/report.txt(name is the incident name): one line<대상>: <원인 코드>(target: cause code) per target, plusevidence: <원인을 보여 준 출력 한 줄>(one line of output that showed the cause). Copy the evidence as is from output you actually saw. - Common mistake 1: if you edit a binary instead of a configuration file in step 4, the system breaks. Check the configuration file list first.
- Common mistake 2: if you remove and reinstall the package in the incident, the operator's edited configuration is pushed aside to
.rpmsaveand the defaults come in.
Installed list and detailed information
Create the /root/rpmlab directory and write the number of installed packages as a single numeric line in /root/rpmlab/count.txt.
Direction: the truth about the installed list lives in the local database (/var/lib/rpm). Tools: the query option asking for everything installed, and line counting. Concretely: that query prints one per line with no header, so count it as is; counting with a tool that adds a header gives one too many.
Distinguishing files, configuration, and documentation
Save the list of documentation files of the coreutils-common package to /root/rpmlab/docs.txt.
Direction: a package remembers its files marked by kind (regular, configuration, documentation); thanks to this marker, upgrades can preserve configuration separately. Tools: the three query options that give the full list, the configuration list, and the documentation list. Concretely: if you save the full list, the line count will not match.
Finding the owner of a file
Write only the name of the package that owns /usr/bin/ls on one line in /root/rpmlab/owner.txt.
Direction: give one file and ask for its owning package. The local database has a file→package index, so it answers immediately. Tools: the query option that takes a path, and an output format. Concretely: the default output is name-version-release.architecture, so specifying a format that extracts only the name is safer than cutting at hyphens by hand.
Detecting tampering
Pick one configuration file of an installed package, append one line to its content, verify that package, and save the lines showing tampering to /root/rpmlab/verify.txt. (Pick a safe file such as /etc/DIR_COLORS. The grader compares the lines you wrote with the current verification result again.)
Direction: package verification compares the size, mode, digest, owner, and time recorded at install time with the current file. Tools: a query for the configuration file list, a query for a file's owning package, and the package verification option. Concretely: check the position among the nine at the front of the output that says the content changed, and the c marker before the path. Picking a binary fails.
Reading install scripts
Query the pre- and post-install scripts (scriptlets) attached to the openssh-server package and save the whole output to /root/rpmlab/scripts.txt. Look in it for the line that creates an account during installation.
Direction: a package brings not only a bundle of files but also shell fragments that run as root before and after installation. This is the part to read first before importing an unfamiliar package. Tool: the option that shows scriptlets in query mode. Concretely: header lines such as preinstall and postinstall and the line that creates an account must be included. Saving only a part fails.
Building an rpm
Create the ~/rpmbuild build tree, bundle /opt/fixtures/rpmbuild/labhub-tool-1.0 into labhub-tool-1.0.tar.gz and put it in SOURCES/, put /opt/fixtures/rpmbuild/labhub-tool.spec in SPECS/, and build. An .rpm must appear under ~/rpmbuild/RPMS/noarch/.
Direction: the build tool requires a fixed directory structure (recipe, raw materials, workbench, fake root, results). Tools: the build tree directories, compressing the source, and the build option that makes both binary and source packages. Concretely: the top-level directory name inside the archive must be the same as the spec's %{name}-%{version} for %setup to find it and go in. If it fails, read from the last error line of the build output.
Taking the built package apart
Query the .rpm you built without installing it, and save the information and the file list to /root/rpmlab/pkg-info.txt and /root/rpmlab/pkg-files.txt respectively.
Direction: import review happens before installation; you must query the file path, not an installed package name. Tool: the option that tells info and file list queries "this is a file". Concretely: if that option is missing, all you get is a 'not installed' error.
Install and run it to check
Install the labhub-tool you built and run the command. Then make /root/rpmlab/report.txt with the following 4 lines: NAME=labhub-tool / VERSION=<설치된 버전-릴리스> (installed version-release) / ARCH=<설치된 아키텍처> (installed architecture) / RUN=<labhub-tool 명령 실행 결과 첫 줄 그대로> (the first line of the labhub-tool command's output, as is). The installed files must show no differences at all in package verification.
Direction: installing a single local file without a repository is the job of the low-level tool. After installation the truth is in the rpmdb, so extract the values with queries. Tools: installing a local file (including the option to overwrite the same version again), a query with an output format, package verification. Concretely: RUN copies as is the first line of actually running the command.
Incident: an integrity check reported five changed files. Who changed them?
Running incident rh-config-drift leaves five files of the in-house package acme-portal changed (read the ticket first). Classify each of the five targets, portal.conf, acme-portal (the executable), rotate-hook, state (a directory), and README, as one of binary-replaced (the executable's content changed), config-edited (an operator edited the configuration: normal), mode-changed (the permissions changed), mtime-only (only the time changed: harmless), or owner-changed (the owner changed), and restore the ones that are not normal operator changes to the original state in the package. End state: package verification shows only the one line of the operator's configuration change (the documentation line where only the time changed may remain). Losing the operator's setting (max_clients = 512) or deleting files or the package is a failure. In /root/incident/rh-config-drift/report.txt, write five lines <대상>: <코드> (target: code) and evidence: <관찰한 출력 한 줄> (one line of output you observed).
Direction: even the same "changed" splits into an incident or normal depending on what changed (content, size, mode, owner, time) and what the file is (configuration, documentation, executable). Tools: package verification and its nine positions, queries for the configuration and documentation file lists, file attributes, and a way to bring the package in again at the same version (the in-house repository has the distribution build). Concretely: the positions in the verification output are in the order S M 5 D L U G T P, and a dot means no problem. Look at the c and d markers before the path as well. Configuration files are %config(noreplace), so bringing in the same version again keeps the operator's edits, but removing and reinstalling pushes them aside to .rpmsave.