~asac/live-build/virtual-hdd-mtab-hack

« back to all changes in this revision

Viewing changes to examples/hooks/all_chroot_losetup-lukshome.sh

  • Committer: Daniel Baumann
  • Date: 2009-11-22 13:38:00 UTC
  • Revision ID: git-v1:a62f12110b19a52a58d7eae871012202cfa16055
Renaming categories to archive areas (Closes: #519690).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
 
 
3
# This hook was based and adapted from:
 
4
# http://lists.debian.org/debian-live/2009/04/msg00186.html
 
5
# ---------------------------------------------------------
 
6
#
 
7
#
 
8
# NOTE 1: this was not tested with persistent boot option,
 
9
# but it seems logic that persistent and lukshome can't
 
10
# coexist as boot options (because of snapshots and others), so lukshome
 
11
# won't be executed if any persistent option is given on boot.
 
12
#
 
13
# NOTE 2: if using an USB key, it will eventualy end up failing someday.
 
14
# You should backup the encrypted disk image file itself (luks-home.img) to
 
15
# prevent loosing your data.
 
16
#
 
17
# This hook will create 3 files:
 
18
#
 
19
# /usr/local/sbin/create-lukshome-file.sh
 
20
#       script to create an disk file image (luks-home.img) with a
 
21
#       LUKS encrypted ext2 filesystem inside to be placed in a
 
22
#       partition labeled lukshome.
 
23
#
 
24
# /usr/local/sbin/lukshome.sh
 
25
#       detects a partition with lukshome label, updates fstab and crypttab so
 
26
#       the encrypted file is mounted later in a loopback device (/dev/loopX).
 
27
#       It also changes /etc/init.d/umountfs so the encrypted home is correctly
 
28
#       umounted.
 
29
#
 
30
# /usr/share/initramfs-tools/scripts/live-bottom/13live_luks_home
 
31
#       a live-initramfs hook to execute lukshome.sh script
 
32
#       in initrd.
 
33
#
 
34
#
 
35
# HOWTO lukshome
 
36
# --------------
 
37
#
 
38
# First build your live system with this hook inside config/chroot_local-hooks/.
 
39
# If you have an existing live-helper build directory with a previous live
 
40
# build, you might have to run
 
41
#
 
42
#       lh_clean
 
43
#       lh_clean --stage
 
44
#
 
45
# to make sure this hook is included in the live system. Then (re)build your
 
46
# live system.
 
47
#
 
48
#       lh_build
 
49
#
 
50
# After booting your (re)built live system, setup the encrypted losetup
 
51
# filesystem to be used as /home using the instructions present in the
 
52
# create-lukshome-file.sh script.
 
53
#
 
54
# Reboot and now use the "lukshome" boot option to mount the encrypted /home,
 
55
# like in using "persistent" boot option with a home-rw file in some partition.
 
56
#
 
57
 
 
58
 
 
59
echo "I: to see how use lukshome hook run create-lukshome-file.sh as root."
 
60
echo "I: creating script /usr/local/sbin/create-lukshome-file.sh"
 
61
cat > /usr/local/sbin/create-lukshome-file.sh << 'EOF'
 
62
#!/bin/sh
 
63
 
 
64
# This script is to create an encrypted filesystem in a file to
 
65
# be used as /home in a live system built with Debian Live Helper with
 
66
# the lukshome hook in config/chroot_local-hooks/.
 
67
#
 
68
# The lukshome boot option will do the following:
 
69
#       - search for a partition with label 'lukshome'
 
70
#         (btw, you can't use the live system partition itself)
 
71
#       - mount the partition as /luks-home in the live system
 
72
#       - open /luks-home/luks-home.img file as a loopback device (/dev/loop)
 
73
#       - open the loopback device with cryptsetup
 
74
#       - mount the encrypted filesystem as /home
 
75
#
 
76
# This script will only create the luks-home.img file. Next are details of how
 
77
# to use this script.
 
78
#
 
79
# CAUTION! THIS CAN WIPE YOUR DATA, backup first!
 
80
# Be sure to understand what you will do, or you can end up
 
81
# wiping disks or partitions you don't want to!
 
82
#
 
83
# Login as root:
 
84
#       $ sudo -i
 
85
#
 
86
# Create a mountpoint (don't use /mnt, it will be used by this script):
 
87
#       # mkdir /media/target
 
88
#
 
89
# !!! ***  Skip the next line if you don't want to wipe a partition  *** !!!
 
90
# !!! * Just change the partition label to 'lukshome' (without quotes) * !!!
 
91
# Create an ext2 filesystem in a partition with 'lukshome' label:
 
92
#       # mkfs.ext2 -L lukshome /dev/the_partition_to_be_used
 
93
#
 
94
# Mount the partition and cd into it:
 
95
#       # mount /dev/the_partition_to_be_used /media/target
 
96
#       # cd /media/target
 
97
#
 
98
# Create the encrypted file:
 
99
#       # create-lukshome-file.sh
 
100
#
 
101
# The script is located in /usr/local/sbin/, so it's in root $PATH.
 
102
# It will copy the directories in /home/* into the file.
 
103
# Now return to $HOME to be able to umount the target partition:
 
104
#       # cd
 
105
#
 
106
# Umount the target partition:
 
107
#       # umount /media/target
 
108
#
 
109
# Reboot and use the "lukshome" boot option to mount the encrypted /home,
 
110
# like in using "persistent" boot option with a home-rw file in some partition.
 
111
#
 
112
# Press Shift-PgUp/Shift-PgDn to scrool the instructions on the screen.
 
113
 
 
114
 
 
115
# check if root/sudo
 
116
if [ "${USER}" != "root" ]
 
117
then
 
118
        echo " ** Please run this script as root or with sudo."
 
119
        exit 1
 
120
fi
 
121
 
 
122
# check if /mnt is available and empty
 
123
mount | grep "/mnt" > /dev/null
 
124
MNT_IS_MOUNTED=${?}
 
125
if [ "${MNT_IS_MOUNTED}" == 0 ]
 
126
then
 
127
        echo "** ERROR: /mnt is mounted at the moment. Please umount it to use this script."
 
128
        exit 1
 
129
fi
 
130
if [ "$(ls -A /mnt)" ]
 
131
then
 
132
        echo "** ERROR: /mnt is not empty. An empty /mnt is needed to use this script."
 
133
        exit 1
 
134
fi
 
135
 
 
136
# check if /dev/mapper/luks-home is available
 
137
if [ -f /dev/mapper/luks-home ]
 
138
then
 
139
        echo "** ERROR: /dev/mapper/luks-home is being used at the moment. Please run «cryptsetup remove luks-home» to use this script."
 
140
        exit 1
 
141
fi
 
142
 
 
143
 
 
144
# show instructions
 
145
echo ""
 
146
echo "** Instructions to use create-lukshome-file.sh (this script):"
 
147
sed -n '2,51p' /usr/local/sbin/create-lukshome-file.sh | sed 's/^.//'
 
148
echo ""
 
149
 
 
150
 
 
151
# proceed?
 
152
echo "** Do you want to proceed with this script? (y/N)"
 
153
read CONFIRM
 
154
 
 
155
case "${CONFIRM}" in
 
156
        y*|Y*)
 
