libvbuilder.sh.in 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #!/bin/bash
  2. setup-vbuilder(){
  3. ## check $SUDO_USER and $USERHELPER_UID
  4. RPM_SIGN_USER=$SUDO_USER
  5. if [ -z "${RPM_SIGN_USER}" ]; then
  6. RPM_SIGN_USER=$(getent passwd $USERHELPER_UID | cut -d":" -f1)
  7. if [ -z "${RPM_SIGN_USER}" ]; then
  8. echo $"W: \$SUDO_USER and \$USERHELPER_UID are empty"
  9. return 1
  10. fi
  11. fi
  12. ## get $RPM_SIGN_USER's home directory
  13. HOME=$(getent passwd $RPM_SIGN_USER | cut -d":" -f6)
  14. ## load default settings
  15. VBUILDER_CONF=/etc/vbootstrap/vbuilder.conf
  16. if [ -r $VBUILDER_CONF ]; then
  17. . $VBUILDER_CONF
  18. [ $? -eq 1 ] && return 1
  19. fi
  20. [ -z "${DEFAULT_VERSION}" ] && \
  21. DEFAULT_VERSION=@@VBUILDER_DEFAULT_VERSION@@
  22. [ -z "${CATEGORIES}" ] && \
  23. CATEGORIES=@@VBUILDER_CATEGORIES@@
  24. [ -z "${VBOOTSTRAP_DIR}" ] && \
  25. VBOOTSTRAP_DIR=@@VBUILDER_VBOOTSTRAP_DIR@@
  26. [ -z "${UNIONFS_DIR}" ] && \
  27. UNIONFS_DIR=@@VBUILDER_UNIONFS_DIR@@
  28. [ -z "${CACHE_DIR}" ] && \
  29. CACHE_DIR=@@VBUILDER_CACHE_DIR@@
  30. [ -z "${BUILT_RPMS_DIR}" ] && \
  31. BUILT_RPMS_DIR=@@VBUILDER_BUILT_RPMS_DIR@@
  32. ## set default version for vbootstrap
  33. VERSION=$DEFAULT_VERSION
  34. ## set current stable relase version
  35. STABLE_VERSION=@@VBUILDER_STABLE_VERSION@@
  36. ## set default chroot archtecture
  37. UARCH=$(uname -i)
  38. case "${UARCH}" in
  39. arm*)
  40. UARCH="arm"
  41. ;;
  42. esac
  43. ## set boolian variables
  44. with_setup_vbootstrap=0
  45. with_dist_upgrade=0
  46. with_unionfs=0
  47. with_sign=0
  48. with_no_install=0
  49. with_login=0
  50. with_debug=0
  51. with_actions=0
  52. with_ix86_on_x86_64=0
  53. with_category_main=0
  54. with_category_plus=0
  55. with_category_nonfree=0
  56. with_category_test=0
  57. with_category_proposed_updates=0
  58. with_category_security=0
  59. return 0
  60. }
  61. setup-vbootstrap(){
  62. if [ ${with_setup_vbootstrap} -eq 0 ]; then
  63. with_setup_vbootstrap=1
  64. ## check debug mode
  65. [ ${with_debug} -eq 1 ] && \
  66. cat $VBUILDER_CONF && \
  67. set && set -x
  68. ## check some directories
  69. ## Note: create $BUILT_RPMS_DIR in RPM_Build()
  70. [ -d $VBOOTSTRAP_DIR ] || mkdir -p $VBOOTSTRAP_DIR
  71. [ -d $CACHE_DIR ] || mkdir -p $CACHE_DIR
  72. ## check chroot version
  73. case ${VERSION} in
  74. 4.2|5.2|VineSeed)
  75. ;;
  76. *)
  77. echo $"E: ${VERSION} is NOT supported"
  78. return 1
  79. ;;
  80. esac
  81. case "${VARCH}" in
  82. arm*)
  83. VARCH="arm"
  84. ;;
  85. esac
  86. ## check a chroot archtecture
  87. if [ -z ${VARCH} ]; then
  88. VARCH=${UARCH}
  89. else
  90. case "${VARCH}" in
  91. i386|x86_64)
  92. [ "${UARCH}" = "ppc" -o "${UARCH}" = "arm" ] && \
  93. echo $"E: arch ${VARCH} is NOT supported on ${UARCH}" && return 1
  94. ;;
  95. ppc)
  96. [ "${UARCH}" = "i386" -o "${UARCH}" = "x86_64" -o "${UARCH}" = "arm" ] && \
  97. echo $"E: arch ${VARCH} is NOT supported on ${UARCH}" && return 1
  98. ;;
  99. arm)
  100. [ "${UARCH}" = "i386" -o "${UARCH}" = "x86_64" -o "${UARCH}" = "ppc" ] && \
  101. echo $"E: arch ${VARCH} is NOT supported on ${UARCH}" && return 1
  102. ;;
  103. esac
  104. fi
  105. ##!! 4.2 is NO support on VARCH=x86_64 or VARCH=arch
  106. if [ "${VERSION}" = "4.2" ]; then
  107. if [ "${VARCH}" = "x86_64" -o "${VARCH}" = "arm" ]; then
  108. echo $"E: ${VERSION}_${VARCH} is NOT supported"
  109. return 1
  110. fi
  111. fi
  112. ## set the profile <version>_<arch>
  113. PROFILE=${VERSION}_${VARCH}
  114. ## check support ${PROFILE}
  115. if [ -z "$(/usr/sbin/vbootstrap | sed -e s/^Usage:.*// -e s/^E:.*// | grep -m 1 ${PROFILE})" ]; then
  116. echo $"E: ${PROFILE} is NOT supported"
  117. return 1
  118. fi
  119. ## check ${VERSION} equals VineSeed*, when with_dist_upgrade=1
  120. if [ $with_dist_upgrade -eq 1 ]; then
  121. if [ "${VERSION}" != "VineSeed" ]; then
  122. echo $"E: version ${VERSION} does not support --dist-upgrade option"
  123. return 1
  124. fi
  125. fi
  126. ## support i386 chroot on x86_64 below:
  127. [ "${UARCH}" = "x86_64" -a "${VARCH}" = "i386" ] && \
  128. with_ix86_on_x86_64=1
  129. ## check apt categories
  130. ## "main" category is unconditionally permited
  131. with_category_main=1
  132. for cat in $(echo ${CATEGORIES} | sed -e "s/,/ /"g); do
  133. case $cat in
  134. main)
  135. with_category_main=1
  136. ;;
  137. plus)
  138. with_category_plus=1
  139. ;;
  140. nonfree)
  141. with_category_nonfree=1
  142. ;;
  143. test)
  144. ## "test" category only exists in VineSeed
  145. [ "${VERSION}" = "VineSeed" ] || \
  146. echo $"E: No such category exists: $cat" && return 1
  147. with_category_test=1
  148. ;;
  149. proposed-updates)
  150. ##!! "proposed-updates" category does not exist in 4.2
  151. [ "${VERSION}" = "4.2" ] && \
  152. echo $"E: No such category exists: $cat" && return 1
  153. with_category_proposed_updates=1
  154. ;;
  155. security)
  156. ## "security" category does not exist in VineSeed
  157. [ "${VERSION}" = "VineSeed" ] && \
  158. echo $"E: No such category exists: $cat" && return 1
  159. with_category_security=1
  160. ;;
  161. *)
  162. echo $"E: No such category exists: $cat" && return 1
  163. ;;
  164. esac
  165. done
  166. ## check build target option ${TARGET}
  167. if [ ! -z "${TARGET}" ]; then
  168. RPM_TARGET_LIST="$(cat /usr/lib/rpm/rpmrc | grep arch_canon: | sed -e "s/arch_canon:[[:blank:]]*\(.*\):.*/\1/")"
  169. RPM_TARGET_LIST="${RPM_TARGET_LIST} noarch"
  170. if [ -z "$(echo ${RPM_TARGET_LIST} | grep ${TARGET})" ]; then
  171. echo $"E: rpm build target ${TARGET} is NOT supported"
  172. return 1
  173. fi
  174. fi
  175. ## set ${RPM_PKG_ARCH_LIST}
  176. RPM_PKG_ARCH_LIST="RPMS/i386 RPMS/i686 RPMS/x86_64 RPMS/ppc RPMS/noarch RPMS/armv3l RPMS/armv4l RPMS/armv4tl RPMS/armv5tejl RPMS/armv5tel RPMS/armv6l RPMS/armv7l SRPMS"
  177. [ -z "${TARGET}" ] || \
  178. RPM_PKG_ARCH_LIST="RPMS/${TARGET} ${RPM_PKG_ARCH_LIST}"
  179. fi
  180. ## set global variables
  181. BUILD_ROOT=${VBOOTSTRAP_DIR}/${PROFILE}
  182. BUILD_USER=vbuilder
  183. BUILD_DIR=/home/${BUILD_USER}/rpm
  184. UNIONFS_ROOT=${UNIONFS_DIR}/${PROFILE}
  185. ARCHIVES_DIR=${BUILD_ROOT}/var/cache/apt/archives
  186. EXTERNAL_ARCHIVES_DIR=${CACHE_DIR}/${PROFILE}/apt/archives
  187. __chroot_sh="/usr/sbin/chroot ${BUILD_ROOT} /bin/sh -c -l"
  188. mkdir -p $VBOOTSTRAP_DIR
  189. return 0
  190. }
  191. setup-vbootstrap-rpm(){
  192. setup-vbootstrap || return 1
  193. ## check ${BUILD_ROOT}
  194. [ -d ${BUILD_ROOT} ] || Build
  195. ## setarch ix86 if ix86 chroot on x86_64 host
  196. [ $with_ix86_on_x86_64 -eq 1 ] && \
  197. __chroot_sh="/usr/sbin/chroot ${BUILD_ROOT} setarch ${VARCH} /bin/sh -c -l"
  198. DIST_RELEASE=$(cat ${BUILD_ROOT}/etc/vine-release | cut -f3 -d" " | cut -f1 -d.)
  199. if [ -f $RPM_PKG ]; then
  200. BASE_RPM_PKG=$(basename $RPM_PKG)
  201. cp -f $RPM_PKG $BUILD_ROOT${BUILD_DIR}
  202. else
  203. BASE_RPM_PKG=$RPM_PKG
  204. fi
  205. return 0
  206. }
  207. ## recover apt-get data on host/chroot
  208. apt-get-update(){
  209. case $1 in
  210. --host)
  211. echo -n $"apt-get update on host ... "
  212. apt-get -qq update > /dev/null 2>&1
  213. echo $"done."
  214. ;;
  215. --chroot)
  216. echo -n $"apt-get update on chroot ... "
  217. $__chroot_sh 'apt-get -qq update' > /dev/null 2>&1
  218. echo $"done."
  219. ;;
  220. *)
  221. echo apt-get-update: unknown option $1
  222. exit 1
  223. ;;
  224. esac
  225. }
  226. ## mount-chroot {|--umount} [file system|name]
  227. ## support file systems: /home /tmp /sys /proc /dev/shm /dev/pts /dev
  228. ## support names: vfs archives_dir
  229. ## NOTE: /tmp needs for applications which use X
  230. ## vfs is virtual file systems
  231. ## archives_dir uses to mount ${EXTERNAL_ARCHIVES_DIR} to ${ARCHIVES_DIR}
  232. ## unionfs_dir covers ${BUILD_ROOT} with unionfs
  233. mount-chroot(){
  234. if [ "$1" = "--umount" ]; then
  235. mount-chroot-umount $2 || return 1
  236. else
  237. mount-chroot-mount $1 || return 1
  238. fi
  239. return 0
  240. }
  241. ## mount-chroot-umount [file system|name]
  242. mount-chroot-umount(){
  243. local fs=$1
  244. case $fs in
  245. /home|/tmp|/sys|/proc|/dev/shm|/dev/pts|/dev)
  246. [ -d ${BUILD_ROOT}${fs} ] || return 1
  247. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] || \
  248. umount ${BUILD_ROOT}${fs}
  249. if [ ! -z "$(mount | grep ${BUILD_ROOT}${fs})" ]; then
  250. echo $"Retry lazy unmount ${BUILD_ROOT}${fs} ... "
  251. umount -l ${BUILD_ROOT}${fs}
  252. echo $"done."
  253. fi
  254. ;;
  255. vfs)
  256. for dir in /sys /proc /dev/shm /dev/pts /dev; do
  257. mount-chroot-umount ${dir} || return 1
  258. done
  259. ;;
  260. archives_dir)
  261. [ -d ${ARCHIVES_DIR} ] || return 1
  262. [ -z "$(mount | grep ${ARCHIVES_DIR})" ] || \
  263. umount ${ARCHIVES_DIR}
  264. ;;
  265. unionfs_dir)
  266. [ -d ${BUILD_ROOT} ] || return 1
  267. [ -z "$(mount | grep ${BUILD_ROOT} | grep unionfs)" ] || \
  268. umount ${BUILD_ROOT}
  269. if [ ! -z "$(mount | grep ${BUILD_ROOT} | grep unionfs)" ]; then
  270. echo $"Retry lazy unmount ${BUILD_ROOT} ... "
  271. umount -l ${BUILD_ROOT}
  272. echo $"done."
  273. fi
  274. ;;
  275. *)
  276. echo mount-chroot-umount: unknown file system $fs
  277. exit 1
  278. ;;
  279. esac
  280. return 0
  281. }
  282. ## mount-chroot-mount [file system|name]
  283. mount-chroot-mount(){
  284. local fs=$1
  285. case $fs in
  286. /home)
  287. [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
  288. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
  289. mount -o _netdev,rbind ${fs} ${BUILD_ROOT}${fs}
  290. ;;
  291. /tmp|/dev)
  292. [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
  293. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
  294. mount --bind -o _netdev ${fs} ${BUILD_ROOT}${fs}
  295. ;;
  296. /sys)
  297. [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
  298. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
  299. mount -o _netdev -t sysfs vbuildersysfs ${BUILD_ROOT}${fs}
  300. ;;
  301. /proc)
  302. [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
  303. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
  304. mount -o _netdev -t proc vbuilderproc ${BUILD_ROOT}${fs}
  305. ;;
  306. /dev/shm)
  307. [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
  308. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
  309. mount -o _netdev -t tmpfs vbuildertmpfs ${BUILD_ROOT}${fs}
  310. ;;
  311. /dev/pts)
  312. [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
  313. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
  314. mount -o gid=5,mode=620,_netdev -t devpts vbuilderdevpts ${BUILD_ROOT}${fs}
  315. ;;
  316. vfs)
  317. for dir in /dev /dev/pts /dev/shm /proc /sys; do
  318. mount-chroot-mount ${dir} || return 1
  319. done
  320. ;;
  321. archives_dir)
  322. [ -d ${EXTERNAL_ARCHIVES_DIR} ] || mkdir -p ${EXTERNAL_ARCHIVES_DIR}
  323. [ -d ${ARCHIVES_DIR} ] || mkdir -p ${ARCHIVES_DIR}
  324. [ -z "$(mount | grep ${ARCHIVES_DIR})" ] && \
  325. mount --bind -o _netdev ${EXTERNAL_ARCHIVES_DIR} ${ARCHIVES_DIR}
  326. [ -d ${ARCHIVES_DIR}/partial ] || mkdir -p ${ARCHIVES_DIR}/partial
  327. ;;
  328. unionfs_dir)
  329. if [ $with_unionfs -eq 1 ]; then
  330. [ -d ${UNIONFS_ROOT} ] || mkdir -p ${UNIONFS_ROOT}
  331. [ -z "$(mount | grep ${UNIONFS_ROOT})" ] && \
  332. mount -t unionfs -o dirs=${UNIONFS_ROOT}=rw:${BUILD_ROOT}=ro unionfs ${BUILD_ROOT}
  333. unionctl ${BUILD_ROOT} --list
  334. fi
  335. ;;
  336. *)
  337. echo mount-chroot-mount: unknown file system $fs
  338. exit 1
  339. ;;
  340. esac
  341. return 0
  342. }
  343. ### end of file