auto-format.sh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/bin/bash
  2. exec &>> /var/log/boot/autoformat.log
  3. set -ex
  4. MAGIC=${MAGIC:-"boot2docker, please format-me"}
  5. DEVS=(${AUTOFORMAT})
  6. for dev in ${DEVS[@]}; do
  7. if [ -b "${dev}" ]; then
  8. # Test for our magic string (it means that the disk was made by ./boot2docker init)
  9. HEADER=`dd if=${dev} bs=1 count=${#MAGIC} 2>/dev/null`
  10. if [ "$HEADER" = "$MAGIC" ]; then
  11. # save the preload userdata.tar file
  12. dd if=${dev} of=/userdata.tar bs=1 count=8192
  13. elif ! od -A d -N 1048576 ${dev} | head -n 3 | diff ./od-1m0 - >/dev/null 2>&1; then
  14. # do not auto-format if the disk does not begin with 1MB filled with 00
  15. continue
  16. fi
  17. if [ -e "/userdata.tar" ]; then
  18. mkfs.ext4 -L B2D_STATE ${dev}
  19. mkdir -p /mnt/new-root
  20. mount -t ext4 ${dev} /mnt/new-root
  21. pushd /mnt/new-root
  22. mkdir -p ./var/lib/rancher/conf/cloud-config.d
  23. echo $(tar -xvf /userdata.tar)
  24. AUTHORIZED_KEY1=$(cat ./.ssh/authorized_keys)
  25. AUTHORIZED_KEY2=$(cat ./.ssh/authorized_keys2)
  26. tee ./var/lib/rancher/conf/cloud-config.d/machine.yml << EOF
  27. #cloud-config
  28. rancher:
  29. network:
  30. interfaces:
  31. eth0:
  32. dhcp: true
  33. eth1:
  34. dhcp: true
  35. lo:
  36. address: 127.0.0.1/8
  37. ssh_authorized_keys:
  38. - ${AUTHORIZED_KEY1}
  39. - ${AUTHORIZED_KEY2}
  40. users:
  41. - name: docker
  42. ssh_authorized_keys:
  43. - ${AUTHORIZED_KEY1}
  44. - ${AUTHORIZED_KEY2}
  45. EOF
  46. popd
  47. umount /mnt/new-root
  48. else
  49. mkfs.ext4 -L RANCHER_STATE ${dev}
  50. fi
  51. # do not check another device
  52. break
  53. fi
  54. done