Building Your Own Repository
Goal
You build an apt repository that works without the internet from scratch and install packages from it. You confirm by hand that a repository is, in fact, "a directory + an index file". At the end, you find where the chain of trust broke in an internal repository complete with signatures, and mend it.
Why it matters
Whether it is an air-gapped network, an internal standard image, or a CI cache, what you ultimately need is a controllable repository. All apt reads from a repository is the Packages index and the Release metadata, so if you make just those two, even a file:/// path becomes a complete repository. Once you know this structure, the cause of "I added the package but it doesn't show up" splits immediately into a missed index rebuild or a client cache. The earlier steps skip signing with a trust option (trusted=yes) to focus on the structure.
The incident in the last step is the other side of that. A production repository proves that "this file is the one we approved" with a chain running key → Release signature → hashes in Packages → .deb, and if one link in the chain breaks, apt refuses to install. Turning off the check with trusted=yes then is not a fix but removing the alarm: find which link broke and why, and mend only that link.
Steps
- Create the
/root/repodirectory, build/opt/fixtures/pkg/labhub-tooland/opt/fixtures/pkg/labhub-extraeach into a.deb, and put them inside/root/repo. - Create the
/root/repo/Packagesindex. It must contain thePackage:entries of both packages. - Create the compressed
/root/repo/Packages.gz. - Create the
/root/repo/Releasefile. The six keysOrigin,Label,Suite,Codename,Architectures, andComponentsmust each have one line, and theArchitecturesvalue must includeall. - Register this repository in
/etc/apt/sources.list.d/labhub-local.list. The path is/root/repo, in flat format. - Refresh the index, and save the output of
apt-cache policy labhub-extrato/root/repo-check/policy.txt. - Install
labhub-extrawith apt. Because of the dependency relationship,labhub-toolmust be installed along with it. - Incident: run
incident pk-repo-trustand read the ticket.corp-backupandmedia-codecwill not install from the signed internal repositoriescorp-toolsandcorp-mediarespectively. Without turning off signature checks, get both packages installing with apt, and in/root/incident/pk-repo-trust/report.txtwrite a cause code for each repository (expired-key,hash-mismatch,no-pubkey,stale-index,unsigned) and oneevidence:line.
Notes
- How you pass the repository directory to the index tool (as
.or as an absolute path) changes theFilename:value. apt finds files by "repository URI + Filename", so read the paths in the error messages afterapt-get updateand installs carefully. - The format of a sources.list line is
deb [옵션] <URI> <suite> [component...](the option and the URI are placeholders), and for a flat repository you write./in the suite position. Thesigned-by=<키링>(keyring) option means "this repository is trusted only with this key". - Common mistake 1: you did not run
apt-get updateafter creating the index, so "not found" keeps appearing. - Common mistake 2: a
NO_PUBKEY/not signederror appears because there is no signature. The earlier steps get past it with a trust option, but the incident's repositories are signed, so that option is not the answer. - Common mistake 3: putting the public key into a personal keyring with
gpg --import. apt looks only at the file thatsigned-bypoints to. - Incident tools:
incident listshows the list, andincident show <이름>(name) shows the ticket (a colleague's report) again. Raising the same incident again returns it to its original broken state. - Write the report to
/root/incident/<이름>/report.txt(name is the incident name): one line<이름>: <원인 코드>(name: cause code) per target, plusevidence: <관찰한 출력 한 줄>(one line of output you observed). The evidence line is checked against the output actually seen in the incident. - Grading does not look only at the report. The actual state, such as the dpkg status DB, apt's candidate, and signature verification, must be right, and workarounds that only erase the symptom (forced installs, removals, turning off signatures) fail.
Preparing the package pool
Create the /root/repo directory, build /opt/fixtures/pkg/labhub-tool and /opt/fixtures/pkg/labhub-extra each into a .deb, and put them inside /root/repo.
Build the two fixtures separately and gather them in the same directory. The file names are up to you, but the extension must be .deb.
Creating the Packages index
Create the /root/repo/Packages index. It must contain the Package: entries of both packages.
The index tool (dpkg-scanpackages) opens the .deb files in the directory, reads their control fields, and appends Filename, Size, and SHA256. Depending on how you pass the directory (as the current directory from inside the repository, or as an absolute path), Filename becomes a relative or an absolute path; apt finds files by "repository URI + Filename", so think about which it should be. If there is no table overriding priorities (override), give an empty file in its place.
Creating the compressed index
Create the compressed /root/repo/Packages.gz.
apt looks for Packages.gz first. It is handier for debugging to keep the original as well.
Writing the Release file
Create the /root/repo/Release file. The six keys Origin, Label, Suite, Codename, Architectures, and Components must each have one line, and the Architectures value must include all.
The six keys Origin, Label, Suite, Codename, Architectures, and Components go in one line each. Choose the values freely, but Architectures must include all.
Registering the repository
Register this repository in /etc/apt/sources.list.d/labhub-local.list. The path is /root/repo, in flat format.
For a flat repository, add ./ after the path. There is no signature, so a trust option is needed.
Refreshing the index and checking
Refresh the index, and save the output of apt-cache policy labhub-extra to /root/repo-check/policy.txt.
After apt-get update, use apt-cache policy to see which repository the candidate came from. The repository path must be visible in the output.
You get stuck here once. If Could not open file ... (13: Permission denied) appears, it is a permissions problem. apt drops only the download part to an unprivileged user called _apt (APT::Sandbox::User), and /root is 700, so that user cannot go inside. Since the repository is under /root, you only need to open the one traverse permission (x) for other users (o). The permission to list the contents (r) is still closed.
There is also a way to turn off the sandbox (-o APT::Sandbox::User=root), but that covers up the problem. This is exactly why real-world repositories are placed in /srv or /var/www.
Installing from my repository
Install labhub-extra with apt. Because of the dependency relationship, labhub-tool must be installed along with it.
If you install the one that has the dependency, apt pulls in the rest on its own. This is where it differs from dpkg -i.
Incident: installs fail from two internal repositories
Running incident pk-repo-trust gives you a server that has just been attached to two signed internal repositories (corp-tools and corp-media) (read the ticket first). Symptom: neither corp-backup (corp-tools) nor media-codec (corp-media) installs with apt. Running the repositories is your job too. Make both packages install normally with apt, and make the next index refresh pass without warnings. Turning off signature checks with [trusted=yes] or the like, re-signing a repository with a different key, or putting the .deb in directly with dpkg is a failure. Revoked keys must be removed from the keyring, and only builds approved for release (/srv/repos/admin/builds) may go out in the repositories. In /root/incident/pk-repo-trust/report.txt, write two lines <저장소>: <코드> (repository: code; corp-tools: and corp-media:) and evidence: <관찰한 출력 한 줄> (one line of output you observed). The code is one of expired-key (the signing key expired), hash-mismatch (the index's hash and the file differ), no-pubkey (the client does not know the signing key), stale-index (this server's index is stale), or unsigned (the repository has no signature).
Direction: both are "it won't install", but the stage at which apt stopped differs: did it stop at update, where it downloads the index and verifies the signature, or at install, where it downloads the file and compares the hash? Find which link of the chain of trust (key → Release → Packages → .deb) broke. Tools: the deployment log (/var/log/corp-deploy.log), gpg --show-keys, gpgv, sha256sum, the repository's Packages and Release, and the notices in /srv/repos/keys. Concretely: compare the key ID printed in the error with the fingerprints of the keys actually in the keyring and with the announced fingerprint. If the hashes differ, put side by side the SHA256 written in the index, the file in the pool, and the approved build artifact in /srv/repos/admin. apt reads only OpenPGP public key files (binary .gpg or .asc) as keyrings.