enter 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #!/bin/sh -e
  2. sinfo() {
  3. echo ${BASH:+-e} "\033[1;33;41m$@\033[0m"
  4. }
  5. # Must have root directory as an argument
  6. if [ -z "$1" ]; then
  7. echo "$0 <livecd root> [copy] [-c 'shell command']"
  8. exit 1
  9. fi
  10. # Launch in a separate mount namespace
  11. if [ unshare != "$1" ]; then
  12. exec unshare -m -- "$0" unshare "$@"
  13. else
  14. shift
  15. fi
  16. # Variables
  17. SRC=`dirname $0`/src
  18. RSYNC="rsync -rlptOHS"
  19. version=`cat ${SRC}/../conf/version`
  20. environment="LVERSION=${version} LOGNAME=root USER=root HOME=/root HOSTNAME=liberte"
  21. for env in LANG TERM RSYNC_PROXY http_proxy https_proxy ftp_proxy no_proxy; do
  22. if printenv ${env} 1>/dev/null; then
  23. environment="${environment} ${env}=`printenv ${env}`"
  24. fi
  25. done
  26. # Tweak target directory if alternative is given,
  27. # otherwise, synchronize configuration
  28. if [ "$2" = copy ]; then
  29. PHASE=$2
  30. LIVECD=$1/${PHASE}
  31. shift 2
  32. else
  33. PHASE=src
  34. LIVECD=$1/${PHASE}
  35. shift
  36. # NOTE: non-pruned files: /, /etc/portage/gnupg, /etc/privoxy, /var/lib/portage
  37. # /home/anon/persist/security/pgp/*.{gpg,crt}
  38. # Specify permissions completely, to be independent from ${SRC}/* chmods
  39. sinfo "Copying configuration files:"
  40. ${RSYNC} --chmod=u=rwX,go=rX -i \
  41. --exclude=/root/ \
  42. --exclude=/home/anon/ \
  43. --exclude=/home/nofw/ \
  44. --exclude=/var/lib/portage/ \
  45. --exclude=/usr/local/portage/ \
  46. --exclude=/etc/sudoers \
  47. --exclude=/etc/sysctl.conf \
  48. --exclude=/etc/polkit-1 \
  49. --exclude=/etc/portage/gnupg/ -C ${SRC}/ ${LIVECD}
  50. ${RSYNC} --chmod=u=rwX,go=rX -i --delete-excluded \
  51. -C --exclude=.gitignore ${SRC}/root ${LIVECD}
  52. ${RSYNC} --chmod=u=rwX,go= -i -C ${SRC}/etc/portage/gnupg ${LIVECD}/etc/portage
  53. ${RSYNC} --chmod=u=rwX,go=rX,Dg+s -i -C ${SRC}/var/lib/portage ${LIVECD}/var/lib
  54. ${RSYNC} --chmod=u=rwX,go=rX -i --delete --exclude=/portage/distfiles -C ${SRC}/usr/local/portage ${LIVECD}/usr/local
  55. if [ -e ${LIVECD}/var/log/privoxy ]; then
  56. chown -hR --from=`stat -c %u:%g ${LIVECD}/var/log/privoxy` 0:0 ${LIVECD}/etc/privoxy
  57. fi
  58. ${RSYNC} --chmod=ug=r,o= -i ${SRC}/etc/sudoers ${LIVECD}/etc
  59. ${RSYNC} --chmod=u=rw,g=r,o= -i ${SRC}/etc/sysctl.conf ${LIVECD}/etc
  60. if [ -e ${LIVECD}/etc/polkit-1/rules.d ]; then
  61. ${RSYNC} --chmod=u=rwX,go=rX -i -C \
  62. ${SRC}/etc/polkit-1/rules.d/* ${LIVECD}/etc/polkit-1/rules.d
  63. fi
  64. mkdir -p -m 755 ${LIVECD}/usr/local/addons
  65. ${RSYNC} --chmod=u=rwX,go=rX -i --delete-excluded -C ${SRC}/../conf/certs ${LIVECD}/usr/local/addons
  66. # rwX------
  67. ${RSYNC} --chmod=u=rwX,go= -i --delete \
  68. --exclude=/anon/persist/security/pgp/\*.gpg --exclude=/anon/persist/security/pgp/\*.crt \
  69. --exclude=/anon/persist/cables/queue --exclude=/anon/persist/cables/rqueue \
  70. --exclude=/anon/persist/mail/inbox -C ${SRC}/home/anon ${LIVECD}/home
  71. ${RSYNC} --chmod=ug=rwX,o=,Do+t -i --delete-excluded \
  72. -C ${SRC}/home/anon/persist/cables/queue ${SRC}/home/anon/persist/cables/rqueue \
  73. ${LIVECD}/home/anon/persist/cables
  74. ${RSYNC} --chmod=ug=rwX,o=,Do+t -i --delete-excluded \
  75. -C ${SRC}/home/anon/persist/mail/inbox ${LIVECD}/home/anon/persist/mail
  76. chown -hR --from=0:0 2101:9000 ${LIVECD}/home/anon
  77. ${RSYNC} --chmod=u=rwX,go= -i --delete-excluded -C ${SRC}/home/nofw ${LIVECD}/home
  78. chown -hR --from=0:0 2102:9001 ${LIVECD}/home/nofw
  79. if [ -e /etc/resolv.conf ]; then
  80. ${RSYNC} -L --chmod=u=rw,go=r -i /etc/resolv.conf ${LIVECD}/etc
  81. fi
  82. fi
  83. environment="${environment} PHASE=${PHASE}"
  84. setarch=`which setarch`
  85. chroot=`which chroot`
  86. tty=`tty`
  87. mounted=
  88. sinfo "Mounting system directories"
  89. [ "${tty#/dev/pts/}" = "${tty}" ] || tty=
  90. [ -L ${LIVECD}/dev/fd ] || ln -sfT /proc/self/fd ${LIVECD}/dev/fd
  91. loop=`losetup -f`
  92. for mp in /proc /dev/null /dev/random /dev/urandom /dev/tty /dev/ptmx /dev/pts /dev/shm ${loop} ${tty}; do
  93. if [ ${mp#/dev/loop} = ${mp} ]; then
  94. lmp=${LIVECD}${mp}
  95. else
  96. lmp=${LIVECD}/dev/loop
  97. fi
  98. if [ ! -e ${lmp} ]; then
  99. if [ -d ${mp} ]; then
  100. mkdir -m 755 ${lmp}
  101. elif [ -e ${mp} ]; then
  102. touch ${lmp}
  103. fi
  104. fi
  105. mount -B ${mp} ${lmp}
  106. mounted="${lmp} ${mounted}"
  107. done
  108. mount -rB -o remount ${LIVECD}/proc
  109. if [ ${PHASE} = src ]; then
  110. mkdir -p -m 755 ${LIVECD}/../copy ${LIVECD}/../dist
  111. mount -B ${LIVECD}/../copy ${LIVECD}/mnt/live
  112. mount -B ${LIVECD}/../dist ${LIVECD}/mnt/boot
  113. elif [ ${PHASE} = copy -a -d ${LIVECD}/usr/portage ]; then
  114. mount -B ${LIVECD}/../src/usr/portage ${LIVECD}/usr/portage
  115. mount -rB -o remount ${LIVECD}/usr/portage
  116. fi
  117. sinfo "Environment:"
  118. echo "${environment}" | tr ' ' '\n' | sed 's/^/ /'
  119. sinfo "Launching chrooted shell in ${LIVECD}"
  120. set +e
  121. env -i ${environment} "${setarch}" i686 "${chroot}" ${LIVECD} /bin/bash -l "$@"
  122. retval=$?
  123. if [ ${retval} != 0 ]; then
  124. sinfo "Failed."
  125. fi
  126. # Unnecessary with unshare -m, but stale processes might hold the mounts
  127. sinfo "Unmounting system directories"
  128. if [ ${PHASE} = src ]; then
  129. umount ${LIVECD}/mnt/live ${LIVECD}/mnt/boot
  130. elif [ ${PHASE} = copy -a -d ${LIVECD}/usr/portage ]; then
  131. umount ${LIVECD}/usr/portage
  132. fi
  133. umount -l ${mounted}
  134. # Condition is false for empty and for block-device ${tty} (i.e., unmount failed)
  135. [ ! -f ${LIVECD}${tty} ] || rm ${LIVECD}${tty}
  136. exit ${retval}