Skip to content

Commit c58910b

Browse files
realrajaryansaehejkang
authored andcommitted
add volume prune command (apple#783)
- Closes apple#508. ## Type of Change - [ ] Bug fix - [x] New feature - [ ] Breaking change - [ ] Documentation update ## Motivation and Context Adds a `container volume prune` command that removes volumes with no container references and reports the amount of disk space reclaimed. This helps users clean up unused volumes and easily reclaim disk space. Also updates the `volume delete` documentation to clarify and highlight how the `--all` flag works. ## Testing - [x] Tested locally - [x] Added/updated tests - [x] Added/updated docs
1 parent 4d19352 commit c58910b

File tree

9 files changed

+332
-4
lines changed

9 files changed

+332
-4
lines changed

Sources/ContainerClient/Core/ClientVolume.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,18 @@ public struct ClientVolume {
8181
return try JSONDecoder().decode(Volume.self, from: responseData)
8282
}
8383

84+
public static func prune() async throws -> ([String], UInt64) {
85+
let client = XPCClient(service: serviceIdentifier)
86+
let message = XPCMessage(route: .volumePrune)
87+
let reply = try await client.send(message)
88+
89+
guard let responseData = reply.dataNoCopy(key: .volumes) else {
90+
return ([], 0)
91+
}
92+
93+
let volumeNames = try JSONDecoder().decode([String].self, from: responseData)
94+
let size = reply.uint64(key: .size)
95+
return (volumeNames, size)
96+
}
97+
8498
}

Sources/ContainerClient/Core/XPC+.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ public enum XPCRoute: String {
147147
case volumeDelete
148148
case volumeList
149149
case volumeInspect
150+
case volumePrune
150151

151152
case ping
152153

Sources/ContainerCommands/Volume/VolumeCommand.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ extension Application {
2626
VolumeDelete.self,
2727
VolumeList.self,
2828
VolumeInspect.self,
29+
VolumePrune.self,
2930
],
3031
aliases: ["v"]
3132
)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//===----------------------------------------------------------------------===//
2+
// Copyright © 2025 Apple Inc. and the container project authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// https://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//===----------------------------------------------------------------------===//
16+
17+
import ArgumentParser
18+
import ContainerClient
19+
import Foundation
20+
21+
extension Application.VolumeCommand {
22+
public struct VolumePrune: AsyncParsableCommand {
23+
public init() {}
24+
public static let configuration = CommandConfiguration(
25+
commandName: "prune",
26+
abstract: "Remove volumes with no container references")
27+
28+
@OptionGroup
29+
var global: Flags.Global
30+
31+
public func run() async throws {
32+
let (volumeNames, size) = try await ClientVolume.prune()
33+
let formatter = ByteCountFormatter()
34+
let freed = formatter.string(fromByteCount: Int64(size))
35+
36+
if volumeNames.isEmpty {
37+
print("No volumes to prune")
38+
} else {
39+
print("Pruned volumes:")
40+
for name in volumeNames {
41+
print(name)
42+
}
43+
print()
44+
}
45+
print("Reclaimed \(freed) in disk space")
46+
}
47+
}
48+
}

Sources/Helpers/APIServer/APIServer+Start.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ extension APIServer {
264264
routes[XPCRoute.volumeDelete] = harness.delete
265265
routes[XPCRoute.volumeList] = harness.list
266266
routes[XPCRoute.volumeInspect] = harness.inspect
267+
routes[XPCRoute.volumePrune] = harness.prune
267268
}
268269
}
269270
}

Sources/Services/ContainerAPIService/Volumes/VolumesHarness.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,15 @@ public struct VolumesHarness: Sendable {
9292
reply.set(key: .volume, value: data)
9393
return reply
9494
}
95+
96+
@Sendable
97+
public func prune(_ message: XPCMessage) async throws -> XPCMessage {
98+
let (volumeNames, size) = try await service.prune()
99+
let data = try JSONEncoder().encode(volumeNames)
100+
101+
let reply = message.reply()
102+
reply.set(key: .volumes, value: data)
103+
reply.set(key: .size, value: size)
104+
return reply
105+
}
95106
}

Sources/Services/ContainerAPIService/Volumes/VolumesService.swift

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,77 @@ public actor VolumesService {
7272
}
7373
}
7474

