-
Notifications
You must be signed in to change notification settings - Fork 15
/
replace_zfsbootmenu.sh
executable file
·132 lines (111 loc) · 2.82 KB
/
replace_zfsbootmenu.sh
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
#!/bin/bash
#####
# Tries to find existing zfsbootmenu.EFI file and replace this with another one
# Creates a backup of the existing file
####
# @since: 2023-11-10
# @author: stev leibelt <artodeto@bazzline.net>
####
function _echo_if_be_verbose ()
{
if [[ ${BE_VERBOSE} -eq 1 ]];
then
echo "${@}"
fi
}
function _exit_if_last_exit_code_is_not_zero ()
{
local ERROR_MESSAGE
local LAST_COMMAND
local LAST_EXIT_CODE
ERROR_MESSAGE="${2:-'Something went wrong'}"
LAST_COMMAND="${@:2}"
LAST_EXIT_CODE="${1:-1}"
if [[ ${LAST_EXIT_CODE} -ne 0 ]];
then
echo ":: Error"
echo " Last exit code >>${LAST_EXIT_CODE}<<"
echo " Last command >>${LAST_COMMAND}<<"
echo " >>${ERROR_MESSAGE}<<."
exit "${LAST_EXIT_CODE}"
fi
}
function _usage ()
{
echo ":: Usage ${0} <options> -i <file>"
echo " -i <file> File path to new ZBM.EFI"
echo ""
echo "Options"
echo " -h Shows this help"
echo " -o <file> File path to existing vmlinuz.EFI"
echo " -v Enables verbose output"
echo ""
exit 0
}
function _main ()
{
local BASEPATH_OF_THIS_SCRIPT
local BE_VERBOSE
local CURRENT_DATE
local PATH_TO_NEW_ZBM
local PATH_TO_OLD_ZBM
local SHOW_USAGE
BASEPATH_OF_THIS_SCRIPT=$(cd $(dirname "${BASH_SOURCE[0]}"); pwd)
BE_VERBOSE=0
CURRENT_DATE=$(date +'%Y%m%d')
PATH_TO_NEW_ZBM="${BASEPATH_OF_THIS_SCRIPT}/vmlinuz.EFI"
PATH_TO_OLD_ZBM="/mnt/efi/EFI/ZBM/vmlinuz.EFI"
SHOW_USAGE=0
OPTARG=""
OPTIND=1
while getopts "hi:o:v" CURRENT_OPTION;
do
case ${CURRENT_OPTION} in
h)
SHOW_USAGE=1
;;
i)
PATH_TO_NEW_ZBM="${OPTARG}"
;;
o)
PATH_TO_OLD_ZBM="${OPTARG}"
;;
v)
BE_VERBOSE=1
;;
*)
echo "${CURRENT_OPTION} is invalid and not supported"
;;
esac
done
if [[ ${SHOW_USAGE} -eq 1 ]];
then
_usage
fi
if [[ ${UID} != 0 ]];
then
echo ":: Script needs to be executed as root"
exit 10
fi
if [[ "${PATH_TO_NEW_ZBM}" == "" ]];
then
echo ":: No file path to new ZBM.EFI provided"
exit 20
fi
if [[ ! -f "${PATH_TO_NEW_ZBM}" ]];
then
echo ":: Invalid input file provided"
echo " >>${PATH_TO_NEW_ZBM}<< does not exist"
exit 25
fi
if [[ -f "${PATH_TO_OLD_ZBM}" ]];
then
_echo_if_be_verbose "Renaming >>${PATH_TO_OLD_ZBM}<< to >>${PATH_TO_OLD_ZBM}.${CURRENT_DATE}<<"
mv "${PATH_TO_OLD_ZBM}" "${PATH_TO_OLD_ZBM}.${CURRENT_DATE}"
_exit_if_last_exit_code_is_not_zero ${?} "Renaming >>{$PATH_TO_OLD_ZBM}<< to >>${PATH_TO_OLD_ZBM}.${CURRENT_DATE}<< failed."
fi
_echo_if_be_verbose "Copy >>${PATH_TO_NEW_ZBM}<< to >>${PATH_TO_OLD_ZBM}<<"
cp "${PATH_TO_NEW_ZBM}" "${PATH_TO_OLD_ZBM}"
_exit_if_last_exit_code_is_not_zero ${?} "Copy >>${PATH_TO_NEW_ZBM}<< to >>${PATH_TO_OLD_ZBM}<<"
}
_main "${@}"