-
Notifications
You must be signed in to change notification settings - Fork 0
/
Create-EncryptedVdiFile.bash
executable file
·64 lines (48 loc) · 1.43 KB
/
Create-EncryptedVdiFile.bash
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
#!/bin/bash
# Importing function run_as_root
source RunAsRoot.bash
# Running as root
run_as_root
# Variables
vdi_file_path="$1"
vdi_file_max_size="$2"
# Installing requirements
dnf install qemu-img --assumeyes
dnf install cryptsetup --assumeyes
dnf install udisks --assumeyes
# Getting the vdi file path if it is not provided as an argument
if [ -z "$vdi_file_path" ]; then
echo "VDI file path: "
read -r vdi_file_path
fi
# Getting the vdi file size if it is not provided as an argument
if [ -z "$vdi_file_max_size" ]; then
echo "VDI file max size: "
read -r vdi_file_max_size
fi
# Checking if the vdi file already exists
if [ -f "$vdi_file_path" ]; then
echo "VDI file already exists"
exit 1
fi
# Creating VDI file
qemu-img create -f vdi "$vdi_file_path" "$vdi_file_max_size"
# Path to the destination device file where the vdi file will be mounted
destination_device_path="/dev/nbd0"
# Exiting if the vdi file is already mounted
if [ -f "$destination_device_path" ]; then
echo "VDI file is already mounted"
exit 1
fi
# Removing Module NBD
rmmod nbd
# Adding Module NBD
modprobe nbd max_part=1
# Mounting vdi file
qemu-nbd --connect "$destination_device_path" "$vdi_file_path"
# Creating encrypted device
cryptsetup luksFormat "$destination_device_path"
# Opening encrypted device
cryptsetup luksOpen "$destination_device_path" my_encrypted_volume
# Creating ext4 file system
mkfs.ext4 /dev/mapper/my_encrypted_volume