auto-format.sh 1.8 KB

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