vbuilder.sh.in 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/bin/bash
  2. # -*- coding: utf-8-unix -*-
  3. ## default settings
  4. VBOOTSTRAP_DIR=/var/local/vbootstrap
  5. [ -d $VBOOTSTRAP_DIR ] || mkdir -p $VBOOTSTRAP_DIR
  6. DEFAULT_VERSION=@@DEFAULT_VERSION@@
  7. VERSION=$DEFAULT_VERSION
  8. Usage(){
  9. cat<<EOF
  10. Usage: $(basename $0) {--version [version]} {--arch [arch]} {--with-compat32} {--build-rpm [src.rpm]|--install-rpm [arch.rpm]|--remove-rpm [package]|--build|--clean}
  11. --version: set [version] (default: ${DEFAULT_VERSION})
  12. --arch: set [arch] (default: $(uname -i))
  13. --with-compat32: build with compat32 on boostrap
  14. --build-rpm: build [src.rpm] on boostrap
  15. --install-rpm: install [arch.rpm] on boostrap
  16. --remove-rpm: remove [package] on boostrap
  17. --build: build boostrap of [version]
  18. --clean: clean boostrap of [version]
  19. For example,
  20. * make plain build environment:
  21. $(basename $0) --clean --build
  22. * build source RPM:
  23. $(basename $0) --build-rpm [src.rpm]
  24. * make Vine Linux 4.2 environment:
  25. $(basename $0) --version 4.2 --clean --build
  26. * i386 chroot on x86_64:
  27. $(basename $0) --arch i386 --clean --build
  28. * build compat32:
  29. $(basename $0) --arch i386 --with-compat32 --build-rpm [src.rpm]
  30. EOF
  31. exit 1
  32. }
  33. FLAG_setup_vbootstrap=0
  34. setup-vbootstrap(){
  35. if [ ${FLAG_setup_vbootstrap} -eq 0 ]; then
  36. FLAG_setup_vbootstrap=1
  37. [ -z ${VARCH} ] && VARCH=$(uname -i)
  38. [ "${VARCH}" != "$(uname -i)" ] && VERSION=${VERSION}_${VARCH}
  39. fi
  40. BUILD_ROOT=${VBOOTSTRAP_DIR}/${VERSION}
  41. BUILD_USER=vbuilder
  42. CACHE_DIR=${VBOOTSTRAP_DIR}/cache/${VERSION}
  43. BUILD_DIR=/home/${BUILD_USER}/rpm
  44. mkdir -p $VBOOTSTRAP_DIR
  45. }
  46. ##############################################################################
  47. Clean(){
  48. setup-vbootstrap
  49. umount ${BUILD_ROOT}/proc
  50. if [ -d ${BUILD_ROOT}/var/cache/apt/archives ]; then
  51. [ -d ${CACHE_DIR}/apt/archives ] || mkdir -p ${CACHE_DIR}/apt/archives
  52. mv -f ${BUILD_ROOT}/var/cache/apt/archives/*.rpm ${CACHE_DIR}/apt/archives/
  53. fi
  54. rm -rf $BUILD_ROOT/
  55. }
  56. Build(){
  57. setup-vbootstrap
  58. /usr/sbin/vbootstrap ${VERSION} ${BUILD_ROOT}
  59. [ -d ${BUILD_ROOT}/proc ] || mkdir -p ${BUILD_ROOT}/proc
  60. mount -t proc proc ${BUILD_ROOT}/proc
  61. [ -d ${CACHE_DIR} -a "${VARCH}" = "$(uname -i)" ] && \
  62. cp -f ${CACHE_DIR}/apt/archives/*.rpm ${BUILD_ROOT}/var/cache/apt/archives/
  63. /usr/sbin/chroot ${BUILD_ROOT} \
  64. /bin/sh -c 'apt-get update && apt-get -y dist-upgrade'
  65. /usr/sbin/chroot ${BUILD_ROOT} \
  66. /bin/sh -c 'apt-get -y install apt-sourceslist-plus && apt-get update'
  67. /usr/sbin/chroot ${BUILD_ROOT} \
  68. /bin/sh -c 'apt-get -y install apt-sourceslist-nonfree && apt-get update'
  69. /usr/sbin/chroot ${BUILD_ROOT} \
  70. /bin/sh -c 'apt-get -y install build-essential self-build-setup'
  71. /usr/sbin/chroot ${BUILD_ROOT} \
  72. /bin/sh -c 'apt-get -y install etcskel shadow-utils'
  73. /usr/sbin/chroot ${BUILD_ROOT} /bin/sh -c 'cd /dev && /sbin/MAKEDEV console'
  74. /usr/sbin/chroot ${BUILD_ROOT} /bin/sh -c 'cd /dev && /sbin/MAKEDEV null'
  75. /usr/sbin/chroot ${BUILD_ROOT} /bin/sh -c 'cd /dev && /sbin/MAKEDEV zero'
  76. /usr/sbin/chroot ${BUILD_ROOT} /bin/sh -c '/usr/sbin/pwconv'
  77. /usr/sbin/chroot ${BUILD_ROOT} /bin/sh -c "/usr/sbin/useradd ${BUILD_USER}"
  78. }
  79. setup-vbootstrap-rpm(){
  80. setup-vbootstrap
  81. if [ -f $RPM_PKG ]; then
  82. BASE_RPM_PKG=$(basename $RPM_PKG)
  83. cp -f $RPM_PKG $BUILD_ROOT${BUILD_DIR}
  84. else
  85. BASE_RPM_PKG=$RPM_PKG
  86. fi
  87. }
  88. RPM_Remove(){
  89. setup-vbootstrap-rpm
  90. if [ -f $RPM_PKG ]; then
  91. echo "$RPM_PKG: not package name."
  92. exit 1
  93. fi
  94. /usr/sbin/chroot ${BUILD_ROOT} \
  95. /bin/sh -c "apt-get -y remove $BASE_RPM_PKG"
  96. }
  97. RPM_Install(){
  98. setup-vbootstrap-rpm
  99. /usr/sbin/chroot ${BUILD_ROOT} \
  100. /bin/sh -c "cd ${BUILD_DIR} && apt-get -y install $BASE_RPM_PKG"
  101. }
  102. RPM_Build(){
  103. setup-vbootstrap-rpm
  104. if [ ! -f $RPM_PKG ]; then
  105. echo "$RPM_PKG: not source RPM package."
  106. exit 1
  107. fi
  108. /usr/sbin/chroot ${BUILD_ROOT} \
  109. /bin/sh -c "cd ${BUILD_DIR} && apt-get -y build-dep $BASE_RPM_PKG"
  110. /usr/sbin/chroot ${BUILD_ROOT} \
  111. /bin/sh -c "cd ${BUILD_DIR} && su ${BUILD_USER} -c 'rpmbuild --rebuild $RPM_OPTS $BASE_RPM_PKG'"
  112. /usr/sbin/chroot ${BUILD_ROOT} \
  113. /bin/sh -c "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/$/ \\/g')"
  114. }
  115. [ $# -eq 0 ] && Usage
  116. while [ $# -gt 0 ]; do
  117. case "$1" in
  118. --version)
  119. shift;
  120. VERSION=$1
  121. ;;
  122. --arch)
  123. shift;
  124. VARCH=$1
  125. ;;
  126. --with-compat32)
  127. RPM_OPTS="--with compat32"
  128. ;;
  129. --build-rpm)
  130. shift;
  131. RPM_PKG=$1
  132. RPM_Build
  133. ;;
  134. --install-rpm)
  135. shift;
  136. RPM_PKG=$1
  137. RPM_Install
  138. ;;
  139. --remove-rpm)
  140. shift;
  141. RPM_PKG=$1
  142. RPM_Remove
  143. ;;
  144. --build)
  145. Build
  146. ;;
  147. --clean)
  148. Clean
  149. ;;
  150. *)
  151. Usage
  152. ;;
  153. esac
  154. shift
  155. done
  156. exit