Run sync and partprobe after wiping fs
[ta/ipa-deployer.git] / work / dib-ipa-element / virtmedia-netconf / ironic-virtmedia-netconfig / bin / erase-oldfs.sh
1 #!/bin/bash
2 # Copyright 2019 Nokia
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16
17 SYS_BLOCK="/sys/class/block"
18
19 if ( grep -q "^flags.*hypervisor" /proc/cpuinfo ); then
20     echo "Not excuting in Virtual machine"
21 else
22     function is_partition(){
23         device=$1
24         if [ -e $SYS_BLOCK/$device/partition ];then
25             return 0
26         else
27             return 1
28         fi
29     }
30
31     function is_removable(){
32         device=$1
33         sysdev=$SYS_BLOCK/$device
34         if ( is_partition $device );then
35             removable=$(readlink -f $sysdev/..)/removable
36         else
37             removable=$sysdev/removable
38         fi
39         if [ -e $removable ] && [ $(cat $removable) -eq 1 ];then
40             return 0
41         else
42             return 1
43         fi
44
45     }
46
47     function is_loop(){
48         device=$1
49         if [ -e $SYS_BLOCK/$device/loop ]; then
50             return 0
51         fi
52         return 1
53     }
54
55
56     device_list=$(ls $SYS_BLOCK)
57     read -r -a hd_devices <<< $device_list
58
59
60     for hd_dev in ${hd_devices[@]}; do
61         if [ -b /dev/$hd_dev ] && (( is_removable $hd_dev ) || ( is_partition $hd_dev ) || ( is_loop $hd_dev )); then
62             echo "Removable or partition $hd_dev. Skipping..."
63             continue
64         elif ! [ -b /dev/$hd_dev ]; then
65              continue
66         fi
67         wipefs --all /dev/$hd_dev
68         sgdisk -Z -o /dev/$hd_dev
69         dd if=/dev/zero of=/dev/$hd_dev bs=1M count=1
70     done
71     sync
72     udevadm settle
73     partprobe
74 fi