summarylogtreecommitdiffstats
path: root/image-umount
blob: 04159a44b9457c80e4a0ddf8eca3acaa5420012a (plain)
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
#!/bin/bash
# Simona Pisano - 2018-11-18
# simona-scripts
# Libertamente utilizzabile sotto GPL v3
#srun-umount-img() {

[[ $1 == "" ]] && exit 0 #TO-DO attivare invece la richiesta dei parametri?

EchoReset="\e[0m"
echo -e -n "$EchoReset"
OK="\E[32;40m[OK]${EchoReset}"
WARN="\E[33;40m[OK]${EchoReset}"
ERR="\E[31;40m[OK]${EchoReset}"
#echo -e "$OK $WARN $ERR"

if [[ $1 == "-h" || $1 == "--help" ]] ; then
  cat << EOF
image-umount command help

More details and supported formats are supplied inside 'image-mount -h' script.

Usage
  image-umount [--help|-h]
  image-umount reset [path] (try)
  image-umount image.ext [format] [mount-path] [--force-iso-stdmount]
    --force-iso-stdmount   -> force in iso mount 'mount -o loop' instead 'fuseiso'

Warning:
  1) format must be explicily specified if file extension do not say itself the correct format.
  2) Warning: all parameters are case sensitive.

Default parameters:
  image-umount image.raw raw 0 \$HOME/mount/image
Minimal usage:
  image-umount file.ext
Typical usage:
  image-umount file.ext 2
EOF
  exit 0
elseif [[ $1 == "--version" ]]
  echo "image-umount Version 0.0 (simonascripts package)"
  exit 0
fi

# ------------------------------------------------------------------------------------------------------------------------
# RESET ------------------------------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------------------------------

if [[ $1 == "reset" ]] ; then
  mount_where="$HOME/mount/image" #default
  [[ $2 != "" ]] && mount_where=$2
  cd "$mount_where"
  [[ $? -ne 0 ]] && echo -e "$WARN Fail 'cd \"$mount_where\"' cmd! Reset so don't Exit."
  if [[ -r /bin/qemu-nbd ]] ; then
    sudo qemu-nbd -d /dev/nbd0 >/dev/null
    [[ $? -ne 0 ]] && echo -e "$WARN Fail undo 'qemu-nbd -d /dev/nbd0' cmd!" || echo -e "$OK qemu nbd stopped... success."
  fi
  if [[ -r /bin/fuseiso ]] ; then
    for dir in "$mount_where" ; do
      fusermount -u "$dir"
      [[ $? -ne 0 ]] && echo -e "$WARN Fail undo 'fusemount -u \"$dir\"' cmd!" || echo -e "$OK fusemount success."
    done
  fi
  if [[ -r /bin/cryptsetup ]] ; then
    sudo cryptsetup close crypt-disk
    [[ $? -ne 0 ]] && echo -e "$WARN Fail 'cryptsetup close crypt-disk' cmd!" || echo -e "$OK 'cryptsetup close crypt-disk' success."
  fi
  #if [[ -r /bin/mksquash ]] ; then
    #TO-DO
  #fi

  for dir in "$mount_where" ; do
    sudo umount "$dir"
    [[ $? -ne 0 ]] && echo -e "$WARN Fail umount \"$dir\" !" || echo -e "$OK umount ok"
  done
  sudo umount "$mount_where"
  [[ $? -ne 0 ]] && echo -e "$WARN Fail umount \"$mount_where\"' ! Reset so don't Exit." || echo -e "$OK umount success."
  exit 0
  echo "All done."
fi

# ------------------------------------------------------------------------------------------------------------------------
# INPUT PARAMETER --------------------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------------------------------

allow_formats="raw qcow2 vdi blkdebug blklogwrites blkreplay blkverify bochs cloop copy-on-read dmg file ftp ftps gluster host_cdrom host_device http https iscsi iser luks nbd nfs null-aio null-co nvme parallels qcow qed quorum rbd replication sheepdog ssh throttle vhdx vmdk vpc vvfat"
allow_formats_iso="iso nrg bin img mdf"
allow_formats_crypto="tcrypt plain luks luks1 luks2 loopaes"

#├żarameter $1
full_path_image="image.raw" #default
par=$1
if [[ $1 != "" && ${par:0:1} != "-" ]] ; then
  full_path_image=$1
else
  echo "Default: 'image.raw' image file"
fi
image="${full_path_image##*/}" # estract substring: take only final string after last /
ext="${image##*.}" #estract sbustring: take only final string aster last .

#├żarameter $2
format="" #no default
par=$2
if [[ $2 != "" && ${par:0:1} != "-" ]] ; then
  format=$2
else
  [[ $ext != "" ]] && format=$ext
fi

[[ $format == "" ]] && { echo "Parameter missing. Please insert format as second command parameter."; exit 1; }

#├żarameter $3
mount_where="$HOME/mount/image" #default
[[ $3 != "" ]] && mount_where=$3

[[ ! -d "$mount_where/$image" ]] && { echo -e "$ERR Directory $mount_where/$image do not exist!! Exit."; exit 1; }

