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
|
package main
import (
"encoding/base64"
"fmt"
"net/http"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
incus "github.com/lxc/incus/v6/client"
"github.com/lxc/incus/v6/internal/linux"
internalUtil "github.com/lxc/incus/v6/internal/util"
"github.com/lxc/incus/v6/shared/api"
"github.com/lxc/incus/v6/shared/idmap"
"github.com/lxc/incus/v6/shared/revert"
"github.com/lxc/incus/v6/shared/subprocess"
localtls "github.com/lxc/incus/v6/shared/tls"
"github.com/lxc/incus/v6/shared/util"
)
func serverIsConfigured(client incus.InstanceServer) (bool, error) {
// Look for networks.
networks, err := client.GetNetworkNames()
if err != nil {
return false, fmt.Errorf("Failed to list networks: %w", err)
}
if !slices.Contains(networks, "incusbr0") {
// Couldn't find incusbr0.
return false, nil
}
// Look for storage pools.
pools, err := client.GetStoragePoolNames()
if err != nil {
return false, fmt.Errorf("Failed to list storage pools: %w", err)
}
if !slices.Contains(pools, "default") {
// No storage pool found.
return false, nil
}
return true, nil
}
func serverInitialConfiguration(client incus.InstanceServer) error {
// Load current server config.
info, _, err := client.GetServer()
if err != nil {
return fmt.Errorf("Failed to get server info: %w", err)
}
availableBackends := linux.AvailableStorageDrivers(internalUtil.VarPath(), info.Environment.StorageSupportedDrivers, internalUtil.PoolTypeLocal)
// Load the default profile.
var profileNeedsUpdate bool
profile, profileEtag, err := client.GetProfile("default")
if err != nil {
return fmt.Errorf("Failed to load default profile: %w", err)
}
// Look for storage pools.
pools, err := client.GetStoragePools()
if err != nil {
return fmt.Errorf("Failed to list storage pools: %w", err)
}
if len(pools) == 0 {
pool := api.StoragePoolsPost{}
pool.Config = map[string]string{}
pool.Name = "default"
// Check if ZFS supported.
if slices.Contains(availableBackends, "zfs") {
pool.Driver = "zfs"
// Check if zsys.
poolName, _ := subprocess.RunCommand("zpool", "get", "-H", "-o", "value", "name", "rpool")
if strings.TrimSpace(poolName) == "rpool" {
pool.Config["source"] = "rpool/incus"
}
} else {
// Fallback to dir backend.
pool.Driver = "dir"
}
// Create the storage pool.
err := client.CreateStoragePool(pool)
if err != nil {
return fmt.Errorf("Failed to create storage pool: %w", err)
}
// Add to default profile in default project.
profile.Devices["root"] = map[string]string{
"type": "disk",
"pool": "default",
"path": "/",
}
profileNeedsUpdate = true
}
// Look for networks.
networks, err := client.GetNetworks()
if err != nil {
return fmt.Errorf("Failed to list networks: %w", err)
}
found := false
for _, network := range networks {
if network.Managed {
found = true
break
}
}
if !found {
// Create incusbr0.
network := api.NetworksPost{}
network.Config = map[string]string{}
network.Type = "bridge"
network.Name = "incusbr0"
err := client.CreateNetwork(network)
if err != nil {
return fmt.Errorf("Failed to create network: %w", err)
}
// Add to default profile in default project.
profile.Devices["eth0"] = map[string]string{
"type": "nic",
"network": "incusbr0",
"name": "eth0",
}
profileNeedsUpdate = true
}
// Update the default profile.
if profileNeedsUpdate {
err = client.UpdateProfile("default", profile.Writable(), profileEtag)
if err != nil {
return fmt.Errorf("Failed to update default profile: %w", err)
}
}
return nil
}
func serverSetupUser(uid uint32) error {
projectName := fmt.Sprintf("user-%d", uid)
networkName := fmt.Sprintf("incusbr-%d", uid)
if len(networkName) > 15 {
// For long UIDs, use a shorter slightly less descriptive interface name.
networkName = fmt.Sprintf("user-%d", uid)
}
userPath := internalUtil.VarPath("users", fmt.Sprintf("%d", uid))
// User account.
out, err := subprocess.RunCommand("getent", "passwd", fmt.Sprintf("%d", uid))
if err != nil {
return fmt.Errorf("Failed to retrieve user information: %w", err)
}
pw := strings.Split(out, ":")
if len(pw) != 7 {
return fmt.Errorf("Invalid user entry")
}
// Setup reverter.
revert := revert.New()
defer revert.Fail()
// Create certificate directory.
err = os.MkdirAll(userPath, 0o700)
if err != nil {
return fmt.Errorf("Failed to create user directory: %w", err)
}
revert.Add(func() { _ = os.RemoveAll(userPath) })
// Generate certificate.
if !util.PathExists(filepath.Join(userPath, "client.crt")) || !util.PathExists(filepath.Join(userPath, "client.key")) {
err = localtls.FindOrGenCert(filepath.Join(userPath, "client.crt"), filepath.Join(userPath, "client.key"), true, false)
if err != nil {
return fmt.Errorf("Failed to generate user certificate: %w", err)
}
}
// Connect to the daemon.
client, err := incus.ConnectIncusUnix("", nil)
if err != nil {
return fmt.Errorf("Unable to connect to the daemon: %w", err)
}
_, _, _ = client.GetServer()
if !slices.Contains(projectNames, projectName) {
// Create the project.
err := client.CreateProject(api.ProjectsPost{
Name: projectName,
ProjectPut: api.ProjectPut{
Description: fmt.Sprintf("User restricted project for %q (%s)", pw[0], pw[2]),
Config: map[string]string{
"features.images": "true",
"features.networks": "false",
"features.networks.zones": "true",
"features.profiles": "true",
"features.storage.volumes": "true",
"features.storage.buckets": "true",
"restricted": "true",
"restricted.containers.nesting": "allow",
"restricted.devices.disk": "allow",
"restricted.devices.disk.paths": pw[5],
"restricted.devices.gpu": "allow",
"restricted.idmap.uid": pw[2],
"restricted.idmap.gid": pw[3],
"restricted.networks.access": networkName,
},
},
})
if err != nil {
return fmt.Errorf("Unable to create project: %w", err)
}
revert.Add(func() { _ = client.DeleteProject(projectName) })
// Create user-specific bridge.
network := api.NetworksPost{}
network.Config = map[string]string{}
network.Type = "bridge"
network.Name = networkName
network.Description = fmt.Sprintf("Network for user restricted project %s", projectName)
err = client.CreateNetwork(network)
if err != nil && !api.StatusErrorCheck(err, http.StatusConflict) {
return fmt.Errorf("Failed to create network: %w", err)
}
// Setup default profile.
req := api.ProfilePut{
Description: "Default Incus profile",
Devices: map[string]map[string]string{
"root": {
"type": "disk",
"path": "/",
"pool": "default",
},
"eth0": {
"type": "nic",
"name": "eth0",
"network": networkName,
},
},
}
// Add uid/gid map if possible.
pwUID, err := strconv.ParseInt(pw[2], 10, 64)
if err != nil {
return err
}
pwGID, err := strconv.ParseInt(pw[3], 10, 64)
if err != nil {
return err
}
idmapset, err := idmap.NewSetFromSystem("", "root")
if err != nil && err != idmap.ErrSubidUnsupported {
return fmt.Errorf("Failed to load system idmap: %w", err)
}
idmapAllowed := true
if idmapset != nil {
entries := []idmap.Entry{
{IsUID: true, HostID: pwUID, MapRange: 1},
{IsGID: true, HostID: pwGID, MapRange: 1},
}
if !idmapset.Includes(&idmap.Set{Entries: entries}) {
idmapAllowed = false
}
}
if idmapAllowed {
req.Config = map[string]string{
"raw.idmap": fmt.Sprintf("uid %d %d\ngid %d %d", pwUID, pwUID, pwGID, pwGID),
}
}
err = client.UseProject(projectName).UpdateProfile("default", req, "")
if err != nil {
return fmt.Errorf("Unable to update the default profile: %w", err)
}
}
// Parse the certificate.
x509Cert, err := localtls.ReadCert(filepath.Join(userPath, "client.crt"))
if err != nil {
return fmt.Errorf("Unable to read user certificate: %w", err)
}
// Delete the certificate from the trust store if it already exists.
fingerprint := localtls.CertFingerprint(x509Cert)
_ = client.DeleteCertificate(fingerprint)
// Add the certificate to the trust store.
err = client.CreateCertificate(api.CertificatesPost{
CertificatePut: api.CertificatePut{
Name: fmt.Sprintf("incus-user-%d", uid),
Type: "client",
Restricted: true,
Projects: []string{projectName},
Certificate: base64.StdEncoding.EncodeToString(x509Cert.Raw),
},
})
if err != nil {
return fmt.Errorf("Unable to add user certificate: %w", err)
}
revert.Add(func() { _ = client.DeleteCertificate(localtls.CertFingerprint(x509Cert)) })
// Add the new project to our list.
if !slices.Contains(projectNames, projectName) {
projectNames = append(projectNames, projectName)
}
revert.Success()
return nil
}
|