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
|
# bootcd-run.lib
# vim: set filetype=sh :
# version: $Version: $
# copyright: Bernd Schumacher <bernd.schumacher@hpe.com> (2001-2020)
# license: GNU General Public License, version 2
# comment: use ANSW to change the next point from external
# description: To understand what to do with this library you can download
# the test programs with wget
#
# info [--ignore_silent] <msg>
info()
{
if [ "$1" = "--ignore_silent" ]; then
shift
ia_logerr "$@"
else
[ "${ia_use_silent:-}" ] && ia_log "$@" || ia_logerr "$@"
fi
}
log()
{
ia_log "$@"
}
warn()
{
if [ $# -gt 0 ]; then
if [ "$yes" ]; then
A="i"
ia_logerr "$(/bin/echo -e "--- WARNING ---\n$*\n$A ($yes)")"
else
A="$(ia_ask "$(/bin/echo -e "--- WARNING ---\n$*")" "" "" \
"--- (e)xit (i)gnore --- " "^(e|i)$")"
if [ "$A" = "e" ]; then
exit 1
fi
fi
fi
}
# err [--code <exit-code>] "text"
err()
{
local code
code="1"
if [ "$1" = "--code" ]; then
code=$2
shift 2
fi
ia_logerr "--- FATAL ERROR ---"
ia_logerr "$@"
exit $code
}
# example:
# fun() { ... RETURN_ERR="this is an error"; return <X>; ... }
# ia_add "fun; RETURN_CODE=\$?"
# ia_nocheck && ia_add "chk_RETURN_ERR"
chk_RETURN_ERR()
{
if [ "${RETURN_ERR:-}" ]; then
err --code "${RETURN_CODE:-1}" "$RETURN_ERR"
fi
}
dbglog()
{
dbg "$@"
ia_log "$*"
}
# create_host_keys <DIR>
# if ssh_host_key, ssh_host_rsa or ... exists in <DIR>
# then recreate it.
create_host_keys()
{
eval "$ia_init"
# Now only ssh versions which support Option -t are supported
ia_add "dbg \"create_host_keys start\""
F=ssh_host_key
if [ -f $1/$F ]; then
ia_add "rm -f $1/$F $1/$F.pub"
ia_add "ssh-keygen -b 1024 -f $1/$F -N '' -t rsa1 2>&1 | log"
fi
F=ssh_host_rsa_key
if [ -f $1/$F ]; then
ia_add "rm -f $1/$F $1/$F.pub"
ia_add "ssh-keygen -b 1024 -f $1/$F -N '' -t rsa 2>&1 | log"
fi
F=ssh_host_dsa_key
if [ -f $1/$F ]; then
ia_add "rm -f $1/$F $1/$F.pub"
ia_add "ssh-keygen -b 1024 -f $1/$F -N '' -t dsa 2>&1 | log"
fi
ia -c
}
# function: mk_empty_dir -- create directory
# get: $1 -- directory
# $2 -- permissions (optional)
mk_empty_dir()
{
local dir
local message
local perm
local A
dir=$1
[ $# -ge 2 ] && perm=$2
message="Directory \"$dir\" exist's, and will be removed"
A=""
if [ "$yes" ]; then
log "$message"
A="y"
else
A="$(ia_ask "$message" "" "y" "(Y/n) " "^(y|n)$")"
fi
if [ "$A" = "y" ]; then
rm -rf $dir
else
err "directory \"$dir\" must not exist."
fi
}
# function: check_tools -- check if tools exist
# use ./ to search in local directory
# get: $1..$n -- tools
check_tools()
{
while [ $# -gt 0 ]; do
echo -n >&2 " check for \"$1\" ..."
if [ "$(echo $1 |cut -c1-2)" = "./" ]; then
[ ! -f "$1" ] && err "Can't find file \"$1\"!"
else
[ ! "$(which $1)" ] && err "Can't find tool \"$1\" in PATH!"
fi
echo >&2 "ok"
shift
done
}
# function: prepare_chroot <workdir> [-i]
# globals: IAHEAD
prepare_chroot()
{
local p i ion itype selinuxmp
eval "$ia_init"
p="PATH=/usr/sbin:/usr/bin:/sbin:/bin"
local workdir="$1"
# if possible use --bind to prevent error in chroot: "Device <X> not
# initialized in udev database even after waiting 10000000 microseconds"
# run:tmpfs is needed for lvm
for i in dev:devtmpfs proc:proc sys:sysfs sys/firmware/efi/efivars:efivarfs dev/pts:devpts run:tmpfs; do
ion="$(echo "$i" | cut -d: -f1)"
itype="$(echo "$i" | cut -d: -f2)"
if [ "$(grep "\<$itype$" /proc/filesystems)" ]; then
[ "$(mount | grep "on /$ion type $itype ")" ] && \
ia_add "mount --bind /$ion $workdir/$ion" || \
ia_add "mount -t $itype $itype $workdir/$ion"
fi
done
ia_add "$p chroot $workdir sh -c \"[ -f /etc/mtab ] && mv /etc/mtab /etc/mtab.bootcd || rm -f /etc/mtab.bootcd\""
ia_add "$p chroot $workdir sh -c \"grep -v -e \\\"^sunrpc\>\\\" -e \\\"^rootfs\>\\\" /proc/mounts >/etc/mtab\""
# we need selinux mounted to install openssh_server without
# error "chage: Permission denied."
selinuxmp="$(sed -E -n -e "s|^selinuxfs\s+(\S+).*$|\1|p" /proc/mounts)"
if [ "$selinuxmp" ]; then
ia_add "mkdir -p $workdir$selinuxmp"
ia_add "mount -t selinuxfs none $workdir$selinuxmp"
fi
ia -c
}
# umount_recursive mountdpoint
umount_recursive()
{
local r n lazy
while :; do
r="$(mount | sed -E -n -e "s|.* on ($1/\S+) .*|\1|p" | head -1)"
[ "$r" ] && umount_recursive "$r" || break
done
# umount $1, try max n+1 times
n=3
while [ $n -gt 0 ]; do
[ $n -ge 2 ] && lazy="" || lazy="--lazy"
umount $lazy $1 >/dev/null 2>&1 # ignore errors first tries
[ "$(mount | grep " on $1 ")" ] || break
n=$((n - 1))
sleep 1
[ $n -ne 0 ] || umount $lazy $1 # show errors on last try
done
}
# unprepare_chroot <workdir> [-i]
# globals IAHEAD
unprepare_chroot()
{
local p i selinuxmp
eval "$ia_init"
p="PATH=/usr/sbin:/usr/bin:/sbin:/bin"
local workdir="$1"
ia_add "$p chroot $workdir sh -c \"[ -f /etc/mtab.bootcd ] && mv /etc/mtab.bootcd /etc/mtab || rm -f /etc/mtab\""
selinuxmp="$(sed -E -n -e "s|^selinuxfs\s+(\S+).*$|\1|p" /proc/mounts)"
if [ "$selinuxmp" ]; then
ia_add "umount_recursive $workdir$selinuxmp"
fi
for i in run dev/pts sys proc dev; do
ia_add "umount_recursive $workdir/$i"
done
ia -c
}
trapfunc()
{
ia_log "trap CMD=<$CMD>"
trap "" 0 2 # Ignore Traps
if [ "$CMD" = "iso" ]; then
cleanup_iso
elif [ "$CMD" = "disk" ]; then
info "trap ignored"
fi
}
cleanup_iso()
{
info "--- Cleanup ---"
# Be sure that VAR and CHANGES have no spaces
# to not accidently remove anything (for example VAR="/ /var")
if ! isoneword "$VAR"; then
echo "Error in VAR <$VAR>" >&2
exit 1
fi
if ! isoneword "$CHANGES"; then
echo "Error in CHANGES <$CHANGES>" >&2
exit 1
fi
rm -f "$VAR/cdimage"
rm -rf "$VAR/input_dir"
rm -rf "$VAR/compressed_dir"
rm -rf "$VAR/nocompress_dir"
umount "$VAR/mnt" 2>/dev/null
[ -d $VAR/mnt ] && rmdir "$VAR/mnt"
# do not use "rm -r $VAR"
rm -rf "$CHANGES"
[ -d $VAR -a ! -f $VAR/cdimage.iso ] && rmdir "$VAR" 2>/dev/null || :
}
split_args()
{
local i
local ret
ret=""
for i in "$@"; do
ret="$ret \"$(/bin/echo -E "$i" | ia_easy_backslash)\""
done
/bin/echo -E "$ret" | sed "s/^ //"
}
#. /usr/share/shellia/ia
#fun() { echo "fun #=$# 1=<$1> 2=<$2> 3=<$3>"; }
#eval "$ia_init"
#ia_add "fun $(split_args "$@")"
#ia
## call with sh ./bootcd-run.lib "a \" a" "b"
## to get fun #=2 1=<a " a> 2=<b> 3=<>
|