| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
# luksformat - wrapper around LUKS-capable cryptsetup and mkfs for easy |
| 4 |
# creation of an encrypted device. |
| 5 |
# |
| 6 |
# (C) 2005 Canonical Ltd. |
| 7 |
# Author: Martin Pitt <[email protected]> |
| 8 |
# License: GNU General Public License, v2 or any later |
| 9 |
# (https://www.gnu.org/copyleft/gpl.html) |
| 10 |
|
| 11 |
use Getopt::Long qw(:config pass_through); |
| 12 |
|
| 13 |
BEGIN { |
| 14 |
eval 'use Locale::gettext'; |
| 15 |
if ($@) { |
| 16 |
*gettext = sub { shift }; |
| 17 |
*textdomain = sub { "" }; |
| 18 |
*LC_MESSAGES = sub { 5 }; |
| 19 |
} |
| 20 |
eval { |
| 21 |
require POSIX; |
| 22 |
import POSIX qw(setlocale); |
| 23 |
}; |
| 24 |
if ($@) { |
| 25 |
*setlocale = sub { return 1 }; |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
setlocale(LC_MESSAGES, ""); |
| 30 |
textdomain("luksformat"); |
| 31 |
|
| 32 |
if ($> != 0) { |
| 33 |
print STDERR gettext("This program needs to be started as root\n"); |
| 34 |
exit 1; |
| 35 |
} |
| 36 |
|
| 37 |
sub usage() { |
| 38 |
print gettext("luksformat - Create and format an encrypted LUKS device |
| 39 |
Usage: luksformat [-t <file system>] <device> [ mkfs options ]\n\n"); |
| 40 |
exit 1; |
| 41 |
} |
| 42 |
|
| 43 |
# default file system |
| 44 |
$fs = 'vfat'; |
| 45 |
exit 1 unless GetOptions ('t|type=s' => \$fs); |
| 46 |
|
| 47 |
GetOptions ('help', \$help); |
| 48 |
if (($#ARGV < 0) || ($help)) { |
| 49 |
usage(); |
| 50 |
} |
| 51 |
|
| 52 |
$device = shift(@ARGV); |
| 53 |
|
| 54 |
open(MOUNTS, "/proc/mounts"); |
| 55 |
while (<MOUNTS>) { |
| 56 |
die sprintf(gettext("Error: device mounted: %s\n"), $device) if (/\Q$device\E/) |
| 57 |
} |
| 58 |
|
| 59 |
if (-x "/usr/sbin/mkfs.$fs") { |
| 60 |
$mkfs = "/usr/sbin/mkfs.$fs"; |
| 61 |
} |
| 62 |
elsif (-x "/usr/bin/mkfs.$fs") { |
| 63 |
$mkfs = "/usr/bin/mkfs.$fs"; |
| 64 |
} |
| 65 |
elsif (-x "/sbin/mkfs.$fs") { |
| 66 |
$mkfs = "/sbin/mkfs.$fs"; |
| 67 |
} |
| 68 |
elsif (-x "/bin/mkfs.$fs") { |
| 69 |
$mkfs = "/bin/mkfs.$fs"; |
| 70 |
} |
| 71 |
else { |
| 72 |
printf STDERR (gettext("Error: invalid file system: %s\n"), $fs); |
| 73 |
exit 1; |
| 74 |
} |
| 75 |
|
| 76 |
# generate temporary mapped device name which is not yet used |
| 77 |
$name = ""; |
| 78 |
for ($i = 1; $i < 100; $i++) { |
| 79 |
if (! -e "/dev/mapper/luksformat$i") { |
| 80 |
$name = "luksformat$i"; |
| 81 |
last; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
$name or die sprintf(gettext("Error: could not generate temporary mapped device name")); |
| 86 |
|
| 87 |
# we do not need to be overly concerned with race conditions here, cryptsetup |
| 88 |
# will just fail if the name already exists now. |
| 89 |
printf (gettext("Creating encrypted device on %s...\n"), $device); |
| 90 |
if ((system 'cryptsetup', 'luksFormat', $device)) { |
| 91 |
die sprintf(gettext("Could not create LUKS device %s"), $device); |
| 92 |
} |
| 93 |
|
| 94 |
print gettext("Please enter your passphrase again to verify it\n"); |
| 95 |
if ((system 'cryptsetup', 'open', '--type', 'luks', $device, $name) != 0) { |
| 96 |
print STDERR gettext("The passphrases you entered were not identical\n"); |
| 97 |
exit 1; |
| 98 |
} |
| 99 |
|
| 100 |
$result = system $mkfs, "/dev/mapper/$name", @ARGV; |
| 101 |
print "\n"; |
| 102 |
system 'udevadm', 'settle', '--timeout=30'; |
| 103 |
system 'cryptsetup', 'luksClose', $name; |
| 104 |
|
| 105 |
die sprintf(gettext("Could not format device with file system %s"), $fs) if $result; |
| 106 |
|
| 107 |
__END__ |
| 108 |
|
| 109 |
=head1 NAME |
| 110 |
|
| 111 |
luksformat - Create and format an encrypted LUKS device |
| 112 |
|
| 113 |
=head1 SYNOPSIS |
| 114 |
|
| 115 |
B<luksformat> [B<-t> I<fstype>] I<device> [ mkfs options ] |
| 116 |
|
| 117 |
=head1 DESCRIPTION |
| 118 |
|
| 119 |
B<luksformat> is a wrapper around B<cryptsetup> and B<mkfs> which provides an |
| 120 |
easy interface for creating an encrypted device that follows the LUKS standard |
| 121 |
and for putting a file system onto the encrypted device. |
| 122 |
|
| 123 |
The default file system is B<vfat> since that is most commonly used on |
| 124 |
removable devices. However, you can specify any available file system with the |
| 125 |
B<-t> option. |
| 126 |
|
| 127 |
=head1 SEE ALSO |
| 128 |
|
| 129 |
L<cryptsetup(8)>, L<mkfs(8)> |
| 130 |
|
| 131 |
=head1 AUTHOR |
| 132 |
|
| 133 |
This program was written by Martin Pitt <[email protected]>. |