forceIsoStdMount=false #default par
while [ $# -ne 0 ] ; do
  arg="$1"
  case "$arg" in
    --force-iso-stdmount) forceIsoStdMount=true ;;
    *) ;;
  esac
  shift
done

# ------------------------------------------------------------------------------------------------------------------------
# WORK -------------------------------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------------------------------

#before exit for mounted directory if inside
if [[ $PWD == "$mount_where/$image" ]] ; then
  cd "$mount_where"
  [[ $? -ne 0 ]] && { echo -e "$ERR Fail 'cd \"$mount_where\"' cmd! Exit."; exit 1; }
fi

# vm images -----------------------------------------------------------------------------------------------------------
if [[ $allow_formats =~ $format ]] ; then

  [[ ! -r /bin/qemu-nbd ]] && { echo -e "$ERR Missing qemu-nbd cmd (qemu pkg?)! Exit"; exit 1; }

  sudo umount "$mount_where/$image"
  [[ ! -d "$mount_where/$image" ]] && echo -e "$OK umount $mount_where/$image done." || echo -e "$WARN Fail umount $mount_where/$image!!"

  sudo qemu-nbd -d /dev/nbd0 >/dev/null
  [[ $? -ne 0 ]] && { echo -e "$ERR Fail 'qemu-nbd -d /dev/nbd0' cmd! Exit."; exit 1; } || echo -e "$OK qemu nbd stopped... success."

# iso images ----------------------------------------------------------------------------------------------------------
elif [[ $allow_formats_iso =~ $format ]] ; then

  if [[ -r /bin/fuseiso && "$forceIsoStdMount" != true ]] ; then

    fusermount -u "$mount_where/$image"
    [[ $? -ne 0 ]] && { echo -e "$ERR Fail 'fusemount -u \"$mount_where/$image\"' cmd! Exit."; exit 1; }
    echo-e "$OK umount (fusermount -u) $mount_where/$image success."
  else

    sudo umount "$mount_where/$image"
    [[ $? -ne 0 ]] && { echo -e "$ERR Fail 'umount \"$mount_where/$image\"' cmd! Exit."; exit 1; }
    echo -e "$OK umount (sudo umount loop) $mount_where/$image success."

  fi

# crypto images -------------------------------------------------------------------------------------------------------
elif [[ $allow_formats_crypto =~ $format ]] ; then

  [[ ! -r /bin/cryptsetup ]] && { echo -e "$ERR Missing cryptsetup cmd (cryptsetup pkg?)! Exit."; exit 1; }

  sudo umount "$mount_where/$image"
  [[ $? -ne 0 ]] && { echo -e "$ERR Fail 'umount \"$mount_where/$image\"' cmd! Exit."; exit 1; }

  sudo cryptsetup close crypt-disk
  [[ $? -ne 0 ]] && { echo -e "$ERR Fail 'cryptsetup close crypt-disk' cmd! Exit."; exit 1; }
  echo -e "$OK Umount \"$mount_where/$image\"..."

# sqhash images -------------------------------------------------------------------------------------------------------
elif [[ "squashfs" =~ $format ]] ; then
  [[ ! -r /bin/mksquash ]] && { echo -e "$ERR Missing mksquash cmd (squash-tool pkg?)! Exit"; exit 1; }

  cd "$mount_where/$image"
  [[ $? -ne 0 ]] && { echo -e "$ERR Fail 'cd $mount_where/$image' cmd!! Exit."; exit 1; }

  sudo mksquash * $image
  [[ $? -ne 0 ]] && { echo -e "$ERR Fail 'mksquash * $image' cmd!! Exit."; exit 1; } || echo -e "$OK mksquash success."

  cp "$mount_where/$image/$image" "$full_path_image"
  if [[ $? -ne 0 ]] ; then
    echo -e "$WARN Fail copy update squashfs over original image! Check file '$mount_where/$image/$image' and remember to do 'rm -r $mount_where/$image'."
    exit 1
  else
    echo -e "$OK Created new updated squash image file sucessfully."
  fi

  rm "$mount_where/$image/$image" #erase copy of sqhash filesystem image file
  [ $? -ne 0 ] && echo -e "$WARN Fail removing image file dupe '$mount_where/$image/$image'!"

else
  [[ $format == "" ]] && detail="absent" || detail="unknown"
  echo -e "$ERR Supplied format $format ${detail}. Exit."
  exit 1
fi

for dir in $mount_where/$image/[0-9] ; do [[ -d "$dir" ]] && rmdir "$dir"; done
rmdir "$mount_where/$image"
if [[ $? -ne 0 ]] ; then
  echo -e "$ERR Fail 'rmdir $mount_where/$image' cmd! Exit."
  sudo rm -r "$mount_where/$image"
  [[ $? -ne 0 ]] && echo -e "$WARN sudo rm -r $mount_where/$image fail!" || echo -e "$OK Sudo removed dir '$mount_where/$image' success."
  exit 1
else
  echo -e "$OK Removed dir '$mount_where/$image' success."
fi

echo "All done."
exit 0