summaryrefslogtreecommitdiff
path: root/share/hydractl/provision
blob: 415c0c09b726ea3cae03e94c7a04e23b2d5ca6fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#!/bin/bash
#
# System installer.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with this program.  If not, see
# <http://www.gnu.org/licenses/>.

# Load.
source $APP_BASE/lib/hydra/functions || exit 1
hydra_config_load

# Create a logical volume.
function hydra_provision_lvcreate {
  local volume="$1"
  local size="$2"
  local space

  if [ -z "$volume" ] || [ "$size" == "0" ]; then
    return
  fi

  if ! lvdisplay $vg/$volume &> /dev/null; then
    echo "Creating logical volume $volume..."
    if [ "$size" == "-1" ]; then
      space="-l 100%FREE"
    else
      space="-L $size"
    fi

    # See http://forums.funtoo.org/viewtopic.php?id=1206
    #     https://bbs.archlinux.org/viewtopic.php?id=124615
    if [ "$disable_zeroing" == "y" ]; then
      hydra_sudo_run lvcreate -Z n $space -n $volume $vg
    else
      hydra_sudo_run lvcreate $space -n $volume $vg
    fi
  fi
}

# Cryptsetup wrapper.
function hydra_cryptsetup {
  if [ ! -z "$1" ] && [ -b "$1" ]; then
    hydra_sudo_run cryptsetup --cipher aes-xts-plain64:sha256 --key-size 512 --hash sha512 --iter-time 5000 --use-random -y -q luksFormat $1
  fi
}

# Create a physical volume.
function hydra_provision_create_volume {
  local volume="$1"

  if [ -z "$volume" ] || [ ! -b "/dev/mapper/$vg-$volume" ]; then
    return
  fi

  if [ "$encrypt" == "y" ]; then
    echo "Creating encrypted $volume device..."
    hydra_cryptsetup /dev/mapper/$vg-$volume
    hydra_sudo_run cryptsetup luksOpen /dev/mapper/$vg-$volume provision-$volume
    hydra_sudo_run mkfs.ext4 /dev/mapper/provision-$volume

    if [ "$volume" == "root" ]; then
      install_device="/dev/mapper/provision-root"
    fi
  else
    echo "Creating $volume device..."
    hydra_sudo_run mkfs.ext4 /dev/mapper/$vg-$volume

    if [ "$volume" == "root" ]; then
      install_device="/dev/mapper/$vg-root"
    fi
  fi
}

# Make sure there is provision config.
function hydra_provision_config {
  local base_arch="`uname -m`"

  if [ "$base_arch" == "x86_64" ]; then
    base_arch="amd64"
  else
    base_arch="i386"
  fi

  hydra_user_config   interactive       y                                "Interactive mode? (y/n)"
  hydra_user_config   device            /dev/sdb                         "Physical device(s) (more than one auto sets RAID mode)"
  hydra_user_config   swap_size         2000                             "Swap size (in MB, 0 to not create it)"
  hydra_user_config   root_size         20G                              "Size of root partition (-1 for all free space)"
  hydra_user_config   home_size         20G                              "Size of home partition (0 to not create it, -1 for all free space)"
  hydra_user_config   var_size          20G                              "Size of var partition  (0 to not create it, -1 for all free space)"
  hydra_user_config   encrypt           y                                "Encrypt volumes? (if RAID, then encryption is default) (y/n)"
  hydra_user_config   garbage           y                                "Pre-fill volumes with garbage? (y/n)"
  hydra_user_config   random_swap       y                                "Random swap? (y/n)"
  hydra_user_config   disable_zeroing   n                                "Disable zeroing of LVM volumes? (y/n)"
  hydra_user_config   hostname          machine                          "Hostname"
  hydra_user_config   domain            example.org                      "Domain"
  hydra_user_config   arch              $base_arch                       "System arch"
  hydra_user_config   version           jessie                           "Distro version"
  hydra_user_config   vg                $hostname                        "Install vg"
  hydra_user_config   grub              y                                "Setup GRUB? (y/n)"
  hydra_user_config   mirror            http://http.debian.net/debian/   "Debian mirror"

  # Check arch
  if [ "$arch" == "armel" ] || [ "$arch" == "armhf" ]; then
    echo "You probably want to run provision-raspi instead of provision"
    exit 1
  fi
}

# Load configuration
hydra_provision_config_load $1

# Parameters
WORK="/tmp/debootstrap"

# Set environment
export LC_ALL=C

# Get config parameters.
hydra_provision_config

# Check for requirements.
for req in debootstrap cryptsetup grub-pc lvm2 parted mdadm; do
  hydra_install_package $req
done

