]> git.proxmox.com Git - mirror_zfs.git/blame - man/man8/zfs-program.8
zfs.8 has wrong description of "zfs program -t"
[mirror_zfs.git] / man / man8 / zfs-program.8
CommitLineData
d99a0153
CW
1.\" This file and its contents are supplied under the terms of the
2.\" Common Development and Distribution License ("CDDL"), version 1.0.
3.\" You may only use this file in accordance with the terms of version
4.\" 1.0 of the CDDL.
5.\"
6.\" A full copy of the text of the CDDL should have accompanied this
7.\" source. A copy of the CDDL is also available via the Internet at
8.\" http://www.illumos.org/license/CDDL.
9.\"
10.\"
c568ab8d 11.\" Copyright (c) 2016, 2019 by Delphix. All Rights Reserved.
d99a0153
CW
12.\"
13.Dd January 21, 2016
14.Dt ZFS-PROGRAM 8
15.Os
16.Sh NAME
17.Nm zfs program
18.Nd executes ZFS channel programs
19.Sh SYNOPSIS
5b72a38d 20.Cm "zfs program"
272b5d73 21.Op Fl jn
d99a0153
CW
22.Op Fl t Ar instruction-limit
23.Op Fl m Ar memory-limit
24.Ar pool
25.Ar script
26.\".Op Ar optional arguments to channel program
27.Sh DESCRIPTION
28The ZFS channel program interface allows ZFS administrative operations to be
29run programmatically as a Lua script.
30The entire script is executed atomically, with no other administrative
31operations taking effect concurrently.
32A library of ZFS calls is made available to channel program scripts.
33Channel programs may only be run with root privileges.
34.Pp
35A modified version of the Lua 5.2 interpreter is used to run channel program
36scripts.
37The Lua 5.2 manual can be found at:
38.Bd -centered -offset indent
39.Lk http://www.lua.org/manual/5.2/
40.Ed
41.Pp
42The channel program given by
43.Ar script
44will be run on
45.Ar pool ,
46and any attempts to access or modify other pools will cause an error.
47.Sh OPTIONS
48.Bl -tag -width "-t"
272b5d73
AP
49.It Fl j
50Display channel program output in JSON format. When this flag is specified and
51standard output is empty - channel program encountered an error. The details of
52such an error will be printed to standard error in plain text.
5b72a38d
SD
53.It Fl n
54Executes a read-only channel program, which runs faster.
55The program cannot change on-disk state by calling functions from the
56zfs.sync submodule.
57The program can be used to gather information such as properties and
58determining if changes would succeed (zfs.check.*).
59Without this flag, all pending changes must be synced to disk before a
60channel program can complete.
d99a0153 61.It Fl t Ar instruction-limit
c568ab8d 62Limit the number of Lua instructions to execute.
d99a0153
CW
63If a channel program executes more than the specified number of instructions,
64it will be stopped and an error will be returned.
65The default limit is 10 million instructions, and it can be set to a maximum of
66100 million instructions.
67.It Fl m Ar memory-limit
68Memory limit, in bytes.
69If a channel program attempts to allocate more memory than the given limit, it
70will be stopped and an error returned.
71The default memory limit is 10 MB, and can be set to a maximum of 100 MB.
72.El
73.Pp
74All remaining argument strings will be passed directly to the Lua script as
75described in the
76.Sx LUA INTERFACE
77section below.
78.Sh LUA INTERFACE
79A channel program can be invoked either from the command line, or via a library
80call to
81.Fn lzc_channel_program .
82.Ss Arguments
83Arguments passed to the channel program are converted to a Lua table.
84If invoked from the command line, extra arguments to the Lua script will be
85accessible as an array stored in the argument table with the key 'argv':
86.Bd -literal -offset indent
87args = ...
88argv = args["argv"]
89-- argv == {1="arg1", 2="arg2", ...}
90.Ed
91.Pp
92If invoked from the libZFS interface, an arbitrary argument list can be
93passed to the channel program, which is accessible via the same
94"..." syntax in Lua:
95.Bd -literal -offset indent
96args = ...
97-- args == {"foo"="bar", "baz"={...}, ...}
98.Ed
99.Pp
100Note that because Lua arrays are 1-indexed, arrays passed to Lua from the
101libZFS interface will have their indices incremented by 1.
102That is, the element
103in
104.Va arr[0]
105in a C array passed to a channel program will be stored in
106.Va arr[1]
107when accessed from Lua.
108.Ss Return Values
109Lua return statements take the form:
110.Bd -literal -offset indent
111return ret0, ret1, ret2, ...
112.Ed
113.Pp
114Return statements returning multiple values are permitted internally in a
115channel program script, but attempting to return more than one value from the
116top level of the channel program is not permitted and will throw an error.
117However, tables containing multiple values can still be returned.
118If invoked from the command line, a return statement:
119.Bd -literal -offset indent
120a = {foo="bar", baz=2}
121return a
122.Ed
123.Pp
124Will be output formatted as:
125.Bd -literal -offset indent
126Channel program fully executed with return value:
127 return:
128 baz: 2
129 foo: 'bar'
130.Ed
131.Ss Fatal Errors
132If the channel program encounters a fatal error while running, a non-zero exit
133status will be returned.
134If more information about the error is available, a singleton list will be
135returned detailing the error:
136.Bd -literal -offset indent
137error: "error string, including Lua stack trace"
138.Ed
139.Pp
140If a fatal error is returned, the channel program may have not executed at all,
141may have partially executed, or may have fully executed but failed to pass a
142return value back to userland.
143.Pp
144If the channel program exhausts an instruction or memory limit, a fatal error
145will be generated and the program will be stopped, leaving the program partially
146executed.
147No attempt is made to reverse or undo any operations already performed.
148Note that because both the instruction count and amount of memory used by a
149channel program are deterministic when run against the same inputs and
150filesystem state, as long as a channel program has run successfully once, you
151can guarantee that it will finish successfully against a similar size system.
152.Pp
153If a channel program attempts to return too large a value, the program will
154fully execute but exit with a nonzero status code and no return value.
155.Pp
156.Em Note:
157ZFS API functions do not generate Fatal Errors when correctly invoked, they
158return an error code and the channel program continues executing.
159See the
160.Sx ZFS API
161section below for function-specific details on error return codes.
162.Ss Lua to C Value Conversion
163When invoking a channel program via the libZFS interface, it is necessary to
164translate arguments and return values from Lua values to their C equivalents,
165and vice-versa.
166.Pp
167There is a correspondence between nvlist values in C and Lua tables.
168A Lua table which is returned from the channel program will be recursively
169converted to an nvlist, with table values converted to their natural
170equivalents:
171.Bd -literal -offset indent
172string -> string
173number -> int64
174boolean -> boolean_value
175nil -> boolean (no value)
176table -> nvlist
177.Ed
178.Pp
179Likewise, table keys are replaced by string equivalents as follows:
180.Bd -literal -offset indent
181string -> no change
182number -> signed decimal string ("%lld")
183boolean -> "true" | "false"
184.Ed
185.Pp
186Any collision of table key strings (for example, the string "true" and a
187true boolean value) will cause a fatal error.
188.Pp
189Lua numbers are represented internally as signed 64-bit integers.
190.Sh LUA STANDARD LIBRARY
191The following Lua built-in base library functions are available:
192.Bd -literal -offset indent
193assert rawlen
194collectgarbage rawget
195error rawset
196getmetatable select
197ipairs setmetatable
198next tonumber
199pairs tostring
200rawequal type
201.Ed
202.Pp
203All functions in the
204.Em coroutine ,
205.Em string ,
206and
207.Em table
208built-in submodules are also available.
209A complete list and documentation of these modules is available in the Lua
210manual.
211.Pp
212The following functions base library functions have been disabled and are
213not available for use in channel programs:
214.Bd -literal -offset indent
215dofile
216loadfile
217load
218pcall
219print
220xpcall
221.Ed
222.Sh ZFS API
223.Ss Function Arguments
224Each API function takes a fixed set of required positional arguments and
225optional keyword arguments.
226For example, the destroy function takes a single positional string argument
227(the name of the dataset to destroy) and an optional "defer" keyword boolean
228argument.
229When using parentheses to specify the arguments to a Lua function, only
230positional arguments can be used:
231.Bd -literal -offset indent
232zfs.sync.destroy("rpool@snap")
233.Ed
234.Pp
235To use keyword arguments, functions must be called with a single argument that
236is a Lua table containing entries mapping integers to positional arguments and
237strings to keyword arguments:
238.Bd -literal -offset indent
239zfs.sync.destroy({1="rpool@snap", defer=true})
240.Ed
241.Pp
242The Lua language allows curly braces to be used in place of parenthesis as
243syntactic sugar for this calling convention:
244.Bd -literal -offset indent
245zfs.sync.snapshot{"rpool@snap", defer=true}
246.Ed
247.Ss Function Return Values
248If an API function succeeds, it returns 0.
249If it fails, it returns an error code and the channel program continues
250executing.
251API functions do not generate Fatal Errors except in the case of an
252unrecoverable internal file system error.
253.Pp
254In addition to returning an error code, some functions also return extra
255details describing what caused the error.
256This extra description is given as a second return value, and will always be a
257Lua table, or Nil if no error details were returned.
258Different keys will exist in the error details table depending on the function
259and error case.
260Any such function may be called expecting a single return value:
261.Bd -literal -offset indent
262errno = zfs.sync.promote(dataset)
263.Ed
264.Pp
265Or, the error details can be retrieved:
266.Bd -literal -offset indent
267errno, details = zfs.sync.promote(dataset)
268if (errno == EEXIST) then
269 assert(details ~= Nil)
270 list_of_conflicting_snapshots = details
271end
272.Ed
273.Pp
274The following global aliases for API function error return codes are defined
275for use in channel programs:
276.Bd -literal -offset indent
277EPERM ECHILD ENODEV ENOSPC
278ENOENT EAGAIN ENOTDIR ESPIPE
279ESRCH ENOMEM EISDIR EROFS
280EINTR EACCES EINVAL EMLINK
281EIO EFAULT ENFILE EPIPE
282ENXIO ENOTBLK EMFILE EDOM
283E2BIG EBUSY ENOTTY ERANGE
284ENOEXEC EEXIST ETXTBSY EDQUOT
285EBADF EXDEV EFBIG
286.Ed
287.Ss API Functions
288For detailed descriptions of the exact behavior of any zfs administrative
289operations, see the main
290.Xr zfs 1
291manual page.
292.Bl -tag -width "xx"
293.It Em zfs.debug(msg)
294Record a debug message in the zfs_dbgmsg log.
295A log of these messages can be printed via mdb's "::zfs_dbgmsg" command, or
296can be monitored live by running:
297.Bd -literal -offset indent
298 dtrace -n 'zfs-dbgmsg{trace(stringof(arg0))}'
299.Ed
300.Pp
301msg (string)
302.Bd -ragged -compact -offset "xxxx"
303Debug message to be printed.
304.Ed
475eca49
CW
305.It Em zfs.exists(dataset)
306Returns true if the given dataset exists, or false if it doesn't.
307A fatal error will be thrown if the dataset is not in the target pool.
308That is, in a channel program running on rpool,
309zfs.exists("rpool/nonexistent_fs") returns false, but
310zfs.exists("somepool/fs_that_may_exist") will error.
311.Pp
312dataset (string)
313.Bd -ragged -compact -offset "xxxx"
314Dataset to check for existence.
315Must be in the target pool.
316.Ed
d99a0153
CW
317.It Em zfs.get_prop(dataset, property)
318Returns two values.
319First, a string, number or table containing the property value for the given
320dataset.
321Second, a string containing the source of the property (i.e. the name of the
322dataset in which it was set or nil if it is readonly).
323Throws a Lua error if the dataset is invalid or the property doesn't exist.
324Note that Lua only supports int64 number types whereas ZFS number properties
325are uint64.
326This means very large values (like guid) may wrap around and appear negative.
327.Pp
328dataset (string)
329.Bd -ragged -compact -offset "xxxx"
330Filesystem or snapshot path to retrieve properties from.
331.Ed
332.Pp
333property (string)
334.Bd -ragged -compact -offset "xxxx"
335Name of property to retrieve.
336All filesystem, snapshot and volume properties are supported except
337for 'mounted' and 'iscsioptions.'
338Also supports the 'written@snap' and 'written#bookmark' properties and
339the '<user|group><quota|used>@id' properties, though the id must be in numeric
340form.
341.Ed
342.El
343.Bl -tag -width "xx"
344.It Sy zfs.sync submodule
345The sync submodule contains functions that modify the on-disk state.
346They are executed in "syncing context".
347.Pp
348The available sync submodule functions are as follows:
349.Bl -tag -width "xx"
350.It Em zfs.sync.destroy(dataset, [defer=true|false])
351Destroy the given dataset.
352Returns 0 on successful destroy, or a nonzero error code if the dataset could
353not be destroyed (for example, if the dataset has any active children or
354clones).
355.Pp
356dataset (string)
357.Bd -ragged -compact -offset "xxxx"
358Filesystem or snapshot to be destroyed.
359.Ed
360.Pp
361[optional] defer (boolean)
362.Bd -ragged -compact -offset "xxxx"
363Valid only for destroying snapshots.
364If set to true, and the snapshot has holds or clones, allows the snapshot to be
365marked for deferred deletion rather than failing.
366.Ed
367.It Em zfs.sync.promote(dataset)
368Promote the given clone to a filesystem.
369Returns 0 on successful promotion, or a nonzero error code otherwise.
370If EEXIST is returned, the second return value will be an array of the clone's
371snapshots whose names collide with snapshots of the parent filesystem.
372.Pp
373dataset (string)
374.Bd -ragged -compact -offset "xxxx"
375Clone to be promoted.
376.Ed
af073689
BL
377.It Em zfs.sync.rollback(filesystem)
378Rollback to the previous snapshot for a dataset.
379Returns 0 on successful rollback, or a nonzero error code otherwise.
380Rollbacks can be performed on filesystems or zvols, but not on snapshots
381or mounted datasets.
382EBUSY is returned in the case where the filesystem is mounted.
383.Pp
384filesystem (string)
385.Bd -ragged -compact -offset "xxxx"
386Filesystem to rollback.
387.Ed
234c91c5
CW
388.It Em zfs.sync.snapshot(dataset)
389Create a snapshot of a filesystem.
390Returns 0 if the snapshot was successfully created,
391and a nonzero error code otherwise.
392.Pp
393Note: Taking a snapshot will fail on any pool older than legacy version 27.
394To enable taking snapshots from ZCP scripts, the pool must be upgraded.
395.Pp
396dataset (string)
397.Bd -ragged -compact -offset "xxxx"
398Name of snapshot to create.
399.Ed
d99a0153
CW
400.El
401.It Sy zfs.check submodule
402For each function in the zfs.sync submodule, there is a corresponding zfs.check
403function which performs a "dry run" of the same operation.
404Each takes the same arguments as its zfs.sync counterpart and returns 0 if the
405operation would succeed, or a non-zero error code if it would fail, along with
406any other error details.
407That is, each has the same behavior as the corresponding sync function except
408for actually executing the requested change.
409For example,
410.Em zfs.check.destroy("fs")
411returns 0 if
412.Em zfs.sync.destroy("fs")
413would successfully destroy the dataset.
414.Pp
415The available zfs.check functions are:
416.Bl -tag -width "xx"
417.It Em zfs.check.destroy(dataset, [defer=true|false])
418.It Em zfs.check.promote(dataset)
af073689 419.It Em zfs.check.rollback(filesystem)
234c91c5 420.It Em zfs.check.snapshot(dataset)
d99a0153
CW
421.El
422.It Sy zfs.list submodule
423The zfs.list submodule provides functions for iterating over datasets and
424properties.
425Rather than returning tables, these functions act as Lua iterators, and are
426generally used as follows:
427.Bd -literal -offset indent
428for child in zfs.list.children("rpool") do
429 ...
430end
431.Ed
432.Pp
433The available zfs.list functions are:
434.Bl -tag -width "xx"
435.It Em zfs.list.clones(snapshot)
436Iterate through all clones of the given snapshot.
437.Pp
438snapshot (string)
439.Bd -ragged -compact -offset "xxxx"
440Must be a valid snapshot path in the current pool.
441.Ed
442.It Em zfs.list.snapshots(dataset)
443Iterate through all snapshots of the given dataset.
444Each snapshot is returned as a string containing the full dataset name, e.g.
445"pool/fs@snap".
446.Pp
447dataset (string)
448.Bd -ragged -compact -offset "xxxx"
449Must be a valid filesystem or volume.
450.Ed
451.It Em zfs.list.children(dataset)
452Iterate through all direct children of the given dataset.
453Each child is returned as a string containing the full dataset name, e.g.
454"pool/fs/child".
455.Pp
456dataset (string)
457.Bd -ragged -compact -offset "xxxx"
458Must be a valid filesystem or volume.
459.Ed
460.It Em zfs.list.properties(dataset)
461Iterate through all user properties for the given dataset.
462.Pp
463dataset (string)
464.Bd -ragged -compact -offset "xxxx"
465Must be a valid filesystem, snapshot, or volume.
466.Ed
467.It Em zfs.list.system_properties(dataset)
468Returns an array of strings, the names of the valid system (non-user defined)
469properties for the given dataset.
470Throws a Lua error if the dataset is invalid.
471.Pp
472dataset (string)
473.Bd -ragged -compact -offset "xxxx"
474Must be a valid filesystem, snapshot or volume.
475.Ed
476.El
477.El
478.Sh EXAMPLES
479.Ss Example 1
480The following channel program recursively destroys a filesystem and all its
481snapshots and children in a naive manner.
482Note that this does not involve any error handling or reporting.
483.Bd -literal -offset indent
484function destroy_recursive(root)
485 for child in zfs.list.children(root) do
486 destroy_recursive(child)
487 end
488 for snap in zfs.list.snapshots(root) do
489 zfs.sync.destroy(snap)
490 end
491 zfs.sync.destroy(root)
492end
493destroy_recursive("pool/somefs")
494.Ed
495.Ss Example 2
496A more verbose and robust version of the same channel program, which
497properly detects and reports errors, and also takes the dataset to destroy
498as a command line argument, would be as follows:
499.Bd -literal -offset indent
500succeeded = {}
501failed = {}
502
503function destroy_recursive(root)
504 for child in zfs.list.children(root) do
505 destroy_recursive(child)
506 end
507 for snap in zfs.list.snapshots(root) do
508 err = zfs.sync.destroy(snap)
509 if (err ~= 0) then
510 failed[snap] = err
511 else
512 succeeded[snap] = err
513 end
514 end
515 err = zfs.sync.destroy(root)
516 if (err ~= 0) then
517 failed[root] = err
518 else
519 succeeded[root] = err
520 end
521end
522
523args = ...
524argv = args["argv"]
525
526destroy_recursive(argv[1])
527
528results = {}
529results["succeeded"] = succeeded
530results["failed"] = failed
531return results
532.Ed
533.Ss Example 3
534The following function performs a forced promote operation by attempting to
535promote the given clone and destroying any conflicting snapshots.
536.Bd -literal -offset indent
537function force_promote(ds)
538 errno, details = zfs.check.promote(ds)
539 if (errno == EEXIST) then
540 assert(details ~= Nil)
541 for i, snap in ipairs(details) do
542 zfs.sync.destroy(ds .. "@" .. snap)
543 end
544 elseif (errno ~= 0) then
545 return errno
546 end
547 return zfs.sync.promote(ds)
548end
549.Ed