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 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
|
package drivers
import (
"cmp"
"context"
"errors"
"fmt"
"math"
"net/http"
"os"
"path/filepath"
"slices"
"sort"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/google/uuid"
internalInstance "github.com/lxc/incus/v6/internal/instance"
"github.com/lxc/incus/v6/internal/server/backup"
"github.com/lxc/incus/v6/internal/server/db"
dbCluster "github.com/lxc/incus/v6/internal/server/db/cluster"
"github.com/lxc/incus/v6/internal/server/device"
deviceConfig "github.com/lxc/incus/v6/internal/server/device/config"
"github.com/lxc/incus/v6/internal/server/device/nictype"
"github.com/lxc/incus/v6/internal/server/instance"
"github.com/lxc/incus/v6/internal/server/instance/instancetype"
"github.com/lxc/incus/v6/internal/server/instance/operationlock"
"github.com/lxc/incus/v6/internal/server/lifecycle"
"github.com/lxc/incus/v6/internal/server/locking"
"github.com/lxc/incus/v6/internal/server/operations"
"github.com/lxc/incus/v6/internal/server/project"
"github.com/lxc/incus/v6/internal/server/state"
storagePools "github.com/lxc/incus/v6/internal/server/storage"
internalUtil "github.com/lxc/incus/v6/internal/util"
"github.com/lxc/incus/v6/shared/api"
"github.com/lxc/incus/v6/shared/logger"
"github.com/lxc/incus/v6/shared/resources"
"github.com/lxc/incus/v6/shared/revert"
"github.com/lxc/incus/v6/shared/subprocess"
"github.com/lxc/incus/v6/shared/util"
)
// Track last autorestart of an instance.
var (
instancesLastRestart = map[int][10]time.Time{}
muInstancesLastRestart sync.Mutex
)
// ErrExecCommandNotFound indicates the command is not found.
var ErrExecCommandNotFound = api.StatusErrorf(http.StatusBadRequest, "Command not found")
// ErrExecCommandNotExecutable indicates the command is not executable.
var ErrExecCommandNotExecutable = api.StatusErrorf(http.StatusBadRequest, "Command not executable")
// ErrInstanceIsStopped indicates that the instance is stopped.
var ErrInstanceIsStopped error = api.StatusErrorf(http.StatusBadRequest, "The instance is already stopped")
// muNUMA is used to serialize NUMA node selection.
var muNUMA sync.Mutex
// deviceManager is an interface that allows managing device lifecycle.
type deviceManager interface {
deviceAdd(dev device.Device, instanceRunning bool) error
deviceRemove(dev device.Device, instanceRunning bool) error
deviceStart(dev device.Device, instanceRunning bool) (*deviceConfig.RunConfig, error)
deviceStop(dev device.Device, instanceRunning bool, stopHookNetnsPath string) error
}
// common provides structure common to all instance types.
type common struct {
op *operations.Operation
state *state.State
architecture int
creationDate time.Time
dbType instancetype.Type
description string
ephemeral bool
expandedConfig map[string]string
expandedDevices deviceConfig.Devices
expiryDate time.Time
id int
lastUsedDate time.Time
localConfig map[string]string
localDevices deviceConfig.Devices
logger logger.Logger
name string
node string
profiles []api.Profile
project api.Project
isSnapshot bool
stateful bool
// Cached handles.
// Do not use these variables directly, instead use their associated get functions so they
// will be initialized on demand.
storagePool storagePools.Pool
// volatileSetPersistDisable indicates whether the VolatileSet function should persist changes to the DB.
volatileSetPersistDisable bool
}
//
// SECTION: property getters
//
// Architecture returns the instance's architecture.
func (d *common) Architecture() int {
return d.architecture
}
// CreationDate returns the instance's creation date.
func (d *common) CreationDate() time.Time {
return d.creationDate
}
// Type returns the instance's type.
func (d *common) Type() instancetype.Type {
return d.dbType
}
// Description returns the instance's description.
func (d *common) Description() string {
return d.description
}
// IsEphemeral returns whether the instanc is ephemeral or not.
func (d *common) IsEphemeral() bool {
return d.ephemeral
}
// ExpandedConfig returns instance's expanded config.
func (d *common) ExpandedConfig() map[string]string {
return d.expandedConfig
}
// ExpandedDevices returns instance's expanded device config.
func (d *common) ExpandedDevices() deviceConfig.Devices {
return d.expandedDevices
}
// ExpiryDate returns when this snapshot expires.
func (d *common) ExpiryDate() time.Time {
if d.isSnapshot {
return d.expiryDate
}
// Return zero time if the instance is not a snapshot.
return time.Time{}
}
func (d *common) shouldAutoRestart() bool {
if !util.IsTrue(d.expandedConfig["boot.autorestart"]) {
return false
}
muInstancesLastRestart.Lock()
defer muInstancesLastRestart.Unlock()
// Check if the instance was ever auto-restarted.
timestamps, ok := instancesLastRestart[d.id]
if !ok || len(timestamps) == 0 {
// If not, record it and allow the auto-restart.
instancesLastRestart[d.id] = [10]time.Time{time.Now()}
return true
}
// If it has been auto-restarted, look for the oldest non-zero timestamp.
oldestIndex := 0
validTimestamps := 0
for i, timestamp := range timestamps {
if timestamp.IsZero() {
// We found an unused slot, lets use it.
timestamps[i] = time.Now()
instancesLastRestart[d.id] = timestamps
return true
}
validTimestamps++
if timestamp.Before(timestamps[oldestIndex]) {
oldestIndex = i
}
}
// Check if the oldest restart was more than a minute ago.
if timestamps[oldestIndex].Before(time.Now().Add(-1 * time.Minute)) {
// Remove the old timestamp and replace it with ours.
timestamps[oldestIndex] = time.Now()
instancesLastRestart[d.id] = timestamps
return true
}
// If not and all slots are used
return false
}
// ID gets instances's ID.
func (d *common) ID() int {
return d.id
}
// LastUsedDate returns the instance's last used date.
func (d *common) LastUsedDate() time.Time {
return d.lastUsedDate
}
// LocalConfig returns the instance's local config.
func (d *common) LocalConfig() map[string]string {
return d.localConfig
}
// LocalDevices returns the instance's local device config.
func (d *common) LocalDevices() deviceConfig.Devices {
return d.localDevices
}
// Name returns the instance's name.
func (d *common) Name() string {
return d.name
}
// CloudInitID returns the cloud-init instance-id.
func (d *common) CloudInitID() string {
id := d.LocalConfig()["volatile.cloud-init.instance-id"]
if id != "" {
return id
}
return d.name
}
// Location returns instance's location.
func (d *common) Location() string {
return d.node
}
// Profiles returns the instance's profiles.
func (d *common) Profiles() []api.Profile {
return d.profiles
}
// Project returns instance's project.
func (d *common) Project() api.Project {
return d.project
}
// IsSnapshot returns whether instance is snapshot or not.
func (d *common) IsSnapshot() bool {
return d.isSnapshot
}
// IsStateful returns whether instance is stateful or not.
func (d *common) IsStateful() bool {
return d.stateful
}
// Operation returns the instance's current operation.
func (d *common) Operation() *operations.Operation {
return d.op
}
//
// SECTION: general functions
//
// Backups returns a list of backups.
func (d *common) Backups() ([]backup.InstanceBackup, error) {
var backupNames []string
// Get all the backups
err := d.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
var err error
backupNames, err = tx.GetInstanceBackups(ctx, d.project.Name, d.name)
return err
})
if err != nil {
return nil, err
}
// Build the backup list
backups := []backup.InstanceBackup{}
for _, backupName := range backupNames {
backup, err := instance.BackupLoadByName(d.state, d.project.Name, backupName)
if err != nil {
return nil, err
}
backups = append(backups, *backup)
}
return backups, nil
}
// DeferTemplateApply records a template trigger to apply on next instance start.
func (d *common) DeferTemplateApply(trigger instance.TemplateTrigger) error {
// Avoid over-writing triggers that have already been set.
if d.localConfig["volatile.apply_template"] != "" {
return nil
}
err := d.VolatileSet(map[string]string{"volatile.apply_template": string(trigger)})
if err != nil {
return fmt.Errorf("Failed to set apply_template volatile key: %w", err)
}
return nil
}
// SetOperation sets the current operation.
func (d *common) SetOperation(op *operations.Operation) {
d.op = op
}
// Snapshots returns a list of snapshots.
func (d *common) Snapshots() ([]instance.Instance, error) {
if d.isSnapshot {
return []instance.Instance{}, nil
}
var snapshotArgs map[int]db.InstanceArgs
// Get all the snapshots for instance.
err := d.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
filter := dbCluster.InstanceSnapshotFilter{
Project: &d.project.Name,
Instance: &d.name,
}
dbSnapshots, err := dbCluster.GetInstanceSnapshots(ctx, tx.Tx(), filter)
if err != nil {
return err
}
dbInstances := make([]dbCluster.Instance, len(dbSnapshots))
for i, s := range dbSnapshots {
dbInstances[i] = s.ToInstance(d.name, d.node, d.dbType, d.architecture)
}
snapshotArgs, err = tx.InstancesToInstanceArgs(ctx, false, dbInstances...)
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
// Allow storage to pre-fetch snapshot details using bulk queries.
pool, err := d.getStoragePool()
if err != nil {
return nil, err
}
err = pool.CacheInstanceSnapshots(d)
if err != nil {
return nil, err
}
snapshots := make([]instance.Instance, 0, len(snapshotArgs))
for _, snapshotArg := range snapshotArgs {
// Populate profile info that was already loaded.
snapshotArg.Profiles = d.profiles
snapInst, err := instance.Load(d.state, snapshotArg, d.project)
if err != nil {
return nil, err
}
// Set the storage pool to the pre-loaded one (for caching).
snapLXC, ok := snapInst.(*lxc)
if ok {
snapLXC.storagePool = pool
}
snapQEMU, ok := snapInst.(*qemu)
if ok {
snapQEMU.storagePool = pool
}
// Pass through the current operation.
snapInst.SetOperation(d.op)
snapshots = append(snapshots, instance.Instance(snapInst))
}
sort.SliceStable(snapshots, func(i, j int) bool {
iCreation := snapshots[i].CreationDate()
jCreation := snapshots[j].CreationDate()
// Prefer sorting by creation date.
if iCreation.Before(jCreation) {
return true
}
// But if creation date is the same, then sort by ID.
if iCreation.Equal(jCreation) && snapshots[i].ID() < snapshots[j].ID() {
return true
}
return false
})
return snapshots, nil
}
// VolatileSet sets one or more volatile config keys.
func (d *common) VolatileSet(changes map[string]string) error {
// Quick check.
for key := range changes {
if !strings.HasPrefix(key, internalInstance.ConfigVolatilePrefix) {
return errors.New("Only volatile keys can be modified with VolatileSet")
}
}
// Update the database if required.
if !d.volatileSetPersistDisable {
var err error
if d.isSnapshot {
err = d.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
return tx.UpdateInstanceSnapshotConfig(d.id, changes)
})
} else {
err = d.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
return tx.UpdateInstanceConfig(d.id, changes)
})
}
if err != nil {
return fmt.Errorf("Failed to set volatile config: %w", err)
}
}
// Apply the change locally.
for key, value := range changes {
if value == "" {
delete(d.expandedConfig, key)
delete(d.localConfig, key)
continue
}
d.expandedConfig[key] = value
d.localConfig[key] = value
}
return nil
}
//
// SECTION: path getters
//
// ConsoleBufferLogPath returns the instance's console buffer log path.
func (d *common) ConsoleBufferLogPath() string {
return filepath.Join(d.LogPath(), "console.log")
}
// DevicesPath returns the instance's devices path.
func (d *common) DevicesPath() string {
name := project.Instance(d.project.Name, d.name)
return internalUtil.VarPath("devices", name)
}
// LogPath returns the instance's log path.
func (d *common) LogPath() string {
name := project.Instance(d.project.Name, d.name)
return internalUtil.LogPath(name)
}
// RunPath returns the instance's runtime path.
func (d *common) RunPath() string {
name := project.Instance(d.project.Name, d.name)
return internalUtil.RunPath(name)
}
// Path returns the instance's path.
func (d *common) Path() string {
return storagePools.InstancePath(d.dbType, d.project.Name, d.name, d.isSnapshot)
}
// ExecOutputPath returns the instance's exec output path.
func (d *common) ExecOutputPath() string {
return filepath.Join(d.Path(), "exec-output")
}
// RootfsPath returns the instance's rootfs path.
func (d *common) RootfsPath() string {
return filepath.Join(d.Path(), "rootfs")
}
// ShmountsPath returns the instance's shared mounts path.
func (d *common) ShmountsPath() string {
name := project.Instance(d.project.Name, d.name)
return internalUtil.VarPath("shmounts", name)
}
// StatePath returns the instance's state path.
func (d *common) StatePath() string {
return filepath.Join(d.Path(), "state")
}
// TemplatesPath returns the instance's templates path.
func (d *common) TemplatesPath() string {
return filepath.Join(d.Path(), "templates")
}
// StoragePool returns the storage pool name.
func (d *common) StoragePool() (string, error) {
pool, err := d.getStoragePool()
if err != nil {
return "", err
}
return pool.Name(), nil
}
//
// SECTION: internal functions
//
// deviceVolatileReset resets a device's volatile data when its removed or updated in such a way
// that it is removed then added immediately afterwards.
func (d *common) deviceVolatileReset(devName string, oldConfig, newConfig deviceConfig.Device) error {
volatileClear := make(map[string]string)
devicePrefix := fmt.Sprintf("volatile.%s.", devName)
newNICType, err := nictype.NICType(d.state, d.project.Name, newConfig)
if err != nil {
return err
}
oldNICType, err := nictype.NICType(d.state, d.project.Name, oldConfig)
if err != nil {
return err
}
// If the device type has changed, remove all old volatile keys.
// This will occur if the newConfig is empty (i.e the device is actually being removed) or
// if the device type is being changed but keeping the same name.
if newConfig["type"] != oldConfig["type"] || newNICType != oldNICType {
for k := range d.localConfig {
if !strings.HasPrefix(k, devicePrefix) {
continue
}
volatileClear[k] = ""
}
return d.VolatileSet(volatileClear)
}
// If the device type remains the same, then just remove any volatile keys that have
// the same key name present in the new config (i.e the new config is replacing the
// old volatile key).
for k := range d.localConfig {
if !strings.HasPrefix(k, devicePrefix) {
continue
}
devKey := strings.TrimPrefix(k, devicePrefix)
_, found := newConfig[devKey]
if found {
volatileClear[k] = ""
}
}
return d.VolatileSet(volatileClear)
}
// deviceVolatileGetFunc returns a function that retrieves a named device's volatile config and
// removes its device prefix from the keys.
func (d *common) deviceVolatileGetFunc(devName string) func() map[string]string {
return func() map[string]string {
volatile := make(map[string]string)
prefix := fmt.Sprintf("volatile.%s.", devName)
for k, v := range d.localConfig {
after, ok := strings.CutPrefix(k, prefix)
if ok {
volatile[after] = v
}
}
return volatile
}
}
// deviceVolatileSetFunc returns a function that can be called to save a named device's volatile
// config using keys that do not have the device's name prefixed.
func (d *common) deviceVolatileSetFunc(devName string) func(save map[string]string) error {
return func(save map[string]string) error {
volatileSave := make(map[string]string)
for k, v := range save {
volatileSave[fmt.Sprintf("volatile.%s.%s", devName, k)] = v
}
return d.VolatileSet(volatileSave)
}
}
// expandConfig applies the config of each profile in order, followed by the local config.
func (d *common) expandConfig() error {
d.expandedConfig = db.ExpandInstanceConfig(d.localConfig, d.profiles)
d.expandedDevices = db.ExpandInstanceDevices(d.localDevices, d.profiles)
return nil
}
// restartCommon handles the common part of instance restarts.
func (d *common) restartCommon(inst instance.Instance, timeout time.Duration) error {
// Setup a new operation for the stop/shutdown phase.
op, err := operationlock.Create(d.Project().Name, d.Name(), d.op, operationlock.ActionRestart, true, true)
if err != nil {
return fmt.Errorf("Create restart operation: %w", err)
}
// Handle ephemeral instances.
ephemeral := inst.IsEphemeral()
ctxMap := logger.Ctx{
"action": "shutdown",
"created": d.creationDate,
"ephemeral": ephemeral,
"used": d.lastUsedDate,
"timeout": timeout,
}
d.logger.Info("Restarting instance", ctxMap)
if ephemeral {
// Unset ephemeral flag
args := db.InstanceArgs{
Architecture: inst.Architecture(),
Config: inst.LocalConfig(),
Description: inst.Description(),
Devices: inst.LocalDevices(),
Ephemeral: false,
Profiles: inst.Profiles(),
Project: inst.Project().Name,
Type: inst.Type(),
Snapshot: inst.IsSnapshot(),
}
err := inst.Update(args, false)
if err != nil {
return err
}
// On function return, set the flag back on
defer func() {
args.Ephemeral = ephemeral
_ = inst.Update(args, false)
}()
}
if timeout == 0 {
err := inst.Stop(false)
if err != nil {
op.Done(err)
return err
}
} else {
if inst.IsFrozen() {
err = errors.New("Instance is not running")
op.Done(err)
return err
}
err := inst.Shutdown(timeout)
if err != nil {
op.Done(err)
return err
}
}
// Setup a new operation for the start phase.
op, err = operationlock.CreateWaitGet(d.Project().Name, d.Name(), d.op, operationlock.ActionRestart, nil, true, false)
if err != nil {
if errors.Is(err, operationlock.ErrNonReusuableSucceeded) {
// An existing matching operation has now succeeded, return.
return nil
}
return fmt.Errorf("Create restart (for start) operation: %w", err)
}
err = inst.Start(false)
if err != nil {
op.Done(err)
return err
}
d.logger.Info("Restarted instance", ctxMap)
d.state.Events.SendLifecycle(d.project.Name, lifecycle.InstanceRestarted.Event(d, nil))
return nil
}
// rebuildCommon handles the common part of instance rebuilds.
func (d *common) rebuildCommon(inst instance.Instance, img *api.Image, op *operations.Operation) error {
instLocalConfig := d.localConfig
// Reset the "image.*" keys.
for k := range instLocalConfig {
if strings.HasPrefix(k, "image.") {
delete(instLocalConfig, k)
}
}
delete(instLocalConfig, "volatile.base_image")
if img != nil {
for k, v := range img.Properties {
instLocalConfig[fmt.Sprintf("image.%s", k)] = v
}
instLocalConfig["volatile.base_image"] = img.Fingerprint
instLocalConfig["volatile.uuid.generation"] = instLocalConfig["volatile.uuid"]
}
// Reset relevant volatile keys.
delete(instLocalConfig, "volatile.idmap.next")
delete(instLocalConfig, "volatile.last_state.idmap")
pool, err := d.getStoragePool()
if err != nil {
return err
}
err = pool.DeleteInstance(inst, op)
if err != nil {
return err
}
// Rebuild as empty if there is no image provided.
if img == nil {
err = pool.CreateInstance(inst, nil)
if err != nil {
return err
}
} else {
err = pool.CreateInstanceFromImage(inst, img.Fingerprint, op)
if err != nil {
return err
}
}
err = d.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
err = dbCluster.UpdateInstanceConfig(ctx, tx.Tx(), int64(inst.ID()), instLocalConfig)
if err != nil {
return err
}
if img != nil {
err = tx.UpdateImageLastUseDate(ctx, inst.Project().Name, img.Fingerprint, time.Now().UTC())
if err != nil {
return err
}
}
return nil
})
if err != nil {
return err
}
d.localConfig = instLocalConfig
return nil
}
// runHooks executes the callback functions returned from a function.
func (d *common) runHooks(hooks []func() error) error {
// Run any post start hooks.
for _, hook := range hooks {
err := hook()
if err != nil {
return err
}
}
return nil
}
// snapshot handles the common part of the snapshotting process.
func (d *common) snapshotCommon(inst instance.Instance, name string, expiry time.Time, stateful bool) error {
reverter := revert.New()
defer reverter.Fail()
// Setup the arguments.
args := db.InstanceArgs{
Project: inst.Project().Name,
Architecture: inst.Architecture(),
Config: inst.LocalConfig(),
Type: inst.Type(),
Snapshot: true,
Devices: inst.LocalDevices(),
Ephemeral: inst.IsEphemeral(),
Name: inst.Name() + internalInstance.SnapshotDelimiter + name,
Profiles: inst.Profiles(),
Stateful: stateful,
ExpiryDate: expiry,
}
// Create the snapshot.
snap, snapInstOp, cleanup, err := instance.CreateInternal(d.state, args, d.op, true, true)
if err != nil {
return fmt.Errorf("Failed creating instance snapshot record %q: %w", name, err)
}
reverter.Add(cleanup)
defer snapInstOp.Done(err)
pool, err := storagePools.LoadByInstance(d.state, snap)
if err != nil {
return err
}
err = pool.CreateInstanceSnapshot(snap, inst, d.op)
if err != nil {
return fmt.Errorf("Create instance snapshot: %w", err)
}
reverter.Add(func() { _ = snap.Delete(true) })
// Mount volume for backup.yaml writing.
_, err = pool.MountInstance(inst, d.op)
if err != nil {
return fmt.Errorf("Create instance snapshot (mount source): %w", err)
}
defer func() { _ = pool.UnmountInstance(inst, d.op) }()
// Attempt to update backup.yaml for instance.
err = inst.UpdateBackupFile()
if err != nil {
return err
}
reverter.Success()
return nil
}
// updateProgress updates the operation metadata with a new progress string.
func (d *common) updateProgress(progress string) {
if d.op == nil {
return
}
meta := d.op.Metadata()
if meta == nil {
meta = make(map[string]any)
}
if meta["container_progress"] != progress {
meta["container_progress"] = progress
_ = d.op.UpdateMetadata(meta)
}
}
// insertConfigkey function attempts to insert the instance config key into the database. If the insert fails
// then the database is queried to check whether another query inserted the same key. If the key is still
// unpopulated then the insert querty is retried until it succeeds or a retry limit is reached.
// If the insert succeeds or the key is found to have been populated then the value of the key is returned.
func (d *common) insertConfigkey(key string, value string) (string, error) {
err := d.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
err := tx.CreateInstanceConfig(ctx, d.id, map[string]string{key: value})
if err == nil {
return nil
}
// Check if something else filled it in behind our back.
existingValue, errCheckExists := tx.GetInstanceConfig(ctx, d.id, key)
if errCheckExists != nil {
return err
}
value = existingValue
return nil
})
if err != nil {
return "", err
}
return value, nil
}
// isRunningStatusCode returns if instance is running from status code.
func (d *common) isRunningStatusCode(statusCode api.StatusCode) bool {
return statusCode != api.Error && statusCode != api.Stopped
}
// isStartableStatusCode returns an error if the status code means the instance cannot be started currently.
func (d *common) isStartableStatusCode(statusCode api.StatusCode) error {
if d.isRunningStatusCode(statusCode) {
return errors.New("The instance is already running")
}
// If the instance process exists but is crashed, don't allow starting until its been cleaned up, as it
// would likely fail to start anyway or leave the old process untracked.
if statusCode == api.Error {
return fmt.Errorf("The instance cannot be started as in %s status", statusCode)
}
return nil
}
// getStartupSnapNameAndExpiry returns the name and expiry for a snapshot to be taken at startup.
func (d *common) getStartupSnapNameAndExpiry(inst instance.Instance) (string, *time.Time, error) {
schedule := strings.ToLower(d.expandedConfig["snapshots.schedule"])
if schedule == "" {
return "", nil, nil
}
triggers := strings.Split(schedule, ", ")
if !slices.Contains(triggers, "@startup") {
return "", nil, nil
}
expiry, err := internalInstance.GetExpiry(time.Now(), d.expandedConfig["snapshots.expiry"])
if err != nil {
return "", nil, err
}
name, err := instance.NextSnapshotName(d.state, inst, "snap%d")
if err != nil {
return "", nil, err
}
return name, &expiry, nil
}
// validateStartup checks any constraints that would prevent start up from succeeding under normal circumstances.
func (d *common) validateStartup(stateful bool, statusCode api.StatusCode) error {
// Because the root disk is special and is mounted before the root disk device is setup we duplicate the
// pre-start check here before the isStartableStatusCode check below so that if there is a problem loading
// the instance status because the storage pool isn't available we don't mask the StatusServiceUnavailable
// error with an ERROR status code from the instance check instead.
_, rootDiskConf, err := internalInstance.GetRootDiskDevice(d.expandedDevices.CloneNative())
if err != nil {
return err
}
if !storagePools.IsAvailable(rootDiskConf["pool"]) {
return api.StatusErrorf(http.StatusServiceUnavailable, "Storage pool %q unavailable on this server", rootDiskConf["pool"])
}
// Validate architecture.
if !slices.Contains(d.state.OS.Architectures, d.architecture) {
return errors.New("Requested architecture isn't supported by this host")
}
// Must happen before creating operation Start lock to avoid the status check returning Stopped due to the
// existence of a Start operation lock.
err = d.isStartableStatusCode(statusCode)
if err != nil {
return err
}
return nil
}
// onStopOperationSetup creates or picks up the relevant operation. This is used in the stopns and stop hooks to
// ensure that a lock on their activities is held before the instance process is stopped. This prevents a start
// request run at the same time from overlapping with the stop process.
// Returns the operation (with the instance initiated marker set if the operation was created).
func (d *common) onStopOperationSetup(target string) (*operationlock.InstanceOperation, error) {
var err error
// Pick up the existing stop operation lock created in Start(), Restart(), Shutdown() or Stop() functions.
// If there is another ongoing operation that isn't in our inheritable list, wait until that has finished
// before proceeding to run the hook.
op := operationlock.Get(d.Project().Name, d.Name())
if op != nil && !op.ActionMatch(operationlock.ActionStart, operationlock.ActionRestart, operationlock.ActionStop, operationlock.ActionRestore, operationlock.ActionMigrate) {
d.logger.Debug("Waiting for existing operation lock to finish before running hook", logger.Ctx{"action": op.Action()})
_ = op.Wait(context.Background())
op = nil
}
if op == nil {
d.logger.Debug("Instance initiated stop", logger.Ctx{"action": target})
action := operationlock.ActionStop
if target == "reboot" {
action = operationlock.ActionRestart
}
op, err = operationlock.Create(d.Project().Name, d.Name(), d.op, action, false, false)
if err != nil {
return nil, fmt.Errorf("Failed creating %q operation: %w", action, err)
}
op.SetInstanceInitiated(true)
} else {
d.logger.Debug("Instance operation lock inherited for stop", logger.Ctx{"action": op.Action()})
}
return op, nil
}
// warningsDelete deletes any persistent warnings for the instance.
func (d *common) warningsDelete() error {
err := d.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
return dbCluster.DeleteWarnings(ctx, tx.Tx(), dbCluster.TypeInstance, d.ID())
})
if err != nil {
return fmt.Errorf("Failed deleting persistent warnings: %w", err)
}
return nil
}
// canMigrate determines if the given instance can be migrated and what kind of migration to attempt.
func (d *common) canMigrate(inst instance.Instance) string {
// Check policy for the instance.
config := d.ExpandedConfig()
val, ok := config["cluster.evacuate"]
if !ok {
val = "auto"
}
// If not using auto, just return the migration type.
if val != "auto" {
return val
}
// Look at attached devices.
for _, entry := range d.ExpandedDevices().Sorted() {
dev, err := d.deviceLoad(inst, entry.Name, entry.Config)
if err != nil {
logger.Warn("Instance will not be migrated due to a device error", logger.Ctx{"project": inst.Project().Name, "instance": inst.Name(), "device": dev.Name(), "err": err})
return "stop"
}
if !dev.CanMigrate() {
logger.Warn("Instance will not be migrated because its device cannot be migrated", logger.Ctx{"project": inst.Project().Name, "instance": inst.Name(), "device": dev.Name()})
return "stop"
}
}
// Check if set up for live migration.
// Limit automatic live-migration to virtual machines for now.
if inst.Type() == instancetype.VM && util.IsTrue(config["migration.stateful"]) {
return "live-migrate"
}
return "migrate"
}
// recordLastState records last power and used time into local config and database config.
func (d *common) recordLastState() error {
var err error
// Record power state.
d.localConfig["volatile.last_state.power"] = instance.PowerStateRunning
d.expandedConfig["volatile.last_state.power"] = instance.PowerStateRunning
// Database updates
return d.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
// Record power state.
err = tx.UpdateInstancePowerState(d.id, instance.PowerStateRunning)
if err != nil {
err = fmt.Errorf("Error updating instance power state: %w", err)
return err
}
// Update time instance last started time.
err = tx.UpdateInstanceLastUsedDate(d.id, time.Now().UTC())
if err != nil {
err = fmt.Errorf("Error updating instance last used: %w", err)
return err
}
return nil
})
}
func (d *common) setCoreSched(pids []int) error {
if !d.state.OS.CoreScheduling {
return nil
}
args := []string{
"forkcoresched",
"0",
}
for _, pid := range pids {
args = append(args, strconv.Itoa(pid))
}
_, err := subprocess.RunCommand(d.state.OS.ExecPath, args...)
return err
}
// getRootDiskDevice gets the name and configuration of the root disk device of an instance.
func (d *common) getRootDiskDevice() (string, map[string]string, error) {
devices := d.ExpandedDevices()
if d.IsSnapshot() {
parentName, _, _ := api.GetParentAndSnapshotName(d.name)
// Load the parent.
storageInstance, err := instance.LoadByProjectAndName(d.state, d.project.Name, parentName)
if err != nil {
return "", nil, err
}
devices = storageInstance.ExpandedDevices()
}
// Retrieve the instance's storage pool.
name, configuration, err := internalInstance.GetRootDiskDevice(devices.CloneNative())
if err != nil {
return "", nil, err
}
return name, configuration, nil
}
// resetInstanceID generates a new UUID and puts it in volatile.
func (d *common) resetInstanceID() error {
err := d.VolatileSet(map[string]string{"volatile.cloud-init.instance-id": uuid.New().String()})
if err != nil {
return fmt.Errorf("Failed to set volatile.cloud-init.instance-id: %w", err)
}
return nil
}
// needsNewInstanceID checks the changed data in an Update call to determine if a new instance-id is necessary.
func (d *common) needsNewInstanceID(changedConfig []string, oldExpandedDevices deviceConfig.Devices) bool {
// Look for cloud-init related config changes.
for _, key := range []string{
"cloud-init.vendor-data",
"cloud-init.user-data",
"cloud-init.network-config",
"user.vendor-data",
"user.user-data",
"user.network-config",
} {
if slices.Contains(changedConfig, key) {
return true
}
}
// Look for changes in network interface names.
getNICNames := func(devs deviceConfig.Devices) []string {
names := make([]string, 0, len(devs))
for devName, dev := range devs {
if dev["type"] != "nic" {
continue
}
if dev["name"] != "" {
names = append(names, dev["name"])
continue
}
configKey := fmt.Sprintf("volatile.%s.name", devName)
volatileName := d.localConfig[configKey]
if volatileName != "" {
names = append(names, dev["name"])
continue
}
names = append(names, devName)
}
return names
}
oldNames := getNICNames(oldExpandedDevices)
newNames := getNICNames(d.expandedDevices)
for _, entry := range oldNames {
if !slices.Contains(newNames, entry) {
return true
}
}
for _, entry := range newNames {
if !slices.Contains(oldNames, entry) {
return true
}
}
return false
}
// getStoragePool returns the current storage pool handle. To avoid a DB lookup each time this
// function is called, the handle is cached internally in the struct.
func (d *common) getStoragePool() (storagePools.Pool, error) {
if d.storagePool != nil {
return d.storagePool, nil
}
var poolName string
err := d.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
var err error
poolName, err = tx.GetInstancePool(ctx, d.Project().Name, d.Name())
if err != nil {
return fmt.Errorf("Failed getting instance pool: %w", err)
}
return nil
})
if err != nil {
return nil, err
}
pool, err := storagePools.LoadByName(d.state, poolName)
if err != nil {
return nil, err
}
d.storagePool = pool
return d.storagePool, nil
}
// deviceLoad instantiates and validates a new device and returns it along with enriched config.
func (d *common) deviceLoad(inst instance.Instance, deviceName string, rawConfig deviceConfig.Device) (device.Device, error) {
var configCopy deviceConfig.Device
var err error
// Create copy of config and load some fields from volatile if device is nic or infiniband.
if slices.Contains([]string{"nic", "infiniband"}, rawConfig["type"]) {
configCopy, err = inst.FillNetworkDevice(deviceName, rawConfig)
if err != nil {
return nil, err
}
} else {
// Otherwise copy the config so it cannot be modified by device.
configCopy = rawConfig.Clone()
}
dev, err := device.New(inst, d.state, deviceName, configCopy, d.deviceVolatileGetFunc(deviceName), d.deviceVolatileSetFunc(deviceName))
// If validation fails with unsupported device type then don't return the device for use.
if errors.Is(err, device.ErrUnsupportedDevType) {
return nil, err
}
// Return device even if error occurs as caller may still attempt to use device for stop and remove.
return dev, err
}
// deviceAdd loads a new device and calls its Add() function.
func (d *common) deviceAdd(dev device.Device, instanceRunning bool) error {
l := d.logger.AddContext(logger.Ctx{"device": dev.Name(), "type": dev.Config()["type"]})
l.Debug("Adding device")
if instanceRunning && !dev.CanHotPlug() {
return errors.New("Device cannot be added when instance is running")
}
return dev.Add()
}
// deviceRemove loads a new device and calls its Remove() function.
func (d *common) deviceRemove(dev device.Device, instanceRunning bool) error {
l := d.logger.AddContext(logger.Ctx{"device": dev.Name(), "type": dev.Config()["type"]})
l.Debug("Removing device")
if instanceRunning && !dev.CanHotPlug() {
return errors.New("Device cannot be removed when instance is running")
}
return dev.Remove()
}
// devicesAdd adds devices to instance.
func (d *common) devicesAdd(inst instance.Instance, instanceRunning bool) (revert.Hook, error) {
reverter := revert.New()
defer reverter.Fail()
for _, entry := range d.expandedDevices.Sorted() {
dev, err := d.deviceLoad(inst, entry.Name, entry.Config)
if err != nil {
if errors.Is(err, device.ErrUnsupportedDevType) {
continue // Skip unsupported device (allows for mixed instance type profiles).
}
// If device conflicts with another device then do not call the deviceAdd function below
// as this could cause the original device to be disrupted (such as allowing conflicting
// static NIC DHCP leases to be created). Instead just log an error.
// This will allow instances to be created with conflicting devices (such as when copying
// or restoring a backup) and allows the user to manually fix the conflicts in order to
// allow the instance to start.
if api.StatusErrorCheck(err, http.StatusConflict) {
d.logger.Error("Failed add validation for device, skipping add action", logger.Ctx{"device": entry.Name, "err": err})
continue
}
// Clear any volatile key that could have been set during validation.
_ = d.deviceVolatileReset(entry.Name, entry.Config, nil)
return nil, fmt.Errorf("Failed add validation for device %q: %w", entry.Name, err)
}
err = d.deviceAdd(dev, instanceRunning)
if err != nil {
return nil, fmt.Errorf("Failed to add device %q: %w", dev.Name(), err)
}
reverter.Add(func() { _ = d.deviceRemove(dev, instanceRunning) })
}
cleanup := reverter.Clone().Fail
reverter.Success()
return cleanup, nil
}
// devicesRegister calls the Register() function on all of the instance's devices.
func (d *common) devicesRegister(inst instance.Instance) {
for _, entry := range d.ExpandedDevices().Sorted() {
err := device.Register(inst, d.state, entry.Name, entry.Config)
if err != nil {
if errors.Is(err, device.ErrUnsupportedDevType) {
continue // Skip unsupported device (allows for mixed instance type profiles).
}
d.logger.Error("Failed to register device", logger.Ctx{"err": err, "device": entry.Name})
continue
}
}
}
// devicesUpdate applies device changes to an instance.
func (d *common) devicesUpdate(inst instance.Instance, removeDevices deviceConfig.Devices, addDevices deviceConfig.Devices, updateDevices deviceConfig.Devices, oldExpandedDevices deviceConfig.Devices, instanceRunning bool, userRequested bool) error {
reverter := revert.New()
defer reverter.Fail()
dm, ok := inst.(deviceManager)
if !ok {
return errors.New("Instance is not compatible with deviceManager interface")
}
// Remove devices in reverse order to how they were added.
for _, entry := range removeDevices.Reversed() {
l := d.logger.AddContext(logger.Ctx{"device": entry.Name, "userRequested": userRequested})
dev, err := d.deviceLoad(inst, entry.Name, entry.Config)
if err != nil {
if errors.Is(err, device.ErrUnsupportedDevType) {
continue // Skip unsupported device (allows for mixed instance type profiles).
}
// Just log an error, but still allow the device to be removed if usable device returned.
l.Error("Failed remove validation for device", logger.Ctx{"err": err})
}
// If a device was returned from deviceLoad even if validation fails, then try to stop and remove.
if dev != nil {
if instanceRunning {
err = dm.deviceStop(dev, instanceRunning, "")
if err != nil {
return fmt.Errorf("Failed to stop device %q: %w", dev.Name(), err)
}
}
err = d.deviceRemove(dev, instanceRunning)
if err != nil && !errors.Is(err, device.ErrUnsupportedDevType) {
return fmt.Errorf("Failed to remove device %q: %w", dev.Name(), err)
}
}
// Check whether we are about to add the same device back with updated config and
// if not, or if the device type has changed, then remove all volatile keys for
// this device (as its an actual removal or a device type change).
err = d.deviceVolatileReset(entry.Name, entry.Config, addDevices[entry.Name])
if err != nil {
return fmt.Errorf("Failed to reset volatile data for device %q: %w", entry.Name, err)
}
}
// Add devices in sorted order, this ensures that device mounts are added in path order.
for _, entry := range addDevices.Sorted() {
l := d.logger.AddContext(logger.Ctx{"device": entry.Name, "userRequested": userRequested})
dev, err := d.deviceLoad(inst, entry.Name, entry.Config)
if err != nil {
if errors.Is(err, device.ErrUnsupportedDevType) {
continue // Skip unsupported device (allows for mixed instance type profiles).
}
if userRequested {
// Clear any volatile key that could have been set during validation.
_ = d.deviceVolatileReset(entry.Name, entry.Config, nil)
return fmt.Errorf("Failed add validation for device %q: %w", entry.Name, err)
}
// If update is non-user requested (i.e from a snapshot restore), there's nothing we can
// do to fix the config and we don't want to prevent the snapshot restore so log and allow.
l.Error("Failed add validation for device, skipping as non-user requested", logger.Ctx{"err": err})
continue
}
err = d.deviceAdd(dev, instanceRunning)
if err != nil {
if userRequested {
return fmt.Errorf("Failed to add device %q: %w", dev.Name(), err)
}
// If update is non-user requested (i.e from a snapshot restore), there's nothing we can
// do to fix the config and we don't want to prevent the snapshot restore so log and allow.
l.Error("Failed to add device, skipping as non-user requested", logger.Ctx{"err": err})
}
reverter.Add(func() { _ = d.deviceRemove(dev, instanceRunning) })
if instanceRunning {
err = dev.PreStartCheck()
if err != nil {
return fmt.Errorf("Failed pre-start check for device %q: %w", dev.Name(), err)
}
_, err := dm.deviceStart(dev, instanceRunning)
if err != nil && !errors.Is(err, device.ErrUnsupportedDevType) {
return fmt.Errorf("Failed to start device %q: %w", dev.Name(), err)
}
reverter.Add(func() { _ = dm.deviceStop(dev, instanceRunning, "") })
}
// For the root disk, call Update as its size may change.
// Update will invoke applyQuota, which resizes the disk if necessary.
if internalInstance.IsRootDiskDevice(dev.Config()) {
err = dev.Update(oldExpandedDevices, instanceRunning)
if err != nil {
return fmt.Errorf("Failed to update device %q: %w", dev.Name(), err)
}
}
}
for _, entry := range updateDevices.Sorted() {
l := d.logger.AddContext(logger.Ctx{"device": entry.Name, "userRequested": userRequested})
dev, err := d.deviceLoad(inst, entry.Name, entry.Config)
if err != nil {
if errors.Is(err, device.ErrUnsupportedDevType) {
continue // Skip unsupported device (allows for mixed instance type profiles).
}
if userRequested {
return fmt.Errorf("Failed update validation for device %q: %w", entry.Name, err)
}
// If update is non-user requested (i.e from a snapshot restore), there's nothing we can
// do to fix the config and we don't want to prevent the snapshot restore so log and allow.
// By not calling dev.Update on validation error we avoid potentially disrupting another
// existing device if this device conflicts with it (such as allowing conflicting static
// NIC DHCP leases to be created).
l.Error("Failed update validation for device, removing device", logger.Ctx{"err": err})
// If a device was returned from deviceLoad when validation fails, then try to stop and
// remove it. This is to prevent devices being left in a state that is different to the
// invalid non-user requested config that has been applied to DB. The safest thing to do
// is to cleanup the device and wait for the config to be corrected.
if dev != nil {
if instanceRunning {
err = dm.deviceStop(dev, instanceRunning, "")
if err != nil {
l.Error("Failed to stop device after update validation failed", logger.Ctx{"err": err})
}
}
err = d.deviceRemove(dev, instanceRunning)
if err != nil && !errors.Is(err, device.ErrUnsupportedDevType) {
l.Error("Failed to remove device after update validation failed", logger.Ctx{"err": err})
}
}
continue
}
err = dev.Update(oldExpandedDevices, instanceRunning)
if err != nil {
return fmt.Errorf("Failed to update device %q: %w", dev.Name(), err)
}
}
reverter.Success()
return nil
}
// devicesRemove runs device removal function for each device.
func (d *common) devicesRemove(inst instance.Instance) {
for _, entry := range d.expandedDevices.Reversed() {
dev, err := d.deviceLoad(inst, entry.Name, entry.Config)
if err != nil {
if errors.Is(err, device.ErrUnsupportedDevType) {
continue // Skip unsupported device (allows for mixed instance type profiles).
}
// Just log an error, but still allow the device to be removed if usable device returned.
d.logger.Error("Failed remove validation for device", logger.Ctx{"device": entry.Name, "err": err})
}
// If a usable device was returned from deviceLoad try to remove anyway, even if validation fails.
// This allows for the scenario where a new version has additional validation restrictions
// than older versions and we still need to allow previously valid devices to be stopped even if
// they are no longer considered valid.
if dev != nil {
err = d.deviceRemove(dev, false)
if err != nil {
d.logger.Error("Failed to remove device", logger.Ctx{"device": dev.Name(), "err": err})
}
}
}
}
// updateBackupFileLock acquires the update backup file lock that protects concurrent access to actions that will call UpdateBackupFile() as part of their operation.
func (d *common) updateBackupFileLock(ctx context.Context) (locking.UnlockFunc, error) {
parentName, _, _ := api.GetParentAndSnapshotName(d.Name())
return locking.Lock(ctx, fmt.Sprintf("instance_updatebackupfile_%s_%s", d.Project().Name, parentName))
}
// deleteSnapshots calls the deleteFunc on each of the instance's snapshots in reverse order.
func (d *common) deleteSnapshots(deleteFunc func(snapInst instance.Instance) error) error {
snapInsts, err := d.Snapshots()
if err != nil {
return err
}
snapInstsCount := len(snapInsts)
for k := range snapInsts {
// Delete the snapshots in reverse order.
k = snapInstsCount - 1 - k
err = deleteFunc(snapInsts[k])
if err != nil {
return fmt.Errorf("Failed deleting snapshot %q: %w", snapInsts[k].Name(), err)
}
}
return nil
}
// balanceNUMANodes looks at all other instances and picks the least used NUMA node(s).
func (d *common) balanceNUMANodes() error {
muNUMA.Lock()
defer muNUMA.Unlock()
// Get the CPU information.
cpu, err := resources.GetCPU()
if err != nil {
return err
}
// Get a list of NUMA nodes.
nodes := []uint64{}
for _, cpuSocket := range cpu.Sockets {
for _, cpuCore := range cpuSocket.Cores {
for _, cpuThread := range cpuCore.Threads {
if !slices.Contains(nodes, cpuThread.NUMANode) {
nodes = append(nodes, cpuThread.NUMANode)
}
}
}
}
// Shortcut on single-node systems.
if len(nodes) == 1 {
return d.VolatileSet(map[string]string{"volatile.cpu.nodes": fmt.Sprintf("%d", nodes[0])})
}
// Get all local instances.
insts, err := instance.LoadNodeAll(d.state, instancetype.Any)
if err != nil {
return err
}
// Record current NUMA assignment (number of instance).
numaUsage := map[int64]int{}
for _, inst := range insts {
conf := inst.ExpandedConfig()
// Ignore ourselves.
if inst.ID() == d.id {
continue
}
// Ignore instances without any NUMA pinning.
if conf["limits.cpu.nodes"] == "" {
continue
}
// Parse the used NUMA nodes.
nodes := conf["limits.cpu.nodes"]
if nodes == "balanced" {
nodes = conf["volatile.cpu.nodes"]
}
numaNodeSet, err := resources.ParseNumaNodeSet(nodes)
if err != nil {
continue
}
for _, numaNode := range numaNodeSet {
numaUsage[numaNode]++
}
}
// Sort NUMA nodes by usage.
slices.SortFunc(nodes, func(i, j uint64) int {
return cmp.Compare(numaUsage[int64(i)], numaUsage[int64(j)])
})
// If `limits.cpu` is greater than the number of CPUs per NUMA node,
// then figure out how many NUMA nodes to use.
conf := d.ExpandedConfig()
cpusPerNumaNode := int(cpu.Total) / len(nodes)
limitsCPU, err := strconv.Atoi(conf["limits.cpu"])
if err == nil && limitsCPU > cpusPerNumaNode {
numaNodesToUse := int(math.Ceil(float64(limitsCPU) / float64(cpusPerNumaNode)))
selectedNumaNodes := make([]string, numaNodesToUse)
for i, node := range nodes[:numaNodesToUse] {
selectedNumaNodes[i] = strconv.FormatUint(node, 10)
}
joinedNumaNodes := strings.Join(selectedNumaNodes, ",")
return d.VolatileSet(map[string]string{"volatile.cpu.nodes": joinedNumaNodes})
}
return d.VolatileSet(map[string]string{"volatile.cpu.nodes": fmt.Sprintf("%d", nodes[0])})
}
// Gets the process starting time.
func (d *common) processStartedAt(pid int) (time.Time, error) {
if pid < 1 {
return time.Time{}, fmt.Errorf("Invalid PID %q", pid)
}
file, err := os.Stat(fmt.Sprintf("/proc/%d", pid))
if err != nil {
return time.Time{}, err
}
linuxInfo, ok := file.Sys().(*syscall.Stat_t)
if !ok {
return time.Time{}, errors.New("Bad stat type")
}
return time.Unix(int64(linuxInfo.Ctim.Sec), int64(linuxInfo.Ctim.Nsec)), nil
}
// ETag returns the instance configuration ETag data for pre-condition validation.
func (d *common) ETag() []any {
if d.IsSnapshot() {
return []any{d.expiryDate}
}
// Prepare the ETag
etag := []any{d.architecture, d.ephemeral, d.profiles, d.localDevices.Sorted()}
configKeys := make([]string, 0, len(d.localConfig))
for k := range d.localConfig {
configKeys = append(configKeys, k)
}
sort.Strings(configKeys)
for _, k := range configKeys {
etag = append(etag, fmt.Sprintf("%s=%s", k, d.localConfig[k]))
}
return etag
}
// ClearLimitsCPUNodes clears the "volatile.cpu.nodes" configuration if necessary.
func (d *common) ClearLimitsCPUNodes(changedConfig []string) {
if !slices.Contains(changedConfig, "limits.cpu.nodes") {
return
}
value := d.expandedConfig["limits.cpu.nodes"]
if value == "balanced" {
return
}
d.localConfig["volatile.cpu.nodes"] = ""
}
|