1
1
I'd like to take output from two commands –
zpool list
zfs list
and for each found pool:
zpool get all nameofpool
and for each found file system:
zfs get all nameoffilesystem
Background and environment
I'm making local changes to script that's integral to OS X,
/usr/bin/sysdiagnose
#!/bin/sh
for starters- always run with superuser privileges
- sometimes effectively headless (triggered by a key chord), so the output must be a file.
First experiment
Based on the example at #65 Remove all ZFS snapshots:
#!/bin/sh
for dataset in `zfs list -H | cut -f 1`
do
zfs get all $dataset
done
That works, but not where there's a space in the name of the dataset. For example where the file system is zhandy/Pocket Time Machine
the output includes:
cannot open 'zhandy/Pocket': dataset does not exist
cannot open 'Time': dataset does not exist
cannot open 'Machine': dataset does not exist
Second experiment
… was based on the first answer to this question – using IFS
– and made the script more like Apple's. See revision 4.
Third experiment
Based on the accepted answer to this question – with IFS
, and quotation marks for "$dataset"
:
#!/bin/sh
data_directory_path=~/Desktop
ECHO=/bin/echo
ZFS=/usr/sbin/zfs
ZPOOL=/usr/sbin/zpool
# If there exists a zfs binary, get some ZFS information
if [ -f "${ZFS}" ]
then
"${ECHO}" "Recording ZFS pool version information ..."
"${ZPOOL}" upgrade &> ${data_directory_path}/zpool\ upgrade.txt
"${ECHO}" " listing all ZFS pools ..."
"${ZPOOL}" list &> ${data_directory_path}/zpool\ list.txt
"${ECHO}" " detailed health status and verbose data error information ..."
"${ZPOOL}" status -v &> ${data_directory_path}/zpool\ status.txt
"${ECHO}" " pools that are available but not currently imported"
"${ZPOOL}" import &> ${data_directory_path}/zpool\ import.txt
"${ECHO}" "Recording ZFS file system version information ..."
"${ZFS}" upgrade &> ${data_directory_path}/zfs\ upgrade.txt
"${ECHO}" " listing all ZFS file systems ..."
"${ZFS}" list &> ${data_directory_path}/zfs\ list.txt
"${ECHO}" " all properties of each file system"
OLD_IFS=$IFS
IFS=$'\n'
for dataset in `zfs list -H | cut -f 1`
do
"${ZFS}" get all "$dataset" &> ${data_directory_path}/ZFS\ file\ system\ properties.txt
done
IFS=$OLD_IFS
"${ECHO}" "Listing the contents of /dev/dsk"
"${LS}" -@adel /Volumes &> ${data_directory_path}/ls-dev-dsk.txt
"${ECHO}" "Listing the contents of /var/zfs/dsk"
"${LS}" -@adel /Volumes &> ${data_directory_path}/ls-var-zfs-dsk.txt
fi
Amongst the resulting files, ZFS file system properties.txt
lists properties for just one ZFS file system … a dataset with white space in its name.
The most desirable end result is properties:
- for all ZFS file systems
- in a file.
Removing the following string –
&> ${data_directory_path}/ZFS\ file\ system\ properties.txt
– does get properties for all ZFS file systems, in a window of Terminal but not in a file. That's enough for me to accept an answer.
The output to file criterion, which wasn't in my first edition of the question, may be easily answered elsewhere.
Appreciating the age of the question and that it might have been simplified for brevity, what was wrong with simply
for DS in $(zfs list -H -o name); do zfs get all "$DS"; done
? I've been using that idiom in my own scripts for a while and am hoping I haven't missed anything (note the use of-o name
and"
-quoting to limit the output (no longer needscut
) and handle spaces in dataset names (even though space is supposed to be an invalid char for dataset names) – jimbobmcgee – 2019-07-15T11:52:15.100Chat, with reference to the second edition of this question: http://chat.stackexchange.com/transcript/message/7661588#7661588
– Graham Perrin – 2013-01-14T09:30:01.380Alongside the accepted answer below, with this answer (to How can I take the output of a shell script and place it in a file on the command line?) I realise that for the output to file – for the append – I need
– Graham Perrin – 2013-01-16T07:23:06.543>>
(not&>
or&>>
). In feedback to Apple I asked forsysdiagnose
to be added to Apple Open Source – if this happens I'll make public my other changes to the script, most of which are unrelated to ZFS.