-
Notifications
You must be signed in to change notification settings - Fork 0
/
zfs.subr
128 lines (105 loc) · 2.86 KB
/
zfs.subr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
if [ ! "$_CBSD_ZFS_SUBR" ]; then
_CBSD_ZFS_SUBR=1
###
# return 0 if $1 is not valid ZFS mountpoint
# return 1 if $1 is valid ZFS mountpoint and mounted
# return 2 if $1 is valid ZFS mountpoint and not mounted
# if zfsmnt /mnt; then
# echo " not zfs (mounted or not) mountpoint"
# fi
# export ZPOOL for name ZFS for $1 mountpoint
zfsmnt() {
local _A
ZPOOL=$(/sbin/zfs list -Ho name,mountpoint | while read _name _mnt; do
[ "${_mnt}" = "${1}" ] && echo ${_name} && exit 2
done)
if [ $? -eq 2 ]; then
# Check for mounted
_A=$( /sbin/zfs get -Ho value mounted ${ZPOOL} )
[ "${_A}" = "yes" ] && return 1
return 2
else
return 0
fi
}
# return 0 if $1 is not valid ZFS filesystem
# return 1 if $1 is valid ZFS mountpoint and mounted
# return 2 if $1 is valid ZFS mountpoint and not mounted
# if zfsfs /mnt; then
# echo " not zfs (mounted or not) mounted"
# fi
zfsfs() {
local _A
_A=$(/sbin/zfs list -Ho name | while read _name; do
[ "${_name}" = "${1}" ] && exit 2
done)
if [ $? -eq 2 ]; then
# Check for mounted
_A=$( /sbin/zfs get -Ho value mounted ${1} )
[ "${_A}" = "yes" ] && return 1
return 2
else
return 0
fi
}
# export zmnt if $zroot exist, return 1
getmnt() {
local _res
[ -z "$1" ] && return 1
zmnt=$( /sbin/zfs get -Ho value mountpoint ${1} )
return $?
}
# return 0 if ${ZPOOL}/$1 zfs source exist
# if zfsroot jail1; then
# echo "zroot/$jail1 exist
# fi
zfsroot() {
[ -z "$1" ] && return 0
/sbin/zfs list -H -o name | while read _mnt; do
[ "$_mnt" = "${ZPOOL}/${1}" ] && exit 0
done
return $?
}
# $1 - oldjname
# $2 - newjname
zfs_snapshot_rename()
{
local _fs _jname _mod _snapname _oldjname _newjname
_oldjname="$1"
_newjname="$2"
[ -z "${_oldjname}" -o -z "${_newjname}" ] && return 0
# [ -n "${_oldjname}" ] && _mod="${ZPOOL}/${_oldjname}"
_mod="${ZPOOL}"
for _fs in $( /sbin/zfs list -H -r -t snapshot -o name ${_mod} 2>/dev/null ); do
_jname=$( /sbin/zfs get -H -o value -s local cbsdsnap:jname "${_fs}" 2>/dev/null )
[ -z "${_jname}" ] && continue
[ -n "${_oldjname}" -a "${_oldjname}" != "${_jname}" ] && continue
_snapname=$( /sbin/zfs get -H -o value -s local cbsdsnap:snapname "${_fs}" 2>/dev/null )
[ -z "${_snapname}" ] && continue
/sbin/zfs set cbsdsnap:jname=${_newjname} ${ZPOOL}@${_snapname}
# ${ECHO} "${MAGENTA}ZFS snapshot renamed: ${GREEN}${_snapname}${NORMAL}"
done
}
# test if symlink point to zvol
# extract and return 0 with is_zvol params as zvol
# $1 - path to symlink
is_getzvol()
{
is_zvol=
local _zvol_pref=
local _res=1
local tmp_zvol=
[ -z "${1}" ] && return 1
[ ! -h "${1}" ] && return 1
tmp_zvol=$( /usr/bin/readlink ${1} 2>/dev/null )
if [ -n "${tmp_zvol}" ]; then
_zvol_pref=$( substr --pos=0 --len=10 --str=${tmp_zvol} )
[ "${_zvol_pref}" = "/dev/zvol/" ] && _res=0
is_zvol=$( echo ${tmp_zvol} |/usr/bin/sed s:/dev/zvol/::g )
else
is_zvol=
fi
return ${_ret}
}
###
fi