libvbuilder.sh.in 13 KB

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