# Warning.
if [ "$interactive" == "y" ]; then
  echo "WARNING: about to create partitions on $device!"
  echo "WARNING: make sure you have backups of the important data from this device!"
  echo "Press ENTER to continue, Ctrl-C to abort."
  read answer
fi

# Check number of devices
num_devices="`echo $device | wc -w`"

# RAID: do this instead of regular partitioning.
if [ "$num_devices" != "1" ]; then
  # Force encryption and fix device name.
  encrypt="y"
  physical_devices="$device"
  device="/dev/md/$hostname"

  for dev in $physical_devices; do
    hydra_sudo_run parted -s -- $dev mklabel gpt
    hydra_sudo_run parted -s -- $dev mkpart  ext4 1M   100%
    hydra_sudo_run parted -s -- $dev set     1    raid on
  done

  mdadm --create --verbose $device --level=1 --raid-devices=$num_devices $physical_devices

  # See https://bbs.archlinux.org/viewtopic.php?id=148250
  hydra_sudo_run dd if=/dev/zero of=$device bs=1M count=32

  boot_device="$device"
  syst_device="$device"
else
  # Regular disk partitioning.
  hydra_sudo_run parted -s -- $device mklabel gpt
  hydra_sudo_run parted -s -- $device unit    MB mkpart    non-fs 2  3
  hydra_sudo_run parted -s -- $device set     1  bios_grub on

  if [ "$encrypt" == "y" ]; then
    hydra_sudo_run parted -s -- $device unit MB mkpart ext2 3 -1
    hydra_sudo_run parted -s -- $device set  2  lvm    on

    boot_device="$device"2
    syst_device="$device"2
  else
    hydra_sudo_run parted -s -- $device unit MB mkpart ext2 3   200
    hydra_sudo_run parted -s -- $device unit MB mkpart ext2 200 -1
    hydra_sudo_run parted -s -- $device set  3  lvm    on

    boot_device="$device"2
    syst_device="$device"3

    # Use absolute path
    reboot_device="`blkid | grep ^$boot_device: | cut -d ' ' -f 2 | sed -e 's/"//g'`"
  fi
fi

hydra_sudo_run parted -s -- $device set 2 boot on

# Create volumes.
echo "Creating the needed disk volumes..."

if ! pvdisplay $syst_device &> /dev/null; then
  echo "Creating physical volume..."
  hydra_sudo_run pvcreate $syst_device
fi

if ! vgdisplay $vg &> /dev/null; then
  echo "Creating volume group..."
  hydra_sudo_run vgcreate $vg $syst_device
fi

# Activate volume group
hydra_sudo_run vgchange -a y $vg

# Create swap partition
if [ "$swap_size" != "0" ]; then
  hydra_provision_lvcreate swap $swap_size
fi

# Create root partition.
hydra_provision_lvcreate root $root_size

# Create home partition.
if [ "$home_size" != "0" ]; then
  hydra_provision_lvcreate home $home_size
fi

# Create var partition.
if [ "$var_size" != "0" ]; then
  hydra_provision_lvcreate var  $var_size
fi

# Garbage.
if [ "$garbage" == "y" ]; then
  echo "Filling volumes with garbage..."

  hydra_sudo_run dd if=/dev/urandom of=/dev/mapper/$vg-root

  if [ -b "/dev/mapper/$vg-home" ]; then
    hydra_sudo_run dd if=/dev/urandom of=/dev/mapper/$vg-home
  fi

  if [ -b "/dev/mapper/$vg-var" ]; then
    hydra_sudo_run dd if=/dev/urandom of=/dev/mapper/$vg-var
  fi

  if [ "$swap_size" != "0" ]; then
    hydra_sudo_run dd if=/dev/urandom of=/dev/mapper/$vg-swap
  fi
fi

# Make sure that the mountpoint exists
hydra_sudo_run mkdir -p $WORK

# Setup mountpoint and make sure it's not mounted due to a failed install.
if cat /proc/mounts | cut -d ' ' -f 2 | grep -q "^$WORK$"; then
  hydra_sudo_run umount $WORK

  for folder in proc dev home var boot sys; do
    if cat /proc/mounts | cut -d ' ' -f 2 | grep -q "^$WORK/$folder$"; then
      hydra_sudo_run umount $WORK/$folder
    fi
  done
fi

# Create swap fs.
if [ "$swap_size" != "0" ] && [ "$random_swap" != "y" ]; then
  hydra_cryptsetup                   /dev/mapper/$vg-swap
  hydra_sudo_run cryptsetup luksOpen /dev/mapper/$vg-swap provision-swap
  hydra_sudo_run mkswap              /dev/mapper/provision-swap
fi

# Create root fs
hydra_provision_create_volume root

# Create home fs.
if [ "$home_size" != "0" ]; then
  hydra_provision_create_volume home
fi

# Create var fs.
if [ "$var_size" != "0" ]; then
  hydra_provision_create_volume var
