vbuilder.sh.in 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. #!/bin/bash
  2. # -*- coding: utf-8-unix -*-
  3. Usage_C(){
  4. cat<<EOF
  5. Usage: $(basename $0) {--version [version]} {--arch [arch]} {--dist-upgrade} {--target [target]} {--with-compat32} {--build|--clean|--build-rpm [src.rpm]|--install-rpm [arch.rpm|package]|--remove-rpm [package]}
  6. Options:
  7. --version: set [version] (default: ${DEFAULT_VERSION})
  8. --arch: set [arch] (default: $(uname -i))
  9. --dist-upgrade: make VineSeed bootstrap via ${STABLE_VERSION}
  10. --target: build rpms with [target]
  11. --with-compat32: build rpms with compat32 on boostrap
  12. Actions:
  13. --clean: clean boostrap of [version]
  14. --build: build boostrap of [version]
  15. --build-rpm: build [src.rpm] on boostrap
  16. --install-rpm: install [arch.rpm|package] on boostrap
  17. --remove-rpm: remove [package] on boostrap
  18. For example,
  19. * make a clean/plain build environment on the current archtecture:
  20. $(basename $0) --clean --build
  21. * build a source rpm:
  22. $(basename $0) --build-rpm [src.rpm]
  23. * make a plain build environment for Vine Linux 4.2:
  24. $(basename $0) --version 4.2 --clean --build
  25. * make a i386 chroot on x86_64:
  26. $(basename $0) --arch i386 --clean --build
  27. * build a kernel package with target i686
  28. $(basename $0) --target i686 --build-rpm [kernel src.rpm]
  29. * build a compat32 package:
  30. $(basename $0) --arch i386 --with-compat32 --build-rpm [src.rpm]
  31. $(/usr/sbin/vbootstrap | sed -e s/^Usage:.*// -e s/^E:.*//)
  32. EOF
  33. }
  34. Usage_ja(){
  35. Usage_C
  36. }
  37. Msg_MissingParameter_C(){
  38. para=$1
  39. cat<<EOF
  40. E: Missing some parameter after ${para}
  41. EOF
  42. }
  43. Msg_MissingParameter_ja(){
  44. para=$1
  45. cat<<EOF
  46. E: ${para} 以後のいくつかのパラメータに間違いがあります
  47. EOF
  48. }
  49. Msg_NoSupportVARCH_C(){
  50. cat<<EOF
  51. E: arch ${VARCH} is NO support on $(uname -i)
  52. EOF
  53. }
  54. Msg_NoSupportVARCH_ja(){
  55. cat<<EOF
  56. E: ${VARCH} アーキテクチャは $(uname -i) 上で非サポートです
  57. EOF
  58. }
  59. Msg_NoSupportVERSION_C(){
  60. cat<<EOF
  61. E: ${VERSION} is NO support
  62. EOF
  63. }
  64. Msg_NoSupportVERSION_ja(){
  65. cat<<EOF
  66. E: バージョン ${VERSION} は非サポートです
  67. EOF
  68. }
  69. Msg_NoSupportDistUpgradeVERSION_C(){
  70. cat<<EOF
  71. E: version ${VERSION} does not support --dist-upgrade option
  72. EOF
  73. }
  74. Msg_NoSupportDistUpgradeVERSION_ja(){
  75. cat<<EOF
  76. E: バージョン ${VERSION} は --dist-upgrade オプションを非サポートです
  77. EOF
  78. }
  79. Msg_NoSupportTARGET_C(){
  80. cat<<EOF
  81. E: rpm build target ${TARGET} is NO support
  82. EOF
  83. }
  84. Msg_NoSupportTARGET_ja(){
  85. cat<<EOF
  86. E: rpm ビルドターゲット ${TARGET} は非サポートです
  87. EOF
  88. }
  89. Msg_NotPackageName_C(){
  90. cat<<EOF
  91. E: $RPM_PKG is not package name
  92. EOF
  93. }
  94. Msg_NotPackageName_ja(){
  95. cat<<EOF
  96. E: $RPM_PKG はパッケージ名でありません
  97. EOF
  98. }
  99. Msg_NotSourceRPM_C(){
  100. cat<<EOF
  101. E: $RPM_PKG is not source RPM package
  102. EOF
  103. }
  104. Msg_NotSourceRPM_ja(){
  105. cat<<EOF
  106. E: $RPM_PKG はソース RPM パッケージでありません
  107. EOF
  108. }
  109. ##############################################################################
  110. check-parameter(){
  111. if [ -z "$*" ]; then
  112. Usage_$LOCALE
  113. return 1
  114. fi
  115. while [ ! -z "$*" ]; do
  116. case $1 in
  117. --version|--arch|--target|--build-rpm|--install-rpm|--remove-rpm)
  118. shift
  119. check-next-parameter $1 || return 1
  120. ;;
  121. --dist-upgrade|--with-compat32|--build|--clean)
  122. ;;
  123. *)
  124. Msg_MissingParameter_$LOCALE $1
  125. return 1
  126. ;;
  127. esac
  128. shift
  129. done
  130. return 0
  131. }
  132. check-next-parameter(){
  133. arg=$1
  134. if [ -z "${arg}" ]; then
  135. Msg_MissingParameter_$LOCALE ${arg}
  136. return 1
  137. fi
  138. if [ $(echo ${arg} | grep '^-') ]; then
  139. Msg_MissingParameter_$LOCALE ${arg}
  140. return 1
  141. fi
  142. return 0
  143. }
  144. setup-vbuilder(){
  145. ## load default settings
  146. if [ -r /etc/vbootstrap/vbuilder.conf ]; then
  147. . /etc/vbootstrap/vbuilder.conf
  148. fi
  149. [ -z "${VBOOTSTRAP_DIR}" ] && \
  150. VBOOTSTRAP_DIR=@@VBUILDER_VBOOTSTRAP_DIR@@
  151. [ -z "${DEFAULT_VERSION}" ] && \
  152. DEFAULT_VERSION=@@VBUILDER_DEFAULT_VERSION@@
  153. [ -z "${BUILT_RPMS_DIR}" ] && \
  154. BUILT_RPMS_DIR=@@VBUILDER_BUILT_RPMS_DIR@@
  155. [ -d $VBOOTSTRAP_DIR ] || mkdir -p $VBOOTSTRAP_DIR
  156. VERSION=$DEFAULT_VERSION
  157. ## current stable relase version
  158. STABLE_VERSION=@@VBUILDER_STABLE_VERSION@@
  159. ## set locale
  160. case $LANG in
  161. ja*) LOCALE=ja ;;
  162. *) LOCALE=C ;;
  163. esac
  164. ## set boolian
  165. with_setup_vbootstrap=0
  166. with_dist_upgrade=0
  167. }
  168. setup-vbootstrap(){
  169. if [ ${with_setup_vbootstrap} -eq 0 ]; then
  170. with_setup_vbootstrap=1
  171. ## check a chroot archtecture
  172. if [ ! -z ${VARCH} ]; then
  173. case "${VARCH}" in
  174. i386)
  175. [ "$(uname -i)" = "ppc" ] && \
  176. Msg_NoSupportVARCH_$LOCALE
  177. [ $? -eq 0 ] && exit 1
  178. ;;
  179. x86_64)
  180. [ "$(uname -i)" = "ppc" ] && \
  181. Msg_NoSupportVARCH_$LOCALE
  182. [ $? -eq 0 ] && exit 1
  183. ;;
  184. ppc)
  185. [ "$(uname -i)" = "i386" -o "$(uname -i)" = "x86_64" ] && \
  186. Msg_NoSupportVARCH_$LOCALE
  187. [ $? -eq 0 ] && exit 1
  188. ;;
  189. esac
  190. fi
  191. [ -z ${VARCH} ] && VARCH=$(uname -i)
  192. ##!! 4.2 is NO support on VARCH=x86_64
  193. if [ "${VERSION}" = "4.2" -a "${VARCH}" = "x86_64" ]; then
  194. Msg_NoSupportVERSION_${LOCALE}
  195. exit 1
  196. fi
  197. ## support i386 chroot on x86_64 below:
  198. [ "${VARCH}" != "$(uname -i)" ] && VERSION=${VERSION}_${VARCH}
  199. ## check support ${VERSION}
  200. if [ -z "$(/usr/sbin/vbootstrap | sed -e s/^Usage:.*// -e s/^E:.*// | grep -m 1 ${VERSION})" ]; then
  201. Msg_NoSupportVERSION_$LOCALE
  202. exit 1
  203. fi
  204. ## check ${VERSION} equals VineSeed*, when with_dist_upgrade=1
  205. if [ $with_dist_upgrade -eq 1 ]; then
  206. if [ "$(echo ${VERSION} | sed -e "s/\(VineSeed\).*/\1/")" != "VineSeed" ]; then
  207. Msg_NoSupportDistUpgradeVERSION_$LOCALE
  208. exit 1
  209. fi
  210. fi
  211. ## check build target option ${TARGET}
  212. if [ ! -z "${TARGET}" ]; then
  213. RPM_TARGET_LIST="$(cat /usr/lib/rpm/rpmrc | grep arch_canon: | sed -e "s/arch_canon:[[:blank:]]*\(.*\):.*/\1/")"
  214. if [ -z "$(echo $RPM_TARGET_LIST | grep $TARGET)" ]; then
  215. Msg_NoSupportTARGET_$LOCALE
  216. exit 1
  217. fi
  218. fi
  219. fi
  220. BUILD_ROOT=${VBOOTSTRAP_DIR}/${VERSION}
  221. BUILD_USER=vbuilder
  222. BUILD_DIR=/home/${BUILD_USER}/rpm
  223. ARCHIVES_DIR=${BUILD_ROOT}/var/cache/apt/archives
  224. CACHE_DIR=${VBOOTSTRAP_DIR}/cache/${VERSION}/apt/archives
  225. __chroot_sh="/usr/sbin/chroot ${BUILD_ROOT} /bin/sh -c"
  226. mkdir -p $VBOOTSTRAP_DIR
  227. }
  228. setup-vbootstrap-rpm(){
  229. setup-vbootstrap
  230. ## check ${BUILD_ROOT}
  231. [ -d ${BUILD_ROOT} ] || Build
  232. DIST_RELEASE=$(cat ${BUILD_ROOT}/etc/vine-release | cut -f3 -d" " | cut -f1 -d.)
  233. if [ -f $RPM_PKG ]; then
  234. BASE_RPM_PKG=$(basename $RPM_PKG)
  235. cp -f $RPM_PKG $BUILD_ROOT${BUILD_DIR}
  236. else
  237. BASE_RPM_PKG=$RPM_PKG
  238. fi
  239. }
  240. ## recover apt-get data on host/chroot
  241. apt-get-update(){
  242. case $1 in
  243. --host)
  244. echo -n "apt-get update on host ... "
  245. apt-get update > /dev/null 2>&1
  246. echo "done."
  247. ;;
  248. --chroot)
  249. echo -n "apt-get update on chroot ... "
  250. $__chroot_sh 'apt-get update' > /dev/null 2>&1
  251. echo "done."
  252. ;;
  253. *)
  254. echo apt-get-update: unknown option $1
  255. exit 1
  256. ;;
  257. esac
  258. }
  259. ## mount /home
  260. mount_home(){
  261. if [ "$1" = "--umount" ]; then
  262. [ -d ${BUILD_ROOT}/home ] || return 1
  263. [ -z "$(mount | grep ${BUILD_ROOT}/home)" ] || \
  264. umount ${BUILD_ROOT}/home
  265. else
  266. [ -d ${BUILD_ROOT}/home ] || mkdir -p ${BUILD_ROOT}/home
  267. [ -z "$(mount | grep ${BUILD_ROOT}/home)" ] && \
  268. mount -o rbind /home ${BUILD_ROOT}/home
  269. fi
  270. }
  271. ## mount /tmp for applications which use X
  272. mount_tmp(){
  273. if [ "$1" = "--umount" ]; then
  274. [ -d ${BUILD_ROOT}/tmp ] || return 1
  275. [ -z "$(mount | grep ${BUILD_ROOT}/tmp)" ] || \
  276. umount ${BUILD_ROOT}/tmp
  277. else
  278. [ -d ${BUILD_ROOT}/tmp ] || mkdir -p ${BUILD_ROOT}/tmp
  279. [ -z "$(mount | grep ${BUILD_ROOT}/tmp)" ] && \
  280. mount --bind /tmp ${BUILD_ROOT}/tmp
  281. fi
  282. }
  283. ## mount virtual file systems
  284. mount_vfs(){
  285. if [ "$1" = "--umount" ]; then
  286. # for dir in /sys /proc /dev/shm /dev/pts /dev; do
  287. # [ -z "$(mount | grep ${BUILD_ROOT}${dir})" ] || \
  288. # umount ${BUILD_ROOT}${dir}
  289. # done
  290. [ -d ${BUILD_ROOT}/proc ] || return 1
  291. [ -z "$(mount | grep ${BUILD_ROOT}/proc)" ] || \
  292. umount ${BUILD_ROOT}/proc
  293. else
  294. # for dir in /dev /dev/pts /dev/shm /proc /sys; do
  295. # [ -d ${BUILD_ROOT}${dir} ] || mkdir -p ${BUILD_ROOT}${dir}
  296. # mount --bind ${dir} ${BUILD_ROOT}${dir}
  297. # done
  298. [ -d ${BUILD_ROOT}/proc ] || mkdir -p ${BUILD_ROOT}/proc
  299. [ -z "$(mount | grep ${BUILD_ROOT}/proc)" ] && \
  300. mount --bind /proc ${BUILD_ROOT}/proc
  301. fi
  302. }
  303. ## mount ${CACHE_DIR} to ${ARCHIVES_DIR}
  304. mount_cache_dir(){
  305. if [ "$1" = "--umount" ]; then
  306. [ -d ${ARCHIVES_DIR} ] || return 1
  307. [ -z "$(mount | grep ${ARCHIVES_DIR})" ] || \
  308. umount ${ARCHIVES_DIR}
  309. else
  310. [ -d ${CACHE_DIR} ] || mkdir -p ${CACHE_DIR}
  311. [ -d ${ARCHIVES_DIR} ] || mkdir -p ${ARCHIVES_DIR}
  312. [ -z "$(mount | grep ${ARCHIVES_DIR})" ] && \
  313. mount --bind ${CACHE_DIR} ${ARCHIVES_DIR}
  314. [ -d ${ARCHIVES_DIR}/partial ] || mkdir -p ${ARCHIVES_DIR}/partial
  315. fi
  316. }
  317. ##############################################################################
  318. Clean(){
  319. setup-vbootstrap
  320. # mount_home --umount
  321. # mount_tmp --umount
  322. mount_vfs --umount
  323. mount_cache_dir --umount
  324. apt-get-update --host
  325. if [ -d ${BUILD_ROOT} ]; then
  326. echo -n "Cleaning build root \"${BUILD_ROOT}\" ... "
  327. rm -rf ${BUILD_ROOT}
  328. echo "done."
  329. fi
  330. echo "Cleanup a build farm for ${VERSION} done."
  331. }
  332. Build(){
  333. setup-vbootstrap
  334. if [ $with_dist_upgrade -eq 1 ]; then
  335. ## make bootstrap of ${STABLE_VERSION}
  336. /usr/sbin/vbootstrap \
  337. $(echo ${VERSION} | sed -e "s/VineSeed/${STABLE_VERSION}/") \
  338. ${BUILD_ROOT}
  339. ## aim apt-line to VineSeed
  340. sed -i "s/apt ${STABLE_VERSION}/apt VineSeed/g" \
  341. ${BUILD_ROOT}/etc/apt/sources.list.d/main.list
  342. else
  343. /usr/sbin/vbootstrap ${VERSION} ${BUILD_ROOT}
  344. fi
  345. mount_cache_dir
  346. mount_vfs
  347. # mount_tmp
  348. # mount_home
  349. $__chroot_sh 'apt-get update && apt-get -y dist-upgrade'
  350. ##!! 4.2 has no apt-sourceslist-{plus,nonfree} packages
  351. if [ "$(echo ${VERSION} | sed s/_i386//)" != "4.2" ]; then
  352. $__chroot_sh 'apt-get update && apt-get -y install apt-sourceslist-plus'
  353. $__chroot_sh 'apt-get update && apt-get -y install apt-sourceslist-nonfree'
  354. fi
  355. if [ $with_dist_upgrade -eq 1 ]; then
  356. $__chroot_sh 'apt-get update && apt-get -y dist-upgrade'
  357. fi
  358. $__chroot_sh 'apt-get -y install build-essential'
  359. $__chroot_sh 'apt-get -y install self-build-setup'
  360. $__chroot_sh 'apt-get -y install etcskel shadow-utils'
  361. $__chroot_sh 'cd /dev && /sbin/MAKEDEV console'
  362. $__chroot_sh 'cd /dev && /sbin/MAKEDEV null'
  363. $__chroot_sh 'cd /dev && /sbin/MAKEDEV zero'
  364. $__chroot_sh '/usr/sbin/pwconv'
  365. $__chroot_sh "/usr/sbin/useradd ${BUILD_USER}"
  366. ##!! for rpm-4.8.0 or higher
  367. ##!! (See http://trac.vinelinux.org/wiki/Vine6/AboutUpdateToolchain)
  368. if [ "$(echo ${VERSION} | sed -e "s/\(VineSeed\).*/\1/")" = "VineSeed" ]; then
  369. $__chroot_sh "sed -i -e 's/^%_topdir/#%_topdir/' /home/${BUILD_USER}/.rpmmacros"
  370. fi
  371. # mount_home --umount
  372. # mount_tmp --umount
  373. mount_vfs --umount
  374. mount_cache_dir --umount
  375. apt-get-update --host
  376. echo "Making a build farm for ${VERSION} done."
  377. }
  378. RPM_Remove(){
  379. setup-vbootstrap-rpm
  380. mount_cache_dir
  381. mount_vfs
  382. if [ -f $RPM_PKG ]; then
  383. Msg_NotPackageName_$LOCALE
  384. exit 1
  385. fi
  386. $__chroot_sh "apt-get -y remove $BASE_RPM_PKG"
  387. mount_vfs --umount
  388. mount_cache_dir --umount
  389. apt-get-update --host
  390. }
  391. RPM_Install(){
  392. setup-vbootstrap-rpm
  393. mount_cache_dir
  394. mount_vfs
  395. apt-get-update --chroot
  396. $__chroot_sh "cd ${BUILD_DIR} && apt-get -y install $BASE_RPM_PKG"
  397. mount_vfs --umount
  398. mount_cache_dir --umount
  399. apt-get-update --host
  400. }
  401. RPM_Build(){
  402. setup-vbootstrap-rpm
  403. mount_cache_dir
  404. mount_vfs
  405. if [ ! -f $RPM_PKG ]; then
  406. Msg_NotSourceRPM_$LOCALE
  407. exit 1
  408. fi
  409. RPM_PKG_USER=$(stat -c %U $RPM_PKG)
  410. RPM_PKG_GROUP=$(stat -c %G $RPM_PKG)
  411. local __install="install -p -v -o ${RPM_PKG_USER} -g ${RPM_PKG_GROUP}"
  412. RPM_PKG_ARCH_LIST="RPMS/i386 RPMS/x86_64 RPMS/ppc RPMS/noarch SRPMS"
  413. [ -z "${TARGET}" ] || \
  414. RPM_PKG_ARCH_LIST="RPMS/${TARGET} ${RPM_PKG_ARCH_LIST}"
  415. ## make src.rpm for $VERSION
  416. $__chroot_sh "cd ${BUILD_DIR} && su ${BUILD_USER} -c 'rpm -ivh $BASE_RPM_PKG'"
  417. $__chroot_sh "cd ${BUILD_DIR} && su ${BUILD_USER} -c 'rpmbuild -bs --nodeps --clean --rmsource --rmspec ${BUILD_DIR}/SPECS/*.spec'"
  418. ## change ${DIST_RELEASE}
  419. BASE_RPM_PKG=$(echo $BASE_RPM_PKG | sed -e "s/vl\([0-9]*\)\./vl${DIST_RELEASE}\./")
  420. ## rebuild $BASE_RPM_PKG on ${DIST_RELEASE}
  421. $__chroot_sh "cd ${BUILD_DIR}/SRPMS && apt-get -y build-dep $BASE_RPM_PKG"
  422. $__chroot_sh "cd ${BUILD_DIR}/SRPMS && su ${BUILD_USER} -c 'rpmbuild --rebuild $RPM_OPTS $BASE_RPM_PKG'"
  423. $__chroot_sh "cd ${BUILD_DIR} && apt-get -y install $(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')"
  424. ## copy built rpms to ${HOME}/rpm/ for each archtectures
  425. echo "Copying built rpms to ${HOME}/rpm/ for each archtectures ... "
  426. for i in $RPM_PKG_ARCH_LIST; do \
  427. if [ -d $BUILD_ROOT${BUILD_DIR}/${i} ]; then
  428. [ -d ${BUILT_RPMS_DIR}/${i} ] || \
  429. $__install -d ${BUILT_RPMS_DIR}/${i}/
  430. find $BUILD_ROOT${BUILD_DIR}/${i} -type f -regex '.*\.rpm' \
  431. -exec $__install -m0644 {} ${BUILT_RPMS_DIR}/${i}/ \;
  432. fi
  433. done
  434. mount_vfs --umount
  435. mount_cache_dir --umount
  436. apt-get-update --host
  437. echo "done."
  438. }
  439. ##############################################################################
  440. setup-vbuilder
  441. check-parameter $* || exit 1
  442. while [ $# -gt 0 ]; do
  443. tmpARG=$1
  444. case $tmpARG in
  445. --version|--arch|--target|--build-rpm|--install-rpm|--remove-rpm)
  446. shift
  447. ;;
  448. --dist-upgrade|--with-compat32|--build|--clean)
  449. ;;
  450. *)
  451. echo unknown option $1
  452. exit 1
  453. ;;
  454. esac
  455. case $tmpARG in
  456. --version)
  457. VERSION=$1
  458. ;;
  459. --arch)
  460. VARCH=$1
  461. ;;
  462. --dist-upgrade)
  463. with_dist_upgrade=1
  464. ;;
  465. --target)
  466. TARGET=$1
  467. RPM_OPTS="${RPM_OPTS} --target $TARGET"
  468. ;;
  469. --with-compat32)
  470. RPM_OPTS="${RPM_OPTS} --with compat32"
  471. ;;
  472. --build-rpm)
  473. RPM_PKG=$1
  474. RPM_Build || exit 1
  475. ;;
  476. --install-rpm)
  477. RPM_PKG=$1
  478. RPM_Install || exit 1
  479. ;;
  480. --remove-rpm)
  481. RPM_PKG=$1
  482. RPM_Remove || exit 1
  483. ;;
  484. --build)
  485. Build
  486. ;;
  487. --clean)
  488. Clean
  489. ;;
  490. esac
  491. shift
  492. done
  493. exit