TT Lab
Blog Get started
Learn Learning paths Courses

Package Management

Building Your Own Repository

Continue in TT Lab

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

  1. 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.
  2. Create the /root/repo/Packages index. It must contain the Package: entries of both packages.
  3. Create the compressed /root/repo/Packages.gz.
  4. 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.
  5. Register this repository in /etc/apt/sources.list.d/labhub-local.list. The path is /root/repo, in flat format.
  6. Refresh the index, and save the output of apt-cache policy labhub-extra to /root/repo-check/policy.txt.
  7. Install labhub-extra with apt. Because of the dependency relationship, labhub-tool must be installed along with it.
  8. Incident: run incident pk-repo-trust and read the ticket. corp-backup and media-codec will not install from the signed internal repositories corp-tools and corp-media respectively. Without turning off signature checks, get both packages installing with apt, and in /root/incident/pk-repo-trust/report.txt write a cause code for each repository (expired-key, hash-mismatch, no-pubkey, stale-index, unsigned) and one evidence: line.

Notes

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.