项目作者: juju2013

项目描述 :
Alpine linux installation on encrypted root file system for scaleway's baremetal server
高级语言: Shell
项目地址: git://github.com/juju2013/scw-alpine-encrypted.git
创建时间: 2017-09-22T15:23:10Z
项目社区:https://github.com/juju2013/scw-alpine-encrypted

开源协议:MIT License

下载


Alpine linux for scaleway

Scaleway does not provide alpine Linux images to their ARM instances, neither for cloud kvm nor bare-metal instances.

This is a shell script to deploy an alpine Linux with encrypted file-system to them.

Singularities:

  • Target ARM, both armv7 and aarch64
  • Boot over your own HTTP server
  • Encrypted root file-system with interactif passphrase prompt to unlock
  • Btrfs for fun

Prerequisites:

  • A working linux system with chroot capability
    • Same arch as target, known as build machine
  • A http server, known as boot server
  • scw-cli
  • Scaleway’s public infrastructure
  • That’s all

Usage:

On build machine

Checkout this repo:

  1. git clone https://github.com/juju2013/scw-alpine-encrypted
  2. cd scw-alpine-encrypted
  • Check build.sh and adapte it to your needs
  • Copy your own ssh public key(s), ending with .pub, to keys/
  • run ./build.sh

That’s all. In out/ folder, there’ll be 2 files:

  • ${ARCH}init.tar - your initrd. You’’ll need this one every time your target system boots
  • ${ARCH}root.tar - your root file-system, you’ll need it only for the first installation

On boot server

  • Copy those init.tar and root.tar to somewhere you can wget

On the target

  • Goto your console, select your target and add following tags:
  1. boot=rescue rescue_image=http://your_boot_server/path_to_your_init.tar

Fire up (or reset) your instance and connect to it’s console:

  1. scw attach your_instance

When the boot is finished, you’ll reach some point like that:

  1. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
  2. vda 253:0 0 46.6G 0 disk
  3. ├─vda1 253:1 0 46.5G 0 part
  4. └─vda15 253:15 0 100M 0 part
  5. mount: mounting /dev/mapper/cryptroot on /mnt failed: No such file or directory
  6. Filesystem 1K-blocks Used Available Use% Mounted on
  7. udev 1015648 0 1015648 0% /dev
  8. tmpfs 204048 10140 193908 5% /run
  9. none 1020228 202340 817888 20% /
  10. bash: cannot set terminal process group (-1): Not a tty
  11. bash: no job control in this shell
  12. bash-5.0#

From there (you’ll lose all your data on /dev/vda1), enter commands after bash-5.0#, the orther lines are command output:

  1. bash-5.0# cryptsetup luksFormat /dev/vda1
  2. WARNING: Device /dev/vda1 already contains a 'ext4' superblock signature.
  3. WARNING!
  4. ========
  5. This will overwrite data on /dev/vda1 irrevocably.
  6. Are you sure? (Type uppercase yes): YES
  7. Enter passphrase for /dev/vda1:
  8. Verify passphrase:
  9. [ 193.092325] NET: Registered protocol family 38
  10. [ 193.158943] cryptd: max_cpu_qlen set to 1000

Then

  1. cryptsetup open /dev/vda1 cryptroot
  2. Enter passphrase for /dev/vda1:
  3. bash-5.0# mkfs.btrfs /dev/mapper/cryptroot
  4. ...
  5. bash-5.0# mount /dev/mapper/cryptroot /mnt/
  6. bash-5.0# btrfs sub create /mnt/root
  7. Create subvolume '/mnt/root'
  8. bash-5.0# cd /mnt/root
  9. bash-5.0# wget -O - http://your_boot_server/aarch64root.tar | tar xpf -
  10. bash-5.0# mount --bind /dev dev
  11. bash-5.0# mount --bind /sys sys
  12. bash-5.0# mount --bind /proc proc
  13. bash-5.0# chroot . /bin/bash

At this point, you’re going to setup your alpine Linux system. Customize it as you want(most defaults should be ok):

  1. setup-alpine

After the setup:

  1. bash-5.0# exit
  2. exit
  3. bash-5.0# exit
  4. exit
  5. Now switching to real root
  6. switch_root: cannot access /sbin/init: No such file or directory
  7. switch_root: failed to execute /sbin/init: No such file or directory

The new system will panic, that’s OK, simply reboot it throu the console.

After a while, your console will show:

  1. [ 27.400885] random: crng init done
  2. Loading kernel modules ...
  3. modprobe: module dm-mod not found in modules.dep
  4. [ 48.390779] xor: measuring software checksum speed
  5. ...
  6. Starting mount real root device:
  7. WARNING: Locking directory /run/cryptsetup is missing!
  8. Enter passphrase for /dev/vda1:

Enter your passphrase, and you’ll see:

  1. [ 115.616635] NET: Registered protocol family 38
  2. [ 115.681441] cryptd: max_cpu_qlen set to 1000
  3. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
  4. vda 253:0 0 46.6G 0 disk
  5. ├─vda1 253:1 0 46.5G 0 part
  6. └─cryptroot 252:0 0 46.5G 0 crypt
  7. └─vda15 253:15 0 100M 0 part
  8. [ 116.031438] BTRFS: device fsid f2fbe983-e033-494c-83cd-72e81b519c1f devid 1 transid 16 /dev/mapper/cryptroot
  9. [ 116.041778] BTRFS info (device dm-0): disk space caching is enabled
  10. [ 116.046932] BTRFS info (device dm-0): has skinny extents
  11. Filesystem 1K-blocks Used Available Use% Mounted on
  12. udev 1015648 0 1015648 0% /dev
  13. tmpfs 204048 10140 193908 5% /run
  14. none 1020228 202340 817888 20% /
  15. /dev/mapper/cryptroot
  16. 48708300 88288 48106176 0% /mnt
  17. bash: cannot set terminal process group (-1): Not a tty
  18. bash: no job control in this shell

Type simply exit, then you’ll see:

  1. exit
  2. Now switching to real root
  3. OpenRC 0.41.2.6fc2696f3e is starting up Linux 4.19.53-mainline-rev1 (aarch64)
  4. ...

That’s all.

You can now ssh root@your_instance_ip. And next boots will repeat the same sequence, it’s done!

Caveats:

  • HTTP only, no https. The first wget is from busybox and it seams too complicate to require SSL at the very first stage. Also migitated by the third point.
  • No swap with btrfs. If you realy want one, allocate another 50GB SSD (it costs 1€/month) and make your swap partition there. You can
    allocate the remaining space to your /, after having cryptsetup it.
  • Encryption is not bullet proof. Scaleway owns your kernel and first initrd. It’s not the right way to prevent any 3 letters agencies, not even your cloud provider’s
    employees to access your data. (But it does prevent most leaks by the eventual fault of any sub-contractors).