libvbuilder.sh.in 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. UNIONFS_WORK=${UNIONFS_DIR}/unionfs_work
  271. __chroot_sh="/usr/sbin/chroot ${BUILD_ROOT} /bin/sh -c -l"
  272. mkdir -p $VBOOTSTRAP_DIR
  273. return 0
  274. }
  275. setup-vbootstrap-rpm(){
  276. setup-vbootstrap || return 1
  277. ## check ${BUILD_ROOT}
  278. [ -d ${BUILD_ROOT} ] || Build
  279. ## setarch ix86 if ix86 chroot on x86_64 host
  280. [ $with_ix86_on_x86_64 -eq 1 ] && \
  281. __chroot_sh="/usr/sbin/chroot ${BUILD_ROOT} setarch ${VARCH} /bin/sh -c -l"
  282. DIST_RELEASE=$(cat ${BUILD_ROOT}/etc/vine-release | cut -f3 -d" " | cut -f1 -d.)
  283. if [ -f $RPM_PKG ]; then
  284. BASE_RPM_PKG=$(basename $RPM_PKG)
  285. cp -f $RPM_PKG $BUILD_ROOT${BUILD_DIR}
  286. else
  287. BASE_RPM_PKG=$RPM_PKG
  288. fi
  289. return 0
  290. }
  291. ## recover apt-get data on host/chroot
  292. apt-get-update(){
  293. case $1 in
  294. --host)
  295. echo -n $"apt-get update on host ... "
  296. apt-get -qq update > /dev/null 2>&1
  297. echo $"done."
  298. ;;
  299. --chroot)
  300. echo -n $"apt-get update on chroot ... "
  301. $__chroot_sh 'apt-get -qq update' > /dev/null 2>&1
  302. echo $"done."
  303. ;;
  304. *)
  305. echo apt-get-update: unknown option $1
  306. exit 1
  307. ;;
  308. esac
  309. }
  310. ## mount-chroot {|--umount} [file system|name]
  311. ## support file systems: /home /tmp /sys /proc /dev/shm /dev/pts /dev
  312. ## support names: vfs archives_dir
  313. ## NOTE: /tmp needs for applications which use X
  314. ## vfs is virtual file systems
  315. ## archives_dir uses to mount ${EXTERNAL_ARCHIVES_DIR} to ${ARCHIVES_DIR}
  316. ## unionfs_dir covers ${BUILD_ROOT} with unionfs
  317. mount-chroot(){
  318. if [ "$1" = "--umount" ]; then
  319. mount-chroot-umount $2 || return 1
  320. else
  321. mount-chroot-mount $1 || return 1
  322. fi
  323. return 0
  324. }
  325. ## mount-chroot-umount [file system|name]
  326. mount-chroot-umount(){
  327. local fs=$1
  328. case $fs in
  329. /home|/tmp|/sys|/proc|/dev/shm|/dev/pts|/dev)
  330. [ -d ${BUILD_ROOT}${fs} ] || return 1
  331. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] || \
  332. umount ${BUILD_ROOT}${fs}
  333. if [ ! -z "$(mount | grep ${BUILD_ROOT}${fs})" ]; then
  334. echo $"Retry lazy unmount ${BUILD_ROOT}${fs} ... "
  335. umount -l ${BUILD_ROOT}${fs}
  336. echo $"done."
  337. fi
  338. ;;
  339. vfs)
  340. for dir in /sys /proc /dev/shm /dev/pts /dev; do
  341. mount-chroot-umount ${dir} || return 1
  342. done
  343. ;;
  344. archives_dir)
  345. [ -d ${ARCHIVES_DIR} ] || return 1
  346. [ -z "$(mount | grep ${ARCHIVES_DIR})" ] || \
  347. umount ${ARCHIVES_DIR}
  348. ;;
  349. unionfs_dir)
  350. [ -d ${BUILD_ROOT} ] || return 1
  351. [ -z "$(mount | grep ${BUILD_ROOT} | egrep '(unionfs|aufs|overlay)')" ] || \
  352. umount ${BUILD_ROOT}
  353. if [ ! -z "$(mount | grep ${BUILD_ROOT} | egrep '(unionfs|aufs|overlay)')" ]; then
  354. echo $"Retry lazy unmount ${BUILD_ROOT} ... "
  355. umount -l ${BUILD_ROOT}
  356. echo $"done."
  357. fi
  358. ;;
  359. *)
  360. echo mount-chroot-umount: unknown file system $fs
  361. exit 1
  362. ;;
  363. esac
  364. return 0
  365. }
  366. ## mount-chroot-mount [file system|name]
  367. mount-chroot-mount(){
  368. local fs=$1
  369. case $fs in
  370. /home)
  371. [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
  372. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
  373. mount -o _netdev,rbind ${fs} ${BUILD_ROOT}${fs}
  374. ;;
  375. /tmp|/dev)
  376. [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
  377. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
  378. mount --bind -o _netdev ${fs} ${BUILD_ROOT}${fs}
  379. ;;
  380. /sys)
  381. [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
  382. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
  383. mount -o _netdev -t sysfs vbuildersysfs ${BUILD_ROOT}${fs}
  384. ;;
  385. /proc)
  386. [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
  387. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
  388. mount -o _netdev -t proc vbuilderproc ${BUILD_ROOT}${fs}
  389. ;;
  390. /dev/shm)
  391. [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
  392. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
  393. mount -o _netdev -t tmpfs vbuildertmpfs ${BUILD_ROOT}${fs}
  394. ;;
  395. /dev/pts)
  396. [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
  397. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
  398. mount -o gid=5,mode=620,_netdev -t devpts vbuilderdevpts ${BUILD_ROOT}${fs}
  399. ;;
  400. vfs)
  401. for dir in /dev /dev/pts /dev/shm /proc /sys; do
  402. mount-chroot-mount ${dir} || return 1
  403. done
  404. ;;
  405. archives_dir)
  406. [ -d ${EXTERNAL_ARCHIVES_DIR} ] || mkdir -p ${EXTERNAL_ARCHIVES_DIR}
  407. [ -d ${ARCHIVES_DIR} ] || mkdir -p ${ARCHIVES_DIR}
  408. [ -z "$(mount | grep ${ARCHIVES_DIR})" ] && \
  409. mount --bind -o _netdev ${EXTERNAL_ARCHIVES_DIR} ${ARCHIVES_DIR}
  410. [ -d ${ARCHIVES_DIR}/partial ] || mkdir -p ${ARCHIVES_DIR}/partial
  411. ;;
  412. unionfs_dir)
  413. if [ $with_unionfs -eq 1 ]; then
  414. [ -d ${UNIONFS_ROOT} ] || mkdir -p ${UNIONFS_ROOT}
  415. if [ -d ${UNIONFS_WORK} ]; then
  416. #workdir needs emply
  417. rm -rf ${UNIONFS_WORK}; mkdir -p ${UNIONFS_WORK}
  418. else
  419. mkdir -p ${UNIONFS_WORK}
  420. fi
  421. /sbin/modprobe overlay >& /dev/null ||:
  422. /sbin/modprobe aufs >& /dev/null ||:
  423. #overlayfs
  424. if ( grep -q overlay /proc/filesystems ) ; then
  425. [ -z "$(mount | grep ${UNIONFS_ROOT})" ] && \
  426. mount -t overlay overlay -o lowerdir=${BUILD_ROOT},upperdir=${UNIONFS_ROOT},workdir=${UNIONFS_WORK} ${BUILD_ROOT}
  427. #aufs
  428. elif ( grep -q aufs /proc/filesystems ) ; then
  429. [ -z "$(mount | grep ${UNIONFS_ROOT})" ] && \
  430. mount -t aufs -o br=${UNIONFS_ROOT}=rw:${BUILD_ROOT}=ro aufs ${BUILD_ROOT}
  431. else
  432. #unionfs
  433. [ -z "$(mount | grep ${UNIONFS_ROOT})" ] && \
  434. mount -t unionfs -o dirs=${UNIONFS_ROOT}=rw:${BUILD_ROOT}=ro unionfs ${BUILD_ROOT}
  435. unionctl ${BUILD_ROOT} --list
  436. fi
  437. fi
  438. ;;
  439. *)
  440. echo mount-chroot-mount: unknown file system $fs
  441. exit 1
  442. ;;
  443. esac
  444. return 0
  445. }
  446. ### end of file