157
                echo ""
 
158
        ;;
 
159
        *)
 
160
                exit 0
 
161
        ;;
 
162
esac
 
163
 
 
164
 
 
165
# create file
 
166
echo ""
 
167
echo "** Please type the size of the file disk image."
 
168
echo "Size of the file in MB: "
 
169
read FILE_SIZE
 
170
 
 
171
echo ""
 
172
echo "** Creating file luks-home.img."
 
173
echo "** Filling file image with /dev/urandom output. It will take some time."
 
174
echo "(Edit this script to use /dev/random. It's know to more secure but "
 
175
echo "it will take a *very* long time to complete."
 
176
dd if=/dev/urandom of=luks-home.img bs=1M count=${FILE_SIZE}
 
177
# To use /dev/random comment the line above and uncomment the next line
 
178
#dd if=/dev/random of=luks-home.img ibs=128 obs=128 count=$((8192*${FILE_SIZE}))
 
179
# You might have to increase kernel entropy by moving the mouse, typing keyboard,
 
180
# make the computer read disk or use network connections.
 
181
echo "** Done."
 
182
echo ""
 
183
 
 
184
# losetup
 
185
FREE_LOSETUP=$(losetup -f)
 
186
echo "** Using ${FREE_LOSETUP} to open luks-home.img"
 
