1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
|
//go:build !remote
package libpod
import (
"bufio"
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"slices"
"strings"
"sync"
"syscall"
"time"
"github.com/containers/buildah/pkg/parse"
"github.com/containers/common/libimage"
"github.com/containers/common/libnetwork/network"
nettypes "github.com/containers/common/libnetwork/types"
"github.com/containers/common/pkg/cgroups"
"github.com/containers/common/pkg/config"
"github.com/containers/common/pkg/secrets"
systemdCommon "github.com/containers/common/pkg/systemd"
"github.com/containers/image/v5/pkg/sysregistriesv2"
is "github.com/containers/image/v5/storage"
"github.com/containers/image/v5/types"
"github.com/containers/podman/v5/libpod/define"
"github.com/containers/podman/v5/libpod/events"
"github.com/containers/podman/v5/libpod/lock"
"github.com/containers/podman/v5/libpod/plugin"
"github.com/containers/podman/v5/libpod/shutdown"
"github.com/containers/podman/v5/pkg/domain/entities"
"github.com/containers/podman/v5/pkg/domain/entities/reports"
"github.com/containers/podman/v5/pkg/rootless"
"github.com/containers/podman/v5/pkg/systemd"
"github.com/containers/podman/v5/pkg/util"
"github.com/containers/storage"
"github.com/containers/storage/pkg/fileutils"
"github.com/containers/storage/pkg/lockfile"
"github.com/containers/storage/pkg/unshare"
"github.com/docker/docker/pkg/namesgenerator"
"github.com/hashicorp/go-multierror"
jsoniter "github.com/json-iterator/go"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
)
// Set up the JSON library for all of Libpod
var json = jsoniter.ConfigCompatibleWithStandardLibrary
// A RuntimeOption is a functional option which alters the Runtime created by
// NewRuntime
type RuntimeOption func(*Runtime) error
type storageSet struct {
RunRootSet bool
GraphRootSet bool
StaticDirSet bool
VolumePathSet bool
GraphDriverNameSet bool
TmpDirSet bool
}
// Runtime is the core libpod runtime
type Runtime struct {
config *config.Config
storageConfig storage.StoreOptions
storageSet storageSet
state State
store storage.Store
storageService *storageService
imageContext *types.SystemContext
defaultOCIRuntime OCIRuntime
ociRuntimes map[string]OCIRuntime
runtimeFlags []string
network nettypes.ContainerNetwork
conmonPath string
libimageRuntime *libimage.Runtime
libimageEventsShutdown chan bool
lockManager lock.Manager
// Worker
workerChannel chan func()
workerGroup sync.WaitGroup
// syslog describes whenever logrus should log to the syslog as well.
// Note that the syslog hook will be enabled early in cmd/podman/syslog_linux.go
// This bool is just needed so that we can set it for netavark interface.
syslog bool
// doReset indicates that the runtime will perform a system reset.
// A reset will remove all containers, pods, volumes, networks, etc.
// A number of validation checks are relaxed, or replaced with logic to
// remove as much of the runtime as possible if they fail. This ensures
// that even a broken Libpod can still be removed via `system reset`.
// This does not actually perform a `system reset`. That is done by
// calling "Reset()" on the returned runtime.
doReset bool
// doRenumber indicates that the runtime will perform a system renumber.
// A renumber will reassign lock numbers for all containers, pods, etc.
// This will not perform the renumber itself, but will ignore some
// errors related to lock initialization so a renumber can be performed
// if something has gone wrong.
doRenumber bool
// valid indicates whether the runtime is ready to use.
// valid is set to true when a runtime is returned from GetRuntime(),
// and remains true until the runtime is shut down (rendering its
// storage unusable). When valid is false, the runtime cannot be used.
valid bool
// mechanism to read and write even logs
eventer events.Eventer
// secretsManager manages secrets
secretsManager *secrets.SecretsManager
}
// SetXdgDirs ensures the XDG_RUNTIME_DIR env and XDG_CONFIG_HOME variables are set.
// containers/image uses XDG_RUNTIME_DIR to locate the auth file, XDG_CONFIG_HOME is
// use for the containers.conf configuration file.
func SetXdgDirs() error {
if !rootless.IsRootless() {
return nil
}
// Set up XDG_RUNTIME_DIR
runtimeDir := os.Getenv("XDG_RUNTIME_DIR")
if runtimeDir == "" {
var err error
runtimeDir, err = util.GetRootlessRuntimeDir()
if err != nil {
return err
}
}
if err := os.Setenv("XDG_RUNTIME_DIR", runtimeDir); err != nil {
return fmt.Errorf("cannot set XDG_RUNTIME_DIR: %w", err)
}
if rootless.IsRootless() && os.Getenv("DBUS_SESSION_BUS_ADDRESS") == "" {
sessionAddr := filepath.Join(runtimeDir, "bus")
if err := fileutils.Exists(sessionAddr); err == nil {
os.Setenv("DBUS_SESSION_BUS_ADDRESS", fmt.Sprintf("unix:path=%s", sessionAddr))
}
}
// Set up XDG_CONFIG_HOME
if cfgHomeDir := os.Getenv("XDG_CONFIG_HOME"); cfgHomeDir == "" {
cfgHomeDir, err := util.GetRootlessConfigHomeDir()
if err != nil {
return err
}
if err := os.Setenv("XDG_CONFIG_HOME", cfgHomeDir); err != nil {
return fmt.Errorf("cannot set XDG_CONFIG_HOME: %w", err)
}
}
return nil
}
// NewRuntime creates a new container runtime
// Options can be passed to override the default configuration for the runtime
func NewRuntime(ctx context.Context, options ...RuntimeOption) (*Runtime, error) {
conf, err := config.Default()
if err != nil {
return nil, err
}
return newRuntimeFromConfig(ctx, conf, options...)
}
// NewRuntimeFromConfig creates a new container runtime using the given
// configuration file for its default configuration. Passed RuntimeOption
// functions can be used to mutate this configuration further.
// An error will be returned if the configuration file at the given path does
// not exist or cannot be loaded
func NewRuntimeFromConfig(ctx context.Context, userConfig *config.Config, options ...RuntimeOption) (*Runtime, error) {
return newRuntimeFromConfig(ctx, userConfig, options...)
}
func newRuntimeFromConfig(ctx context.Context, conf *config.Config, options ...RuntimeOption) (*Runtime, error) {
runtime := new(Runtime)
if conf.Engine.OCIRuntime == "" {
conf.Engine.OCIRuntime = "runc"
// If we're running on cgroups v2, default to using crun.
if onCgroupsv2, _ := cgroups.IsCgroup2UnifiedMode(); onCgroupsv2 {
conf.Engine.OCIRuntime = "crun"
}
}
runtime.config = conf
if err := SetXdgDirs(); err != nil {
return nil, err
}
storeOpts, err := storage.DefaultStoreOptions()
if err != nil {
return nil, err
}
runtime.storageConfig = storeOpts
// Overwrite config with user-given configuration options
for _, opt := range options {
if err := opt(runtime); err != nil {
return nil, fmt.Errorf("configuring runtime: %w", err)
}
}
if err := makeRuntime(ctx, runtime); err != nil {
return nil, err
}
if err := shutdown.Register("libpod", func(sig os.Signal) error {
if runtime.store != nil {
_, _ = runtime.store.Shutdown(false)
}
return nil
}); err != nil && !errors.Is(err, shutdown.ErrHandlerExists) {
logrus.Errorf("Registering shutdown handler for libpod: %v", err)
}
if err := shutdown.Start(); err != nil {
return nil, fmt.Errorf("starting shutdown signal handler: %w", err)
}
runtime.config.CheckCgroupsAndAdjustConfig()
return runtime, nil
}
func getLockManager(runtime *Runtime) (lock.Manager, error) {
var err error
var manager lock.Manager
switch runtime.config.Engine.LockType {
case "file":
lockPath := filepath.Join(runtime.config.Engine.TmpDir, "locks")
manager, err = lock.OpenFileLockManager(lockPath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
manager, err = lock.NewFileLockManager(lockPath)
if err != nil {
return nil, fmt.Errorf("failed to get new file lock manager: %w", err)
}
} else {
return nil, err
}
}
case "", "shm":
lockPath := define.DefaultSHMLockPath
if rootless.IsRootless() {
lockPath = fmt.Sprintf("%s_%d", define.DefaultRootlessSHMLockPath, rootless.GetRootlessUID())
}
// Set up the lock manager
manager, err = lock.OpenSHMLockManager(lockPath, runtime.config.Engine.NumLocks)
if err != nil {
switch {
case errors.Is(err, os.ErrNotExist):
manager, err = lock.NewSHMLockManager(lockPath, runtime.config.Engine.NumLocks)
if err != nil {
return nil, fmt.Errorf("failed to get new shm lock manager: %w", err)
}
case errors.Is(err, syscall.ERANGE) && runtime.doRenumber:
logrus.Debugf("Number of locks does not match - removing old locks")
// ERANGE indicates a lock numbering mismatch.
// Since we're renumbering, this is not fatal.
// Remove the earlier set of locks and recreate.
if err := os.Remove(filepath.Join("/dev/shm", lockPath)); err != nil {
return nil, fmt.Errorf("removing libpod locks file %s: %w", lockPath, err)
}
manager, err = lock.NewSHMLockManager(lockPath, runtime.config.Engine.NumLocks)
if err != nil {
return nil, err
}
default:
return nil, err
}
}
default:
return nil, fmt.Errorf("unknown lock type %s: %w", runtime.config.Engine.LockType, define.ErrInvalidArg)
}
return manager, nil
}
func getDBState(runtime *Runtime) (State, error) {
// TODO - if we further break out the state implementation into
// libpod/state, the config could take care of the code below. It
// would further allow to move the types and consts into a coherent
// package.
backend, err := config.ParseDBBackend(runtime.config.Engine.DBBackend)
if err != nil {
return nil, err
}
// get default boltdb path
baseDir := runtime.config.Engine.StaticDir
if runtime.storageConfig.TransientStore {
baseDir = runtime.config.Engine.TmpDir
}
boltDBPath := filepath.Join(baseDir, "bolt_state.db")
switch backend {
case config.DBBackendDefault:
// for backwards compatibility check if boltdb exists, if it does not we use sqlite
if err := fileutils.Exists(boltDBPath); err != nil {
if errors.Is(err, fs.ErrNotExist) {
// need to set DBBackend string so podman info will show the backend name correctly
runtime.config.Engine.DBBackend = config.DBBackendSQLite.String()
return NewSqliteState(runtime)
}
// Return error here some other problem with the boltdb file, rather than silently
// switch to sqlite which would be hard to debug for the user return the error back
// as this likely a real bug.
return nil, err
}
runtime.config.Engine.DBBackend = config.DBBackendBoltDB.String()
fallthrough
case config.DBBackendBoltDB:
return NewBoltState(boltDBPath, runtime)
case config.DBBackendSQLite:
return NewSqliteState(runtime)
default:
return nil, fmt.Errorf("unrecognized database backend passed (%q): %w", backend.String(), define.ErrInvalidArg)
}
}
// Make a new runtime based on the given configuration
// Sets up containers/storage, state store, OCI runtime
func makeRuntime(ctx context.Context, runtime *Runtime) (retErr error) {
// Find a working conmon binary
cPath, err := runtime.config.FindConmon()
if err != nil {
return err
}
runtime.conmonPath = cPath
if runtime.config.Engine.StaticDir == "" {
runtime.config.Engine.StaticDir = filepath.Join(runtime.storageConfig.GraphRoot, "libpod")
runtime.storageSet.StaticDirSet = true
}
if runtime.config.Engine.VolumePath == "" {
runtime.config.Engine.VolumePath = filepath.Join(runtime.storageConfig.GraphRoot, "volumes")
runtime.storageSet.VolumePathSet = true
}
// Make the static files directory if it does not exist
if err := os.MkdirAll(runtime.config.Engine.StaticDir, 0700); err != nil {
// The directory is allowed to exist
if !errors.Is(err, os.ErrExist) {
return fmt.Errorf("creating runtime static files directory %q: %w", runtime.config.Engine.StaticDir, err)
}
}
// Create the TmpDir if needed
if err := os.MkdirAll(runtime.config.Engine.TmpDir, 0751); err != nil {
return fmt.Errorf("creating runtime temporary files directory: %w", err)
}
// Create the volume path if needed.
// This is not strictly necessary at this point, but the path not
// existing can cause troubles with DB path validation on OSTree based
// systems. Ref: https://github.com/containers/podman/issues/23515
if err := os.MkdirAll(runtime.config.Engine.VolumePath, 0700); err != nil {
return fmt.Errorf("creating runtime volume path directory: %w", err)
}
// Set up the state.
runtime.state, err = getDBState(runtime)
if err != nil {
return err
}
// Grab config from the database so we can reset some defaults
dbConfig, err := runtime.state.GetDBConfig()
if err != nil {
if runtime.doReset {
// We can at least delete the DB and the static files
// directory.
// Can't safely touch anything else because we aren't
// sure of other directories.
if err := runtime.state.Close(); err != nil {
logrus.Errorf("Closing database connection: %v", err)
} else {
if err := os.RemoveAll(runtime.config.Engine.StaticDir); err != nil {
logrus.Errorf("Removing static files directory %v: %v", runtime.config.Engine.StaticDir, err)
}
}
}
return fmt.Errorf("retrieving runtime configuration from database: %w", err)
}
runtime.mergeDBConfig(dbConfig)
checkCgroups2UnifiedMode(runtime)
logrus.Debugf("Using graph driver %s", runtime.storageConfig.GraphDriverName)
logrus.Debugf("Using graph root %s", runtime.storageConfig.GraphRoot)
logrus.Debugf("Using run root %s", runtime.storageConfig.RunRoot)
logrus.Debugf("Using static dir %s", runtime.config.Engine.StaticDir)
logrus.Debugf("Using tmp dir %s", runtime.config.Engine.TmpDir)
logrus.Debugf("Using volume path %s", runtime.config.Engine.VolumePath)
logrus.Debugf("Using transient store: %v", runtime.storageConfig.TransientStore)
// Validate our config against the database, now that we've set our
// final storage configuration
if err := runtime.state.ValidateDBConfig(runtime); err != nil {
// If we are performing a storage reset: continue on with a
// warning. Otherwise we can't `system reset` after a change to
// the core paths.
if !runtime.doReset {
return err
}
logrus.Errorf("Runtime paths differ from those stored in database, storage reset may not remove all files")
}
if runtime.config.Engine.Namespace != "" {
return fmt.Errorf("namespaces are not supported by this version of Libpod, please unset the `namespace` field in containers.conf: %w", define.ErrNotImplemented)
}
needsUserns := os.Geteuid() != 0
if !needsUserns {
hasCapSysAdmin, err := unshare.HasCapSysAdmin()
if err != nil {
return err
}
needsUserns = !hasCapSysAdmin
}
// Set up containers/storage
var store storage.Store
if needsUserns {
logrus.Debug("Not configuring container store")
} else if err := runtime.configureStore(); err != nil {
// Make a best-effort attempt to clean up if performing a
// storage reset.
if runtime.doReset {
if err := runtime.removeAllDirs(); err != nil {
logrus.Errorf("Removing libpod directories: %v", err)
}
}
return fmt.Errorf("configure storage: %w", err)
}
defer func() {
if retErr != nil && store != nil {
// Don't forcibly shut down
// We could be opening a store in use by another libpod
if _, err := store.Shutdown(false); err != nil {
logrus.Errorf("Removing store for partially-created runtime: %s", err)
}
}
}()
// Set up the eventer
eventer, err := runtime.newEventer()
if err != nil {
return err
}
runtime.eventer = eventer
// Set up containers/image
if runtime.imageContext == nil {
runtime.imageContext = &types.SystemContext{
BigFilesTemporaryDir: parse.GetTempDir(),
}
}
runtime.imageContext.SignaturePolicyPath = runtime.config.Engine.SignaturePolicyPath
// Get us at least one working OCI runtime.
runtime.ociRuntimes = make(map[string]OCIRuntime)
// Initialize remaining OCI runtimes
for name, paths := range runtime.config.Engine.OCIRuntimes {
ociRuntime, err := newConmonOCIRuntime(name, paths, runtime.conmonPath, runtime.runtimeFlags, runtime.config)
if err != nil {
// Don't fatally error.
// This will allow us to ship configs including optional
// runtimes that might not be installed (crun, kata).
// Only an infof so default configs don't spec errors.
logrus.Debugf("Configured OCI runtime %s initialization failed: %v", name, err)
continue
}
runtime.ociRuntimes[name] = ociRuntime
}
// Do we have a default OCI runtime?
if runtime.config.Engine.OCIRuntime != "" {
// If the string starts with / it's a path to a runtime
// executable.
if strings.HasPrefix(runtime.config.Engine.OCIRuntime, "/") {
ociRuntime, err := newConmonOCIRuntime(runtime.config.Engine.OCIRuntime, []string{runtime.config.Engine.OCIRuntime}, runtime.conmonPath, runtime.runtimeFlags, runtime.config)
if err != nil {
return err
}
runtime.ociRuntimes[runtime.config.Engine.OCIRuntime] = ociRuntime
runtime.defaultOCIRuntime = ociRuntime
} else {
ociRuntime, ok := runtime.ociRuntimes[runtime.config.Engine.OCIRuntime]
if !ok {
return fmt.Errorf("default OCI runtime %q not found: %w", runtime.config.Engine.OCIRuntime, define.ErrInvalidArg)
}
runtime.defaultOCIRuntime = ociRuntime
}
}
logrus.Debugf("Using OCI runtime %q", runtime.defaultOCIRuntime.Path())
// Do we have at least one valid OCI runtime?
if len(runtime.ociRuntimes) == 0 {
return fmt.Errorf("no OCI runtime has been configured: %w", define.ErrInvalidArg)
}
// Do we have a default runtime?
if runtime.defaultOCIRuntime == nil {
return fmt.Errorf("no default OCI runtime was configured: %w", define.ErrInvalidArg)
}
// the store is only set up when we are in the userns so we do the same for the network interface
if !needsUserns {
netBackend, netInterface, err := network.NetworkBackend(runtime.store, runtime.config, runtime.syslog)
if err != nil {
return err
}
runtime.config.Network.NetworkBackend = string(netBackend)
runtime.network = netInterface
}
// We now need to see if the system has restarted
// We check for the presence of a file in our tmp directory to verify this
// This check must be locked to prevent races
runtimeAliveFile := filepath.Join(runtime.config.Engine.TmpDir, "alive")
aliveLock, err := runtime.getRuntimeAliveLock()
if err != nil {
return fmt.Errorf("acquiring runtime init lock: %w", err)
}
// Acquire the lock and hold it until we return
// This ensures that no two processes will be in runtime.refresh at once
aliveLock.Lock()
doRefresh := false
unLockFunc := aliveLock.Unlock
defer func() {
if unLockFunc != nil {
unLockFunc()
}
}()
err = fileutils.Exists(runtimeAliveFile)
if err != nil {
// If we need to refresh, then it is safe to assume there are
// no containers running. Create immediately a namespace, as
// we will need to access the storage.
if needsUserns {
// warn users if mode is rootless and cgroup manager is systemd
// and no valid systemd session is present
// warn only whenever new namespace is created
if runtime.config.Engine.CgroupManager == config.SystemdCgroupsManager {
unified, _ := cgroups.IsCgroup2UnifiedMode()
if unified && rootless.IsRootless() && !systemd.IsSystemdSessionValid(rootless.GetRootlessUID()) {
logrus.Debug("Invalid systemd user session for current user")
}
}
unLockFunc()
unLockFunc = nil
pausePid, err := util.GetRootlessPauseProcessPidPath()
if err != nil {
return fmt.Errorf("could not get pause process pid file path: %w", err)
}
// create the path in case it does not already exists
// https://github.com/containers/podman/issues/8539
if err := os.MkdirAll(filepath.Dir(pausePid), 0o700); err != nil {
return fmt.Errorf("could not create pause process pid file directory: %w", err)
}
became, ret, err := rootless.BecomeRootInUserNS(pausePid)
if err != nil {
return err
}
if became {
// Check if the pause process was created. If it was created, then
// move it to its own systemd scope.
systemdCommon.MovePauseProcessToScope(pausePid)
// gocritic complains because defer is not run on os.Exit()
// However this is fine because the lock is released anyway when the process exits
//nolint:gocritic
os.Exit(ret)
}
}
// If the file doesn't exist, we need to refresh the state
// This will trigger on first use as well, but refreshing an
// empty state only creates a single file
// As such, it's not really a performance concern
if errors.Is(err, os.ErrNotExist) {
doRefresh = true
} else {
return fmt.Errorf("reading runtime status file %s: %w", runtimeAliveFile, err)
}
}
runtime.lockManager, err = getLockManager(runtime)
if err != nil {
return err
}
// Mark the runtime as valid - ready to be used, cannot be modified
// further.
// Need to do this *before* refresh as we can remove containers there.
// Should not be a big deal as we don't return it to users until after
// refresh runs.
runtime.valid = true
// Setup the worker channel early to start accepting jobs from refresh,
// but do not start to execute the jobs right away. The runtime is not
// ready at this point.
runtime.setupWorkerQueue()
// If we need to refresh the state, do it now - things are guaranteed to
// be set up by now.
if doRefresh {
// Ensure we have a store before refresh occurs
if runtime.store == nil {
if err := runtime.configureStore(); err != nil {
return fmt.Errorf("configure storage: %w", err)
}
}
if err2 := runtime.refresh(ctx, runtimeAliveFile); err2 != nil {
return err2
}
}
// Check current boot ID - will be written to the alive file.
if err := runtime.checkBootID(runtimeAliveFile); err != nil {
return err
}
runtime.startWorker()
return nil
}
// TmpDir gets the current Libpod temporary files directory.
func (r *Runtime) TmpDir() (string, error) {
if !r.valid {
return "", define.ErrRuntimeStopped
}
return r.config.Engine.TmpDir, nil
}
// GetConfig returns the configuration used by the runtime.
// Note that the returned value is not a copy and must hence
// only be used in a reading fashion.
func (r *Runtime) GetConfigNoCopy() (*config.Config, error) {
if !r.valid {
return nil, define.ErrRuntimeStopped
}
return r.config, nil
}
// GetConfig returns a copy of the configuration used by the runtime.
// Please use GetConfigNoCopy() in case you only want to read from
// but not write to the returned config.
func (r *Runtime) GetConfig() (*config.Config, error) {
rtConfig, err := r.GetConfigNoCopy()
if err != nil {
return nil, err
}
config := new(config.Config)
// Copy so the caller won't be able to modify the actual config
if err := JSONDeepCopy(rtConfig, config); err != nil {
return nil, fmt.Errorf("copying config: %w", err)
}
return config, nil
}
// libimageEventsMap translates a libimage event type to a libpod event status.
var libimageEventsMap = map[libimage.EventType]events.Status{
libimage.EventTypeImagePull: events.Pull,
libimage.EventTypeImagePullError: events.PullError,
libimage.EventTypeImagePush: events.Push,
libimage.EventTypeImageRemove: events.Remove,
libimage.EventTypeImageLoad: events.LoadFromArchive,
libimage.EventTypeImageSave: events.Save,
libimage.EventTypeImageTag: events.Tag,
libimage.EventTypeImageUntag: events.Untag,
libimage.EventTypeImageMount: events.Mount,
libimage.EventTypeImageUnmount: events.Unmount,
}
// libimageEvents spawns a goroutine which will listen for events on
// the libimage.Runtime. The goroutine will be cleaned up implicitly
// when the main() exists.
func (r *Runtime) libimageEvents() {
r.libimageEventsShutdown = make(chan bool)
toLibpodEventStatus := func(e *libimage.Event) events.Status {
status, found := libimageEventsMap[e.Type]
if !found {
return "Unknown"
}
return status
}
eventChannel := r.libimageRuntime.EventChannel()
go func() {
sawShutdown := false
for {
// Make sure to read and write all events before
// shutting down.
for len(eventChannel) > 0 {
libimageEvent := <-eventChannel
e := events.Event{
ID: libimageEvent.ID,
Name: libimageEvent.Name,
Status: toLibpodEventStatus(libimageEvent),
Time: libimageEvent.Time,
Type: events.Image,
}
if libimageEvent.Error != nil {
e.Error = libimageEvent.Error.Error()
}
if err := r.eventer.Write(e); err != nil {
logrus.Errorf("Unable to write image event: %q", err)
}
}
if sawShutdown {
close(r.libimageEventsShutdown)
return
}
select {
case <-r.libimageEventsShutdown:
sawShutdown = true
case <-time.After(100 * time.Millisecond):
}
}
}()
}
// DeferredShutdown shuts down the runtime without exposing any
// errors. This is only meant to be used when the runtime is being
// shutdown within a defer statement; else use Shutdown
func (r *Runtime) DeferredShutdown(force bool) {
_ = r.Shutdown(force)
}
// Shutdown shuts down the runtime and associated containers and storage
// If force is true, containers and mounted storage will be shut down before
// cleaning up; if force is false, an error will be returned if there are
// still containers running or mounted
func (r *Runtime) Shutdown(force bool) error {
if !r.valid {
return nil
}
if r.workerChannel != nil {
r.workerGroup.Wait()
close(r.workerChannel)
}
r.valid = false
// Shutdown all containers if --force is given
if force {
ctrs, err := r.state.AllContainers(false)
if err != nil {
logrus.Errorf("Retrieving containers from database: %v", err)
} else {
for _, ctr := range ctrs {
if err := ctr.StopWithTimeout(r.config.Engine.StopTimeout); err != nil {
logrus.Errorf("Stopping container %s: %v", ctr.ID(), err)
}
}
}
}
var lastError error
// If no store was requested, it can be nil and there is no need to
// attempt to shut it down
if r.store != nil {
// Wait for the events to be written.
if r.libimageEventsShutdown != nil {
// Tell loop to shutdown
r.libimageEventsShutdown <- true
// Wait for close to signal shutdown
<-r.libimageEventsShutdown
}
// Note that the libimage runtime shuts down the store.
if err := r.libimageRuntime.Shutdown(force); err != nil {
lastError = fmt.Errorf("shutting down container storage: %w", err)
}
}
if err := r.state.Close(); err != nil {
if lastError != nil {
logrus.Error(lastError)
}
lastError = err
}
return lastError
}
// Reconfigures the runtime after a reboot
// Refreshes the state, recreating temporary files
// Does not check validity as the runtime is not valid until after this has run
func (r *Runtime) refresh(ctx context.Context, alivePath string) error {
logrus.Debugf("Podman detected system restart - performing state refresh")
// Clear state of database if not running in container
if !graphRootMounted() {
// First clear the state in the database
if err := r.state.Refresh(); err != nil {
return err
}
}
// Next refresh the state of all containers to recreate dirs and
// namespaces, and all the pods to recreate cgroups.
// Containers, pods, and volumes must also reacquire their locks.
ctrs, err := r.state.AllContainers(false)
if err != nil {
return fmt.Errorf("retrieving all containers from state: %w", err)
}
pods, err := r.state.AllPods()
if err != nil {
return fmt.Errorf("retrieving all pods from state: %w", err)
}
vols, err := r.state.AllVolumes()
if err != nil {
return fmt.Errorf("retrieving all volumes from state: %w", err)
}
// No locks are taken during pod, volume, and container refresh.
// Furthermore, the pod/volume/container refresh() functions are not
// allowed to take locks themselves.
// We cannot assume that any pod/volume/container has a valid lock until
// after this function has returned.
// The runtime alive lock should suffice to provide mutual exclusion
// until this has run.
for _, ctr := range ctrs {
if err := ctr.refresh(); err != nil {
logrus.Errorf("Refreshing container %s: %v", ctr.ID(), err)
}
// This is the only place it's safe to use ctr.state.State unlocked
// We're holding the alive lock, guaranteed to be the only Libpod on the system right now.
if (ctr.AutoRemove() && ctr.state.State == define.ContainerStateExited) || ctr.state.State == define.ContainerStateRemoving {
opts := ctrRmOpts{
// Don't force-remove, we're supposed to be fresh off a reboot
// If we have to force something is seriously wrong
Force: false,
RemoveVolume: true,
}
// This container should have autoremoved before the
// reboot but did not.
// Get rid of it.
if _, _, err := r.removeContainer(ctx, ctr, opts); err != nil {
logrus.Errorf("Unable to remove container %s which should have autoremoved: %v", ctr.ID(), err)
}
}
}
for _, pod := range pods {
if err := pod.refresh(); err != nil {
logrus.Errorf("Refreshing pod %s: %v", pod.ID(), err)
}
}
for _, vol := range vols {
if err := vol.refresh(); err != nil {
logrus.Errorf("Refreshing volume %s: %v", vol.Name(), err)
}
}
// Create a file indicating the runtime is alive and ready
file, err := os.OpenFile(alivePath, os.O_RDONLY|os.O_CREATE, 0644)
if err != nil {
return fmt.Errorf("creating runtime status file: %w", err)
}
defer file.Close()
r.NewSystemEvent(events.Refresh)
return nil
}
// Info returns the store and host information
func (r *Runtime) Info() (*define.Info, error) {
return r.info()
}
// generateName generates a unique name for a container or pod.
func (r *Runtime) generateName() (string, error) {
for {
name := namesgenerator.GetRandomName(0)
// Make sure container with this name does not exist
if _, err := r.state.LookupContainer(name); err == nil {
continue
} else if !errors.Is(err, define.ErrNoSuchCtr) {
return "", err
}
// Make sure pod with this name does not exist
if _, err := r.state.LookupPod(name); err == nil {
continue
} else if !errors.Is(err, define.ErrNoSuchPod) {
return "", err
}
return name, nil
}
// The code should never reach here.
}
// Configure store and image runtime
func (r *Runtime) configureStore() error {
store, err := storage.GetStore(r.storageConfig)
if err != nil {
return err
}
r.store = store
is.Transport.SetStore(store)
// Set up a storage service for creating container root filesystems from
// images
r.storageService = getStorageService(r.store)
runtimeOptions := &libimage.RuntimeOptions{
SystemContext: r.imageContext,
}
libimageRuntime, err := libimage.RuntimeFromStore(store, runtimeOptions)
if err != nil {
return err
}
r.libimageRuntime = libimageRuntime
// Run the libimage events routine.
r.libimageEvents()
return nil
}
// LibimageRuntime ... to allow for a step-by-step migration to libimage.
func (r *Runtime) LibimageRuntime() *libimage.Runtime {
return r.libimageRuntime
}
// SystemContext returns the imagecontext
func (r *Runtime) SystemContext() *types.SystemContext {
// Return the context from the libimage runtime. libimage is sensitive
// to a number of env vars.
return r.libimageRuntime.SystemContext()
}
// GetOCIRuntimePath retrieves the path of the default OCI runtime.
func (r *Runtime) GetOCIRuntimePath() string {
return r.defaultOCIRuntime.Path()
}
// DefaultOCIRuntime return copy of Default OCI Runtime
func (r *Runtime) DefaultOCIRuntime() OCIRuntime {
return r.defaultOCIRuntime
}
// StorageConfig retrieves the storage options for the container runtime
func (r *Runtime) StorageConfig() storage.StoreOptions {
return r.storageConfig
}
func (r *Runtime) GarbageCollect() error {
return r.store.GarbageCollect()
}
// RunRoot retrieves the current c/storage temporary directory in use by Libpod.
func (r *Runtime) RunRoot() string {
if r.store == nil {
return ""
}
return r.store.RunRoot()
}
// GraphRoot retrieves the current c/storage directory in use by Libpod.
func (r *Runtime) GraphRoot() string {
if r.store == nil {
return ""
}
return r.store.GraphRoot()
}
// GetPodName retrieves the pod name associated with a given full ID.
// If the given ID does not correspond to any existing Pod or Container,
// ErrNoSuchPod is returned.
func (r *Runtime) GetPodName(id string) (string, error) {
if !r.valid {
return "", define.ErrRuntimeStopped
}
return r.state.GetPodName(id)
}
// DBConfig is a set of Libpod runtime configuration settings that are saved in
// a State when it is first created, and can subsequently be retrieved.
type DBConfig struct {
LibpodRoot string
LibpodTmp string
StorageRoot string
StorageTmp string
GraphDriver string
VolumePath string
}
// mergeDBConfig merges the configuration from the database.
func (r *Runtime) mergeDBConfig(dbConfig *DBConfig) {
c := &r.config.Engine
if !r.storageSet.RunRootSet && dbConfig.StorageTmp != "" {
if r.storageConfig.RunRoot != dbConfig.StorageTmp &&
r.storageConfig.RunRoot != "" {
logrus.Debugf("Overriding run root %q with %q from database",
r.storageConfig.RunRoot, dbConfig.StorageTmp)
}
r.storageConfig.RunRoot = dbConfig.StorageTmp
}
if !r.storageSet.GraphRootSet && dbConfig.StorageRoot != "" {
if r.storageConfig.GraphRoot != dbConfig.StorageRoot &&
r.storageConfig.GraphRoot != "" {
logrus.Debugf("Overriding graph root %q with %q from database",
r.storageConfig.GraphRoot, dbConfig.StorageRoot)
}
r.storageConfig.GraphRoot = dbConfig.StorageRoot
}
if !r.storageSet.GraphDriverNameSet && dbConfig.GraphDriver != "" {
if r.storageConfig.GraphDriverName != dbConfig.GraphDriver &&
r.storageConfig.GraphDriverName != "" {
logrus.Errorf("User-selected graph driver %q overwritten by graph driver %q from database - delete libpod local files (%q) to resolve. May prevent use of images created by other tools",
r.storageConfig.GraphDriverName, dbConfig.GraphDriver, r.storageConfig.GraphRoot)
}
r.storageConfig.GraphDriverName = dbConfig.GraphDriver
}
if !r.storageSet.StaticDirSet && dbConfig.LibpodRoot != "" {
if c.StaticDir != dbConfig.LibpodRoot && c.StaticDir != "" {
logrus.Debugf("Overriding static dir %q with %q from database", c.StaticDir, dbConfig.LibpodRoot)
}
c.StaticDir = dbConfig.LibpodRoot
}
if !r.storageSet.TmpDirSet && dbConfig.LibpodTmp != "" {
if c.TmpDir != dbConfig.LibpodTmp && c.TmpDir != "" {
logrus.Debugf("Overriding tmp dir %q with %q from database", c.TmpDir, dbConfig.LibpodTmp)
}
c.TmpDir = dbConfig.LibpodTmp
}
if !r.storageSet.VolumePathSet && dbConfig.VolumePath != "" {
if c.VolumePath != dbConfig.VolumePath && c.VolumePath != "" {
logrus.Debugf("Overriding volume path %q with %q from database", c.VolumePath, dbConfig.VolumePath)
}
c.VolumePath = dbConfig.VolumePath
}
}
func (r *Runtime) EnableLabeling() bool {
return r.config.Containers.EnableLabeling
}
// Reload reloads the configurations files
func (r *Runtime) Reload() error {
if err := r.reloadContainersConf(); err != nil {
return err
}
if err := r.reloadStorageConf(); err != nil {
return err
}
// Invalidate the registries.conf cache. The next invocation will
// reload all data.
sysregistriesv2.InvalidateCache()
return nil
}
// reloadContainersConf reloads the containers.conf
func (r *Runtime) reloadContainersConf() error {
config, err := config.Reload()
if err != nil {
return err
}
r.config = config
logrus.Infof("Applied new containers configuration: %v", config)
return nil
}
// reloadStorageConf reloads the storage.conf
func (r *Runtime) reloadStorageConf() error {
configFile, err := storage.DefaultConfigFile()
if err != nil {
return err
}
storage.ReloadConfigurationFile(configFile, &r.storageConfig)
logrus.Infof("Applied new storage configuration: %v", r.storageConfig)
return nil
}
// getVolumePlugin gets a specific volume plugin.
func (r *Runtime) getVolumePlugin(volConfig *VolumeConfig) (*plugin.VolumePlugin, error) {
// There is no plugin for local.
name := volConfig.Driver
timeout := volConfig.Timeout
if name == define.VolumeDriverLocal || name == "" {
return nil, nil
}
pluginPath, ok := r.config.Engine.VolumePlugins[name]
if !ok {
if name == define.VolumeDriverImage {
return nil, nil
}
return nil, fmt.Errorf("no volume plugin with name %s available: %w", name, define.ErrMissingPlugin)
}
return plugin.GetVolumePlugin(name, pluginPath, timeout, r.config)
}
// GetSecretsStorageDir returns the directory that the secrets manager should take
func (r *Runtime) GetSecretsStorageDir() string {
return filepath.Join(r.store.GraphRoot(), "secrets")
}
// SecretsManager returns the directory that the secrets manager should take
func (r *Runtime) SecretsManager() (*secrets.SecretsManager, error) {
if r.secretsManager == nil {
manager, err := secrets.NewManager(r.GetSecretsStorageDir())
if err != nil {
return nil, err
}
r.secretsManager = manager
}
return r.secretsManager, nil
}
func graphRootMounted() bool {
f, err := os.OpenFile("/run/.containerenv", os.O_RDONLY, os.ModePerm)
if err != nil {
return false
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
if scanner.Text() == "graphRootMounted=1" {
return true
}
}
return false
}
func (r *Runtime) graphRootMountedFlag(mounts []spec.Mount) string {
root := r.store.GraphRoot()
for _, val := range mounts {
if strings.HasPrefix(root, val.Source) {
return "graphRootMounted=1"
}
}
return ""
}
// Returns a copy of the runtime alive lock
func (r *Runtime) getRuntimeAliveLock() (*lockfile.LockFile, error) {
return lockfile.GetLockFile(filepath.Join(r.config.Engine.TmpDir, "alive.lck"))
}
// Network returns the network interface which is used by the runtime
func (r *Runtime) Network() nettypes.ContainerNetwork {
return r.network
}
// GetDefaultNetworkName returns the network interface which is used by the runtime
func (r *Runtime) GetDefaultNetworkName() string {
return r.config.Network.DefaultNetwork
}
// RemoteURI returns the API server URI
func (r *Runtime) RemoteURI() string {
return r.config.Engine.RemoteURI
}
// SetRemoteURI records the API server URI
func (r *Runtime) SetRemoteURI(uri string) {
r.config.Engine.RemoteURI = uri
}
// Get information on potential lock conflicts.
// Returns a map of lock number to object(s) using the lock, formatted as
// "container <id>" or "volume <id>" or "pod <id>", and an array of locks that
// are currently being held, formatted as []uint32.
// If the map returned is not empty, you should immediately renumber locks on
// the runtime, because you have a deadlock waiting to happen.
func (r *Runtime) LockConflicts() (map[uint32][]string, []uint32, error) {
// Make an internal map to store what lock is associated with what
locksInUse := make(map[uint32][]string)
ctrs, err := r.state.AllContainers(false)
if err != nil {
return nil, nil, err
}
for _, ctr := range ctrs {
lockNum := ctr.lock.ID()
ctrString := fmt.Sprintf("container %s", ctr.ID())
locksInUse[lockNum] = append(locksInUse[lockNum], ctrString)
}
pods, err := r.state.AllPods()
if err != nil {
return nil, nil, err
}
for _, pod := range pods {
lockNum := pod.lock.ID()
podString := fmt.Sprintf("pod %s", pod.ID())
locksInUse[lockNum] = append(locksInUse[lockNum], podString)
}
volumes, err := r.state.AllVolumes()
if err != nil {
return nil, nil, err
}
for _, vol := range volumes {
lockNum := vol.lock.ID()
volString := fmt.Sprintf("volume %s", vol.Name())
locksInUse[lockNum] = append(locksInUse[lockNum], volString)
}
// Now go through and find any entries with >1 item associated
toReturn := make(map[uint32][]string)
for lockNum, objects := range locksInUse {
// If debug logging is requested, just spit out *every* lock in
// use.
logrus.Debugf("Lock number %d is in use by %v", lockNum, objects)
if len(objects) > 1 {
toReturn[lockNum] = objects
}
}
locksHeld, err := r.lockManager.LocksHeld()
if err != nil {
if errors.Is(err, define.ErrNotImplemented) {
logrus.Warnf("Could not retrieve currently taken locks as the lock backend does not support this operation")
return toReturn, []uint32{}, nil
}
return nil, nil, err
}
return toReturn, locksHeld, nil
}
// PruneBuildContainers removes any build containers that were created during the build,
// but were not removed because the build was unexpectedly terminated.
//
// Note: This is not safe operation and should be executed only when no builds are in progress. It can interfere with builds in progress.
func (r *Runtime) PruneBuildContainers() ([]*reports.PruneReport, error) {
stageContainersPruneReports := []*reports.PruneReport{}
containers, err := r.store.Containers()
if err != nil {
return stageContainersPruneReports, err
}
for _, container := range containers {
path, err := r.store.ContainerDirectory(container.ID)
if err != nil {
return stageContainersPruneReports, err
}
if err := fileutils.Exists(filepath.Join(path, "buildah.json")); err != nil {
continue
}
report := &reports.PruneReport{
Id: container.ID,
}
size, err := r.store.ContainerSize(container.ID)
if err != nil {
report.Err = err
}
report.Size = uint64(size)
if err := r.store.DeleteContainer(container.ID); err != nil {
report.Err = errors.Join(report.Err, err)
}
stageContainersPruneReports = append(stageContainersPruneReports, report)
}
return stageContainersPruneReports, nil
}
// SystemCheck checks our storage for consistency, and depending on the options
// specified, will attempt to remove anything which fails consistency checks.
func (r *Runtime) SystemCheck(ctx context.Context, options entities.SystemCheckOptions) (entities.SystemCheckReport, error) {
what := storage.CheckEverything()
if options.Quick {
what = storage.CheckMost()
}
if options.UnreferencedLayerMaximumAge != nil {
tmp := *options.UnreferencedLayerMaximumAge
what.LayerUnreferencedMaximumAge = &tmp
}
storageReport, err := r.store.Check(what)
if err != nil {
return entities.SystemCheckReport{}, err
}
if len(storageReport.Containers) == 0 &&
len(storageReport.Layers) == 0 &&
len(storageReport.ROLayers) == 0 &&
len(storageReport.Images) == 0 &&
len(storageReport.ROImages) == 0 {
// no errors detected
return entities.SystemCheckReport{}, nil
}
mapErrorSlicesToStringSlices := func(m map[string][]error) map[string][]string {
if len(m) == 0 {
return nil
}
mapped := make(map[string][]string, len(m))
for k, errs := range m {
strs := make([]string, len(errs))
for i, e := range errs {
strs[i] = e.Error()
}
mapped[k] = strs
}
return mapped
}
report := entities.SystemCheckReport{
Errors: true,
Layers: mapErrorSlicesToStringSlices(storageReport.Layers),
ROLayers: mapErrorSlicesToStringSlices(storageReport.ROLayers),
Images: mapErrorSlicesToStringSlices(storageReport.Images),
ROImages: mapErrorSlicesToStringSlices(storageReport.ROImages),
Containers: mapErrorSlicesToStringSlices(storageReport.Containers),
}
if !options.Repair && report.Errors {
// errors detected, no corrective measures to be taken
return report, err
}
// get a list of images that we knew of before we tried to clean up any
// that were damaged
imagesBefore, err := r.store.Images()
if err != nil {
return report, fmt.Errorf("getting a list of images before attempting repairs: %w", err)
}
repairOptions := storage.RepairOptions{
RemoveContainers: options.RepairLossy,
}
var containers []*Container
if repairOptions.RemoveContainers {
// build a list of the containers that we claim as ours that we
// expect to be removing in a bit
for containerID := range storageReport.Containers {
ctr, lookupErr := r.state.LookupContainer(containerID)
if lookupErr != nil {
// we're about to remove it, so it's okay that
// it isn't even one of ours
continue
}
containers = append(containers, ctr)
}
}
// run the cleanup
merr := multierror.Append(nil, r.store.Repair(storageReport, &repairOptions)...)
if repairOptions.RemoveContainers {
// get the list of containers that storage will still admit to knowing about
containersAfter, err := r.store.Containers()
if err != nil {
merr = multierror.Append(merr, fmt.Errorf("getting a list of containers after attempting repairs: %w", err))
}
for _, ctr := range containers {
// if one of our containers that we tried to remove is
// still on disk, report an error
if slices.IndexFunc(containersAfter, func(containerAfter storage.Container) bool {
return containerAfter.ID == ctr.ID()
}) != -1 {
merr = multierror.Append(merr, fmt.Errorf("clearing storage for container %s: %w", ctr.ID(), err))
continue
}
// remove the container from our database
if removeErr := r.state.RemoveContainer(ctr); removeErr != nil {
merr = multierror.Append(merr, fmt.Errorf("updating state database to reflect removal of container %s: %w", ctr.ID(), removeErr))
continue
}
if report.RemovedContainers == nil {
report.RemovedContainers = make(map[string]string)
}
report.RemovedContainers[ctr.ID()] = ctr.config.Name
}
}
// get a list of images that are still around after we clean up any
// that were damaged
imagesAfter, err := r.store.Images()
if err != nil {
merr = multierror.Append(merr, fmt.Errorf("getting a list of images after attempting repairs: %w", err))
}
for _, imageBefore := range imagesBefore {
if slices.IndexFunc(imagesAfter, func(imageAfter storage.Image) bool {
return imageAfter.ID == imageBefore.ID
}) == -1 {
if report.RemovedImages == nil {
report.RemovedImages = make(map[string][]string)
}
report.RemovedImages[imageBefore.ID] = slices.Clone(imageBefore.Names)
}
}
if merr != nil {
err = merr.ErrorOrNil()
}
return report, err
}
func (r *Runtime) GetContainerExitCode(id string) (int32, error) {
return r.state.GetContainerExitCode(id)
}
|