-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup_luks.sh
executable file
·83 lines (64 loc) · 1.74 KB
/
setup_luks.sh
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
#!/bin/bash
#This script clones the given partition on a qcow2 disk to a new encrypted partition
#on a new qcow2 disk
set -e
SRC_DEVICE=/dev/nbd0
SRC_FOLDER=$(mktemp -d)
DST_DEVICE=/dev/nbd1
DST_FOLDER=$(mktemp -d)
SRC_IMAGE=
DST_IMAGE=
HASH_TREE=hash_tree.bin
ROOT_HASH=roothash.txt
NON_INTERACTIVE=""
#Paramters for the encryption
LUKS_PARAMS="--cipher aes-xts-random --integrity hmac-sha256"
SCRIPT_PATH=$(realpath `dirname $0`)
. $SCRIPT_PATH/common.sh
trap clean_up EXIT
usage() {
echo "$0 [options]"
echo ""
echo "-in PATH.qcow2 [Mandatory] Path to unencrypted input qcow2 disk image"
echo "-out PATH.qcow2 [Optional] Path where the encrypted qcow2 disk is created. Defaults to the directory of the input file with -encrypted suffix"
echo ""
exit
}
if [ $# -eq 0 ]; then
usage
fi
while [ -n "$1" ]; do
case "$1" in
-in) SRC_IMAGE="$2"
shift
;;
-out) DST_IMAGE="$2"
shift
;;
*)
usage
;;
esac
shift
done
if [ -z "$DST_IMAGE" ]; then
FILE_NO_EXTENSION="${SRC_IMAGE%.*}"
DST_IMAGE="${FILE_NO_EXTENSION}-encrypted.qcow2"
fi
echo "Creating output image.."
create_output_image
echo "Initializing NBD module.."
initialize_nbd
echo "Finding root filesystem.."
find_root_fs_device
echo "Rootfs device selected: $SRC_ROOT_FS_DEVICE"
echo "Formatting LUKS.."
sudo cryptsetup luksFormat --type luks2 $DST_DEVICE $LUKS_PARAMS
sudo cryptsetup luksOpen $DST_DEVICE snpguard_root
echo "Creating ext4 partition and mounting.."
sudo mkfs.ext4 /dev/mapper/snpguard_root
sudo mount $SRC_ROOT_FS_DEVICE $SRC_FOLDER
sudo mount /dev/mapper/snpguard_root $DST_FOLDER
echo "Copying files (this may take some time).."
copy_filesystem
echo "Success. Your encrypted disk image is at $DST_IMAGE"