{"record":{"id":"90889e2e5edfccb3","repo":"containerd/containerd","slug":"failed-to-truncate-file-q-w","errorCode":null,"errorMessage":"failed to truncate file %q: %w","messagePattern":"failed to truncate file %q: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/mount/manager/mkfs.go","lineNumber":129,"sourceCode":"\t\t\tbinary = \"mkfs.xfs\"\n\t\t\tif id != \"\" {\n\t\t\t\tcreateArgs = append(createArgs, []string{\"-m\", fmt.Sprintf(\"uuid=%s\", id)}...)\n\t\t\t}\n\t\tdefault:\n\t\t\treturn mount.Mount{}, fmt.Errorf(\"unsupported filesystem %q: %w\", fs, errdefs.ErrInvalidArgument)\n\t\t}\n\n\t\tf, err := r.OpenFile(subpath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0640)\n\t\tif err != nil {\n\t\t\treturn mount.Mount{}, fmt.Errorf(\"failed to create file %q: %w\", m.Source, err)\n\t\t}\n\n\t\tcreateArgs = append(createArgs, f.Name())\n\n\t\terr = f.Truncate(size)\n\t\tf.Close()\n\t\tif err != nil {\n\t\t\treturn mount.Mount{}, fmt.Errorf(\"failed to truncate file %q: %w\", m.Source, err)\n\t\t}\n\n\t\tif err := createWritableImage(ctx, binary, createArgs...); err != nil {\n\t\t\treturn mount.Mount{}, fmt.Errorf(\"failed format %q: %w\", m.Source, err)\n\t\t}\n\t} else {\n\t\treturn mount.Mount{}, fmt.Errorf(\"failed to stat %q: %w\", m.Source, err)\n\t}\n\n\treturn m, nil\n}\n\nfunc createWritableImage(ctx context.Context, binary string, args ...string) error {\n\tcmd := exec.CommandContext(ctx, binary, args...)\n\tout, err := cmd.CombinedOutput()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"%s failed: %s: %w\", filepath.Base(binary), out, err)\n\t}","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/containerd/containerd/blob/4246446a2bf7d03837b0244118d858799393bd80/core/mount/manager/mkfs.go#L111-L147","documentation":"Thrown by mount manager's mkfs-based Transform when os.File.Truncate fails while sizing a new sparse/backing file for a writable block image (e.g. ext4 formatted loopback device). The wrapped error is the underlying ftruncate(2)/syscall failure; the mount is not created. It indicates the backing file could not be resized to the requested size before formatting.","triggerScenarios":"Calling the manager Transform (New/Create mount flow) for a mount whose Source is a file that must be created and truncated to `size`; f.Truncate(size) returns a non-nil error (permission denied, ENOSPC, size negative or exceeding filesystem limits, file on read-only fs).","commonSituations":"Creating a loopback-backed writable snapshot on a full disk (ENOSPC); target directory not writable by the daemon user; requesting a size larger than the backing filesystem allows; Source path pointing at a read-only bind location.","solutions":["Check free space on the filesystem holding the Source file (df -h) and free space or reduce the requested size.","Verify the process user has write permission on the Source path's directory.","Ensure the requested size is a valid positive value within fs limits (check the size field passed to Transform).","If on a read-only rootfs, move the image path to a writable volume."],"exampleFix":"// before\ncreateArgs = append(createArgs, f.Name())\nerr = f.Truncate(size) // fails with ENOSPC\n// after\nif err := checkFreeSpace(filepath.Dir(m.Source), size); err != nil {\n    return mount.Mount{}, fmt.Errorf(\"insufficient space for %q: %w\", m.Source, err)\n}\nerr = f.Truncate(size)","handlingStrategy":"validation","validationCode":"info, err := os.Stat(filepath.Dir(m.Source))\nif err != nil { return fmt.Errorf(\"image dir unavailable: %w\", err) }\nif size <= 0 { return errors.New(\"image size must be positive\") }\nif st, err := statfs(filepath.Dir(m.Source)); err == nil && uint64(size) > st.Avail { return errors.New(\"insufficient disk space\") }","typeGuard":"func validImageTarget(path string, size int64) bool {\n    st, err := os.Stat(filepath.Dir(path))\n    return err == nil && st.IsDir() && size > 0\n}","tryCatchPattern":"var merr *mountError\nif err := mgr.Transform(ctx, mnt); err != nil {\n    if strings.Contains(err.Error(), \"failed to truncate\") {\n        // inspect wrapped syscall error: ENOSPC -> free space; EACCES -> permissions\n        var pe *os.PathError\n        if errors.As(err, &pe) && errors.Is(pe.Err, syscall.ENOSPC) { freeSpaceAndRetry() }\n    }\n}","preventionTips":["Monitor free space on the volume holding snapshot/image files.","Run the daemon as a user with write access to the image directory.","Clamp requested image sizes to available capacity before creating mounts."],"tags":["filesystem","truncate","mount","storage"],"backgroundTag":"file-truncate-failed","analyzedSha":"4246446a2bf7d03837b0244118d858799393bd80","analyzedAt":"2026-09-02T00:14:43.053Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}