forked from cbsd/cbsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem.subr
349 lines (299 loc) · 8.78 KB
/
system.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# if is_mounted /tmp; then ...mounted..
# - check for dir is mountpoint
is_mounted()
{
local tst
[ ! -d "${1}" ] && return 1
tst=`df -l ${1} | tail +2 | awk '{ print $6 }'`
[ "${tst}" = "${1}" ]
return $?
}
# if on_mounted /usr/src/base; then ...mounted..
# - check if dir mounted to other place
on_mounted()
{
[ ! -d "${1}" ] && return 1
mount |grep " on " | while read _device on _mountpt; do
[ "$_device" != "$1" ] || exit 2
done
[ $? -eq 2 ] && return 0
return 1
}
# check for base dir existance
# if not exist - ask and try to fetch from repository
# exit when no base and repo failure
# $BASE_DIR - alternative (prefered) path to base dir
# or
# $arch and $ver (opt: $basename) must be set
get_base()
{
[ "${ver}" = "empty" ] && return 0
# fill $BASE_DIR
init_basedir
# check for $BASE_DIR existance
if [ ! -d "${BASE_DIR}" ]; then
getyesno "No ${arch}-${target_arch}-${ver} base. Try to fetch from remote repository?"
[ $? -eq 1 ] && err 1 "${MAGENTA}No base data on: ${GREEN}${BASE_DIR}${NORMAL}"
cbsd repo action=get sources=base arch=${arch} ver=${ver} target_arch=${target_arch}
fi
[ ! -d "${BASE_DIR}" ] && err 1 "${MAGENTA}No base data on: ${GREEN}${BASE_DIR}${NORMAL}"
}
# Mount base -b or default to destination dir $path
# -b base path
# -k kernel path
# -s source path
# -o obj path
# -p ports path
# -d distfiles path
# -c /var/db/ports dbportspath
# -x additional mount, fstab format record, eg: "linsys /compat/linux/sys linsysfs rw 0 0"
# -t target (override path param)
# -v ver
# -a arch
mountbase()
{
local basepath kernelpath sourcepath objpath portspath distfilespath dbportspath MNTCODE
local _base_mounted=0
xfstab="${ftmpdir}/mountbase.$$"
MNTCODE=0
while getopts "b:k:s:o:p:d:c:x:t:v:a:" opt; do
case "$opt" in
b) basepath="${OPTARG}" ;;
k) kernelpath="${OPTARG}" ;;
s) sourcepath="${OPTARG}" ;;
o) objpath="${OPTARG}" ;;
p) portspath="${OPTARG}" ;;
d) distfilespath="${OPTARG}" ;;
c) dbportspath="${OPTARG}" ;;
x) echo "${OPTARG}" >> "${xfstab}" ;;
t) path="${OPTARG}" ;;
v) ver="${OPTARG}" ;;
a) arch="${OPTARG}" ;;
esac
shift $(($OPTIND - 1))
done
#already mounted?
is_mounted ${path} && _base_mounted=1
if [ ${_base_mounted} -eq 0 ]; then
#test for zfs mounted & mount if not
case $zfsfeat in
1) . $zfstool
[ $baserw -eq 1 ] && path=$data
zfsmnt ${path}
[ $? -eq 2 ] && zfs mount "${ZPOOL}"
;;
esac
if [ "${baserw}" = "0" ]; then
get_base
else
[ ! -f "${path}/bin/sh" ] && get_base
fi
[ ! -d "${path}" ] && mkdir -p ${path}
if [ "${baserw}" = "0" ]; then
#check for md-based base location (postfix -md)
if [ -f "${BASE_DIR}-md/bin/sh" ]; then
echo "Mount MD-based base location as base..."
mount -t nullfs -oro "${BASE_DIR}-md" ${path}
MNTCODE=$?
else
mount -t nullfs -oro ${BASE_DIR} ${path}
MNTCODE=$?
fi
fi
# [ ! -d "${path}/var/cache/pkg" ] && mkdir -p "${path}/var/cache/pkg"
fi # is _base_mounted=0
# Other part
if [ -n "${kernelpath}" -a -d "${kernelpath}" ]; then
[ ! -d "${path}/boot/kernel" ] && mkdir -p ${path}/boot/kernel
mount -t nullfs -oro ${kernelpath} ${path}/boot/kernel
fi
if [ -n "${sourcepath}" -a -d "${sourcepath}" ]; then
[ ! -d "${path}/usr/src" ] && mkdir -p ${path}/usr/src
mount -t nullfs -oro ${sourcepath} ${path}/usr/src
fi
if [ -n "${objpath}" -a -d "${objpath}" ]; then
[ ! -d "${path}/usr/obj" ] && mkdir -p ${path}/usr/obj
mount -t nullfs -orw ${objpath} ${path}/usr/obj
fi
if [ -n "${portspath}" -a -d "${portspath}" ]; then
[ ! -d "${path}/usr/ports" ] && mkdir -p ${path}/usr/ports
mount -t nullfs -orw ${portspath} ${path}/usr/ports
fi
if [ -n "${distfilespath}" -a -z "${portspath}" -a -d "${distfilespath}" ]; then
[ ! -d "${path}/usr/ports/distfiles" ] && mkdir -p ${path}/usr/ports/distfiles
mount -t nullfs -orw ${path}/usr/ports/distfiles
fi
# Finally mount devfs
#/sbin/mount -t devfs devfs ${path}/dev
#if [ -n "$devfs_ruleset" ]; then
# . /etc/rc.subr
# devfs_rulesets_from_file ${etcdir}/devfs.rules
# devfs -m ${path}/dev rule -s ${devfsrules} applyset
#fi
if [ -f "${xfstab}" ]; then
cbsd mountfstab jroot="${path}" fstab="${xfstab}" jname="${jname}"
rm -f "${xfstab}"
fi
return $MNTCODE
}
mountfstab()
{
local _res=$(/usr/local/bin/cbsd mountfstab jroot=${path} fstab=${mount_fstab} jname="${jname}" )
if [ $? -ne 0 ]; then
# force unmount it better then..
echo "Invalid fstab file: ${_res}"
_res=`cbsd jcleanup jname=${jname}`
continue
fi
if [ -f "${mount_fstab}.local" ]; then
cbsd mountfstab jroot=${path} fstab=${mount_fstab}.local jname="${jname}"
# cleanup for local?
fi
}
unmountbase()
{
[ $baserw -eq 0 ] && umount -f ${path}
cbsd unmountfstab jroot=${path} fstab=${mount_fstab} > /dev/null 2>&1
cbsd jcleanup jname=${jname} > /dev/null 2>&1
}
# Unmount all in $path or $1
# if exist $2 - do not unmount root of $1
umount_cdirs()
{
local _unmount_root=1
[ -n "${1}" ] && path="$1"
[ -n "${2}" ] && _unmount_root=0
# when error before path, we do not have any mounts by scripts
[ -z "${path}" ] && return 0
MOUNT_LIST=`mount |sort -r| awk -F" on " '{print $2}'`
MPATH=""
for mount_point in $MOUNT_LIST; do
case $mount_point in
${path}/*)
[ -n "${mount_point}" ] && MPATH="${MPATH} $path${mount_point#$path}"
;;
esac
done
[ -n "${MPATH}" ] && umount -f ${MPATH}
#finaly unmount cdir
if [ ${_unmount_root} -eq 1 ]; then
is_mounted ${path} && umount -f ${path}
fi
}
# populate $2 chroot dir from $1 base directory
# when baserw set to 0, just create default hier
populate_cdir()
{
local _dir _dst _i
[ -d "${1}" -a -d "${2}" ] || err 1 "No such base version on ${1}"
_dir=$1
_dst=$2
#JAILNODATA sample
#[ -z "${JAILNODATA}" -a "${baserw}" = "0" ] && JAILNODATA="${_dir}/.cshrc.*|\
#${_dir}/dev.*|\
#${_dir}/bin.*|\
#${_dir}/media.*|\
#${_dir}/rescue.*|\
#${_dir}/sys.*|\
#${_dir}/.profile.*|\
#${_dir}/boot.*|\
#${_dir}/lib.*|\
#${_dir}/mnt.*|\
#${_dir}/COPYRIGHT.*|\
#${_dir}/libexec.*|\
#${_dir}/proc.*|\
#${_dir}/sbin.*|\
#${_dir}/usr/bin.*|\
#${_dir}/usr/games.*|\
#${_dir}/usr/include.*|\
#${_dir}/usr/lib.*|\
#${_dir}/usr/lib32.*|\
#${_dir}/usr/libdata.*|\
#${_dir}/usr/libexec.*|\
#${_dir}/usr/local.*|\
#${_dir}/usr/sbin.*|\
#${_dir}/usr/share.*|"
JAILDATA="${_dir}/compat \
${_dir}/dev \
${_dir}/etc \
${_dir}/home \
${_dir}/root \
${_dir}/tmp \
${_dir}/usr \
${_dir}/var \
${_dir}/.cshrc \
${_dir}/.profile \
${_dir}/COPYRIGHT"
if [ "$baserw" = "1" ]; then
# cd ${_dir} && pax -p eme -X -rw . ${_dst}
cd ${_dir} && pax -p eme -rw . ${_dst}
else
if [ -n "${JAILNODATA}" ]; then
cd ${_dir} && find -E ${_dir} \( -type f -or -type d -or -type l \) -and -not -regex \"$JAILNODATA\" -print |sed s:${_dir}:./:g |cpio -pdmu ${_dst}
elif [ -n "${JAILDATA}" ]; then
for _i in ${JAILDATA}; do
cp -a ${_i} ${_dst}
done
fi
fi
[ "${applytpl}" = "1" ] && truncate -s0 ${_dst}/etc/motd
return 0
}
# populate or remove system files from jail data dir
# $1 - jail data dir
# $2 - mode: 1 - write (populate), 2 - readonly (remove)
switch_baserw()
{
[ ! -d "${1}" ] && return 0
get_base
# populate
if [ "${2}" = "1" ]; then
JAILNODATA="${_dir}/|\
${BASE_DIR}/dev.*|\
${BASE_DIR}/etc.*|\
${BASE_DIR}/sys.*|\
${BASE_DIR}/proc.*|\
${BASE_DIR}/root.*|"
${ECHO} "${MAGENTA}Populate jail data from: ${GREEN}${BASE_DIR}${NORMAL}"
cd $BASE_DIR && find -E ${BASE_DIR} \( -type f -or -type d -or -type l \) -and -not -regex \"$JAILNODATA\" -print |sed s:${BASE_DIR}:./:g |cpio -pdmu ${1}
[ ! -d "${1}/dev" ] && mkdir "${1}/dev"
#remove system fstab
[ -f "${jailfstabdir}/${jailfstabpref}${jname}" ] && rm -f "${jailfstabdir}/${jailfstabpref}${jname}"
elif [ "$2" = "2" ]; then
# switch to basero
# create system fstab
cat > ${mount_fstab} << EOF
# Please do not edit this file for additional fstabs
# Use ${jailfstabdir}/${jailfstabpref}local instead
${data}/etc /etc nullfs rw 0 0
${data}/root /root nullfs rw 0 0
${data}/tmp /tmp nullfs rw 0 0
${data}/usr/home /usr/home nullfs rw 0 0
${data}/usr/local /usr/local nullfs rw 0 0
${data}/usr/compat /usr/compat nullfs rw 0 0
${data}/var /var nullfs rw 0 0
#
EOF
#todo: remove file from data listed in base jail
REMOVEDIR="bin \
lib \
libexec \
rescue \
sbin \
usr/bin \
usr/games \
usr/include \
usr/lib \
usr/lib32 \
usr/libdata \
usr/libexec \
usr/sbin \
usr/share"
[ -z "${data}" ] && return 0
${ECHO} "${MAGENTA}Reduce jail data by switching from baserw -> basero: ${GREEN}${data}${NORMAL}"
for i in ${REMOVEDIR}; do
[ -d "${data}/${i}" ] && chflags -R noschg ${data}/${i} && rm -rf ${data}/${i}
done
fi
# populate
}