533b3e7cf938ea143e6fd9dbfe74834e7da39c3a
[stack/code/debianlive.git] / make-persistence.sh
1 #!/bin/bash
2
3 if [ $# != 2 ];then
4         echo "$0, Wrong argument number"
5         echo "    launch with $0 <devicename> <binary.img>"
6         exit 1
7 fi
8 if [ ! -f ./password ]; then
9         echo "Create a file ./password that is needed for encrypting luks persistence"
10         exit 1
11 fi
12 if [ $(id -u) != 0 ];then
13         echo "Must run as root"
14         exit 1
15 fi
16 if [ ! -x $(which parted) ];then
17         echo "parted executable must be in search path and persmission garanted"
18         exit 1
19 fi
20 if [ ! -x $(which cryptsetup) ];then
21         echo "cryptsetup executable must be in search path and persmission garanted"
22         exit 1
23 fi
24 device=$1
25
26 echo "Creating on device $device"
27 sleep 3
28
29 # Check for bad block on the device:
30 ###badblocks -c 10240 -s -w -t random -v "${devicel}" || exit 1
31
32
33 # Random data on the device:
34 ###dd if=/dev/urandom of="${devicel}" || exit 1
35
36
37 # DD THE binary.img to a usb
38 #dd if=binary.img of="${device}" || exit 1
39
40 # Make the partition
41 img_bytes=$(stat -c %s $2)
42 img_bytes=$((img_bytes+1))
43
44 parted "${device}" -- mkpart primary "${img_bytes}B" -1 || exit 1
45
46 # Ecnrypt partition
47 cryptsetup --verbose --batch-mode luksFormat "${device}2" <<<password || exit 1
48
49 # Open partition
50 cryptsetup luksOpen "${device}2" my_usb <<<password || exit 1
51
52 # Make FS with label: "persistence"
53 mkfs.ext3 -L persistence /dev/mapper/my_usb || exit 1
54
55 # Make a mount point
56 if [ ! -d "/mnt/my_usb" ];then
57         mkdir -p /mnt/my_usb || exit 1
58 fi
59
60 # Mount the partition
61 mount /dev/mapper/my_usb /mnt/my_usb/ -o noatime,nodiratime || exit 1
62
63 # Make the persistence.conf file
64 echo "/ union" > /mnt/persistence.conf  || exit 1
65
66 # Umount
67 umount /dev/mapper/my_usb || exit 1
68
69 # Close LUKS
70 cryptsetup luksClose /dev/mapper/my_usb || exit 1
71
72 echo "All done."