fi

# Mount root volume.
hydra_sudo_run mount $install_device $WORK/

# Mount additional volumes.
if [ "$home_size" != "0" ]; then
  mkdir $WORK/home

  if [ "$encrypt" == "y" ]; then
    hydra_sudo_run mount /dev/mapper/provision-home $WORK/home
  else
    hydra_sudo_run mount /dev/mapper/$vg-home $WORK/home
  fi
fi

if [ "$var_size" != "0" ]; then
  hydra_sudo_run mkdir $WORK/var

  if [ "$encrypt" == "y" ]; then
    hydra_sudo_run mount /dev/mapper/provision-var $WORK/var
  else
    hydra_sudo_run mount /dev/mapper/$vg-var $WORK/var
  fi
fi

# Non-interactive installation
APT_INSTALL="hydra_sudo_run LC_ALL=C DEBIAN_FRONTEND=noninteractive chroot $WORK/ apt-get install"

# Initial system install.
echo "Installing base system..."
hydra_sudo_run LC_ALL=C DEBIAN_FRONTEND=noninteractive debootstrap --arch=$arch $version $WORK/ $mirror

# Initial configuration.
echo "Applying initial configuration..."
hydra_sudo_run mount none -t proc $WORK/proc
hydra_sudo_run mount none -t sysfs $WORK/sys
hydra_sudo_run mount -o bind /dev/ $WORK/dev
echo LANG=C | $SUDO tee $WORK/etc/default/locale > /dev/null

# Resolver configuration.
echo "domain $domain"            | $SUDO tee    $WORK/etc/resolv.conf > /dev/null
echo "search $hostname.$domain"  | $SUDO tee -a $WORK/etc/resolv.conf > /dev/null
grep nameserver /etc/resolv.conf | $SUDO tee -a $WORK/etc/resolv.conf > /dev/null

# Hostname configuration.
echo $hostname.$domain     | $SUDO tee    $WORK/etc/hostname > /dev/null
echo "127.0.0.1 localhost" | $SUDO tee -a $WORK/etc/hosts    > /dev/null

# This ordering is important for facter correctly guess the domain name
echo "127.0.0.1 $hostname.$domain $hostname" | $SUDO tee -a $WORK/etc/hosts > /dev/null

# Invert hostname contents to avoid http://projects.puppetlabs.com/issues/2533
tac $WORK/etc/hosts | $SUDO tee $WORK/etc/hosts.new > /dev/null
hydra_sudo_run mv $WORK/etc/hosts.new $WORK/etc/hosts

# Initial upgrade.
echo "Applying initial upgrades..."
hydra_sudo_run chroot $WORK/ apt-get update
hydra_sudo_run chroot $WORK/ apt-get upgrade -y
$APT_INSTALL locales cryptsetup lvm2 initramfs-tools -y

# Crypttab.
echo "Configuring crypttab..."
echo "# <target name> <source device>   <key file>  <options>" | $SUDO tee $WORK/etc/crypttab > /dev/null

if [ "$encrypt" == "y" ]; then
  cat <<-EOF | $SUDO tee $WORK/etc/crypttab > /dev/null
root            /dev/mapper/$vg-root      none            luks
EOF
fi

if [ "$home_size" != "0" ] && [ "$encrypt" == "y" ]; then
  cat <<-EOF | $SUDO tee -a $WORK/etc/crypttab > /dev/null
home            /dev/mapper/$vg-home      none            luks
EOF
fi

if [ "$var_size" != "0" ] && [ "$encrypt" == "y" ]; then
  cat <<-EOF | $SUDO tee -a $WORK/etc/crypttab > /dev/null
var             /dev/mapper/$vg-var       none            luks
EOF
fi

if [ "$swap_size" != "0" ] && [ "$random_swap" == "y" ]; then
  cat <<-EOF | $SUDO tee -a $WORK/etc/crypttab > /dev/null
swap            /dev/mapper/$vg-swap      /dev/random     swap,cipher=aes-xts-plain64:sha256
EOF
fi

if [ "$swap_size" != "0" ] && [ "$random_swap" != "y" ]; then
  cat <<-EOF | $SUDO tee -a $WORK/etc/crypttab > /dev/null
swap            /dev/mapper/$vg-swap      none        luks,swap
EOF
fi

# Fstab.
echo "Configuring fstab..."
echo "" | $SUDO tee $WORK/etc/fstab > /dev/null
if [ "$swap_size" != "0" ]; then
  cat <<-EOF | $SUDO tee -a $WORK/etc/fstab > /dev/null
/dev/mapper/swap      none           swap  sw                                 0 0
EOF
fi

if [ "$encrypt" == "y" ]; then
  cat <<-EOF | $SUDO tee -a $WORK/etc/fstab > /dev/null
