~ubuntu-branches/ubuntu/saucy/ecryptfs-utils/saucy

« back to all changes in this revision

Viewing changes to .pc/setup-swap-skip-zram.patch/src/utils/ecryptfs-setup-swap

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2012-04-18 15:52:45 UTC
  • Revision ID: package-import@ubuntu.com-20120418155245-ehdhkj9xfwehhgdt
Tags: 96-0ubuntu3
src/utils/ecryptfs-setup-swap: Skip /dev/zram* swap devices
(LP: #979350).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh -e
 
2
#    ecryptfs-setup-swap
 
3
#    Copyright (C) 2008 Canonical Ltd.
 
4
#
 
5
#    Authors: Dustin Kirkland <kirkland@ubuntu.com>
 
6
#
 
7
#    This program is free software; you can redistribute it and/or modify
 
8
#    it under the terms of the GNU General Public License as published by
 
9
#    the Free Software Foundation; version 2 of the License.
 
10
#
 
11
#    This program is distributed in the hope that it will be useful,
 
12
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
#    GNU General Public License for more details.
 
15
#
 
16
#    You should have received a copy of the GNU General Public License
 
17
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 
 
19
# The cryptswap setup used here follows a guide published at:
 
20
#  * http://ubuntumagnet.com/2007/11/creating-encrypted-swap-file-ubuntu-using-cryptsetup
 
21
 
 
22
TEXTDOMAIN="ecryptfs-utils"
 
23
 
 
24
error() {
 
25
        echo `gettext "ERROR:"` "$@" 1>&2
 
26
        exit 1
 
27
}
 
28
 
 
29
info() {
 
30
        echo `gettext "INFO:"` "$@"
 
31
}
 
32
 
 
33
warn() {
 
34
        echo `gettext "WARNING:"` "$@" 1>&2
 
35
}
 
36
 
 
37
usage() {
 
38
        echo
 
39
        echo `gettext "Usage:"`
 
40
        echo "  $0 [-f|--force] [-n|--no-reload]"
 
41
        echo
 
42
        exit 1
 
43
}
 
44
 
 
45
# Handle command line options
 
46
FORCE=0
 
47
while [ ! -z "$1" ]; do
 
48
        case "$1" in
 
49
                -f|--force)
 
50
                        FORCE=1
 
51
                        shift 1
 
52
                ;;
 
53
                -n|--no-reload)
 
54
                        NO_RELOAD=1
 
55
                        shift 1
 
56
                ;;
 
57
                *)
 
58
                        usage
 
59
                ;;
 
60
        esac
 
61
done
 
62
 
 
63
# Ensure that cryptsetup is available
 
64
[ -x /sbin/cryptsetup ] || error `gettext "Please install"` "'cryptsetup'"
 
65
 
 
66
# Ensure that we're running with root privileges
 
67
[ -w /etc/passwd ] || error `gettext "This program must be run with 'sudo', or as root"`
 
68
 
 
69
# Count swap spaces available
 
70
if [ $(grep -c "^/" /proc/swaps) -eq 0 ]; then
 
71
        mem=$(grep "^MemTotal:" /proc/meminfo | awk '{print $2}')
 
72
        swapsize=$((4*$mem))
 
73
        info "You do not currently have any swap space defined."
 
74
        echo
 
75
        echo `gettext "You can create a swap file by doing:"`
 
76
        echo " $ sudo dd if=/dev/zero of=/swapfile count=$swapsize"
 
77
        echo " $ sudo mkswap /swapfile"
 
78
        echo " $ sudo swapon /swapfile"
 
79
        echo
 
80
        echo `gettext "And then re-run"` "$0"
 
81
        echo
 
82
        exit 0
 
83
fi
 
84
 
 
85
swaps=$(grep "^/" /proc/swaps | awk '{print $1}')
 
