forked from opoet7/usb-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_functions
More file actions
162 lines (153 loc) · 3.43 KB
/
_functions
File metadata and controls
162 lines (153 loc) · 3.43 KB
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
#!/bin/bash
#######################################
# Writes text to stderr
# Arguments:
# Log text
# Returns:
# None
#######################################
_log() {
>&2 echo "$*"
}
#######################################
# Writes error to stderr
# Will exit with code 1 or optional error code != 0
# Arguments:
# Error text
# Optional error code
# Returns:
# None
#######################################
_error() {
>&2 echo "$1"
}
#######################################
# Executes given command line
# Arguments:
# Command line
# Returns:
# Return code of execution
#######################################
_exec() {
local cmd="$*"
_log "Exec: ´$cmd´"
result=$(bash -c "$cmd" 2>&1)
resultCode="$?"
if [ "$resultCode" -ne 0 ]; then
_error "Exec failed: ´$result´"
fi
return ${resultCode}
}
#######################################
# Umount tmp mount points quietly and remove mountpoint
# Globals:
# None
# Arguments:
# tmp mount point
# Returns:
# None
#######################################
_umount() {
local mnt="$*"
sync
if [[ -n "$mnt" ]]; then
if mountpoint "$mnt" &>/dev/null; then
umount -df "$mnt" &>/dev/null
fi
fi
# needs some time before can be removed after umount
sleep 1
if [ -d "$mnt" ]; then
rm -rf "$mnt"
fi
}
#######################################
# Checks availability of cmd line tools
# Arguments:
# List of cmd line tools
# Returns:
# None
#######################################
_check_tools() {
local tools=("$@")
for tool in "${tools[@]}"; do
if ! hash "$tool" &>/dev/null; then
_error "Please install software package what provides: $tool"
return 1
fi
done
}
#######################################
# Try to find mbr.bin from syslinux package
# Arguments:
# None
# Returns:
# Path to mbr.bin
#######################################
_get_mbr_bin_path() {
local possible_locations=('/usr/share/syslinux/mbr.bin' '/usr/lib/syslinux/mbr.bin' '/usr/lib/syslinux/mbr/mbr.bin')
mbr_bin=""
for mbrBinLocation in ${possible_locations[*]}; do
if [ -f "${mbrBinLocation}" ]; then
mbr_bin="${mbrBinLocation}"
break
fi
done
if [ -z "$mbr_bin" ]; then
_error "No mbr.bin found in one of the locations ´${possible_locations[*]}´"
return 1
else
echo "$mbr_bin"
fi
}
#######################################
# Mounts given iso file to /mnt/<isofilename>
# Arguments:
# Path to ISO file
# Returns:
# Path to mount
#######################################
_mount_iso() {
local iso="$1"
if [ ! -f "$iso" ]; then
_error "File not found ´$iso´."
return 1
fi
local mnt
mnt="$(_mnt_point "$iso")"
if ! _exec "mkdir -p \"${mnt}\" && mount -t iso9660 -o loop \"$iso\" \"${mnt}\""; then
return 1
fi
echo "$mnt"
}
#######################################
#
# Arguments:
# device or file
# Returns:
# mount point
#######################################
_mnt_point() {
local file="$1"
echo "/mnt/$(basename "$file")"
}
#######################################
# Mounts given device to /mnt/<device name>
# Arguments:
# Path to device e.g. /dev/sda1
# Returns:
# Path to mount
#######################################
_mount_device() {
local device="$1"
if [ ! -b "$device" ]; then
_error "Device not found ´$device´."
return 1
fi
local mnt
mnt="$(_mnt_point "$device")"
if ! _exec "mkdir -p \"${mnt}\" && mount \"$device\" \"${mnt}\""; then
return 1
fi
echo "$mnt"
}