summaryrefslogtreecommitdiff
path: root/.local/bin/create-cryptusb
blob: 77d232e721bd560a94dec285b92b6064ad2d5e10 (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
#!/bin/sh
# Prompts for and creates a LUKS encrypted partition on a device

# Get disks connected to the system that are hotpluggable (USBs)
devices="$(lsblk -lp -o NAME,SIZE,HOTPLUG,TYPE | grep "1 disk" | awk '{print $1,"-",$2}')"
[ "$devices" = "" ] && echo "no devices available" && exit 0

# Prompt for device selection from the user
select=$(echo "$devices" | dmenu -i -p "Select a device")
[ "$select" = "" ] && echo "no device selected" && exit 0

# Get confirmation since this is a potentially dangerous operation
yn=$(echo "No\nYes" | dmenu -i -p "Create encrypted partition on $select")
[ "$yn" != "Yes" ] && exit 0

# Get device path from selection string
usb=$(echo $select | cut -d ' ' -f 1)
echo $usb

# Create LUKS partition on selected device (user will be promted for password to encrypt)
sudo cryptsetup --type luks2 luksFormat "$usb"

# Open device and create filesystem on partition
echo "creating filesystem"
map_name="crypt-create"
sudo cryptsetup open "$usb" "$map_name"
sudo mkfs.ext4 "/dev/mapper/$map_name"

# Close device after creating filesystem
sudo cryptsetup close "$map_name"

echo "done"