auto-format.sh 1.6 KB

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