75+
public func prune() async throws -> ([String], UInt64) {
76+
try await lock.withLock { _ in
77+
let allVolumes = try await self.store.list()
78+
79+
// do entire prune operation atomically with container list
80+
return try await self.containersService.withContainerList { containers in
81+
var inUseSet = Set<String>()
82+
for container in containers {
83+
for mount in container.configuration.mounts {
84+
if mount.isVolume, let volumeName = mount.volumeName {
85+
inUseSet.insert(volumeName)
86+
}
87+
}
88+
}
89+
90+
let volumesToPrune = allVolumes.filter { volume in
91+
!inUseSet.contains(volume.name)
92+
}
93+
94+
var prunedNames = [String]()
95+
var totalSize: UInt64 = 0
96+
97+
for volume in volumesToPrune {
98+
do {
99+
// calculate actual disk usage before deletion
100+
let volumePath = self.volumePath(for: volume.name)
101+
let actualSize = self.calculateDirectorySize(at: volumePath)
102+
103+
try await self.store.delete(volume.name)
104+
try self.removeVolumeDirectory(for: volume.name)
105+
106+
prunedNames.append(volume.name)
107+
totalSize += actualSize
108+
self.log.info("Pruned volume", metadata: ["name": "\(volume.name)", "size": "\(actualSize)"])
109+
} catch {
110+
self.log.error("Failed to prune volume \(volume.name): \(error)")
111+
}
112+
}
113+
114+
return (prunedNames, totalSize)
115+
}
116+
}
117+
}
118+
119+
private nonisolated func calculateDirectorySize(at path: String) -> UInt64 {
120+
let url = URL(fileURLWithPath: path)
121+
let fileManager = FileManager.default
122+
123+
guard
124+
let enumerator = fileManager.enumerator(
125+
at: url,
126+
includingPropertiesForKeys: [.totalFileAllocatedSizeKey],
127+
options: [.skipsHiddenFiles]
128+
)
129+
else {
130+
return 0
131+
}
132+
133+
var totalSize: UInt64 = 0
134+
for case let fileURL as URL in enumerator {
135+
guard let resourceValues = try? fileURL.resourceValues(forKeys: [.totalFileAllocatedSizeKey]),
136+
let fileSize = resourceValues.totalFileAllocatedSize
137+
else {
138+
continue
139+
}
140+
totalSize += UInt64(fileSize)
141+
}
142+
143+
return totalSize
144+
}
145+
75146
private func parseSize(_ sizeString: String) throws -> UInt64 {
76147
let measurement = try Measurement.parse(parsing: sizeString)
77148
let bytes = measurement.converted(to: .bytes).value
@@ -162,7 +233,8 @@ public actor VolumesService {
162233
format: "ext4",
163234
source: blockPath(for: name),
164235
labels: labels,
165-
options: driverOpts
236+
options: driverOpts,
237+
sizeInBytes: sizeInBytes
166238
)
167239

168240
try await store.create(volume)

Tests/CLITests/Subcommands/Volumes/TestCLIVolumes.swift

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import ContainerClient
1818
import Foundation
1919
import Testing
2020

21+
@Suite(.serialized)
2122
class TestCLIVolumes: CLITest {
2223

2324
func doVolumeCreate(name: String) throws {
@@ -323,4 +324,132 @@ class TestCLIVolumes: CLITest {
323324

324325
#expect(status2 == 0, "second container should succeed")
325326
}
327+
328+
@Test func testVolumePruneNoVolumes() throws {
329+
// Prune with no volumes should succeed with 0 reclaimed
330+
let (output, error, status) = try run(arguments: ["volume", "prune"])
331+
if status != 0 {
332+
throw CLIError.executionFailed("volume prune failed: \(error)")
333+
}
334+
335+
#expect(output.contains("0 B") || output.contains("No volumes to prune"), "should show no space reclaimed or no volumes message")
336+
}
337+
338+
@Test func testVolumePruneUnusedVolumes() throws {
339+
let testName = getTestName()
340+
let volumeName1 = "\(testName)_vol1"
341+
let volumeName2 = "\(testName)_vol2"
342+
343+
// Clean up any existing resources from previous runs
344+
doVolumeDeleteIfExists(name: volumeName1)
345+
doVolumeDeleteIfExists(name: volumeName2)
346+
347+
defer {
348+
doVolumeDeleteIfExists(name: volumeName1)
349+
doVolumeDeleteIfExists(name: volumeName2)
350+
}
351+
352+
try doVolumeCreate(name: volumeName1)
353+
try doVolumeCreate(name: volumeName2)
354+
let (listBefore, _, statusBefore) = try run(arguments: ["volume", "list", "--quiet"])
355+
#expect(statusBefore == 0)
356+
#expect(listBefore.contains(volumeName1))
357+
#expect(listBefore.contains(volumeName2))
358+
359+
// Prune should remove both
360+
let (output, error, status) = try run(arguments: ["volume", "prune"])
361+
if status != 0 {
362+
throw CLIError.executionFailed("volume prune failed: \(error)")
363+
}
364+
365+
#expect(output.contains(volumeName1) || !output.contains("No volumes to prune"), "should prune volume1")
366+
#expect(output.contains(volumeName2) || !output.contains("No volumes to prune"), "should prune volume2")
367+
#expect(output.contains("Reclaimed"), "should show reclaimed space")
368+
369+
// Verify volumes are gone
370+
let (listAfter, _, statusAfter) = try run(arguments: ["volume", "list", "--quiet"])
371+
#expect(statusAfter == 0)
372+
#expect(!listAfter.contains(volumeName1), "volume1 should be pruned")
373+
#expect(!listAfter.contains(volumeName2), "volume2 should be pruned")
374+
}
375+
376+
@Test func testVolumePruneSkipsVolumeInUse() throws {
377+
let testName = getTestName()
378+
let volumeInUse = "\(testName)_inuse"
379+
let volumeUnused = "\(testName)_unused"
380+
let containerName = "\(testName)_c1"
381+
382+
// Clean up any existing resources from previous runs
383+
doVolumeDeleteIfExists(name: volumeInUse)
384+
doVolumeDeleteIfExists(name: volumeUnused)
385+
doRemoveIfExists(name: containerName, force: true)
386+
387+
defer {
388+
try? doStop(name: containerName)
389+
doRemoveIfExists(name: containerName, force: true)
390+
doVolumeDeleteIfExists(name: volumeInUse)
391+
doVolumeDeleteIfExists(name: volumeUnused)
392+
}
393+
394+
try doVolumeCreate(name: volumeInUse)
395+
try doVolumeCreate(name: volumeUnused)
396+
try doLongRun(name: containerName, args: ["-v", "\(volumeInUse):/data"])
397+
try waitForContainerRunning(containerName)
398+
399+
// Prune should only remove the unused volume
400+
let (_, error, status) = try run(arguments: ["volume", "prune"])
401+
if status != 0 {
402+
throw CLIError.executionFailed("volume prune failed: \(error)")
403+
}
404+
405+
// Verify in-use volume still exists
406+
let (listAfter, _, statusAfter) = try run(arguments: ["volume", "list", "--quiet"])
407+
#expect(statusAfter == 0)
408+
#expect(listAfter.contains(volumeInUse), "volume in use should NOT be pruned")
409+
#expect(!listAfter.contains(volumeUnused), "unused volume should be pruned")
410+
411+
try doStop(name: containerName)
412+
doRemoveIfExists(name: containerName, force: true)
413+
doVolumeDeleteIfExists(name: volumeInUse)
414+
}
415+
416+
@Test func testVolumePruneSkipsVolumeAttachedToStoppedContainer() async throws {
417+
let testName = getTestName()
418+
let volumeName = "\(testName)_vol"
419+
let containerName = "\(testName)_c1"
420+
421+
// Clean up any existing resources from previous runs
422+
doVolumeDeleteIfExists(name: volumeName)
423+
doRemoveIfExists(name: containerName, force: true)
424+
425+
defer {
426+
doRemoveIfExists(name: containerName, force: true)
427+
doVolumeDeleteIfExists(name: volumeName)
428+
}
429+
430+
try doVolumeCreate(name: volumeName)
431+
try doCreate(name: containerName, image: alpine, volumes: ["\(volumeName):/data"])
432+
try await Task.sleep(for: .seconds(1))
433+
434+
// Prune should NOT remove the volume (container exists, even if stopped)
435+
let (_, error, status) = try run(arguments: ["volume", "prune"])
436+
if status != 0 {
437+
throw CLIError.executionFailed("volume prune failed: \(error)")
438+
}
439+
440+
let (listAfter, _, statusAfter) = try run(arguments: ["volume", "list", "--quiet"])
441+
#expect(statusAfter == 0)
442+
#expect(listAfter.contains(volumeName), "volume attached to stopped container should NOT be pruned")
443+
444+
doRemoveIfExists(name: containerName, force: true)
445+
let (_, error2, status2) = try run(arguments: ["volume", "prune"])
446+
if status2 != 0 {
447+
throw CLIError.executionFailed("volume prune failed: \(error2)")
448+
}
449+
450+
// Verify volume is gone
451+
let (listFinal, _, statusFinal) = try run(arguments: ["volume", "list", "--quiet"])
452+
#expect(statusFinal == 0)
453+
#expect(!listFinal.contains(volumeName), "volume should be pruned after container is deleted")
454+
}
326455
}

0 commit comments

Comments
 (0)