187
losetup ${FREE_LOSETUP} ./luks-home.img
 
188
echo "** Done."
 
189
echo ""
 
190
 
 
191
# cryptsetup
 
192
echo "** Running cryptsetup."
 
193
echo ""
 
194
echo "** luksFormat"
 
195
cryptsetup luksFormat ${FREE_LOSETUP}
 
196
EXIT_CODE=${?}
 
197
if [ "${EXIT_CODE}" != 0 ]
 
198
then
 
199
        echo "** ERROR: Error while trying to format disk file image."
 
200
        losetup -d ${FREE_LOSETUP}
 
201
        exit 1
 
202
fi
 
203
echo ""
 
204
 
 
205
echo "** luksOpen"
 
206
cryptsetup luksOpen ${FREE_LOSETUP} luks-home
 
207
EXIT_CODE=${?}
 
208
if [ "${EXIT_CODE}" != 0 ]
 
209
then
 
210
        echo "** ERROR: Error while trying to open LUKS file image."
 
211
        losetup -d ${FREE_LOSETUP}
 
212
        exit 1
 
213
fi
 
214
echo ""
 
215
 
 
216
# format encrypted filesystem
 
217
echo "** Now formating /dev/mapper/luks-home"
 
218
mkfs.ext2 /dev/mapper/luks-home
 
219
EXIT_CODE=${?}
 
220
if [ "${EXIT_CODE}" != 0 ]
 
221
then
 
222
        echo "** ERROR: Error while trying to format LUKS file."
 
223
        cryptsetup remove luks-home
 
224
        losetup -d ${FREE_LOSETUP}
 
225
        exit 1
 
226
fi
 
227
echo ""
 
228
 
 
229
# mount in /mnt
 
230
echo "** Now mounting luks-home.img in /mnt"
 
231
mount /dev/mapper/luks-home /mnt
 
232
EXIT_CODE=${?}
 
233
if [ "${EXIT_CODE}" != 0 ]
 
234
then
 
235
        echo "** ERROR: Error while trying to mount LUKS file in /mnt."
 
236
        umount /mnt
 
237
        cryptsetup remove luks-home
 
238
        losetup -d ${FREE_LOSETUP}
 
239
        exit 1
 
240
fi
 
241
echo ""
 
242
 
 
243
# copy files
 
244
HOME_DIR="/home/*"
 
245
 
 
246
echo "** Copying ${HOME_DIR}."
 
247
cp -rav ${HOME_DIR} /mnt
 
248
EXIT_CODE=${?}
 
249
if [ "${EXIT_CODE}" != 0 ]
 
250
then
 
251
        echo "** ERROR: Error while trying to copy files to /mnt."
 
252
        umount /mnt
 
253
        cryptsetup remove luks-home
 
254
        losetup -d ${FREE_LOSETUP}
 
255
        exit 1
 
256
fi
 
257
echo "** Done."
 
258
echo ""
 
259
 
 
260
echo "** All done."
 
261
echo "** Closing losetup, cryptsetup and mounted /mnt."
 
262
# umount and close
 
263
umount /mnt
 
264
cryptsetup remove luks-home
 
265
losetup -d ${FREE_LOSETUP}
 
266
echo "** The disk file image luks-home.img is done and ready. Move it into a partition"
 
267
echo "** with 'lukshome' as label and reboot with lukshome boot option to use it."
 
