libvbuilder.sh.in 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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|6.5|VineSeed)
  145. ;;
  146. 6.?)
  147. VERSION="6.5"
  148. ;;
  149. *)
  150. echo $"E: ${VERSION} is NOT supported"
  151. return 1
  152. ;;
  153. esac
  154. case "${VARCH}" in
  155. arm*)
  156. VARCH="arm"
  157. ;;
  158. esac
  159. ## check a chroot archtecture
  160. if [ -z ${VARCH} ]; then
  161. VARCH=${UARCH}
  162. else
  163. case "${VARCH}" in
  164. i386|x86_64)
  165. [ "${UARCH}" = "ppc" -o "${UARCH}" = "arm" ] && \
  166. echo $"E: arch ${VARCH} is NOT supported on ${UARCH}" && return 1
  167. ;;
  168. ppc)
  169. [ "${UARCH}" = "i386" -o "${UARCH}" = "x86_64" -o "${UARCH}" = "arm" ] && \
  170. echo $"E: arch ${VARCH} is NOT supported on ${UARCH}" && return 1
  171. ;;
  172. arm)
  173. [ "${UARCH}" = "i386" -o "${UARCH}" = "x86_64" -o "${UARCH}" = "ppc" ] && \
  174. echo $"E: arch ${VARCH} is NOT supported on ${UARCH}" && return 1
  175. ;;
  176. esac
  177. fi
  178. ##!! 4.2 is NO support on VARCH=x86_64 or VARCH=arch
  179. if [ "${VERSION}" = "4.2" ]; then
  180. if [ "${VARCH}" = "x86_64" -o "${VARCH}" = "arm" ]; then
  181. echo $"E: ${VERSION}_${VARCH} is NOT supported"
  182. return 1
  183. fi
  184. fi
  185. ## set base profile <version>_<arch>
  186. BASE_PROFILE=${VERSION}_${VARCH}
  187. ## check support ${BASE_PROFILE}
  188. if [ -z "$(/usr/sbin/vbootstrap | sed -e s/^Usage:.*// -e s/^E:.*// | grep -m 1 ${BASE_PROFILE})" ]; then
  189. echo $"E: ${BASE_PROFILE} is NOT supported"
  190. return 1
  191. fi
  192. ## check ${VERSION} equals VineSeed*, when with_dist_upgrade=1
  193. if [ $with_dist_upgrade -eq 1 ]; then
  194. if [ "${VERSION}" != "VineSeed" ]; then
  195. echo $"E: version ${VERSION} does not support --dist-upgrade option"
  196. return 1
  197. fi
  198. fi
  199. ## support i386 chroot on x86_64 below:
  200. [ "${UARCH}" = "x86_64" -a "${VARCH}" = "i386" ] && \
  201. with_ix86_on_x86_64=1
  202. ## check apt categories
  203. ## "main" category is unconditionally permited
  204. with_category_main=1
  205. for cat in $(echo ${CATEGORIES} | sed -e "s/,/ /"g); do
  206. case $cat in
  207. main)
  208. with_category_main=1
  209. ;;
  210. plus)
  211. with_category_plus=1
  212. ;;
  213. nonfree)
  214. with_category_nonfree=1
  215. ;;
  216. test)
  217. ## "test" category only exists in VineSeed
  218. [ "${VERSION}" = "VineSeed" ] || \
  219. echo $"E: No such category exists: $cat" && return 1
  220. with_category_test=1
  221. ;;
  222. proposed-updates)
  223. ##!! "proposed-updates" category does not exist in 4.2
  224. [ "${VERSION}" = "4.2" ] && \
  225. echo $"E: No such category exists: $cat" && return 1
  226. with_category_proposed_updates=1
  227. ;;
  228. security)
  229. ## "security" category does not exist in VineSeed
  230. [ "${VERSION}" = "VineSeed" ] && \
  231. echo $"E: No such category exists: $cat" && return 1
  232. with_category_security=1
  233. ;;
  234. *)
  235. echo $"E: No such category exists: $cat" && return 1
  236. ;;
  237. esac
  238. done
  239. ## check build target option ${TARGET}
  240. if [ ! -z "${TARGET}" ]; then
  241. RPM_TARGET_LIST="$(cat /usr/lib/rpm/rpmrc | grep arch_canon: | sed -e "s/arch_canon:[[:blank:]]*\(.*\):.*/\1/")"
  242. RPM_TARGET_LIST="${RPM_TARGET_LIST} noarch"
  243. if [ -z "$(echo ${RPM_TARGET_LIST} | grep ${TARGET})" ]; then
  244. echo $"E: rpm build target ${TARGET} is NOT supported"
  245. return 1
  246. fi
  247. fi
  248. ## set ${RPM_PKG_ARCH_LIST}
  249. RPM_PKG_ARCH_LIST=" \
  250. RPMS/i386 RPMS/i486 RPMS/i586 RPMS/i686 RPMS/x86_64 RPMS/ppc \
  251. RPMS/noarch \
  252. RPMS/armv3l RPMS/armv4l RPMS/armv4tl \
  253. RPMS/armv5tejl RPMS/armv5tel RPMS/armv6l RPMS/armv7l \
  254. SRPMS"
  255. [ -z "${TARGET}" ] || \
  256. RPM_PKG_ARCH_LIST="RPMS/${TARGET} ${RPM_PKG_ARCH_LIST}"
  257. fi
  258. ## set global variables
  259. BUILD_USER=vbuilder
  260. BUILD_DIR=/home/${BUILD_USER}/rpm
  261. if [ ${with_profile} -eq 1 ]; then
  262. BUILD_ROOT=${VBOOTSTRAP_DIR}/${PROFILE}
  263. UNIONFS_ROOT=${UNIONFS_DIR}/${PROFILE}
  264. else
  265. BUILD_ROOT=${VBOOTSTRAP_DIR}/${BASE_PROFILE}
  266. UNIONFS_ROOT=${UNIONFS_DIR}/${BASE_PROFILE}
  267. fi
  268. ARCHIVES_DIR=${BUILD_ROOT}/var/cache/apt/archives
  269. EXTERNAL_ARCHIVES_DIR=${CACHE_DIR}/${BASE_PROFILE}/apt/archives
  270. __chroot_sh="/usr/sbin/chroot ${BUILD_ROOT} /bin/sh -c -l"
  271. mkdir -p $VBOOTSTRAP_DIR
  272. return 0
  273. }
  274. setup-vbootstrap-rpm(){
  275. setup-vbootstrap || return 1
  276. ## check ${BUILD_ROOT}
  277. [ -d ${BUILD_ROOT} ] || Build
  278. ## setarch ix86 if ix86 chroot on x86_64 host
  279. [ $with_ix86_on_x86_64 -eq 1 ] && \
  280. __chroot_sh="/usr/sbin/chroot ${BUILD_ROOT} setarch ${VARCH} /bin/sh -c -l"
  281. DIST_RELEASE=$(cat ${BUILD_ROOT}/etc/vine-release | cut -f3 -d" " | cut -f1 -d.)
  282. if [ -f $RPM_PKG ]; then
  283. BASE_RPM_PKG=$(basename $RPM_PKG)
  284. cp -f $RPM_PKG $BUILD_ROOT${BUILD_DIR}
  285. else
  286. BASE_RPM_PKG=$RPM_PKG
  287. fi
  288. return 0
  289. }
  290. ## recover apt-get data on host/chroot
  291. apt-get-update(){
  292. case $1 in
  293. --host)
  294. echo -n $"apt-get update on host ... "
  295. apt-get -qq update > /dev/null 2>&1
  296. echo $"done."
  297. ;;
  298. --chroot)
  299. echo -n $"apt-get update on chroot ... "
  300. $__chroot_sh 'apt-get -qq update' > /dev/null 2>&1
  301. echo $"done."
  302. ;;
  303. *)
  304. echo apt-get-update: unknown option $1
  305. exit 1
  306. ;;
  307. esac
  308. }
  309. ## mount-chroot {|--umount} [file system|name]
  310. ## support file systems: /home /tmp /sys /proc /dev/shm /dev/pts /dev
  311. ## support names: vfs archives_dir
  312. ## NOTE: /tmp needs for applications which use X
  313. ## vfs is virtual file systems
  314. ## archives_dir uses to mount ${EXTERNAL_ARCHIVES_DIR} to ${ARCHIVES_DIR}
  315. ## unionfs_dir covers ${BUILD_ROOT} with unionfs
  316. mount-chroot(){
  317. if [ "$1" = "--umount" ]; then
  318. mount-chroot-umount $2 || return 1
  319. else
  320. mount-chroot-mount $1 || return 1
  321. fi
  322. return 0
  323. }
  324. ## mount-chroot-umount [file system|name]
  325. mount-chroot-umount(){
  326. local fs=$1
  327. case $fs in
  328. /home|/tmp|/sys|/proc|/dev/shm|/dev/pts|/dev)
  329. [ -d ${BUILD_ROOT}${fs} ] || return 1
  330. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] || \
  331. umount ${BUILD_ROOT}${fs}
  332. if [ ! -z "$(mount | grep ${BUILD_ROOT}${fs})" ]; then
  333. echo $"Retry lazy unmount ${BUILD_ROOT}${fs} ... "
  334. umount -l ${BUILD_ROOT}${fs}
  335. echo $"done."
  336. fi
  337. ;;
  338. vfs)
  339. for dir in /sys /proc /dev/shm /dev/pts /dev; do
  340. mount-chroot-umount ${dir} || return 1
  341. done
  342. ;;
  343. archives_dir)
  344. [ -d ${ARCHIVES_DIR} ] || return 1
  345. [ -z "$(mount | grep ${ARCHIVES_DIR})" ] || \
  346. umount ${ARCHIVES_DIR}
  347. ;;
  348. unionfs_dir)
  349. [ -d ${BUILD_ROOT} ] || return 1
  350. [ -z "$(mount | grep ${BUILD_ROOT} | egrep '(unionfs|aufs)')" ] || \
  351. umount ${BUILD_ROOT}
  352. if [ ! -z "$(mount | grep ${BUILD_ROOT} | egrep '(unionfs|aufs)')" ]; then
  353. echo $"Retry lazy unmount ${BUILD_ROOT} ... "
  354. umount -l ${BUILD_ROOT}
  355. echo $"done."
  356. fi
  357. ;;
  358. *)
  359. echo mount-chroot-umount: unknown file system $fs
  360. exit 1
  361. ;;
  362. esac
  363. return 0
  364. }
  365. ## mount-chroot-mount [file system|name]
  366. mount-chroot-mount(){
  367. local fs=$1
  368. case $fs in
  369. /home)
  370. [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
  371. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
  372. mount -o _netdev,rbind ${fs} ${BUILD_ROOT}${fs}
  373. ;;
  374. /tmp|/dev)
  375. [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
  376. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
  377. mount --bind -o _netdev ${fs} ${BUILD_ROOT}${fs}
  378. ;;
  379. /sys)
  380. [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
  381. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
  382. mount -o _netdev -t sysfs vbuildersysfs ${BUILD_ROOT}${fs}
  383. ;;
  384. /proc)
  385. [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
  386. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
  387. mount -o _netdev -t proc vbuilderproc ${BUILD_ROOT}${fs}
  388. ;;
  389. /dev/shm)
  390. [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
  391. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
  392. mount -o _netdev -t tmpfs vbuildertmpfs ${BUILD_ROOT}${fs}
  393. ;;
  394. /dev/pts)
  395. [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
  396. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
  397. mount -o gid=5,mode=620,_netdev -t devpts vbuilderdevpts ${BUILD_ROOT}${fs}
  398. ;;
  399. vfs)
  400. for dir in /dev /dev/pts /dev/shm /proc /sys; do
  401. mount-chroot-mount ${dir} || return 1
  402. done
  403. ;;
  404. archives_dir)
  405. [ -d ${EXTERNAL_ARCHIVES_DIR} ] || mkdir -p ${EXTERNAL_ARCHIVES_DIR}
  406. [ -d ${ARCHIVES_DIR} ] || mkdir -p ${ARCHIVES_DIR}
  407. [ -z "$(mount | grep ${ARCHIVES_DIR})" ] && \
  408. mount --bind -o _netdev ${EXTERNAL_ARCHIVES_DIR} ${ARCHIVES_DIR}
  409. [ -d ${ARCHIVES_DIR}/partial ] || mkdir -p ${ARCHIVES_DIR}/partial
  410. ;;
  411. unionfs_dir)
  412. if [ $with_unionfs -eq 1 ]; then
  413. [ -d ${UNIONFS_ROOT} ] || mkdir -p ${UNIONFS_ROOT}
  414. if ( /sbin/modprobe aufs >& /dev/null ) ; then
  415. [ -z "$(mount | grep ${UNIONFS_ROOT})" ] && \
  416. mount -t aufs -o br=${UNIONFS_ROOT}=rw:${BUILD_ROOT}=ro aufs ${BUILD_ROOT}
  417. else
  418. [ -z "$(mount | grep ${UNIONFS_ROOT})" ] && \
  419. mount -t unionfs -o dirs=${UNIONFS_ROOT}=rw:${BUILD_ROOT}=ro unionfs ${BUILD_ROOT}
  420. unionctl ${BUILD_ROOT} --list
  421. fi
  422. fi
  423. ;;
  424. *)
  425. echo mount-chroot-mount: unknown file system $fs
  426. exit 1
  427. ;;
  428. esac
  429. return 0
  430. }
  431. ### end of file