Guest User

Untitled

a guest
Sep 26th, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # Copyright (c) 2012 Igalia, S.L.
  4. #
  5. # Author: Víctor Jáquez <vjaquez@igalia.com>
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License as
  9. # published by the Free Software Foundation; either version 2 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  20. # USA
  21. #
  22.  
  23. set -e
  24.  
  25. trap cleanup 1 2 3 6 15
  26.  
  27. WDIR="$(pwd)"
  28. PROGRAM="$(basename $0)"
  29. IMAGE="${WDIR}/rootfs.squashfs"
  30. FQDN="panda1"
  31.  
  32. usage()
  33. {
  34. cat <<EOF
  35. Usage: $PROGRAM -f <hostname> [OPTION]...
  36. Sync the squashfs image ($IMAGE)
  37. to the root filesystem in a remote system.
  38.  
  39. Options:
  40.  
  41. Required:
  42.  
  43. -f --fqdn <hostname>
  44. Hostname to be used for the target system
  45.  
  46. Additional:
  47.  
  48. -h --help
  49. Display this help
  50. -n --dry-run
  51. Test the rsync command
  52. EOF
  53. exit 0
  54. }
  55.  
  56. cleanup()
  57. {
  58. echo "Cleaning up..."
  59. umount_image
  60. rm -rf $WORKDIR
  61. exit 0
  62. }
  63.  
  64. mount_image()
  65. {
  66. if [ ! -d $MOUNTPOINT ]; then
  67. mkdir -p $MOUNTPOINT
  68. fi
  69.  
  70. mount -o ro,loop $IMAGE $MOUNTPOINT
  71. }
  72.  
  73. umount_image()
  74. {
  75. if grep -q ${MOUNTPOINT} /proc/mounts; then
  76. echo "Umounting temporary Image"
  77. umount -l $MOUNTPOINT
  78. fi
  79. }
  80.  
  81. sync_image()
  82. {
  83. RSYNCOPTS=-"auvHx --delete --super --progress -e ssh --exclude-from=${WORKDIR}/excludes.files"
  84.  
  85. EXTRAOPTS=""
  86. if [ "${DRYRUN}" ]; then
  87. EXTRAOPTS="--dry-run"
  88. fi
  89.  
  90. cd ${MOUNTPOINT}
  91. rsync ${RSYNCOPTS} ${EXTRAOPTS} ./ ${FQDN}:/
  92. cd -
  93. }
  94.  
  95. # we need root
  96. if [ $(id -u) != 0 ]; then
  97. echo "You should run $PROGRAM as root"
  98. exit 2
  99. fi
  100.  
  101. if [ ! -f $IMAGE ]; then
  102. echo "There's no image file"
  103. exit 2
  104. fi
  105.  
  106. # parse commandline options
  107. GETOPT=`getopt -o hf:n --long help,fqdn:,dry-run -n "$PROGRAM" -- "$@"`
  108. if [ $? != 0 ]; then usage; exit 1; fi
  109. eval set -- "$GETOPT"
  110. while true ; do
  111. case $1 in
  112. -h|--help)
  113. usage
  114. shift
  115. ;;
  116. -f|--fqdn)
  117. FQDN="$2"
  118. shift 2
  119. ;;
  120. -n|--dry-run)
  121. DRYRUN=1
  122. shift
  123. ;;
  124. --)
  125. shift
  126. break
  127. ;;
  128. *)
  129. echo "Internal error!";
  130. exit 1
  131. ;;
  132. esac
  133. done
  134.  
  135. WORKDIR=$(mktemp -d --tmpdir=${WDIR})
  136. MOUNTPOINT="${WORKDIR}/rootfs"
  137.  
  138. cat > ${WORKDIR}/excludes.files <<EOF
  139. /usr/local/
  140. /lib/modules/
  141. /home/
  142. /root/
  143. /etc/dropbear/
  144. /etc/init.d/*
  145. /etc/ld.so.conf.d/*
  146. /etc/network/interfaces
  147. /etc/network/if-up.d/ntpdate
  148. /etc/*-
  149. /etc/group
  150. /etc/hostname
  151. /etc/hosts
  152. /etc/inittab
  153. /etc/ld.so.cache
  154. /etc/ld.so.conf
  155. /etc/resolv.conf
  156. EOF
  157.  
  158. mount_image
  159. sync_image
  160. cleanup
Add Comment
Please, Sign In to add comment