2 LUKS_RCSID='$Revision$ $Date:: $'
4 # geninitrd mod: cryptsetup luks
5 USE_LUKS=${USE_LUKS:-yes}
7 # true if root device is crypted with cryptsetup luks
8 # and we should init cryptsetup luks at boot
11 # device to use for name for cryptsetup luks
14 # setup geninitrd module
17 cryptsetup=$(find_tool $initrd_dir/cryptsetup /sbin/cryptsetup-initrd)
19 if [ ! -x /sbin/cryptsetup ] || [ ! -x "$cryptsetup" ]; then
24 # return true if node is cryptsetup luks encrypted
25 # @param string $node device node to be examined
31 if is_no "$USE_LUKS"; then
35 if [ ! -e "$node" ]; then
36 warn "is_luks(): node $node doesn't exist!"
40 local dev dm_name=${node#/dev/mapper/}
41 if [ "$node" = "$dm_name" ]; then
42 debug "is_luks: $node is not device mapper name"
46 dev=$(/sbin/cryptsetup status $dm_name 2>/dev/null | awk '/device:/{print $2}')
48 /sbin/cryptsetup isLuks $dev
55 debug "is_luks: $node is cryptsetup luks"
57 debug "is_luks: $node is not cryptsetup luks"
62 # find modules for $devpath
63 # @param $devpath device to be examined
69 local name=${devpath#/dev/mapper/}
70 LUKSDEV=$(/sbin/cryptsetup status $name 2>/dev/null | awk '/device:/{print $2}')
71 if [ -z "$LUKSDEV" ]; then
72 die "Lost cryptsetup device meanwhile?"
75 find_module "dm-crypt"
84 find_modules_for_devpath $LUKSDEV
88 # generate initrd fragment for cryptsetup luks init
91 if ! is_yes "$have_luks"; then
96 inst_exec $cryptsetup /bin/cryptsetup
101 # TODO: 'udevadm settle' is called by lukssetup, is udev optional?
103 debug "luks: process /etc/crypttab $LUKSDEV"
104 luks_crypttab $LUKSDEV
110 [ "$1" = "/dev/urandom" -o "$1" = "/dev/hw_random" -o "$1" = "/dev/random" ]
113 # produce cryptsetup from $name from /etc/crypttab
117 # copy from /etc/rc.d/init.d/cryptsetup
118 local dst src key opt mode owner
120 while read dst src key opt; do
121 [ -z "$dst" -o "${dst#\#}" != "$dst" ] && continue
122 [ "$src" != "$LUKSDEV" ] && [ "$(readlink -f $src)" != "$LUKSDEV" ] && continue
124 if [ -n "$key" -a "x$key" != "xnone" ]; then
125 if test -e "$key" ; then
126 mode=$(LC_ALL=C ls -l "$key" | cut -c 5-10)
127 owner=$(LC_ALL=C ls -l $key | awk '{ print $3 }')
128 if [ "$mode" != "------" ] && ! key_is_random "$key"; then
129 die "INSECURE MODE FOR $key"
131 if [ "$owner" != root ]; then
132 die "INSECURE OWNER FOR $key"
135 die "Key file for $dst not found"
141 if /sbin/cryptsetup isLuks "$src" 2>/dev/null; then
142 if key_is_random "$key"; then
143 die "$dst: LUKS requires non-random key, skipping"
145 if [ -n "$opt" ]; then
146 warn "$dst: options are invalid for LUKS partitions, ignoring them"
149 keyfile=/etc/.$dst.key
153 debug "+ cryptsetup ${keyfile:+-d $keyfile} luksOpen '$src' '$dst'"
155 # cryptsetup can be called twice and in case on crypt on lvm only second
156 # will succeed because there will be no src device in first cryptsetup call
157 # this can be called multiple times, before lvm and after lvm.
159 if [ \${luksdev##/dev/disk/by-uuid/} != \${luksdev} ]; then
160 src_uuid=\${luksdev##/dev/disk/by-uuid/}
161 while read x y z name; do
162 found_uuid=\$(cryptsetup luksUUID /dev/\${name} 2>/dev/null)
163 if [ "\$found_uuid" = "\$src_uuid" ]; then
167 done < /proc/partitions
170 if [ -e "\$luksdev" ]; then
171 crypt_status=\$(cryptsetup status '$dst')
172 if [ "\${crypt_status%%is inactive.}" != "\$crypt_status" ]; then
174 cryptsetup ${keyfile:+-d $keyfile} luksOpen "\$luksdev" '$dst' <&1
181 die "$dst: only LUKS encryption supported"