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