268
echo ""
 
269
 
 
270
EOF
 
271
 
 
272
chmod 0755 /usr/local/sbin/create-lukshome-file.sh
 
273
 
 
274
 
 
275
 
 
276
echo "I: creating script /usr/local/sbin/lukshome.sh"
 
277
cat > /usr/local/sbin/lukshome.sh << 'EOF'
 
278
#!/bin/sh
 
279
 
 
280
# this script is to be executed by a hook in live-initramfs. It searches
 
281
# for a partition with 'lukshome' label, mounts it as /luks-home, then opens an
 
282
# encrypted disk image file called luks-home.img as a loopback device, opens it
 
283
# with cryptsetup and finally mounts the present filesystem as /home.
 
284
# It also changes /etc/init.d/umountfs to umount the lukshome partition
 
285
#  (/luks-home) and clear the loopback device on shutdown.
 
286
 
 
287
# functions taken from live-helpers
 
288
. /usr/share/initramfs-tools/scripts/live-helpers
 
289
 
 
290
# search for a partition labeled "lukshome"
 
291
for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram | grep -v fd)
 
292
do
 
293
        for dev in $(subdevices "${sysblock}")
 
294
        do
 
295
                devname=$(sys2dev "${dev}")
 
296
                # find partition name and filesystem type
 
297
                if [ "$(/lib/udev/vol_id -l ${devname} 2>/dev/null)" = "lukshome" ]
 
298
                then
 
299
                        # found one partition named "lukshome"
 
300
                        CRYPTHOME="${devname}"
 
301
                        # don't search further
 
302
                        break
 
303
                fi
 
304
        done
 
305
        # if already found, don't search further
 
306
        if [ -n "${CRYPTHOME}" ]
 
307
        then
 
308
                break
 
309
        fi
 
310
done
 
311
 
 
312
# if no partition found, exit
 
313
if [ -z "${CRYPTHOME}" ]
 
314
then
 
315
        echo "Could not find any partition with lukshome label. "
 
316
        echo "Proceeding with no encrypted /home."
 
317
        exit 0
 
318
fi
 
319
 
 
320
# mount partition where file container is
 
321
echo "Mounting /luks-home with ${CRYPTHOME}."
 
322
mkdir -p /luks-home
 
323
mount -t $(get_fstype "${CRYPTHOME}") "${CRYPTHOME}" /luks-home
 
324
 
 
325
# mount losetup encrypted file
 
326
FREE_LOOP="$(/sbin/losetup -f)"
 
327
echo "Opening /luks-home/luks-home.img in ${FREE_LOOP}."
 
328
 
 
329
if [ -f /luks-home/luks-home.img ]
 
330
then
 
331
        /sbin/losetup ${FREE_LOOP} /luks-home/luks-home.img
 
332
 
 
333
        echo "Adding ${FREE_LOOP} home to /etc/crypttab and setting it as /home in /etc/fstab."
 
334
 
 
335
        # update crypttab
 
336
        echo "home      ${FREE_LOOP}    none    luks,check,timeout" >> /etc/crypttab
 
337
 
 
338
        # update fstab
 
339
        echo "/dev/mapper/home  /home   ext2    defaults,noatime        0       0" >> /etc/fstab
 
340
else
 
341
        echo "Did not found any luks-home.img file in ${CRYPTHOME}!"
 
342
        echo "Proceeding with no encrypted /home."
 
343
        umount -r /luks-home
 
344
        exit 0
 
345
fi
 
346
 
 
347
# changes to /etc/init.d/umountfs to make /luks-home being umounted on shutdown
 
348
sed -i 's/[\t]do_stop/CHANGE_HERE/' /etc/init.d/umountfs
 
349
sed -i 's|CHANGE_HERE|  \
 
350
        # added by lukshome hook -  umount \/luks-home to prevent busy device on shutdown \
 
351
        LOOP_LUKSHOME=$(losetup -a \| grep luks-home \|cut -c 1-10) \
 
