Welcome to mirror list, hosted at ThFree Co, Russian Federation.

format-sdb.sh « disk - github.com/nextcloud/vm.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3e826e53084b799e0868dfa5b8d2fe4ab99ceb5f (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
#!/bin/bash

# T&M Hansson IT AB © - 2022, https://www.hanssonit.se/

true
SCRIPT_NAME="Format sdb"
# shellcheck source=lib.sh
source /var/scripts/fetch_lib.sh

# Check if root
root_check

# Needs to be Ubuntu 20.04 and Multiverse
check_distro_version
check_multiverse

MOUNT_=/mnt/"$POOLNAME"

# Needed for partprobe
install_if_not parted

format() {
# umount if mounted
umount /mnt/* &> /dev/null

# mkdir if not existing
mkdir -p "$MOUNT_"

# Check what Hypervisor disks are available
if [ "$SYSVENDOR" == "VMware, Inc." ];
then
    SYSNAME="VMware"
    DEVTYPE=sdb
elif [ "$SYSVENDOR" == "Microsoft Corporation" ];
then
    SYSNAME="Hyper-V"
    DEVTYPE=sdb
elif [ "$SYSVENDOR" == "innotek GmbH" ];
then
    SYSNAME="VirtualBox"
    DEVTYPE=sdb
elif [ "$SYSVENDOR" == "Xen" ];
then
    SYSNAME="Xen/XCP-NG"
    DEVTYPE=xvdb
elif [[ "$SYSVENDOR" == "QEMU" || "$SYSVENDOR" == "Red Hat" ]];
then
    SYSNAME="KVM/QEMU"
    DEVTYPE=vdb
elif [ "$SYSVENDOR" == "DigitalOcean" ];
then
    SYSNAME="DigitalOcean"
    DEVTYPE=sda
elif home_sme_server
then
    SYSNAME="Nextcloud Home/SME Server"
    DEVTYPE=sda
elif [ "$SYSVENDOR" == "UpCloud" ];
then
    if lsblk -e7 -e11 | grep -q sd
    then
        SYSNAME="UpCloud ISCSI/IDE"
        DEVTYPE=sdb
    elif lsblk -e7 -e11 | grep -q vd
    then
        SYSNAME="UpCloud VirtiO"
        DEVTYPE=vdb
    fi
elif partprobe /dev/sdb &>/dev/null;
then
    SYSNAME="machines"
    DEVTYPE=sdb
elif partprobe /dev/nvme0n1  &>/dev/null;
then
    SYSNAME="NVMe"
    DEVTYPE=nvme0n1
else
    msg_box "It seems like you didn't add a second disk. 
To be able to put the DATA on a second drive formatted as ZFS you need to add a second disk to this server.

This script will now exit. Please add a second disk and start over."
    exit 1
fi

# Get the name of the drive
DISKTYPE=$(fdisk -l | grep $DEVTYPE | awk '{print $2}' | cut -d ":" -f1 | head -1)
if [ "$DISKTYPE" != "/dev/$DEVTYPE" ]
then
    msg_box "It seems like your $SYSNAME secondary volume (/dev/$DEVTYPE) does not exist.
This script requires that you mount a second drive to hold the data.

Please shutdown the server and mount a second drive, then start this script again.
You could also try to run this script again, but choose to manually select the disk instead.

If you want help you can buy support in our shop:
https://shop.hanssonit.se/product/premium-support-per-30-minutes/"
    exit 1
fi

# Check if ZFS utils are installed
install_if_not zfsutils-linux 

# Check still not mounted
#These functions return exit codes: 0 = found, 1 = not found
isMounted() { findmnt -rno SOURCE,TARGET "$1" >/dev/null;} #path or device
isDevMounted() { findmnt -rno SOURCE        "$1" >/dev/null;} #device only
isPathMounted() { findmnt -rno        TARGET "$1" >/dev/null;} #path   only
isDevPartOfZFS() { zpool status | grep "$1" >/dev/null;} #device member of a zpool

if isPathMounted "/mnt/ncdata";      #Spaces in path names are ok.
then
    msg_box "/mnt/ncdata is mounted and need to be unmounted before you can run this script."
    exit 1
fi

if isDevMounted "/dev/$DEVTYPE";
then
    msg_box "/dev/$DEVTYPE is mounted and need to be unmounted before you can run this script."
    exit 1
fi

# Universal:
if isMounted "/mnt/ncdata";
then
    msg_box "/mnt/ncdata is mounted and need to be unmounted before you can run this script."
    exit 1
fi

if isMounted "/dev/${DEVTYPE}1";
then
    msg_box "/dev/${DEVTYPE}1 is mounted and need to be unmounted before you can run this script."
    exit 1
fi

if isDevPartOfZFS "$DEVTYPE";
then
    msg_box "/dev/$DEVTYPE is a member of a ZFS pool and needs to be removed from any zpool before you can run this script."
    exit 1
fi

if lsblk -l -n | grep -v mmcblk | grep disk | awk '{ print $1 }' | tail -1 > /dev/null
then
    if [ -z "$PROVISIONING" ]
    then
        msg_box "Formatting your $SYSNAME secondary volume ($DISKTYPE) when you hit OK.

*** WARNING: ALL YOUR DATA WILL BE ERASED! ***"
    fi
    if zpool list | grep "$POOLNAME" > /dev/null
    then
        check_command zpool destroy "$POOLNAME"
    fi
    check_command wipefs -a -f "$DISKTYPE"
    sleep 0.5
    check_command zpool create -f -o ashift=12 "$POOLNAME" "$DISKTYPE"
    check_command zpool set failmode=continue "$POOLNAME"
    check_command zfs set mountpoint="$MOUNT_" "$POOLNAME"
    check_command zfs set compression=lz4 "$POOLNAME"
    check_command zfs set sync=standard "$POOLNAME"
    check_command zfs set xattr=sa "$POOLNAME"
    check_command zfs set primarycache=all "$POOLNAME"
    check_command zfs set atime=off "$POOLNAME"
    check_command zfs set recordsize=128k "$POOLNAME"
    check_command zfs set logbias=latency "$POOLNAME"

else
    msg_box "It seems like /dev/$DEVTYPE does not exist.
This script requires that you mount a second drive to hold the data.

Please shutdown the server and mount a second drive, then start this script again.

If you want help you can buy support in our shop:
https://shop.hanssonit.se/product/premium-support-per-30-minutes/"
    exit 1
fi
}
format

# Do a backup of the ZFS mount
if is_this_installed libzfs2linux
then
    if grep -r $POOLNAME /etc/mtab
    then
        install_if_not zfs-auto-snapshot
        sed -i "s|date --utc|date|g" /usr/sbin/zfs-auto-snapshot
    fi
fi

# Import disk by actual name
if zpool list -v | grep "$DEVTYPE"
then
    check_command partprobe -s
    zpool export $POOLNAME
    zpool import -d /dev/disk/by-id $POOLNAME
fi

# Success!
if grep "$POOLNAME" /etc/mtab
then
    if [ -z "$PROVISIONING" ]
    then
        msg_box "$MOUNT_ mounted successfully as a ZFS volume.

Automatic scrubbing is done monthly via a cronjob that you can find here:
/etc/cron.d/zfsutils-linux

Automatic snapshots are taken with 'zfs-auto-snapshot'. You can list current snapshots with:
'sudo zfs list -t snapshot'.
Manpage is here:
http://manpages.ubuntu.com/manpages/focal/man8/zfs-auto-snapshot.8.html

CURRENT STATUS:
$(zpool status $POOLNAME)

$(zpool list)"
    fi
fi