/dev/mapper/root      /              ext4  defaults,errors=remount-ro         0 1
EOF
else
  cat <<-EOF | $SUDO tee -a $WORK/etc/fstab > /dev/null
/dev/mapper/$vg-root   /              ext4  defaults,errors=remount-ro         0 1
EOF
fi

if [ "$home_size" != "0" ]; then
  if [ "$encrypt" == "y" ]; then
  cat <<-EOF | $SUDO tee -a $WORK/etc/fstab > /dev/null
/dev/mapper/home      /home          ext4  defaults,errors=remount-ro         0 2
EOF
  else
  cat <<-EOF | $SUDO tee -a $WORK/etc/fstab > /dev/null
/dev/mapper/$vg-home   /home          ext4  defaults,errors=remount-ro         0 2
EOF
  fi
fi

if [ "$var_size" != "0" ]; then
  if [ "$encrypt" == "y" ]; then
  cat <<-EOF | $SUDO tee -a $WORK/etc/fstab > /dev/null
/dev/mapper/var       /var           ext4  defaults,errors=remount-ro         0 2
EOF
  else
  cat <<-EOF | $SUDO tee -a $WORK/etc/fstab > /dev/null
/dev/mapper/$vg-var    /var           ext4  defaults,errors=remount-ro         0 2
EOF
  fi
fi

# Kernel.
echo "Installing kernel..."
cat <<-EOF | $SUDO tee $WORK/etc/initramfs-tools/modules > /dev/null
dm-mod
dm-crypt
dm-raid
aes
twofish
sha256
EOF

cat <<-EOF | $SUDO tee $WORK/etc/kernel-img.conf > /dev/null
do_initrd = Yes
EOF

if [ "$arch" == "i386" ]; then
  kernel_arch="686"
else
  kernel_arch="$arch"
fi

# Kernel.
$APT_INSTALL linux-image-$kernel_arch -y

# Initramfs.
echo "Creating initramfs..."
hydra_sudo_run chroot $WORK update-initramfs -v -u

# Grub.
if [ "$grub" == "y" ]; then
  if [ "$encrypt" != "y" ]; then
    echo "Boot device setup..."
    hydra_sudo_run mkfs.ext4 $boot_device
    hydra_sudo_run mount $boot_device $WORK/boot
    echo "$reboot_device /boot ext4 defaults,errors=remount-ro 0 2" | $SUDO tee -a $WORK/etc/fstab > /dev/null
  fi

  echo "Setting up GRUB..."
  $APT_INSTALL grub-pc -y

  if [ "$encrypt" == "y" ]; then
    echo ''                                               | $SUDO tee -a $WORK/etc/default/grub > /dev/null
    echo '# Full Disk Encryption Support'                 | $SUDO tee -a $WORK/etc/default/grub > /dev/null
    echo 'GRUB_ENABLE_CRYPTODISK=y'                       | $SUDO tee -a $WORK/etc/default/grub > /dev/null
    echo 'GRUB_PRELOAD_MODULES="lvm cryptodisk mdraid1x"' | $SUDO tee -a $WORK/etc/default/grub > /dev/null
    hydra_sudo_run chroot $WORK/ update-grub
    hydra_sudo_run chroot $WORK/ grub-install --recheck --force $device

    # Fix menu entry
    hydra_sudo_run sed -i -e "s|root=/dev/mapper/provision-root|root=/dev/mapper/root|g" $WORK/boot/grub/grub.cfg
  fi
fi

# Utils.
echo "Installing basic utilities..."
$APT_INSTALL screen cron lsb-release openssl -y

# Networking.
hydra_provision_networking

# SSH.
echo "Installing OpenSSH daemon..."
$APT_INSTALL openssh-server -y
hydra_sudo_run chroot $WORK/ service ssh stop

# Sudo.
echo "Installing sudo..."
$APT_INSTALL sudo -y
echo "%sudo ALL=NOPASSWD: ALL" | $SUDO tee $WORK/etc/sudoers.d/local > /dev/null
$SUDO chmod 440 $WORK/etc/sudoers.d/local

echo "Choose a root password."
hydra_sudo_run chroot $WORK passwd root

# Fingerprints
hydra_provision_fingerprints

echo "Umounting installation device..."
hydra_sudo_run umount $WORK/sys $WORK/proc $WORK/dev $WORK
hydra_sudo_run cryptsetup luksClose provision-swap
hydra_sudo_run cryptsetup luksClose provision-root
hydra_sudo_run vgchange -a n $vg

cat <<-EOF

Now proceeed with final steps:

  - Create an user account with sudo privileges.
  - Network setup.
  - Review fstab, crypttab and optional GRUB configuration.

See https://padrao.fluxo.info/install for more information.
EOF