vbuilder.sh.in 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. #!/bin/bash
  2. # -*- coding: utf-8-unix -*-
  3. TEXTDOMAIN=vbootstrap
  4. TEXTDOMAINDIR=/usr/share/locale
  5. Usage(){
  6. cat<<EOF
  7. $(basename $0) @@VBUILDER_VERSION@@ $(echo $([ -z "@@VBUILDER_REVISION@@" ] || echo "(r@@VBUILDER_REVISION@@)"))
  8. Usage: $(basename $0) {--profile [profile]} {--version [version]} {--arch [arch]} {--category [categories]} {--fetch-url [fetch_url]} {--dist-upgrade} {--target [target]} {--no-build-essential} {--with-compat32} {--rpmbuild-define [macro_expr]} {--rpmbuild-with [bcond_with]} {--rpmbuild-without [bcond_with]} {--sign} {--no-install} {--debug} {--help} {--bootstrap-dir [directory]} {--unionfs-dir [directory]} {--cache-dir [directory]} {--built-rpms-dir [directory]} {clean|build|build-rpm [src.rpm]|install-rpm [arch.rpm|package]|remove-rpm [package]}
  9. EOF
  10. echo $"
  11. Options:
  12. --profile: set a profile
  13. --version: set [version] (default: ${VERSION})
  14. --arch: set [arch] (default: ${UARCH})
  15. --category: set [categories] (default: ${CATEGORIES})
  16. --fetch-url: set [fetch_url] to fetch packages (default: ${VBOOTSTRAP_FETCH_URL})
  17. --dist-upgrade: make VineSeed bootstrap via ${STABLE_VERSION}
  18. --unionfs: cover a bootstrap with unionfs
  19. --target: build rpms with [target]
  20. --no-build-essential: do not install build-essential
  21. --with-compat32: build rpms with compat32 on bootstrap
  22. --rpmbuild-define: give a option --define [macro_expr] to rpmbuild
  23. --rpmbuild-with: give a option --with [bcond_with] to rpmbuild
  24. --rpmbuild-without: give a option --without [bcond_with] to rpmbuild
  25. --sign: sign built rpms
  26. --no-install: build only a source rpm - do NOT install a built rpm
  27. --login: login in chroot as root user
  28. --bootstrap-dir: set a bootstrap directory (default: ${VBOOTSTRAP_DIR})
  29. --unionfs-dir: set a directory to store unionfs images of vbootstrap (default: ${UNIONFS_DIR})
  30. --cache-dir: set a directory to cache rpms (default: ${CACHE_DIR})
  31. --built-rpms-dir: set a directory to store built rpms in chroot (default: ${BUILT_RPMS_DIR})
  32. --debug: enable debug mode
  33. --help: show this help
  34. "
  35. echo $"
  36. Actions:
  37. clean: clean the bootstrap of [version]
  38. build: build a bootstrap of [version]
  39. build-rpm: build [src.rpm] on a bootstrap
  40. install-rpm: install [arch.rpm|package] on a bootstrap
  41. remove-rpm: remove [package] on a bootstrap
  42. "
  43. echo $"
  44. Examples:
  45. * make a clean/plain build environment on the current archtecture:
  46. $(basename $0) clean build
  47. * build rpms from the specified source rpm:
  48. $(basename $0) build-rpm [src.rpm]
  49. * make a plain build environment for Vine Linux 5.2:
  50. $(basename $0) --version 5.2 clean build
  51. * make a i386 chroot on x86_64:
  52. $(basename $0) --arch i386 clean build
  53. * build a kernel package with target i686:
  54. $(basename $0) --target i686 build-rpm [kernel src.rpm]
  55. * build a compat32 package:
  56. $(basename $0) --arch i386 --with-compat32 build-rpm [src.rpm]
  57. "
  58. /usr/sbin/vbootstrap | sed -e s/^Usage:.*// -e s/^E:.*//
  59. ## TODO: output supported profiles and their names/comments
  60. }
  61. ##############################################################################
  62. initialize-variables(){
  63. ## set boolian variables
  64. with_profile=0
  65. with_setup_vbootstrap=0
  66. with_dist_upgrade=0
  67. with_unionfs=0
  68. with_sign=0
  69. with_no_install=0
  70. with_login=0
  71. with_debug=0
  72. with_actions=0
  73. with_ix86_on_x86_64=0
  74. with_category_main=0
  75. with_category_plus=0
  76. with_category_nonfree=0
  77. with_category_test=0
  78. with_category_proposed_updates=0
  79. with_category_security=0
  80. with_build_essential=1
  81. return 0
  82. }
  83. check-parameter(){
  84. [ -z "$*" ] && Usage && return 1
  85. while [ ! -z "$*" ]; do
  86. case $1 in
  87. --help|help)
  88. setup-vbuilder ||:
  89. Usage
  90. return 1
  91. ;;
  92. --profile)
  93. shift
  94. with_profile=1
  95. PROFILE=$1
  96. check-next-parameter $1 || return 1
  97. ;;
  98. --version|--arch|--category|--fetch-url|--target|--rpmbuild-define|--rpmbuild-with|--rpmbuild-without|--bootstrap-dir|--unionfs-dir|--cache-dir|--built-rpms-dir)
  99. [ $with_actions -eq 1 ] && \
  100. echo $"E: You can give no more options after actions" && \
  101. return 1
  102. shift
  103. check-next-parameter $1 || return 1
  104. ;;
  105. --dist-upgrade|--unionfs|--with-compat32|--sign|--no-install|--login|--no-build-essential|--debug)
  106. [ $with_actions -eq 1 ] && \
  107. echo $"E: You can give no more options after actions" && \
  108. return 1
  109. ;;
  110. --build-rpm|build-rpm|--install-rpm|install-rpm|--remove-rpm|remove-rpm)
  111. with_actions=1
  112. shift
  113. check-next-parameter $1 || return 1
  114. ;;
  115. --build|build|--clean|clean)
  116. with_actions=1
  117. ;;
  118. *)
  119. echo $"E: Missing some parameters after $1"
  120. return 1
  121. ;;
  122. esac
  123. shift
  124. done
  125. [ $with_actions -eq 0 ] && \
  126. echo $"E: You must give at least one action" && return 1
  127. return 0
  128. }
  129. check-next-parameter(){
  130. [ -z "$1" ] && echo $"E: Missing some parameters after $1" && return 1
  131. [ $(echo $1 | grep '^-') ] && \
  132. echo $"E: Missing some parameters after $1" && return 1
  133. return 0
  134. }
  135. ## NOTE: setup-vbuilder() loads
  136. ## - system wide configurations (from /etc/vbootstrap/vbuilder.conf)
  137. ## - given profile (from /etc/vbootstrap/profile.d/*.conf)
  138. ## - given command-line parameters
  139. ## where given command-line parameters override the settings given profile,
  140. ## and the settings given profile override system wide settings.
  141. ## If you use a profile of vbuilder, you may not override the settings
  142. ## given profile.
  143. setup-vbuilder(){
  144. ## check $SUDO_USER and $USERHELPER_UID
  145. RPM_SIGN_USER=$SUDO_USER
  146. if [ -z "${RPM_SIGN_USER}" ]; then
  147. [ -z "${USERHELPER_UID}" ] && \
  148. echo $"W: \$SUDO_USER and \$USERHELPER_UID are empty" && \
  149. return 1
  150. RPM_SIGN_USER=$(getent passwd $USERHELPER_UID | cut -d":" -f1)
  151. fi
  152. ## get $RPM_SIGN_USER's home directory
  153. HOME=$(getent passwd $RPM_SIGN_USER | cut -d":" -f6)
  154. ## load default settings
  155. VBUILDER_CONF=/etc/vbootstrap/vbuilder.conf
  156. if [ -r $VBUILDER_CONF ]; then
  157. . $VBUILDER_CONF
  158. [ $? -eq 1 ] && return 1
  159. fi
  160. [ -z "${CATEGORIES}" ] && \
  161. CATEGORIES=@@VBUILDER_CATEGORIES@@
  162. [ -z "${VBOOTSTRAP_FETCH_URL}" ] && \
  163. VBOOTSTRAP_FETCH_URL=@@VBUILDER_VBOOTSTRAP_FETCH_URL@@
  164. [ -z "${VBOOTSTRAP_DIR}" ] && \
  165. VBOOTSTRAP_DIR=@@VBUILDER_VBOOTSTRAP_DIR@@
  166. [ -z "${UNIONFS_DIR}" ] && \
  167. UNIONFS_DIR=@@VBUILDER_UNIONFS_DIR@@
  168. [ -z "${CACHE_DIR}" ] && \
  169. CACHE_DIR=@@VBUILDER_CACHE_DIR@@
  170. [ -z "${BUILT_RPMS_DIR}" ] && \
  171. BUILT_RPMS_DIR=@@VBUILDER_BUILT_RPMS_DIR@@
  172. ## set default version for vbootstrap
  173. VERSION=@@VBUILDER_DEFAULT_VERSION@@
  174. ## load profile
  175. if [ ${with_profile} -eq 1 ]; then
  176. [ ! -r /etc/vbootstrap/profile.d/${PROFILE}.conf ] && \
  177. echo $"E: No such profile found: ${PROFILE}" && return 1
  178. . /etc/vbootstrap/profile.d/${PROFILE}.conf
  179. [ $? -eq 1 ] && return 1
  180. fi
  181. ## set current stable relase version
  182. STABLE_VERSION=@@VBUILDER_STABLE_VERSION@@
  183. ## set default chroot archtecture
  184. UARCH=$(uname -i)
  185. case "${UARCH}" in
  186. arm*)
  187. UARCH="arm"
  188. ;;
  189. esac
  190. return 0
  191. }
  192. setup-vbootstrap(){
  193. if [ ${with_setup_vbootstrap} -eq 0 ]; then
  194. with_setup_vbootstrap=1
  195. ## check debug mode
  196. [ ${with_debug} -eq 1 ] && \
  197. cat $VBUILDER_CONF && \
  198. set && set -x
  199. ## check some directories
  200. ## Note: create $BUILT_RPMS_DIR in RPM_Build()
  201. [ -d $VBOOTSTRAP_DIR ] || mkdir -p $VBOOTSTRAP_DIR
  202. [ -d $CACHE_DIR ] || mkdir -p $CACHE_DIR
  203. ## check chroot version
  204. case ${VERSION} in
  205. 4.2|5.2|6|VineSeed)
  206. ;;
  207. 6.?)
  208. VERSION="6"
  209. ;;
  210. *)
  211. echo $"E: ${VERSION} is NOT supported"
  212. return 1
  213. ;;
  214. esac
  215. case "${VARCH}" in
  216. arm*)
  217. VARCH="arm"
  218. ;;
  219. esac
  220. ## check a chroot archtecture
  221. if [ -z ${VARCH} ]; then
  222. VARCH=${UARCH}
  223. else
  224. case "${VARCH}" in
  225. i386|x86_64)
  226. [ "${UARCH}" = "ppc" -o "${UARCH}" = "arm" ] && \
  227. echo $"E: arch ${VARCH} is NOT supported on ${UARCH}" && return 1
  228. ;;
  229. ppc)
  230. [ "${UARCH}" = "i386" -o "${UARCH}" = "x86_64" -o "${UARCH}" = "arm" ] && \
  231. echo $"E: arch ${VARCH} is NOT supported on ${UARCH}" && return 1
  232. ;;
  233. arm)
  234. [ "${UARCH}" = "i386" -o "${UARCH}" = "x86_64" -o "${UARCH}" = "ppc" ] && \
  235. echo $"E: arch ${VARCH} is NOT supported on ${UARCH}" && return 1
  236. ;;
  237. esac
  238. fi
  239. ##!! 4.2 is NO support on VARCH=x86_64 or VARCH=arch
  240. if [ "${VERSION}" = "4.2" ]; then
  241. if [ "${VARCH}" = "x86_64" -o "${VARCH}" = "arm" ]; then
  242. echo $"E: ${VERSION}_${VARCH} is NOT supported"
  243. return 1
  244. fi
  245. fi
  246. ## set base profile <version>_<arch>
  247. BASE_PROFILE=${VERSION}_${VARCH}
  248. ## check support ${BASE_PROFILE}
  249. if [ -z "$(/usr/sbin/vbootstrap | sed -e s/^Usage:.*// -e s/^E:.*// | grep -m 1 ${BASE_PROFILE})" ]; then
  250. echo $"E: ${BASE_PROFILE} is NOT supported"
  251. return 1
  252. fi
  253. ## check ${VERSION} equals VineSeed*, when with_dist_upgrade=1
  254. if [ $with_dist_upgrade -eq 1 ]; then
  255. if [ "${VERSION}" != "VineSeed" ]; then
  256. echo $"E: version ${VERSION} does not support --dist-upgrade option"
  257. return 1
  258. fi
  259. fi
  260. ## support i386 chroot on x86_64 below:
  261. [ "${UARCH}" = "x86_64" -a "${VARCH}" = "i386" ] && \
  262. with_ix86_on_x86_64=1
  263. ## check apt categories
  264. ## "main" category is unconditionally permited
  265. with_category_main=1
  266. for cat in $(echo ${CATEGORIES} | sed -e "s/,/ /"g); do
  267. case $cat in
  268. main)
  269. with_category_main=1
  270. ;;
  271. plus)
  272. with_category_plus=1
  273. ;;
  274. nonfree)
  275. with_category_nonfree=1
  276. ;;
  277. test)
  278. ## "test" category only exists in VineSeed
  279. [ "${VERSION}" = "VineSeed" ] || \
  280. echo $"E: No such category exists: $cat" && return 1
  281. with_category_test=1
  282. ;;
  283. proposed-updates)
  284. ##!! "proposed-updates" category does not exist in 4.2
  285. [ "${VERSION}" = "4.2" ] && \
  286. echo $"E: No such category exists: $cat" && return 1
  287. with_category_proposed_updates=1
  288. ;;
  289. security)
  290. ## "security" category does not exist in VineSeed
  291. [ "${VERSION}" = "VineSeed" ] && \
  292. echo $"E: No such category exists: $cat" && return 1
  293. with_category_security=1
  294. ;;
  295. *)
  296. echo $"E: No such category exists: $cat" && return 1
  297. ;;
  298. esac
  299. done
  300. ## check build target option ${TARGET}
  301. if [ ! -z "${TARGET}" ]; then
  302. RPM_TARGET_LIST="$(cat /usr/lib/rpm/rpmrc | grep arch_canon: | sed -e "s/arch_canon:[[:blank:]]*\(.*\):.*/\1/")"
  303. RPM_TARGET_LIST="${RPM_TARGET_LIST} noarch"
  304. if [ -z "$(echo ${RPM_TARGET_LIST} | grep ${TARGET})" ]; then
  305. echo $"E: rpm build target ${TARGET} is NOT supported"
  306. return 1
  307. fi
  308. fi
  309. ## set ${RPM_PKG_ARCH_LIST}
  310. RPM_PKG_ARCH_LIST=" \
  311. RPMS/i386 RPMS/i486 RPMS/i586 RPMS/i686 RPMS/x86_64 RPMS/ppc \
  312. RPMS/noarch \
  313. RPMS/armv3l RPMS/armv4l RPMS/armv4tl \
  314. RPMS/armv5tejl RPMS/armv5tel RPMS/armv6l RPMS/armv7l \
  315. SRPMS"
  316. [ -z "${TARGET}" ] || \
  317. RPM_PKG_ARCH_LIST="RPMS/${TARGET} ${RPM_PKG_ARCH_LIST}"
  318. fi
  319. ## set global variables
  320. BUILD_USER=vbuilder
  321. BUILD_DIR=/home/${BUILD_USER}/rpm
  322. if [ ${with_profile} -eq 1 ]; then
  323. BUILD_ROOT=${VBOOTSTRAP_DIR}/${PROFILE}
  324. UNIONFS_ROOT=${UNIONFS_DIR}/${PROFILE}
  325. else
  326. BUILD_ROOT=${VBOOTSTRAP_DIR}/${BASE_PROFILE}
  327. UNIONFS_ROOT=${UNIONFS_DIR}/${BASE_PROFILE}
  328. fi
  329. ARCHIVES_DIR=${BUILD_ROOT}/var/cache/apt/archives
  330. EXTERNAL_ARCHIVES_DIR=${CACHE_DIR}/${BASE_PROFILE}/apt/archives
  331. __chroot_sh="/usr/sbin/chroot ${BUILD_ROOT} /bin/sh -c -l"
  332. mkdir -p $VBOOTSTRAP_DIR
  333. return 0
  334. }
  335. setup-vbootstrap-rpm(){
  336. setup-vbootstrap || return 1
  337. ## check ${BUILD_ROOT}
  338. [ -d ${BUILD_ROOT} ] || Build
  339. ## setarch ix86 if ix86 chroot on x86_64 host
  340. [ $with_ix86_on_x86_64 -eq 1 ] && \
  341. __chroot_sh="/usr/sbin/chroot ${BUILD_ROOT} setarch ${VARCH} /bin/sh -c -l"
  342. DIST_RELEASE=$(cat ${BUILD_ROOT}/etc/vine-release | cut -f3 -d" " | cut -f1 -d.)
  343. if [ -f $RPM_PKG ]; then
  344. BASE_RPM_PKG=$(basename $RPM_PKG)
  345. cp -f $RPM_PKG $BUILD_ROOT${BUILD_DIR}
  346. else
  347. BASE_RPM_PKG=$RPM_PKG
  348. fi
  349. return 0
  350. }
  351. ## recover apt-get data on host/chroot
  352. apt-get-update(){
  353. case $1 in
  354. --host)
  355. echo -n $"apt-get update on host ... "
  356. apt-get -qq update > /dev/null 2>&1
  357. echo $"done."
  358. ;;
  359. --chroot)
  360. echo -n $"apt-get update on chroot ... "
  361. $__chroot_sh 'apt-get -qq update' > /dev/null 2>&1
  362. echo $"done."
  363. ;;
  364. *)
  365. echo apt-get-update: unknown option $1
  366. exit 1
  367. ;;
  368. esac
  369. }
  370. ## mount-chroot {|--umount} [file system|name]
  371. ## support file systems: /home /tmp /sys /proc /dev/shm /dev/pts /dev
  372. ## support names: vfs archives_dir
  373. ## NOTE: /tmp needs for applications which use X
  374. ## vfs is virtual file systems
  375. ## archives_dir uses to mount ${EXTERNAL_ARCHIVES_DIR} to ${ARCHIVES_DIR}
  376. ## unionfs_dir covers ${BUILD_ROOT} with unionfs
  377. mount-chroot(){
  378. if [ "$1" = "--umount" ]; then
  379. mount-chroot-umount $2 || return 1
  380. else
  381. mount-chroot-mount $1 || return 1
  382. fi
  383. return 0
  384. }
  385. ## mount-chroot-umount [file system|name]
  386. mount-chroot-umount(){
  387. local fs=$1
  388. case $fs in
  389. /home|/tmp|/sys|/proc|/dev/shm|/dev/pts|/dev)
  390. [ -d ${BUILD_ROOT}${fs} ] || return 1
  391. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] || \
  392. umount ${BUILD_ROOT}${fs}
  393. if [ ! -z "$(mount | grep ${BUILD_ROOT}${fs})" ]; then
  394. echo $"Retry lazy unmount ${BUILD_ROOT}${fs} ... "
  395. umount -l ${BUILD_ROOT}${fs}
  396. echo $"done."
  397. fi
  398. ;;
  399. vfs)
  400. for dir in /sys /proc /dev/shm /dev/pts /dev; do
  401. mount-chroot-umount ${dir} || return 1
  402. done
  403. ;;
  404. archives_dir)
  405. [ -d ${ARCHIVES_DIR} ] || return 1
  406. [ -z "$(mount | grep ${ARCHIVES_DIR})" ] || \
  407. umount ${ARCHIVES_DIR}
  408. ;;
  409. unionfs_dir)
  410. [ -d ${BUILD_ROOT} ] || return 1
  411. [ -z "$(mount | grep ${BUILD_ROOT} | egrep '(unionfs|aufs)')" ] || \
  412. umount ${BUILD_ROOT}
  413. if [ ! -z "$(mount | grep ${BUILD_ROOT} | egrep '(unionfs|aufs)')" ]; then
  414. echo $"Retry lazy unmount ${BUILD_ROOT} ... "
  415. umount -l ${BUILD_ROOT}
  416. echo $"done."
  417. fi
  418. ;;
  419. *)
  420. echo mount-chroot-umount: unknown file system $fs
  421. exit 1
  422. ;;
  423. esac
  424. return 0
  425. }
  426. ## mount-chroot-mount [file system|name]
  427. mount-chroot-mount(){
  428. local fs=$1
  429. case $fs in
  430. /home)
  431. [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
  432. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
  433. mount -o _netdev,rbind ${fs} ${BUILD_ROOT}${fs}
  434. ;;
  435. /tmp|/dev)
  436. [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
  437. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
  438. mount --bind -o _netdev ${fs} ${BUILD_ROOT}${fs}
  439. ;;
  440. /sys)
  441. [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
  442. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
  443. mount -o _netdev -t sysfs vbuildersysfs ${BUILD_ROOT}${fs}
  444. ;;
  445. /proc)
  446. [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
  447. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
  448. mount -o _netdev -t proc vbuilderproc ${BUILD_ROOT}${fs}
  449. ;;
  450. /dev/shm)
  451. [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
  452. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
  453. mount -o _netdev -t tmpfs vbuildertmpfs ${BUILD_ROOT}${fs}
  454. ;;
  455. /dev/pts)
  456. [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
  457. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
  458. mount -o gid=5,mode=620,_netdev -t devpts vbuilderdevpts ${BUILD_ROOT}${fs}
  459. ;;
  460. vfs)
  461. for dir in /dev /dev/pts /dev/shm /proc /sys; do
  462. mount-chroot-mount ${dir} || return 1
  463. done
  464. ;;
  465. archives_dir)
  466. [ -d ${EXTERNAL_ARCHIVES_DIR} ] || mkdir -p ${EXTERNAL_ARCHIVES_DIR}
  467. [ -d ${ARCHIVES_DIR} ] || mkdir -p ${ARCHIVES_DIR}
  468. [ -z "$(mount | grep ${ARCHIVES_DIR})" ] && \
  469. mount --bind -o _netdev ${EXTERNAL_ARCHIVES_DIR} ${ARCHIVES_DIR}
  470. [ -d ${ARCHIVES_DIR}/partial ] || mkdir -p ${ARCHIVES_DIR}/partial
  471. ;;
  472. unionfs_dir)
  473. if [ $with_unionfs -eq 1 ]; then
  474. [ -d ${UNIONFS_ROOT} ] || mkdir -p ${UNIONFS_ROOT}
  475. /sbin/modprobe aufs >& /dev/null ||:
  476. if ( grep -q aufs /proc/filesystems ) ; then
  477. [ -z "$(mount | grep ${UNIONFS_ROOT})" ] && \
  478. mount -t aufs -o br=${UNIONFS_ROOT}=rw:${BUILD_ROOT}=ro aufs ${BUILD_ROOT}
  479. else
  480. [ -z "$(mount | grep ${UNIONFS_ROOT})" ] && \
  481. mount -t unionfs -o dirs=${UNIONFS_ROOT}=rw:${BUILD_ROOT}=ro unionfs ${BUILD_ROOT}
  482. unionctl ${BUILD_ROOT} --list
  483. fi
  484. fi
  485. ;;
  486. *)
  487. echo mount-chroot-mount: unknown file system $fs
  488. exit 1
  489. ;;
  490. esac
  491. return 0
  492. }
  493. ## TODO: support spec-validator
  494. ## spec-validator [spec file]
  495. spec-validator(){
  496. echo
  497. return 0
  498. }
  499. ##############################################################################
  500. Clean(){
  501. setup-vbootstrap || return 1
  502. # mount-chroot --umount /home
  503. mount-chroot --umount /tmp
  504. mount-chroot --umount /dev/shm
  505. mount-chroot --umount /dev/pts
  506. mount-chroot --umount /proc
  507. mount-chroot --umount archives_dir
  508. mount-chroot --umount unionfs_dir
  509. apt-get-update --host
  510. ## We remark that we first remove /, and secondly remove /.
  511. ## If we directly remove /, we obtained some non-empty directories:
  512. ## /dev/.udev/rules.d.
  513. if [ $with_unionfs -eq 1 ]; then
  514. if [ -d ${UNIONFS_ROOT} ]; then
  515. echo -n $"Cleaning build root ${UNIONFS_ROOT} via unionfs ... "
  516. rm -rf ${UNIONFS_ROOT} 2>/dev/null
  517. rm -rf ${UNIONFS_ROOT}
  518. echo $"done."
  519. fi
  520. else
  521. if [ -d ${BUILD_ROOT} ]; then
  522. echo -n $"Cleaning build root ${BUILD_ROOT} ... "
  523. rm -rf ${BUILD_ROOT} 2>/dev/null
  524. rm -rf ${BUILD_ROOT}
  525. echo $"done."
  526. fi
  527. fi
  528. echo $"Cleanup a build farm for ${BUILD_ROOT} done."
  529. return 0
  530. }
  531. Build(){
  532. setup-vbootstrap || return 1
  533. if [ $with_dist_upgrade -eq 1 ]; then
  534. ## make bootstrap of ${STABLE_VERSION}
  535. /usr/sbin/vbootstrap \
  536. $(echo ${BASE_PROFILE} | sed -e "s/VineSeed/${STABLE_VERSION}/") \
  537. ${VBOOTSTRAP_FETCH_URL} ${BUILD_ROOT} 2>&1 | \
  538. egrep -v -e '^W: Duplicate sources.list entry' \
  539. -e '^W: .*apt-get update'
  540. ## aim apt-line to VineSeed
  541. sed -i "s/apt ${STABLE_VERSION}/apt VineSeed/g" \
  542. ${BUILD_ROOT}/etc/apt/sources.list.d/main.list
  543. else
  544. /usr/sbin/vbootstrap \
  545. ${BASE_PROFILE} ${VBOOTSTRAP_FETCH_URL} ${BUILD_ROOT} 2>&1 | \
  546. egrep -v -e '^W: Duplicate sources.list entry' \
  547. -e '^W: .*apt-get update'
  548. fi
  549. mount-chroot /proc
  550. mount-chroot archives_dir
  551. mount-chroot /dev/pts
  552. mount-chroot /dev/shm
  553. # mount-chroot /tmp
  554. # mount-chroot /home
  555. ##!! 4.2 has no apt-sourceslist-{plus,nonfree,proposed-updates} packages
  556. case ${VERSION} in
  557. 4.2)
  558. sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list
  559. sed -i -e 's/main plus updates nonfree *$/$(echo ${CATEGORIES} | sed -e "s/,/ /"g) updates/g' ${BUILD_ROOT}/etc/apt/sources.list
  560. $__chroot_sh 'apt-get -qq update && apt-get -qq -y dist-upgrade'
  561. # [ $with_category_security -eq 1 ] && \
  562. # echo
  563. ;;
  564. 5.2|@@VBUILDER_STABLE_VERSION@@)
  565. sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list.d/main.list
  566. $__chroot_sh 'apt-get -qq update && apt-get -qq -y dist-upgrade'
  567. [ $with_category_plus -eq 1 ] && \
  568. $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-plus' && \
  569. sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list.d/plus.list
  570. [ $with_category_nonfree -eq 1 ] && \
  571. $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-nonfree' && \
  572. sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list.d/nonfree.list
  573. [ $with_category_proposed_updates -eq 1 ] && \
  574. $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-proposed-updates' && \
  575. sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list.d/proposed-updates.list
  576. # [ $with_category_security -eq 1 ] && \
  577. # echo
  578. ;;
  579. VineSeed)
  580. sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list.d/main.list
  581. $__chroot_sh 'apt-get -qq update && apt-get -qq -y dist-upgrade'
  582. [ $with_category_plus -eq 1 ] && \
  583. $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-plus' && \
  584. sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list.d/plus.list
  585. [ $with_category_nonfree -eq 1 ] && \
  586. $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-nonfree' && \
  587. sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list.d/nonfree.list
  588. [ $with_category_test -eq 1 ] && \
  589. $__chroot_sh 'apt-get -qq update && apt-get -qq -y install apt-sourceslist-test' && \
  590. sed -i -e "s|@@VBUILDER_VBOOTSTRAP_FETCH_URL@@|${VBOOTSTRAP_FETCH_URL}|g" ${BUILD_ROOT}/etc/apt/sources.list.d/test.list
  591. ;;
  592. esac
  593. [ $with_dist_upgrade -eq 1 ] && \
  594. $__chroot_sh 'apt-get -qq update && apt-get -qq -y dist-upgrade'
  595. [ $with_build_essential -eq 1 ] && \
  596. $__chroot_sh 'apt-get -qq -y install build-essential'
  597. $__chroot_sh 'apt-get -qq -y install etcskel shadow-utils MAKEDEV'
  598. $__chroot_sh 'cd /dev && /sbin/MAKEDEV console'
  599. $__chroot_sh 'cd /dev && /sbin/MAKEDEV ptmx'
  600. $__chroot_sh 'cd /dev && /sbin/MAKEDEV null'
  601. $__chroot_sh 'cd /dev && /sbin/MAKEDEV zero'
  602. $__chroot_sh 'cd /dev && /sbin/MAKEDEV random'
  603. $__chroot_sh 'cd /dev && /sbin/MAKEDEV urandom'
  604. $__chroot_sh 'cd /dev && mkdir -p shm && chmod 777 shm'
  605. $__chroot_sh 'cd /dev && mkdir -p pts && chmod 755 pts'
  606. $__chroot_sh '/usr/sbin/pwconv'
  607. $__chroot_sh "/usr/sbin/useradd ${BUILD_USER}"
  608. ##!! for rpm-4.8.0 or higher
  609. ##!! (See http://trac.vinelinux.org/wiki/Vine6/AboutUpdateToolchain)
  610. if [ "${VERSION}" = "VineSeed" ]; then
  611. $__chroot_sh "sed -i -e 's/^%_topdir/#%_topdir/' /home/${BUILD_USER}/.rpmmacros"
  612. fi
  613. ## set local repositories (profile only)
  614. if [ ${with_profile} -eq 1 ]; then
  615. if [ ! -z "${LOCAL_REPOS}" ]; then
  616. cat >${BUILD_ROOT}/etc/apt/sources.list.d/local.list<<EOF
  617. ${LOCAL_REPOS}
  618. EOF
  619. $__chroot_sh "apt-get update"
  620. fi
  621. ## additional base packages
  622. [ ! -z "${ADD_BASE_PKGS}" ] && \
  623. $__chroot_sh "apt-get -y install ${ADD_BASE_PKGS}"
  624. fi
  625. # mount-chroot --umount /home
  626. # mount-chroot --umount /tmp
  627. mount-chroot --umount /dev/shm
  628. mount-chroot --umount /dev/pts
  629. mount-chroot --umount archives_dir
  630. mount-chroot --umount /proc
  631. apt-get-update --host
  632. echo $"Making a build farm for ${BUILD_ROOT} done."
  633. return 0
  634. }
  635. RPM_Remove(){
  636. setup-vbootstrap-rpm || return 1
  637. mount-chroot unionfs_dir
  638. mount-chroot archives_dir
  639. mount-chroot /proc
  640. mount-chroot /dev/pts
  641. mount-chroot /dev/shm
  642. # apt-get-update --chroot
  643. [ -f $RPM_PKG ] && \
  644. echo $"E: $RPM_PKG is not a package name" && return 1
  645. $__chroot_sh "apt-get -y remove $BASE_RPM_PKG"
  646. mount-chroot --umount /dev/shm
  647. mount-chroot --umount /dev/pts
  648. mount-chroot --umount /proc
  649. mount-chroot --umount archives_dir
  650. mount-chroot --umount unionfs_dir
  651. # apt-get-update --host
  652. return 0
  653. }
  654. RPM_Install(){
  655. setup-vbootstrap-rpm || return 1
  656. mount-chroot unionfs_dir
  657. mount-chroot archives_dir
  658. mount-chroot /proc
  659. mount-chroot /dev/pts
  660. mount-chroot /dev/shm
  661. apt-get-update --chroot
  662. [ $with_category_nonfree -eq 1 ] && \
  663. [ ! -z "$(echo $BASE_RPM_PKG | grep -e 'self-build-' -e 'install-assist-')" ] && \
  664. $__chroot_sh 'rpm -q --quiet self-build-setup || apt-get -qq -y install self-build-setup'
  665. $__chroot_sh "cd ${BUILD_DIR} && apt-get -y install $BASE_RPM_PKG"
  666. mount-chroot --umount /dev/shm
  667. mount-chroot --umount /dev/pts
  668. mount-chroot --umount /proc
  669. mount-chroot --umount archives_dir
  670. mount-chroot --umount unionfs_dir
  671. apt-get-update --host
  672. return 0
  673. }
  674. RPM_Build(){
  675. [ ! -f ${RPM_PKG} ] && \
  676. echo $"E: No such file found: ${RPM_PKG}" && return 1
  677. ## check the extension of given $RPM_PKG
  678. local RPM_PKG_EXT=${RPM_PKG##*.}
  679. case ${RPM_PKG_EXT} in
  680. spec)
  681. ## In this case,
  682. ## the file ${RPM_PKG} is a spec file!
  683. # spec-validator ${RPM_PKG} || return 1
  684. # return 1
  685. ## We assign the variable ${RPM_PKG}
  686. ## the src.rpm generated by the spec file ${RPM_PKG}.
  687. RPM_PKG=$(rpm -E "%{_srcrpmdir}")/$(rpm -q --qf "%{name}-%{version}-%{release}\n" --specfile ${RPM_PKG} | head -n 1).src.rpm
  688. ;;
  689. rpm)
  690. RPM_PKG_EXT=$(echo ${RPM_PKG} | sed -e "s|.*\.\(src\.rpm\)$|\1|")
  691. [ "${RPM_PKG_EXT}" != "src.rpm" ] && \
  692. echo $"E: $RPM_PKG is NOT a source RPM package" && return 1
  693. ;;
  694. *)
  695. echo $"The action build-rpm cannot work such extension: ${RPM_PKG_EXT}"
  696. return 1
  697. ;;
  698. esac
  699. setup-vbootstrap-rpm || return 1
  700. mount-chroot unionfs_dir
  701. mount-chroot archives_dir
  702. mount-chroot /proc
  703. mount-chroot /dev/pts
  704. mount-chroot /dev/shm
  705. apt-get-update --chroot
  706. [ ! -f ${RPM_PKG} ] && \
  707. echo $"E: No such file found: ${RPM_PKG}" && return 1
  708. RPM_PKG_USER=$(stat -c %U $RPM_PKG)
  709. RPM_PKG_GROUP=$(stat -c %G $RPM_PKG)
  710. [ ! -z "${SUDO_UID}" ] && RPM_PKG_USER=${SUDO_UID}
  711. [ ! -z "${SUDO_GID}" ] && RPM_PKG_GROUP=${SUDO_GID}
  712. local __install="install -p -v -o ${RPM_PKG_USER} -g ${RPM_PKG_GROUP}"
  713. ## make src.rpm for $VERSION
  714. $__chroot_sh "cd ${BUILD_DIR} && su ${BUILD_USER} -c 'rpm -ivh $BASE_RPM_PKG'"
  715. $__chroot_sh "cd ${BUILD_DIR} && su ${BUILD_USER} -c 'rpmbuild -bs --nodeps --clean --rmsource --rmspec $RPM_OPTS ${BUILD_DIR}/SPECS/*.spec'"
  716. ## change ${DIST_RELEASE}
  717. BASE_RPM_PKG=$(echo $BASE_RPM_PKG | sed -e "s/vl[0-9]*\([A-Za-z]*[0-9A-Za-z]*\)\./vl${DIST_RELEASE}\1\./")
  718. ## rebuild $BASE_RPM_PKG on ${DIST_RELEASE}
  719. $__chroot_sh "cd ${BUILD_DIR}/SRPMS && apt-get -o APT::Install::Virtual=true -y build-dep $BASE_RPM_PKG"
  720. $__chroot_sh "cd ${BUILD_DIR}/SRPMS && su ${BUILD_USER} -c 'rpmbuild --rebuild $RPM_OPTS $BASE_RPM_PKG'"
  721. BUILT_RPMS_LIST=$(find $BUILD_ROOT${BUILD_DIR}/RPMS -type f -regex '.*\.rpm' | sed -e s@${BUILD_ROOT}@@g -e 's|.*\/compat32-.*||g' -e 's|.*\/.*\.src\.rpm||g' -e 's/$/ \\/g')
  722. [ $with_category_nonfree -eq 1 ] && \
  723. [ ! -z "$(echo $BUILT_RPMS_LIST | grep -e 'self-build-' -e 'install-assist-')" ] && \
  724. $__chroot_sh 'rpm -q --quiet self-build-setup || apt-get -qq -y install self-build-setup'
  725. [ $with_no_install -eq 0 ] && \
  726. $__chroot_sh "cd ${BUILD_DIR} && apt-get -y install $BUILT_RPMS_LIST"
  727. ## copy built rpms to ${HOME}/rpm/ for each archtectures
  728. echo $"Copying built rpms to ${BUILT_RPMS_DIR} for each archtectures ... "
  729. for i in $RPM_PKG_ARCH_LIST; do
  730. [ -d $BUILD_ROOT${BUILD_DIR}/${i} ] || continue
  731. if [ ! -d ${BUILT_RPMS_DIR}/${VERSION}/${i} ]; then
  732. $__install -d ${BUILT_RPMS_DIR}/${VERSION}/${i}/
  733. chown -R ${RPM_PKG_USER}:${RPM_PKG_GROUP} ${BUILT_RPMS_DIR}
  734. fi
  735. find $BUILD_ROOT${BUILD_DIR}/${i} -type f -regex '.*\.rpm' \
  736. -exec $__install -m0644 {} ${BUILT_RPMS_DIR}/${VERSION}/${i}/ \;
  737. done
  738. mount-chroot --umount /dev/shm
  739. mount-chroot --umount /dev/pts
  740. mount-chroot --umount /proc
  741. mount-chroot --umount archives_dir
  742. mount-chroot --umount unionfs_dir
  743. apt-get-update --host
  744. echo $"done."
  745. return 0
  746. }
  747. RPM_Sign(){
  748. [ $with_sign -eq 1 ] || return 1
  749. setup-vbootstrap || return 1
  750. [ -d ${BUILD_ROOT} ] || Build
  751. mount-chroot unionfs_dir
  752. echo $"Signing built rpms using ${RPM_SIGN_USER}'s key: "
  753. su $RPM_SIGN_USER -c "rpmsign --addsign $(for i in $RPM_PKG_ARCH_LIST; do find $BUILD_ROOT${BUILD_DIR}/${i} -type f -regex '.*\.rpm' 2>/dev/null; done | sed -e s,$BUILD_ROOT${BUILD_DIR},${BUILT_RPMS_DIR}/${VERSION},g -e 's/$/ \\/g')"
  754. mount-chroot --umount unionfs_dir
  755. return 0
  756. }
  757. Login_Chroot(){
  758. [ $with_login -eq 1 ] || return 1
  759. setup-vbootstrap || return 1
  760. __chroot="/usr/sbin/chroot ${BUILD_ROOT}"
  761. ## setarch ix86 if ix86 chroot on x86_64 host
  762. [ $with_ix86_on_x86_64 -eq 1 ] && \
  763. __chroot="/usr/sbin/chroot ${BUILD_ROOT} setarch ${VARCH}"
  764. mount-chroot unionfs_dir
  765. mount-chroot archives_dir
  766. mount-chroot /proc
  767. mount-chroot /dev/pts
  768. mount-chroot /dev/shm
  769. mount-chroot /tmp
  770. # mount-chroot /home
  771. # apt-get-update --chroot
  772. ## copy host's configurations of /etc
  773. #passwd shadow group gshadow
  774. for i in resolv.conf hosts; do
  775. cp -pf /etc/${i} ${BUILD_ROOT}/etc
  776. done
  777. cp -Ppf /etc/localtime ${BUILD_ROOT}/etc
  778. $__chroot /bin/bash
  779. # mount-chroot --umount /home
  780. mount-chroot --umount /tmp
  781. mount-chroot --umount /dev/shm
  782. mount-chroot --umount /dev/pts
  783. mount-chroot --umount /proc
  784. mount-chroot --umount archives_dir
  785. mount-chroot --umount unionfs_dir
  786. # apt-get-update --host
  787. return 0
  788. }
  789. ##############################################################################
  790. initialize-variables || exit 1
  791. check-parameter $* || exit 1
  792. setup-vbuilder || exit 1
  793. while [ $# -gt 0 ]; do
  794. tmpARG=$1
  795. case $tmpARG in
  796. --profile)
  797. shift
  798. ;;
  799. --version|--arch|--category|--fetch-url|--target|--rpmbuild-define|--rpmbuild-with|--rpmbuild-without|--bootstrap-dir|--unionfs-dir|--cache-dir|--built-rpms-dir)
  800. shift
  801. ;;
  802. --dist-upgrade|--unionfs|--with-compat32|--sign|--no-install|--login|--no-build-essential|--debug)
  803. ;;
  804. --build-rpm|build-rpm|--install-rpm|install-rpm|--remove-rpm|remove-rpm)
  805. shift
  806. ;;
  807. --build|build|--clean|clean)
  808. ;;
  809. *)
  810. echo unknown option $1
  811. exit 1
  812. ;;
  813. esac
  814. case $tmpARG in
  815. --profile)
  816. ;;
  817. --version)
  818. VERSION=$1
  819. ;;
  820. --arch)
  821. VARCH=$1
  822. ;;
  823. --category)
  824. CATEGORIES=$1
  825. ;;
  826. --fetch-url)
  827. VBOOTSTRAP_FETCH_URL=$1
  828. ;;
  829. --dist-upgrade)
  830. with_dist_upgrade=1
  831. ;;
  832. --unionfs)
  833. with_unionfs=1
  834. ;;
  835. --target)
  836. TARGET=$1
  837. RPM_OPTS="${RPM_OPTS} --target $TARGET"
  838. ;;
  839. --with-compat32)
  840. RPM_OPTS="${RPM_OPTS} --with compat32"
  841. ;;
  842. --rpmbuild-define)
  843. RPM_OPTS="${RPM_OPTS} --define $1"
  844. ;;
  845. --rpmbuild-with)
  846. RPM_OPTS="${RPM_OPTS} --with $1"
  847. ;;
  848. --rpmbuild-without)
  849. RPM_OPTS="${RPM_OPTS} --without $1"
  850. ;;
  851. --sign)
  852. with_sign=1
  853. ;;
  854. --no-install)
  855. with_no_install=1
  856. ;;
  857. --no-build-essential)
  858. with_build_essential=0
  859. ;;
  860. --login)
  861. with_login=1
  862. ;;
  863. --bootstrap-dir)
  864. VBOOTSTRAP_DIR=$1
  865. ;;
  866. --unionfs-dir)
  867. UNIONFS_DIR=$1
  868. ;;
  869. --cache-dir)
  870. CACHE_DIR=$1
  871. ;;
  872. --built-rpms-dir)
  873. BUILT_RPMS_DIR=$1
  874. ;;
  875. --debug)
  876. with_debug=1
  877. ;;
  878. --build-rpm|build-rpm)
  879. RPM_PKG=$1
  880. RPM_Build || exit 1
  881. ;;
  882. --install-rpm|install-rpm)
  883. RPM_PKG=$1
  884. RPM_Install || exit 1
  885. ;;
  886. --remove-rpm|remove-rpm)
  887. RPM_PKG=$1
  888. RPM_Remove || exit 1
  889. ;;
  890. --build|build)
  891. Build || exit 1
  892. ;;
  893. --clean|clean)
  894. Clean || exit 1
  895. ;;
  896. esac
  897. shift
  898. done
  899. RPM_Sign ||:
  900. Login_Chroot ||:
  901. exit