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
|
package shared
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strconv"
incus "github.com/lxc/incus/v6/shared/util"
"golang.org/x/sys/unix"
)
// ChrootMount defines mount args.
type ChrootMount struct {
Source string
Target string
FSType string
Flags uintptr
Data string
IsDir bool
}
// ActiveChroots is a map of all active chroots and their exit functions.
var ActiveChroots = make(map[string]func() error)
func setupMounts(rootfs string, mounts []ChrootMount) error {
// Create a temporary mount path
err := os.MkdirAll(filepath.Join(rootfs, ".distrobuilder"), 0o700)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", filepath.Join(rootfs, ".distrobuilder"), err)
}
for i, mount := range mounts {
// Target path
tmpTarget := filepath.Join(rootfs, ".distrobuilder", fmt.Sprintf("%d", i))
// Create the target mountpoint
if mount.IsDir {
err := os.MkdirAll(tmpTarget, 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", tmpTarget, err)
}
} else {
f, err := os.Create(tmpTarget)
if err != nil {
return fmt.Errorf("Failed to create file %q: %w", tmpTarget, err)
}
f.Close()
}
// Mount to the temporary path
err := unix.Mount(mount.Source, tmpTarget, mount.FSType, mount.Flags, mount.Data)
if err != nil {
return fmt.Errorf("Failed to mount '%s': %w", mount.Source, err)
}
}
return nil
}
func moveMounts(mounts []ChrootMount) error {
for i, mount := range mounts {
// Source path
tmpSource := filepath.Join("/", ".distrobuilder", fmt.Sprintf("%d", i))
// Resolve symlinks
target := mount.Target
for {
// Get information on current target
fi, err := os.Lstat(target)
if err != nil {
break
}
// If not a symlink, we're done
if fi.Mode()&os.ModeSymlink == 0 {
break
}
// If a symlink, resolve it
newTarget, err := os.Readlink(target)
if err != nil {
break
}
target = newTarget
}
// If the target's parent directory is a symlink, we need to resolve that as well.
targetDir := filepath.Dir(target)
if incus.PathExists(targetDir) {
// Get information on current target
fi, err := os.Lstat(targetDir)
if err != nil {
return fmt.Errorf("Failed to stat directory %q: %w", targetDir, err)
}
// If a symlink, resolve it
if fi.Mode()&os.ModeSymlink != 0 {
newTarget, err := os.Readlink(targetDir)
if err != nil {
return fmt.Errorf("Failed to get destination of %q: %w", targetDir, err)
}
targetDir = newTarget
}
}
// Create parent paths if missing
err := os.MkdirAll(targetDir, 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", targetDir, err)
}
// Create target path
if mount.IsDir {
err = os.MkdirAll(target, 0o755)
if err != nil {
return fmt.Errorf("Failed to create directory %q: %w", target, err)
}
} else {
err := os.WriteFile(target, nil, 0o644)
if err != nil {
return fmt.Errorf("Failed to create file %q: %w", target, err)
}
}
// Move the mount to its destination
err = unix.Mount(tmpSource, target, "", unix.MS_MOVE, "")
if err != nil {
return fmt.Errorf("Failed to mount '%s': %w", mount.Source, err)
}
}
// Cleanup our temporary path
err := os.RemoveAll(filepath.Join("/", ".distrobuilder"))
if err != nil {
return fmt.Errorf("Failed to remove directory %q: %w", filepath.Join("/", ".distrobuilder"), err)
}
return nil
}
func killChrootProcesses(rootfs string) error {
// List all files under /proc
proc, err := os.Open(filepath.Join(rootfs, "proc"))
if err != nil {
return fmt.Errorf("Failed to open file %q: %w", filepath.Join(rootfs, "proc"), err)
}
dirs, err := proc.Readdirnames(0)
if err != nil {
return fmt.Errorf("Failed to read directory content of %q: %w", filepath.Join(rootfs, "proc"), err)
}
// Get all processes and kill them
re := regexp.MustCompile(`\d+`)
for _, dir := range dirs {
if re.MatchString(dir) {
link, _ := os.Readlink(filepath.Join(rootfs, "proc", dir, "root"))
if link == rootfs {
pid, _ := strconv.Atoi(dir)
err = unix.Kill(pid, unix.SIGKILL)
if err != nil {
return fmt.Errorf("Failed killing process: %w", err)
}
}
}
}
return nil
}
// SetupChroot sets up mount and files, a reverter and then chroots for you.
func SetupChroot(rootfs string, definition Definition, m []ChrootMount) (func() error, error) {
// Mount the rootfs
err := unix.Mount(rootfs, rootfs, "", unix.MS_BIND, "")
if err != nil {
return nil, fmt.Errorf("Failed to mount '%s': %w", rootfs, err)
}
// Setup all other needed mounts
mounts := []ChrootMount{
{"none", "/proc", "proc", 0, "", true},
{"none", "/sys", "sysfs", 0, "", true},
{"none", "/run", "tmpfs", 0, "", true},
{"none", "/tmp", "tmpfs", 0, "", true},
{"none", "/dev", "tmpfs", 0, "", true},
{"none", "/dev/shm", "tmpfs", 0, "", true},
{"/etc/resolv.conf", "/etc/resolv.conf", "", unix.MS_BIND, "", false},
}
// Keep a reference to the host rootfs and cwd
root, err := os.Open("/")
if err != nil {
return nil, err
}
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
// Setup all needed mounts in a temporary location
if len(m) > 0 {
err = setupMounts(rootfs, append(mounts, m...))
} else {
err = setupMounts(rootfs, mounts)
}
if err != nil {
return nil, fmt.Errorf("Failed to mount filesystems: %w", err)
}
// Chroot into the container's rootfs
err = unix.Chroot(rootfs)
if err != nil {
root.Close()
return nil, err
}
err = unix.Chdir("/")
if err != nil {
return nil, err
}
// Move all the mounts into place
err = moveMounts(append(mounts, m...))
if err != nil {
return nil, err
}
// Populate /dev directory instead of bind mounting it from the host
err = populateDev()
if err != nil {
return nil, fmt.Errorf("Failed to populate /dev: %w", err)
}
// Change permission for /dev/shm
err = unix.Chmod("/dev/shm", 0o1777)
if err != nil {
return nil, fmt.Errorf("Failed to chmod /dev/shm: %w", err)
}
var env Environment
envs := definition.Environment
if envs.ClearDefaults {
env = Environment{}
} else {
env = Environment{
"PATH": EnvVariable{
Value: "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin",
Set: true,
},
"SHELL": EnvVariable{
Value: "/bin/sh",
Set: true,
},
"TERM": EnvVariable{
Value: "xterm",
Set: true,
},
"DEBIAN_FRONTEND": EnvVariable{
Value: "noninteractive",
Set: true,
},
}
}
if len(envs.EnvVariables) > 0 {
imageTargets := ImageTargetUndefined | ImageTargetAll
switch definition.Targets.Type {
case DefinitionFilterTypeContainer:
imageTargets |= ImageTargetContainer
case DefinitionFilterTypeVM:
imageTargets |= ImageTargetVM
}
for _, e := range envs.EnvVariables {
if !ApplyFilter(&e, definition.Image.Release, definition.Image.ArchitectureMapped, definition.Image.Variant, definition.Targets.Type, imageTargets) {
continue
}
entry, ok := env[e.Key]
if ok {
entry.Value = e.Value
entry.Set = true
} else {
env[e.Key] = EnvVariable{
Value: e.Value,
Set: true,
}
}
}
}
// Set environment variables
oldEnv := SetEnvVariables(env)
// Setup policy-rc.d override
policyCleanup := false
if incus.PathExists("/usr/sbin/") && !incus.PathExists("/usr/sbin/policy-rc.d") {
err = os.WriteFile("/usr/sbin/policy-rc.d", []byte(`#!/bin/sh
exit 101
`), 0o755)
if err != nil {
return nil, err
}
policyCleanup = true
}
exitFunc := func() error {
defer root.Close()
// Cleanup policy-rc.d
if policyCleanup {
err = os.Remove("/usr/sbin/policy-rc.d")
if err != nil {
return fmt.Errorf("Failed to remove %q: %w", "/usr/sbin/policy-rc.d", err)
}
}
// Reset old environment variables
SetEnvVariables(oldEnv)
// Switch back to the host rootfs
err = root.Chdir()
if err != nil {
return fmt.Errorf("Failed to chdir: %w", err)
}
err = unix.Chroot(".")
if err != nil {
return fmt.Errorf("Failed to chroot: %w", err)
}
err = unix.Chdir(cwd)
if err != nil {
return fmt.Errorf("Failed to chdir: %w", err)
}
// This will kill all processes in the chroot and allow to cleanly
// unmount everything.
err = killChrootProcesses(rootfs)
if err != nil {
return fmt.Errorf("Failed killing chroot processes: %w", err)
}
// And now unmount the entire tree
err = unix.Unmount(rootfs, unix.MNT_DETACH)
if err != nil {
return fmt.Errorf("Failed unmounting rootfs: %w", err)
}
devPath := filepath.Join(rootfs, "dev")
// Wipe $rootfs/dev
err := os.RemoveAll(devPath)
if err != nil {
return fmt.Errorf("Failed to remove directory %q: %w", devPath, err)
}
ActiveChroots[rootfs] = nil
return os.MkdirAll(devPath, 0o755)
}
ActiveChroots[rootfs] = exitFunc
return exitFunc, nil
}
func populateDev() error {
devs := []struct {
Path string
Major uint32
Minor uint32
Mode uint32
}{
{"/dev/console", 5, 1, unix.S_IFCHR | 0o640},
{"/dev/full", 1, 7, unix.S_IFCHR | 0o666},
{"/dev/null", 1, 3, unix.S_IFCHR | 0o666},
{"/dev/random", 1, 8, unix.S_IFCHR | 0o666},
{"/dev/tty", 5, 0, unix.S_IFCHR | 0o666},
{"/dev/urandom", 1, 9, unix.S_IFCHR | 0o666},
{"/dev/zero", 1, 5, unix.S_IFCHR | 0o666},
}
for _, d := range devs {
if incus.PathExists(d.Path) {
continue
}
dev := unix.Mkdev(d.Major, d.Minor)
err := unix.Mknod(d.Path, d.Mode, int(dev))
if err != nil {
return fmt.Errorf("Failed to create %q: %w", d.Path, err)
}
// For some odd reason, unix.Mknod will not set the mode correctly.
// This fixes that.
err = unix.Chmod(d.Path, d.Mode)
if err != nil {
return fmt.Errorf("Failed to chmod %q: %w", d.Path, err)
}
}
symlinks := []struct {
Symlink string
Target string
}{
{"/dev/fd", "/proc/self/fd"},
{"/dev/stdin", "/proc/self/fd/0"},
{"/dev/stdout", "/proc/self/fd/1"},
{"/dev/stderr", "/proc/self/fd/2"},
}
for _, l := range symlinks {
err := os.Symlink(l.Target, l.Symlink)
if err != nil {
return fmt.Errorf("Failed to create link %q -> %q: %w", l.Symlink, l.Target, err)
}
}
return nil
}
|