vbuilder.sh.in 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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-chroot {|--umount} [file system|name]
  260. ## support file systems: /home /tmp /sys /proc /dev/shm /dev/pts /dev
  261. ## support name: vfs chache_dir
  262. ## NOTE: /tmp needs for applications which use X
  263. ## vfs is virtual file systems
  264. ## cache_dir is ${CACHE_DIR} to ${ARCHIVES_DIR}
  265. mount-chroot(){
  266. if [ "$1" = "--umount" ]; then
  267. mount-chroot-umount $2 || return 1
  268. else
  269. mount-chroot-mount $1 || return 1
  270. fi
  271. return 0
  272. }
  273. ## mount-chroot-umount [file system|name]
  274. mount-chroot-umount(){
  275. local fs=$1
  276. case $fs in
  277. /home|/tmp|/sys|/proc|/dev/shm|/dev/pts|/dev)
  278. [ -d ${BUILD_ROOT}${fs} ] || return 1
  279. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] || \
  280. umount ${BUILD_ROOT}${fs}
  281. return 0
  282. ;;
  283. vfs)
  284. # for dir in /sys /proc /dev/shm /dev/pts /dev; do
  285. # mount-chroot-umount ${dir} || return 1
  286. # done
  287. [ -d ${BUILD_ROOT}/proc ] || return 1
  288. [ -z "$(mount | grep ${BUILD_ROOT}/proc)" ] || \
  289. umount ${BUILD_ROOT}/proc
  290. return 0
  291. ;;
  292. cache_dir)
  293. [ -d ${ARCHIVES_DIR} ] || return 1
  294. [ -z "$(mount | grep ${ARCHIVES_DIR})" ] || \
  295. umount ${ARCHIVES_DIR}
  296. return 0
  297. ;;
  298. *)
  299. echo mount-chroot-umount: unknown file system $fs
  300. exit 1
  301. ;;
  302. esac
  303. }
  304. ## mount-chroot-mount [file system|name]
  305. mount-chroot-mount(){
  306. local fs=$1
  307. local mnt_opts=""
  308. case $fs in
  309. /home)
  310. mnt_opts="-o rbind"
  311. ;;
  312. *)
  313. mnt_opts="--bind"
  314. ;;
  315. esac
  316. case $fs in
  317. /home|/tmp|/sys|/proc|/dev/shm|/dev/pts|/dev)
  318. [ -d ${BUILD_ROOT}${fs} ] || mkdir -p ${BUILD_ROOT}${fs}
  319. [ -z "$(mount | grep ${BUILD_ROOT}${fs})" ] && \
  320. mount ${mnt_opts} ${fs} ${BUILD_ROOT}${fs}
  321. return 0
  322. ;;
  323. vfs)
  324. # for dir in /dev /dev/pts /dev/shm /proc /sys; do
  325. # mount-chroot-mount ${dir} || return 1
  326. # done
  327. mount-chroot-mount /proc || return 1
  328. return 0
  329. ;;
  330. cache_dir)
  331. [ -d ${CACHE_DIR} ] || mkdir -p ${CACHE_DIR}
  332. [ -d ${ARCHIVES_DIR} ] || mkdir -p ${ARCHIVES_DIR}
  333. [ -z "$(mount | grep ${ARCHIVES_DIR})" ] && \
  334. mount ${mnt_opts} ${CACHE_DIR} ${ARCHIVES_DIR}
  335. [ -d ${ARCHIVES_DIR}/partial ] || mkdir -p ${ARCHIVES_DIR}/partial
  336. return 0
  337. ;;
  338. esac
  339. }
  340. ##############################################################################
  341. Clean(){
  342. setup-vbootstrap
  343. # mount-chroot --umount /home
  344. # mount-chroot --umount /tmp
  345. mount-chroot --umount vfs
  346. mount-chroot --umount cache_dir
  347. apt-get-update --host
  348. if [ -d ${BUILD_ROOT} ]; then
  349. echo -n "Cleaning build root \"${BUILD_ROOT}\" ... "
  350. rm -rf ${BUILD_ROOT}
  351. echo "done."
  352. fi
  353. echo "Cleanup a build farm for ${VERSION} done."
  354. }
  355. Build(){
  356. setup-vbootstrap
  357. if [ $with_dist_upgrade -eq 1 ]; then
  358. ## make bootstrap of ${STABLE_VERSION}
  359. /usr/sbin/vbootstrap \
  360. $(echo ${VERSION} | sed -e "s/VineSeed/${STABLE_VERSION}/") \
  361. ${BUILD_ROOT}
  362. ## aim apt-line to VineSeed
  363. sed -i "s/apt ${STABLE_VERSION}/apt VineSeed/g" \
  364. ${BUILD_ROOT}/etc/apt/sources.list.d/main.list
  365. else
  366. /usr/sbin/vbootstrap ${VERSION} ${BUILD_ROOT}
  367. fi
  368. mount-chroot cache_dir
  369. mount-chroot vfs
  370. # mount-chroot /tmp
  371. # mount-chroot /home
  372. $__chroot_sh 'apt-get update && apt-get -y dist-upgrade'
  373. ##!! 4.2 has no apt-sourceslist-{plus,nonfree} packages
  374. if [ "$(echo ${VERSION} | sed s/_i386//)" != "4.2" ]; then
  375. $__chroot_sh 'apt-get update && apt-get -y install apt-sourceslist-plus'
  376. $__chroot_sh 'apt-get update && apt-get -y install apt-sourceslist-nonfree'
  377. fi
  378. if [ $with_dist_upgrade -eq 1 ]; then
  379. $__chroot_sh 'apt-get update && apt-get -y dist-upgrade'
  380. fi
  381. $__chroot_sh 'apt-get -y install build-essential'
  382. $__chroot_sh 'apt-get -y install self-build-setup'
  383. $__chroot_sh 'apt-get -y install etcskel shadow-utils'
  384. $__chroot_sh 'cd /dev && /sbin/MAKEDEV console'
  385. $__chroot_sh 'cd /dev && /sbin/MAKEDEV null'
  386. $__chroot_sh 'cd /dev && /sbin/MAKEDEV zero'
  387. $__chroot_sh '/usr/sbin/pwconv'
  388. $__chroot_sh "/usr/sbin/useradd ${BUILD_USER}"
  389. ##!! for rpm-4.8.0 or higher
  390. ##!! (See http://trac.vinelinux.org/wiki/Vine6/AboutUpdateToolchain)
  391. if [ "$(echo ${VERSION} | sed -e "s/\(VineSeed\).*/\1/")" = "VineSeed" ]; then
  392. $__chroot_sh "sed -i -e 's/^%_topdir/#%_topdir/' /home/${BUILD_USER}/.rpmmacros"
  393. fi
  394. # mount-chroot --umount /home
  395. # mount-chroot --umount /tmp
  396. mount-chroot --umount vfs
  397. mount-chroot --umount cache_dir
  398. apt-get-update --host
  399. echo "Making a build farm for ${VERSION} done."
  400. }
  401. RPM_Remove(){
  402. setup-vbootstrap-rpm
  403. mount-chroot cache_dir
  404. mount-chroot vfs
  405. if [ -f $RPM_PKG ]; then
  406. Msg_NotPackageName_$LOCALE
  407. exit 1
  408. fi
  409. $__chroot_sh "apt-get -y remove $BASE_RPM_PKG"
  410. mount-chroot --umount vfs
  411. mount-chroot --umount cache_dir
  412. apt-get-update --host
  413. }
  414. RPM_Install(){
  415. setup-vbootstrap-rpm
  416. mount-chroot cache_dir
  417. mount-chroot vfs
  418. apt-get-update --chroot
  419. $__chroot_sh "cd ${BUILD_DIR} && apt-get -y install $BASE_RPM_PKG"
  420. mount-chroot --umount vfs
  421. mount-chroot --umount cache_dir
  422. apt-get-update --host
  423. }
  424. RPM_Build(){
  425. setup-vbootstrap-rpm
  426. mount-chroot cache_dir
  427. mount-chroot vfs
  428. if [ ! -f $RPM_PKG ]; then
  429. Msg_NotSourceRPM_$LOCALE
  430. exit 1
  431. fi
  432. RPM_PKG_USER=$(stat -c %U $RPM_PKG)
  433. RPM_PKG_GROUP=$(stat -c %G $RPM_PKG)
  434. local __install="install -p -v -o ${RPM_PKG_USER} -g ${RPM_PKG_GROUP}"
  435. RPM_PKG_ARCH_LIST="RPMS/i386 RPMS/x86_64 RPMS/ppc RPMS/noarch SRPMS"
  436. [ -z "${TARGET}" ] || \
  437. RPM_PKG_ARCH_LIST="RPMS/${TARGET} ${RPM_PKG_ARCH_LIST}"
  438. ## make src.rpm for $VERSION
  439. $__chroot_sh "cd ${BUILD_DIR} && su ${BUILD_USER} -c 'rpm -ivh $BASE_RPM_PKG'"
  440. $__chroot_sh "cd ${BUILD_DIR} && su ${BUILD_USER} -c 'rpmbuild -bs --nodeps --clean --rmsource --rmspec ${BUILD_DIR}/SPECS/*.spec'"
  441. ## change ${DIST_RELEASE}
  442. BASE_RPM_PKG=$(echo $BASE_RPM_PKG | sed -e "s/vl\([0-9]*\)\./vl${DIST_RELEASE}\./")
  443. ## rebuild $BASE_RPM_PKG on ${DIST_RELEASE}
  444. $__chroot_sh "cd ${BUILD_DIR}/SRPMS && apt-get -y build-dep $BASE_RPM_PKG"
  445. $__chroot_sh "cd ${BUILD_DIR}/SRPMS && su ${BUILD_USER} -c 'rpmbuild --rebuild $RPM_OPTS $BASE_RPM_PKG'"
  446. $__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')"
  447. ## copy built rpms to ${HOME}/rpm/ for each archtectures
  448. echo "Copying built rpms to ${HOME}/rpm/ for each archtectures ... "
  449. for i in $RPM_PKG_ARCH_LIST; do \
  450. if [ -d $BUILD_ROOT${BUILD_DIR}/${i} ]; then
  451. [ -d ${BUILT_RPMS_DIR}/${i} ] || \
  452. $__install -d ${BUILT_RPMS_DIR}/${i}/
  453. find $BUILD_ROOT${BUILD_DIR}/${i} -type f -regex '.*\.rpm' \
  454. -exec $__install -m0644 {} ${BUILT_RPMS_DIR}/${i}/ \;
  455. fi
  456. done
  457. mount-chroot --umount vfs
  458. mount-chroot --umount cache_dir
  459. apt-get-update --host
  460. echo "done."
  461. }
  462. ##############################################################################
  463. setup-vbuilder
  464. check-parameter $* || exit 1
  465. while [ $# -gt 0 ]; do
  466. tmpARG=$1
  467. case $tmpARG in
  468. --version|--arch|--target|--build-rpm|--install-rpm|--remove-rpm)
  469. shift
  470. ;;
  471. --dist-upgrade|--with-compat32|--build|--clean)
  472. ;;
  473. *)
  474. echo unknown option $1
  475. exit 1
  476. ;;
  477. esac
  478. case $tmpARG in
  479. --version)
  480. VERSION=$1
  481. ;;
  482. --arch)
  483. VARCH=$1
  484. ;;
  485. --dist-upgrade)
  486. with_dist_upgrade=1
  487. ;;
  488. --target)
  489. TARGET=$1
  490. RPM_OPTS="${RPM_OPTS} --target $TARGET"
  491. ;;
  492. --with-compat32)
  493. RPM_OPTS="${RPM_OPTS} --with compat32"
  494. ;;
  495. --build-rpm)
  496. RPM_PKG=$1
  497. RPM_Build || exit 1
  498. ;;
  499. --install-rpm)
  500. RPM_PKG=$1
  501. RPM_Install || exit 1
  502. ;;
  503. --remove-rpm)
  504. RPM_PKG=$1
  505. RPM_Remove || exit 1
  506. ;;
  507. --build)
  508. Build
  509. ;;
  510. --clean)
  511. Clean
  512. ;;
  513. esac
  514. shift
  515. done
  516. exit