86
 
 
87
filtered_swaps=$(
 
88
for swap in $swaps; do
 
89
        # Make sure this is swap space
 
90
        if [ "$(blkid -o value -s TYPE $swap)" != "swap" ]; then
 
91
                warn "[$swap]" `gettext "does not appear to be swap space, skipping."`
 
92
                continue
 
93
        fi
 
94
        
 
95
        if [ "${swap#/dev/ram}" != "$swap" ]; then
 
96
                warn "[$swap]" `gettext "is a RAM device, skipping."`
 
97
                continue
 
98
        fi
 
99
 
 
100
        # Check if this swap space is already setup for encryption
 
101
        if /sbin/dmsetup table "$swap" 2>/dev/null | grep -qs " crypt "; then
 
102
                warn "[$swap]" `gettext "already appears to be encrypted, skipping."`
 
103
                continue
 
104
        fi
 
105
 
 
106
        base=$(basename "$swap")
 
107
        if grep -qs "^$base.*swap.*cipher" /etc/crypttab 2>/dev/null; then
 
108
                warn "[$swap]" `gettext "already has an entry in /etc/crypttab, skipping."`
 
109
                continue
 
110
        fi
 
111
        if grep -qs "$swap" /etc/initramfs-tools/conf.d/cryptroot 2>/dev/null; then
 
112
                warn "[$swap]" `gettext "already has an entry in /etc/crypttab, skipping."`
 
113
                continue
 
114
        fi
 
115
 
 
116
        echo $swap
 
117
done
 
118
)
 
119
swaps="$filtered_swaps"
 
120
if [ -z "$swaps" ]; then
 
121
        warn "There were no usable swap devices to be encrypted.  Exiting."
 
122
        exit 0
 
123
fi
 
124
##########################################################################
 
125
# Warn the user about breaking hibernate mode
 
126
if [ "$FORCE" != 1 ]; then
 
127
        echo
 
128
        echo `gettext "WARNING:"`
 
129
        echo `gettext "An encrypted swap is required to help ensure that encrypted files are not leaked to disk in an unencrypted format."`
 
130
        echo
 
131
        echo `gettext "HOWEVER, THE SWAP ENCRYPTION CONFIGURATION PRODUCED BY THIS PROGRAM WILL BREAK HIBERNATE/RESUME ON THIS SYSTEM!"`
 
132
        echo
 
133
        echo `gettext "NOTE: Your suspend/resume capabilities will not be affected."`
 
134
        echo
 
135
        echo -n `gettext "Do you want to proceed with encrypting your swap?"` "[y/N]: "
 
136
        CONFIRM=`head -n1`
 
137
        echo
 
138
        if [ "$CONFIRM" != "y" -a "$CONFIRM" != "Y" ]; then
 
139
                echo
 
140
                info `gettext "Aborting."`
 
141
                echo
 
142
                exit 0
 
143
        fi
 
144
fi
 
145
##########################################################################
 
146
 
 
147
 
 
148
i=0
 
149
for swap in $swaps; do
 
150
        info `gettext "Setting up swap:"` "[$swap]"
 
151
        uuid=$(blkid -o value -s UUID $swap)
 
152
        for target in "UUID=$uuid" $swap; do
 
153
                if [ -n "$target" ] && grep -qs "^$target " /etc/fstab; then
 
154
                        sed -i "s:^$target :\#$target :" /etc/fstab
 
155
                        warn "Commented out your unencrypted swap from /etc/fstab"
 
156
                fi
 
157
        done
 
158
 
 
159
        while :; do
 
160
                i=$((i+1))
 
161
                [ -e "/dev/mapper/cryptswap$i" ] || break
 
162
        done
 
163
        # Add crypttab entry
 
164
        echo "cryptswap$i $swap /dev/urandom swap,cipher=aes-cbc-essiv:sha256" >> /etc/crypttab
 
165
 
 
166
        # Add fstab entry
 
167
        echo "/dev/mapper/cryptswap$i none swap sw 0 0" >> /etc/fstab
 
168
done
 
169
 
 
170
if [ "$NO_RELOAD" != 1 ]; then
 
171
        # Turn swap off
 
172
        swapoff -a
 
173
 
 
174
        # Restart cryptdisks
 
175
        /etc/init.d/cryptdisks restart
 
176
 
 
177
        # Turn the swap on
 
178
        swapon -a
 
179
fi
 
180
 
 
181
info `gettext "Successfully setup encrypted swap!"`