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
|
package main
import (
"bytes"
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
runtimeDebug "runtime/debug"
"slices"
"strconv"
"strings"
"github.com/gorilla/mux"
"golang.org/x/sys/unix"
internalInstance "github.com/lxc/incus/v6/internal/instance"
"github.com/lxc/incus/v6/internal/jmap"
"github.com/lxc/incus/v6/internal/server/auth"
"github.com/lxc/incus/v6/internal/server/backup"
"github.com/lxc/incus/v6/internal/server/db"
"github.com/lxc/incus/v6/internal/server/db/cluster"
"github.com/lxc/incus/v6/internal/server/db/query"
"github.com/lxc/incus/v6/internal/server/db/warningtype"
deviceConfig "github.com/lxc/incus/v6/internal/server/device/config"
"github.com/lxc/incus/v6/internal/server/instance"
"github.com/lxc/incus/v6/internal/server/instance/instancetype"
"github.com/lxc/incus/v6/internal/server/project"
"github.com/lxc/incus/v6/internal/server/request"
"github.com/lxc/incus/v6/internal/server/response"
"github.com/lxc/incus/v6/internal/server/state"
storagePools "github.com/lxc/incus/v6/internal/server/storage"
storageDrivers "github.com/lxc/incus/v6/internal/server/storage/drivers"
internalSQL "github.com/lxc/incus/v6/internal/sql"
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/osarch"
"github.com/lxc/incus/v6/shared/revert"
"github.com/lxc/incus/v6/shared/units"
"github.com/lxc/incus/v6/shared/util"
)
var apiInternal = []APIEndpoint{
internalBGPStateCmd,
internalClusterAcceptCmd,
internalClusterAssignCmd,
internalClusterHandoverCmd,
internalClusterRaftNodeCmd,
internalClusterRebalanceCmd,
internalContainerOnStartCmd,
internalContainerOnStopCmd,
internalContainerOnStopNSCmd,
internalVirtualMachineOnResizeCmd,
internalGarbageCollectorCmd,
internalImageOptimizeCmd,
internalImageRefreshCmd,
internalRAFTSnapshotCmd,
internalRebalanceLoadCmd,
internalReadyCmd,
internalShutdownCmd,
internalSQLCmd,
internalWarningCreateCmd,
}
// Daemon management internal commands.
var internalReadyCmd = APIEndpoint{
Path: "ready",
Get: APIEndpointAction{Handler: internalWaitReady, AccessHandler: allowPermission(auth.ObjectTypeServer, auth.EntitlementCanEdit)},
}
var internalShutdownCmd = APIEndpoint{
Path: "shutdown",
Put: APIEndpointAction{Handler: internalShutdown, AccessHandler: allowPermission(auth.ObjectTypeServer, auth.EntitlementCanEdit)},
}
// Internal managemnt traffic.
var internalImageOptimizeCmd = APIEndpoint{
Path: "image-optimize",
Post: APIEndpointAction{Handler: internalOptimizeImage, AccessHandler: allowPermission(auth.ObjectTypeServer, auth.EntitlementCanEdit)},
}
var internalRebalanceLoadCmd = APIEndpoint{
Path: "rebalance",
Get: APIEndpointAction{Handler: internalRebalanceLoad, AccessHandler: allowPermission(auth.ObjectTypeServer, auth.EntitlementCanEdit)},
}
var internalSQLCmd = APIEndpoint{
Path: "sql",
Get: APIEndpointAction{Handler: internalSQLGet, AccessHandler: allowPermission(auth.ObjectTypeServer, auth.EntitlementCanEdit)},
Post: APIEndpointAction{Handler: internalSQLPost, AccessHandler: allowPermission(auth.ObjectTypeServer, auth.EntitlementCanEdit)},
}
// Internal cluster traffic.
var internalClusterAcceptCmd = APIEndpoint{
Path: "cluster/accept",
Post: APIEndpointAction{Handler: internalClusterPostAccept, AccessHandler: allowPermission(auth.ObjectTypeServer, auth.EntitlementCanEdit)},
}
var internalClusterAssignCmd = APIEndpoint{
Path: "cluster/assign",
Post: APIEndpointAction{Handler: internalClusterPostAssign, AccessHandler: allowPermission(auth.ObjectTypeServer, auth.EntitlementCanEdit)},
}
var internalClusterHandoverCmd = APIEndpoint{
Path: "cluster/handover",
Post: APIEndpointAction{Handler: internalClusterPostHandover, AccessHandler: allowPermission(auth.ObjectTypeServer, auth.EntitlementCanEdit)},
}
var internalClusterRaftNodeCmd = APIEndpoint{
Path: "cluster/raft-node/{address}",
Delete: APIEndpointAction{Handler: internalClusterRaftNodeDelete, AccessHandler: allowPermission(auth.ObjectTypeServer, auth.EntitlementCanEdit)},
}
var internalClusterRebalanceCmd = APIEndpoint{
Path: "cluster/rebalance",
Post: APIEndpointAction{Handler: internalClusterPostRebalance, AccessHandler: allowPermission(auth.ObjectTypeServer, auth.EntitlementCanEdit)},
}
// Container hooks.
var internalContainerOnStartCmd = APIEndpoint{
Path: "containers/{instanceRef}/onstart",
Get: APIEndpointAction{Handler: internalContainerOnStart, AccessHandler: allowPermission(auth.ObjectTypeServer, auth.EntitlementCanEdit)},
}
var internalContainerOnStopNSCmd = APIEndpoint{
Path: "containers/{instanceRef}/onstopns",
Get: APIEndpointAction{Handler: internalContainerOnStopNS, AccessHandler: allowPermission(auth.ObjectTypeServer, auth.EntitlementCanEdit)},
}
var internalContainerOnStopCmd = APIEndpoint{
Path: "containers/{instanceRef}/onstop",
Get: APIEndpointAction{Handler: internalContainerOnStop, AccessHandler: allowPermission(auth.ObjectTypeServer, auth.EntitlementCanEdit)},
}
// Virtual machine hooks.
var internalVirtualMachineOnResizeCmd = APIEndpoint{
Path: "virtual-machines/{instanceRef}/onresize",
Get: APIEndpointAction{Handler: internalVirtualMachineOnResize, AccessHandler: allowPermission(auth.ObjectTypeServer, auth.EntitlementCanEdit)},
}
// Debugging.
var internalBGPStateCmd = APIEndpoint{
Path: "debug/bgp",
Get: APIEndpointAction{Handler: internalBGPState, AccessHandler: allowPermission(auth.ObjectTypeServer, auth.EntitlementCanEdit)},
}
var internalGarbageCollectorCmd = APIEndpoint{
Path: "debug/gc",
Get: APIEndpointAction{Handler: internalGC, AccessHandler: allowPermission(auth.ObjectTypeServer, auth.EntitlementCanEdit)},
}
var internalImageRefreshCmd = APIEndpoint{
Path: "debug/image-refresh",
Get: APIEndpointAction{Handler: internalRefreshImage, AccessHandler: allowPermission(auth.ObjectTypeServer, auth.EntitlementCanEdit)},
}
var internalRAFTSnapshotCmd = APIEndpoint{
Path: "debug/raft-snapshot",
Get: APIEndpointAction{Handler: internalRAFTSnapshot, AccessHandler: allowPermission(auth.ObjectTypeServer, auth.EntitlementCanEdit)},
}
var internalWarningCreateCmd = APIEndpoint{
Path: "debug/warnings",
Post: APIEndpointAction{Handler: internalCreateWarning, AccessHandler: allowPermission(auth.ObjectTypeServer, auth.EntitlementCanEdit)},
}
type internalImageOptimizePost struct {
Image api.Image `json:"image" yaml:"image"`
Pool string `json:"pool" yaml:"pool"`
}
type internalWarningCreatePost struct {
Location string `json:"location" yaml:"location"`
Project string `json:"project" yaml:"project"`
EntityTypeCode int `json:"entity_type_code" yaml:"entity_type_code"`
EntityID int `json:"entity_id" yaml:"entity_id"`
TypeCode int `json:"type_code" yaml:"type_code"`
Message string `json:"message" yaml:"message"`
}
// internalCreateWarning creates a warning, and is used for testing only.
func internalCreateWarning(d *Daemon, r *http.Request) response.Response {
body, err := io.ReadAll(r.Body)
if err != nil {
return response.InternalError(err)
}
rdr1 := io.NopCloser(bytes.NewBuffer(body))
rdr2 := io.NopCloser(bytes.NewBuffer(body))
reqRaw := jmap.Map{}
err = json.NewDecoder(rdr1).Decode(&reqRaw)
if err != nil {
return response.BadRequest(err)
}
req := internalWarningCreatePost{}
err = json.NewDecoder(rdr2).Decode(&req)
if err != nil {
return response.BadRequest(err)
}
req.EntityTypeCode, _ = reqRaw.GetInt("entity_type_code")
req.EntityID, _ = reqRaw.GetInt("entity_id")
// Check if the entity exists, and fail if it doesn't.
_, ok := cluster.EntityNames[req.EntityTypeCode]
if req.EntityTypeCode != -1 && !ok {
return response.SmartError(errors.New("Invalid entity type"))
}
err = d.State().DB.Cluster.Transaction(r.Context(), func(ctx context.Context, tx *db.ClusterTx) error {
return tx.UpsertWarning(ctx, req.Location, req.Project, req.EntityTypeCode, req.EntityID, warningtype.Type(req.TypeCode), req.Message)
})
if err != nil {
return response.SmartError(fmt.Errorf("Failed to create warning: %w", err))
}
return response.EmptySyncResponse
}
func internalOptimizeImage(d *Daemon, r *http.Request) response.Response {
s := d.State()
req := &internalImageOptimizePost{}
// Parse the request.
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
return response.BadRequest(err)
}
err = imageCreateInPool(s, &req.Image, req.Pool)
if err != nil {
return response.SmartError(err)
}
return response.EmptySyncResponse
}
func internalRefreshImage(d *Daemon, _ *http.Request) response.Response {
s := d.State()
err := autoUpdateImages(s.ShutdownCtx, s)
if err != nil {
return response.SmartError(err)
}
return response.EmptySyncResponse
}
func internalWaitReady(d *Daemon, _ *http.Request) response.Response {
// Check that we're not shutting down.
isClosing := d.State().ShutdownCtx.Err() != nil
if isClosing {
return response.Unavailable(errors.New("Daemon is shutting down"))
}
if d.waitReady.Err() == nil {
return response.Unavailable(errors.New("Daemon not ready yet"))
}
return response.EmptySyncResponse
}
func internalShutdown(d *Daemon, r *http.Request) response.Response {
force := request.QueryParam(r, "force")
logger.Info("Asked to shutdown by API", logger.Ctx{"force": force})
if d.State().ShutdownCtx.Err() != nil {
return response.SmartError(api.StatusErrorf(http.StatusTooManyRequests, "Shutdown already in progress"))
}
forceCtx, forceCtxCancel := context.WithCancel(context.Background())
if force == "true" {
forceCtxCancel() // Don't wait for operations to finish.
}
return response.ManualResponse(func(w http.ResponseWriter) error {
defer forceCtxCancel()
<-d.setupChan // Wait for daemon to start.
// Run shutdown sequence synchronously.
stopErr := d.Stop(forceCtx, unix.SIGPWR)
err := response.SmartError(stopErr).Render(w)
if err != nil {
return err
}
// Send the response before the daemon process ends.
f, ok := w.(http.Flusher)
if ok {
f.Flush()
} else {
return errors.New("http.ResponseWriter is not type http.Flusher")
}
// Send result of d.Stop() to cmdDaemon so that process stops with correct exit code from Stop().
go func() {
<-r.Context().Done() // Wait until request is finished.
d.shutdownDoneCh <- stopErr
}()
return nil
})
}
// internalContainerHookLoadFromRequestReference loads the container from the instance reference in the request.
// It detects whether the instance reference is an instance ID or instance name and loads instance accordingly.
func internalContainerHookLoadFromReference(s *state.State, r *http.Request) (instance.Instance, error) {
var inst instance.Instance
instanceRef, err := url.PathUnescape(mux.Vars(r)["instanceRef"])
if err != nil {
return nil, err
}
projectName := request.ProjectParam(r)
instanceID, err := strconv.Atoi(instanceRef)
if err == nil {
inst, err = instance.LoadByID(s, instanceID)
if err != nil {
return nil, err
}
} else {
inst, err = instance.LoadByProjectAndName(s, projectName, instanceRef)
if err != nil {
if api.StatusErrorCheck(err, http.StatusNotFound) {
return nil, err
}
// If DB not available, try loading from backup file.
logger.Warn("Failed loading instance from database, trying backup file", logger.Ctx{"project": projectName, "instance": instanceRef, "err": err})
instancePath := filepath.Join(internalUtil.VarPath("containers"), project.Instance(projectName, instanceRef))
inst, err = instance.LoadFromBackup(s, projectName, instancePath, false)
if err != nil {
return nil, err
}
}
}
if inst.Type() != instancetype.Container {
return nil, errors.New("Instance is not container type")
}
return inst, nil
}
func internalContainerOnStart(d *Daemon, r *http.Request) response.Response {
s := d.State()
inst, err := internalContainerHookLoadFromReference(s, r)
if err != nil {
logger.Error("The start hook failed to load", logger.Ctx{"err": err})
return response.SmartError(err)
}
err = inst.OnHook(instance.HookStart, nil)
if err != nil {
logger.Error("The start hook failed", logger.Ctx{"instance": inst.Name(), "err": err})
return response.SmartError(err)
}
return response.EmptySyncResponse
}
func internalContainerOnStopNS(d *Daemon, r *http.Request) response.Response {
s := d.State()
inst, err := internalContainerHookLoadFromReference(s, r)
if err != nil {
logger.Error("The stopns hook failed to load", logger.Ctx{"err": err})
return response.SmartError(err)
}
target := request.QueryParam(r, "target")
if target == "" {
target = "unknown"
}
netns := request.QueryParam(r, "netns")
args := map[string]string{
"target": target,
"netns": netns,
}
err = inst.OnHook(instance.HookStopNS, args)
if err != nil {
logger.Error("The stopns hook failed", logger.Ctx{"instance": inst.Name(), "err": err})
return response.SmartError(err)
}
return response.EmptySyncResponse
}
func internalContainerOnStop(d *Daemon, r *http.Request) response.Response {
s := d.State()
inst, err := internalContainerHookLoadFromReference(s, r)
if err != nil {
logger.Error("The stop hook failed to load", logger.Ctx{"err": err})
return response.SmartError(err)
}
target := request.QueryParam(r, "target")
if target == "" {
target = "unknown"
}
args := map[string]string{
"target": target,
}
err = inst.OnHook(instance.HookStop, args)
if err != nil {
logger.Error("The stop hook failed", logger.Ctx{"instance": inst.Name(), "err": err})
return response.SmartError(err)
}
return response.EmptySyncResponse
}
func internalVirtualMachineOnResize(d *Daemon, r *http.Request) response.Response {
s := d.State()
// Get the instance ID.
instanceID, err := strconv.Atoi(mux.Vars(r)["instanceRef"])
if err != nil {
return response.BadRequest(err)
}
// Get the devices list.
devices := request.QueryParam(r, "devices")
if devices == "" {
return response.BadRequest(errors.New("Resize hook requires a list of devices"))
}
// Load by ID.
inst, err := instance.LoadByID(s, instanceID)
if err != nil {
return response.SmartError(err)
}
// Update the local instance.
for _, dev := range strings.Split(devices, ",") {
fields := strings.SplitN(dev, ":", 2)
if len(fields) != 2 {
return response.BadRequest(fmt.Errorf("Invalid device/size tuple: %s", dev))
}
size, err := strconv.ParseInt(fields[1], 16, 64)
if err != nil {
return response.BadRequest(err)
}
runConf := deviceConfig.RunConfig{}
runConf.Mounts = []deviceConfig.MountEntryItem{
{
DevName: fields[0],
Size: size,
},
}
err = inst.DeviceEventHandler(&runConf)
if err != nil {
return response.InternalError(err)
}
}
return response.EmptySyncResponse
}
// Perform a database dump.
func internalSQLGet(d *Daemon, r *http.Request) response.Response {
s := d.State()
database := r.FormValue("database")
if !slices.Contains([]string{"local", "global"}, database) {
return response.BadRequest(errors.New("Invalid database"))
}
schemaFormValue := r.FormValue("schema")
schemaOnly, err := strconv.Atoi(schemaFormValue)
if err != nil {
schemaOnly = 0
}
var db *sql.DB
if database == "global" {
db = s.DB.Cluster.DB()
} else {
db = s.DB.Node.DB()
}
tx, err := db.BeginTx(r.Context(), nil)
if err != nil {
return response.SmartError(fmt.Errorf("Failed to start transaction: %w", err))
}
defer func() { _ = tx.Rollback() }()
dump, err := query.Dump(r.Context(), tx, schemaOnly == 1)
if err != nil {
return response.SmartError(fmt.Errorf("Failed dump database %s: %w", database, err))
}
return response.SyncResponse(true, internalSQL.SQLDump{Text: dump})
}
// Execute queries.
func internalSQLPost(d *Daemon, r *http.Request) response.Response {
s := d.State()
req := &internalSQL.SQLQuery{}
// Parse the request.
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
return response.BadRequest(err)
}
if !slices.Contains([]string{"local", "global"}, req.Database) {
return response.BadRequest(errors.New("Invalid database"))
}
if req.Query == "" {
return response.BadRequest(errors.New("No query provided"))
}
var db *sql.DB
if req.Database == "global" {
db = s.DB.Cluster.DB()
} else {
db = s.DB.Node.DB()
}
batch := internalSQL.SQLBatch{}
if req.Query == ".sync" {
d.gateway.Sync()
return response.SyncResponse(true, batch)
}
for _, query := range strings.Split(req.Query, ";") {
query = strings.TrimLeft(query, " ")
if query == "" {
continue
}
result := internalSQL.SQLResult{}
tx, err := db.Begin()
if err != nil {
return response.SmartError(err)
}
if strings.HasPrefix(strings.ToUpper(query), "SELECT") {
err = internalSQLSelect(tx, query, &result)
_ = tx.Rollback()
} else {
err = internalSQLExec(tx, query, &result)
if err != nil {
_ = tx.Rollback()
} else {
err = tx.Commit()
}
}
if err != nil {
return response.SmartError(err)
}
batch.Results = append(batch.Results, result)
}
return response.SyncResponse(true, batch)
}
func internalSQLSelect(tx *sql.Tx, query string, result *internalSQL.SQLResult) error {
result.Type = "select"
rows, err := tx.Query(query)
if err != nil {
return fmt.Errorf("Failed to execute query: %w", err)
}
defer func() { _ = rows.Close() }()
result.Columns, err = rows.Columns()
if err != nil {
return fmt.Errorf("Failed to fetch colume names: %w", err)
}
for rows.Next() {
row := make([]any, len(result.Columns))
rowPointers := make([]any, len(result.Columns))
for i := range row {
rowPointers[i] = &row[i]
}
err := rows.Scan(rowPointers...)
if err != nil {
return fmt.Errorf("Failed to scan row: %w", err)
}
for i, column := range row {
// Convert bytes to string. This is safe as
// long as we don't have any BLOB column type.
data, ok := column.([]byte)
if ok {
row[i] = string(data)
}
}
result.Rows = append(result.Rows, row)
}
err = rows.Err()
if err != nil {
return fmt.Errorf("Got a row error: %w", err)
}
return nil
}
func internalSQLExec(tx *sql.Tx, query string, result *internalSQL.SQLResult) error {
result.Type = "exec"
r, err := tx.Exec(query)
if err != nil {
return fmt.Errorf("Failed to exec query: %w", err)
}
result.RowsAffected, err = r.RowsAffected()
if err != nil {
return fmt.Errorf("Failed to fetch affected rows: %w", err)
}
return nil
}
// internalImportFromBackup creates instance, storage pool and volume DB records from an instance's backup file.
// It expects the instance volume to be mounted so that the backup.yaml file is readable.
func internalImportFromBackup(ctx context.Context, s *state.State, projectName string, instName string, allowNameOverride bool) error {
if instName == "" {
return errors.New("The name of the instance is required")
}
storagePoolsPath := internalUtil.VarPath("storage-pools")
storagePoolsDir, err := os.Open(storagePoolsPath)
if err != nil {
return err
}
// Get a list of all storage pools.
storagePoolNames, err := storagePoolsDir.Readdirnames(-1)
if err != nil {
_ = storagePoolsDir.Close()
return err
}
_ = storagePoolsDir.Close()
// Check whether the instance exists on any of the storage pools as either a container or a VM.
instanceMountPoints := []string{}
instancePoolName := ""
instanceType := instancetype.Container
instanceVolType := storageDrivers.VolumeTypeContainer
instanceDBVolType := db.StoragePoolVolumeTypeContainer
for _, volType := range []storageDrivers.VolumeType{storageDrivers.VolumeTypeVM, storageDrivers.VolumeTypeContainer} {
for _, poolName := range storagePoolNames {
volStorageName := project.Instance(projectName, instName)
instanceMntPoint := storageDrivers.GetVolumeMountPath(poolName, volType, volStorageName)
if util.PathExists(instanceMntPoint) {
instanceMountPoints = append(instanceMountPoints, instanceMntPoint)
instancePoolName = poolName
instanceVolType = volType
if volType == storageDrivers.VolumeTypeVM {
instanceType = instancetype.VM
instanceDBVolType = db.StoragePoolVolumeTypeVM
} else {
instanceType = instancetype.Container
instanceDBVolType = db.StoragePoolVolumeTypeContainer
}
}
}
}
// Quick checks.
if len(instanceMountPoints) > 1 {
return fmt.Errorf(`The instance %q seems to exist on multiple storage pools`, instName)
} else if len(instanceMountPoints) != 1 {
return fmt.Errorf(`The instance %q does not seem to exist on any storage pool`, instName)
}
// User needs to make sure that we can access the directory where backup.yaml lives.
instanceMountPoint := instanceMountPoints[0]
isEmpty, err := internalUtil.PathIsEmpty(instanceMountPoint)
if err != nil {
return err
}
if isEmpty {
return fmt.Errorf(`The instance's directory %q appears to be empty. Please ensure that the instance's storage volume is mounted`, instanceMountPoint)
}
// Read in the backup.yaml file.
backupYamlPath := filepath.Join(instanceMountPoint, "backup.yaml")
backupConf, err := backup.ParseConfigYamlFile(backupYamlPath)
if err != nil {
return err
}
if allowNameOverride {
backupConf.Container.Name = instName
}
if instName != backupConf.Container.Name {
return fmt.Errorf("Instance name requested %q doesn't match instance name in backup config %q", instName, backupConf.Container.Name)
}
if backupConf.Pool == nil {
// We don't know what kind of storage type the pool is.
return errors.New("No storage pool struct in the backup file found. The storage pool needs to be recovered manually")
}
// Try to retrieve the storage pool the instance supposedly lives on.
pool, err := storagePools.LoadByName(s, instancePoolName)
if response.IsNotFoundError(err) {
// Create the storage pool db entry if it doesn't exist.
_, err = storagePoolDBCreate(ctx, s, instancePoolName, "", backupConf.Pool.Driver, backupConf.Pool.Config)
if err != nil {
return fmt.Errorf("Create storage pool database entry: %w", err)
}
pool, err = storagePools.LoadByName(s, instancePoolName)
if err != nil {
return fmt.Errorf("Load storage pool database entry: %w", err)
}
} else if err != nil {
return fmt.Errorf("Find storage pool database entry: %w", err)
}
if backupConf.Pool.Name != instancePoolName {
return fmt.Errorf(`The storage pool %q the instance was detected on does not match the storage pool %q specified in the backup file`, instancePoolName, backupConf.Pool.Name)
}
if backupConf.Pool.Driver != pool.Driver().Info().Name {
return fmt.Errorf(`The storage pool's %q driver %q conflicts with the driver %q recorded in the instance's backup file`, instancePoolName, pool.Driver().Info().Name, backupConf.Pool.Driver)
}
// Check snapshots are consistent.
existingSnapshots, err := pool.CheckInstanceBackupFileSnapshots(backupConf, projectName, false, nil)
if err != nil {
return fmt.Errorf("Failed checking snapshots: %w", err)
}
// Check if a storage volume entry for the instance already exists.
var dbVolume *db.StorageVolume
err = s.DB.Cluster.Transaction(ctx, func(ctx context.Context, tx *db.ClusterTx) error {
dbVolume, err = tx.GetStoragePoolVolume(ctx, pool.ID(), projectName, instanceDBVolType, backupConf.Container.Name, true)
if err != nil && !response.IsNotFoundError(err) {
return err
}
return nil
})
if err != nil {
return err
}
if dbVolume != nil {
return fmt.Errorf(`Storage volume for instance %q already exists in the database`, backupConf.Container.Name)
}
err = s.DB.Cluster.Transaction(ctx, func(ctx context.Context, tx *db.ClusterTx) error {
// Check if an entry for the instance already exists in the db.
_, err := tx.GetInstanceID(ctx, projectName, backupConf.Container.Name)
return err
})
if err != nil && !response.IsNotFoundError(err) {
return err
}
if err == nil {
return fmt.Errorf(`Entry for instance %q already exists in the database`, backupConf.Container.Name)
}
if backupConf.Volume == nil {
return errors.New(`No storage volume struct in the backup file found. The storage volume needs to be recovered manually`)
}
var profiles []api.Profile
err = s.DB.Cluster.Transaction(ctx, func(ctx context.Context, tx *db.ClusterTx) error {
profiles, err = tx.GetProfiles(ctx, projectName, backupConf.Container.Profiles)
return err
})
if err != nil {
return fmt.Errorf("Failed loading profiles (%v) for instance: %w", strings.Join(backupConf.Container.Profiles, ", "), err)
}
// Add root device if needed.
if backupConf.Container.Devices == nil {
backupConf.Container.Devices = make(map[string]map[string]string)
}
if backupConf.Container.ExpandedDevices == nil {
backupConf.Container.ExpandedDevices = make(map[string]map[string]string)
}
internalImportRootDevicePopulate(instancePoolName, backupConf.Container.Devices, backupConf.Container.ExpandedDevices, profiles)
reverter := revert.New()
defer reverter.Fail()
if backupConf.Container == nil {
return errors.New("No instance config in backup config")
}
instDBArgs, err := backup.ConfigToInstanceDBArgs(s, backupConf, projectName, true)
if err != nil {
return err
}
_, instOp, cleanup, err := instance.CreateInternal(s, *instDBArgs, nil, true, true)
if err != nil {
return fmt.Errorf("Failed creating instance record: %w", err)
}
reverter.Add(cleanup)
defer instOp.Done(err)
instancePath := storagePools.InstancePath(instanceType, projectName, backupConf.Container.Name, false)
isPrivileged := false
if backupConf.Container.Config["security.privileged"] == "" {
isPrivileged = true
}
err = storagePools.CreateContainerMountpoint(instanceMountPoint, instancePath, isPrivileged)
if err != nil {
return err
}
for _, snap := range existingSnapshots {
snapInstName := fmt.Sprintf("%s%s%s", backupConf.Container.Name, internalInstance.SnapshotDelimiter, snap.Name)
snapErr := s.DB.Cluster.Transaction(ctx, func(ctx context.Context, tx *db.ClusterTx) error {
// Check if an entry for the snapshot already exists in the db.
_, err := tx.GetInstanceSnapshotID(ctx, projectName, backupConf.Container.Name, snap.Name)
return err
})
if snapErr != nil && !response.IsNotFoundError(snapErr) {
return snapErr
}
if snapErr == nil {
return fmt.Errorf(`Entry for snapshot %q already exists in the database`, snapInstName)
}
// Check if a storage volume entry for the snapshot already exists.
var dbVolume *db.StorageVolume
err = s.DB.Cluster.Transaction(ctx, func(ctx context.Context, tx *db.ClusterTx) error {
dbVolume, err = tx.GetStoragePoolVolume(ctx, pool.ID(), projectName, instanceDBVolType, snapInstName, true)
if err != nil && !response.IsNotFoundError(err) {
return err
}
return nil
})
if err != nil {
return err
}
// If the storage volume entry does already exist we error here
if dbVolume != nil {
return fmt.Errorf(`Storage volume for snapshot %q already exists in the database`, snapInstName)
}
baseImage := snap.Config["volatile.base_image"]
arch, err := osarch.ArchitectureID(snap.Architecture)
if err != nil {
return err
}
err = s.DB.Cluster.Transaction(ctx, func(ctx context.Context, tx *db.ClusterTx) error {
profiles, err = tx.GetProfiles(ctx, projectName, snap.Profiles)
return err
})
if err != nil {
return fmt.Errorf("Failed loading profiles (%v) for instance snapshot %q: %w", strings.Join(snap.Profiles, ", "), snapInstName, err)
}
// Add root device if needed.
if snap.Devices == nil {
snap.Devices = make(map[string]map[string]string)
}
if snap.ExpandedDevices == nil {
snap.ExpandedDevices = make(map[string]map[string]string)
}
internalImportRootDevicePopulate(instancePoolName, snap.Devices, snap.ExpandedDevices, profiles)
_, snapInstOp, cleanup, err := instance.CreateInternal(s, db.InstanceArgs{
Project: projectName,
Architecture: arch,
BaseImage: baseImage,
Config: snap.Config,
CreationDate: snap.CreatedAt,
Type: instanceType,
Snapshot: true,
Devices: deviceConfig.NewDevices(snap.Devices),
Ephemeral: snap.Ephemeral,
LastUsedDate: snap.LastUsedAt,
Name: snapInstName,
Profiles: profiles,
Stateful: snap.Stateful,
}, nil, true, true)
if err != nil {
return fmt.Errorf("Failed creating instance snapshot record %q: %w", snap.Name, err)
}
reverter.Add(cleanup)
defer snapInstOp.Done(err)
// Recreate missing mountpoints and symlinks.
volStorageName := project.Instance(projectName, snapInstName)
snapshotMountPoint := storageDrivers.GetVolumeMountPath(instancePoolName, instanceVolType, volStorageName)
snapshotPath := storagePools.InstancePath(instanceType, projectName, backupConf.Container.Name, true)
snapshotTargetPath := storageDrivers.GetVolumeSnapshotDir(instancePoolName, instanceVolType, volStorageName)
err = storagePools.CreateSnapshotMountpoint(snapshotMountPoint, snapshotTargetPath, snapshotPath)
if err != nil {
return err
}
}
reverter.Success()
return nil
}
// internalImportRootDevicePopulate considers the local and expanded devices from backup.yaml as well as the
// expanded devices in the current profiles and if needed will populate localDevices with a new root disk config
// to attempt to maintain the same effective config as specified in backup.yaml. Where possible no new root disk
// device will be added, if the root disk config in the current profiles matches the effective backup.yaml config.
func internalImportRootDevicePopulate(instancePoolName string, localDevices map[string]map[string]string, expandedDevices map[string]map[string]string, profiles []api.Profile) {
// First, check if localDevices from backup.yaml has a root disk.
rootName, _, _ := internalInstance.GetRootDiskDevice(localDevices)
if rootName != "" {
localDevices[rootName]["pool"] = instancePoolName
return // Local root disk device has been set to target pool.
}
// Next check if expandedDevices from backup.yaml has a root disk.
expandedRootName, expandedRootConfig, _ := internalInstance.GetRootDiskDevice(expandedDevices)
// Extract root disk from expanded profile devices.
profileExpandedDevices := db.ExpandInstanceDevices(deviceConfig.NewDevices(localDevices), profiles)
profileExpandedRootName, profileExpandedRootConfig, _ := internalInstance.GetRootDiskDevice(profileExpandedDevices.CloneNative())
// Record whether we need to add a new local disk device.
addLocalDisk := false
// We need to add a local root disk if the profiles don't have a root disk.
if profileExpandedRootName == "" {
addLocalDisk = true
} else {
// Check profile expanded root disk is in the correct pool
if profileExpandedRootConfig["pool"] != instancePoolName {
addLocalDisk = true
} else {
// Check profile expanded root disk config matches the old expanded disk in backup.yaml.
// Excluding the "pool" property, which we ignore, as we have already checked the new
// profile root disk matches the target pool name.
if expandedRootName != "" {
for k := range expandedRootConfig {
if k == "pool" {
continue // Ignore old pool name.
}
if expandedRootConfig[k] != profileExpandedRootConfig[k] {
addLocalDisk = true
break
}
}
for k := range profileExpandedRootConfig {
if k == "pool" {
continue // Ignore old pool name.
}
if expandedRootConfig[k] != profileExpandedRootConfig[k] {
addLocalDisk = true
break
}
}
}
}
}
// Add local root disk entry if needed.
if addLocalDisk {
rootDev := map[string]string{
"type": "disk",
"path": "/",
"pool": instancePoolName,
}
// Inherit any extra root disk config from the expanded root disk from backup.yaml.
if expandedRootName != "" {
for k, v := range expandedRootConfig {
_, found := rootDev[k]
if !found {
rootDev[k] = v
}
}
}
// If there is already a device called "root" in the instance's config, but it does not qualify as
// a root disk, then try to find a free name for the new root disk device.
rootDevName := "root"
for i := range 100 {
if localDevices[rootDevName] == nil {
break
}
rootDevName = fmt.Sprintf("root%d", i)
continue
}
localDevices[rootDevName] = rootDev
}
}
func internalGC(_ *Daemon, _ *http.Request) response.Response {
logger.Infof("Started forced garbage collection run")
runtime.GC()
runtimeDebug.FreeOSMemory()
var m runtime.MemStats
runtime.ReadMemStats(&m)
logger.Infof("Heap allocated: %s", units.GetByteSizeStringIEC(int64(m.Alloc), 2))
logger.Infof("Stack in use: %s", units.GetByteSizeStringIEC(int64(m.StackInuse), 2))
logger.Infof("Requested from system: %s", units.GetByteSizeStringIEC(int64(m.Sys), 2))
logger.Infof("Releasable to OS: %s", units.GetByteSizeStringIEC(int64(m.HeapIdle-m.HeapReleased), 2))
logger.Infof("Completed forced garbage collection run")
return response.EmptySyncResponse
}
func internalRAFTSnapshot(_ *Daemon, _ *http.Request) response.Response {
logger.Warn("Forced RAFT snapshot not supported")
return response.InternalError(errors.New("Not supported"))
}
func internalBGPState(d *Daemon, _ *http.Request) response.Response {
s := d.State()
return response.SyncResponse(true, s.BGP.Debug())
}
func internalRebalanceLoad(d *Daemon, _ *http.Request) response.Response {
err := autoRebalanceCluster(context.TODO(), d)
if err != nil {
return response.SmartError(err)
}
return response.EmptySyncResponse
}
|