352
        if [ -n ${LOOP_LUKSHOME} ] \
 
353
        then \
 
354
                umount -r -d \/home \
 
355
                cryptsetup remove home \
 
356
                losetup -d ${LOOP_LUKSHOME} \
 
357
                umount -r \/luks-home \
 
358
        fi \
 
359
\
 
360
        do_stop \
 
361
|' /etc/init.d/umountfs
 
362
 
 
363
EOF
 
364
 
 
365
chmod 0755 /usr/local/sbin/lukshome.sh
 
366
 
 
367
 
 
368
 
 
369
# scripts/live-bottom/13live_luks_home, right after 12fstab
 
370
echo "I: creating /usr/share/initramfs-tools/scripts/live-bottom/13live_luks_home"
 
371
 
 
372
cat > /usr/share/initramfs-tools/scripts/live-bottom/13live_luks_home << 'EOF'
 
373
#!/bin/sh
 
374
 
 
375
#set -e
 
376
 
 
377
# initramfs-tools header
 
378
 
 
379
PREREQ=""
 
380
 
 
381
prereqs()
 
382
{
 
383
        echo "${PREREQ}"
 
384
}
 
385
 
 
386
case "${1}" in
 
387
        prereqs)
 
388
                prereqs
 
389
                exit 0
 
390
                ;;
 
391
esac
 
392
 
 
393
. /scripts/live-functions
 
394
 
 
395
# live-initramfs hook to use an disk image file with encrypted filesystem as /home.
 
396
 
 
397
log_begin_msg "Executing losetup-lukshome"
 
398
 
 
399
# get boot option lukshome - adapted from live-helpers
 
400
for ARGUMENT in $(cat /proc/cmdline)
 
401
do
 
402
        case "${ARGUMENT}" in
 
403
                lukshome)
 
404
                        LUKSHOME=1
 
405
                        ;;
 
406
        esac
 
407
done
 
408
 
 
409
# don't use persistent* and lukshome
 
410
if [ -n "${PERSISTENT}" ] && [  -n "${LUKSHOME}" ]
 
411
then
 
412
        echo "You should not use persistent and lukshome at the same time."
 
413
        echo "Skipping lukshome. Persistent medium, if any, will be used instead."
 
414
        log_end_msg
 
415
        exit 0
 
416
fi
 
417
 
 
418
# if no lukshome boot option, exit
 
419
if [ -z "${LUKSHOME}" ]
 
420
then
 
421
        log_end_msg
 
422
        exit 0
 
423
fi
 
424
 
 
425
log_begin_msg "Executing lukshome.sh script."
 
426
 
 
427
mount -o bind /sys /root/sys
 
428
mount -o bind /proc /root/proc
 
429
mount -o bind /dev /root/dev
 
430
 
 
431
# lukshome.sh detects lukshome partition and file location, mounts it
 
432
# and opens the file and then updates fstab and crypttab to use it as /home.
 
433
chroot /root /usr/local/sbin/lukshome.sh
 
434
 
 
435
umount /root/sys
 
436
umount /root/proc
 
437
umount /root/dev
 
438
 
 
439
# delete the lukshome scripts, not needed anymore
 
440
# rm -f /root/usr/local/sbin/lukshome.sh
 
441
# rm -f /root/usr/local/sbin/create-lukshome-file.sh
 
442
 
 
443
log_end_msg
 
444
 
 
445
EOF
 
446
 
 
447
chmod 0755 /usr/share/initramfs-tools/scripts/live-bottom/13live_luks_home
 
448
 
 
449
 
 
450
 
 
451
echo "I: update-initramfs to include 13live_luks_home."
 
452
# if you already have installed the update-initramfs.sh hook, you can remove
 
453
# this.
 
454
 
 
455
for KERNEL in /boot/vmlinuz-*
 
456
do
 
457
        VERSION="$(basename ${KERNEL} | sed -e 's|vmlinuz-||')"
 
458
 
 
459
        update-initramfs -k ${VERSION} -t -u
 
460
done