]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - release/tools/vmimage.subr
MFC r323812:
[FreeBSD/stable/10.git] / release / tools / vmimage.subr
1 #!/bin/sh
2 #
3 # $FreeBSD$
4 #
5 #
6 # Common functions for virtual machine image build scripts.
7 #
8
9 export PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
10 trap "cleanup" INT QUIT TRAP ABRT TERM
11
12 write_partition_layout() {
13         if [ -z "${NOSWAP}" ]; then
14                 SWAPOPT="-p freebsd-swap/swapfs::${SWAPSIZE}"
15         fi
16
17         _OBJDIR="$(make -C ${WORLDDIR} -V .OBJDIR)"
18         _OBJDIR="$(realpath ${_OBJDIR})"
19         if [ -d "${_OBJDIR%%/usr/src}/${TARGET}.${TARGET_ARCH}" ]; then
20                 BOOTFILES="/${_OBJDIR%%/usr/src}/${TARGET}.${TARGET_ARCH}/usr/src/sys/boot"
21         else
22                 BOOTFILES="/${_OBJDIR}/sys/boot"
23         fi
24
25         case "${TARGET}:${TARGET_ARCH}" in
26                 amd64:amd64 | i386:i386)
27                         mkimg -s gpt -f ${VMFORMAT} \
28                                 -b ${BOOTFILES}/i386/pmbr/pmbr \
29                                 -p freebsd-boot/bootfs:=${BOOTFILES}/i386/gptboot/gptboot \
30                                 ${SWAPOPT} \
31                                 -p freebsd-ufs/rootfs:=${VMBASE} \
32                                 -o ${VMIMAGE}
33                         ;;
34                 powerpc:powerpc*)
35                         mkimg -s apm -f ${VMFORMAT} \
36                                 -p apple-boot/bootfs:=${BOOTFILES}/powerpc/boot1.chrp/boot1.hfs \
37                                 ${SWAPOPT} \
38                                 -p freebsd-ufs/rootfs:=${VMBASE} \
39                                 -o ${VMIMAGE}
40                         ;;
41                 *)
42                         # ENOTSUPP
43                         return 1
44                         ;;
45         esac
46
47         return 0
48 }
49
50 err() {
51         printf "${@}\n"
52         cleanup
53         return 1
54 }
55
56 cleanup() {
57         if [ -c "${DESTDIR}/dev/null" ]; then
58                 umount_loop ${DESTDIR}/dev 2>/dev/null
59         fi
60         umount_loop ${DESTDIR}
61         if [ ! -z "${mddev}" ]; then
62                 mdconfig -d -u ${mddev}
63         fi
64
65         return 0
66 }
67
68 vm_create_base() {
69         # Creates the UFS root filesystem for the virtual machine disk,
70         # written to the formatted disk image with mkimg(1).
71
72         mkdir -p ${DESTDIR}
73         truncate -s ${VMSIZE} ${VMBASE}
74         mddev=$(mdconfig -f ${VMBASE})
75         newfs /dev/${mddev}
76         mount /dev/${mddev} ${DESTDIR}
77
78         return 0
79 }
80
81 vm_copy_base() {
82         # Creates a new UFS root filesystem and copies the contents of the
83         # current root filesystem into it.  This produces a "clean" disk
84         # image without any remnants of files which were created temporarily
85         # during image-creation and have since been deleted (e.g., downloaded
86         # package archives).
87
88         mkdir -p ${DESTDIR}/old
89         mdold=$(mdconfig -f ${VMBASE})
90         mount /dev/${mdold} ${DESTDIR}/old
91
92         truncate -s ${VMSIZE} ${VMBASE}.tmp
93         mkdir -p ${DESTDIR}/new
94         mdnew=$(mdconfig -f ${VMBASE}.tmp)
95         newfs /dev/${mdnew}
96         mount /dev/${mdnew} ${DESTDIR}/new
97
98         tar -cf- -C ${DESTDIR}/old . | tar -xUf- -C ${DESTDIR}/new
99
100         umount_loop /dev/${mdold}
101         rmdir ${DESTDIR}/old
102         mdconfig -d -u ${mdold}
103
104         umount_loop /dev/${mdnew}
105         rmdir ${DESTDIR}/new
106         tunefs -n enable /dev/${mdnew}
107         mdconfig -d -u ${mdnew}
108         mv ${VMBASE}.tmp ${VMBASE}
109 }
110
111 vm_install_base() {
112         # Installs the FreeBSD userland/kernel to the virtual machine disk.
113
114         cd ${WORLDDIR} && \
115                 make DESTDIR=${DESTDIR} \
116                 installworld installkernel distribution || \
117                 err "\n\nCannot install the base system to ${DESTDIR}."
118
119         # Bootstrap etcupdate(8) and mergemaster(8) databases.
120         mkdir -p ${DESTDIR}/var/db/etcupdate
121         etcupdate extract -B \
122                 -M "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
123                 -s ${WORLDDIR} -d ${DESTDIR}/var/db/etcupdate
124         sh ${WORLDDIR}/release/scripts/mm-mtree.sh -m ${WORLDDIR} \
125                 -F "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
126                 -D ${DESTDIR}
127
128         echo '# Custom /etc/fstab for FreeBSD VM images' \
129                 > ${DESTDIR}/etc/fstab
130         echo '/dev/gpt/rootfs   /       ufs     rw      1       1' \
131                 >> ${DESTDIR}/etc/fstab
132         if [ -z "${NOSWAP}" ]; then
133                 echo '/dev/gpt/swapfs  none    swap    sw      0       0' \
134                         >> ${DESTDIR}/etc/fstab
135         fi
136
137         mkdir -p ${DESTDIR}/dev
138         mount -t devfs devfs ${DESTDIR}/dev
139         chroot ${DESTDIR} /usr/bin/newaliases
140         chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart
141         umount_loop ${DESTDIR}/dev
142
143         cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf
144
145         return 0
146 }
147
148 vm_extra_install_base() {
149         # Prototype.  When overridden, runs extra post-installworld commands
150         # as needed, based on the target virtual machine image or cloud
151         # provider image target.
152
153         return 0
154 }
155
156 vm_extra_enable_services() {
157         if [ ! -z "${VM_RC_LIST}" ]; then
158                 for _rcvar in ${VM_RC_LIST}; do
159                         echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
160                 done
161         fi
162
163         if [ -z "${VMCONFIG}" -o -c "${VMCONFIG}" ]; then
164                 echo 'ifconfig_DEFAULT="DHCP inet6 accept_rtadv"' >> \
165                         ${DESTDIR}/etc/rc.conf
166         fi
167
168         return 0
169 }
170
171 vm_extra_install_packages() {
172         if [ -z "${VM_EXTRA_PACKAGES}" ]; then
173                 return 0
174         fi
175         mkdir -p ${DESTDIR}/dev
176         mount -t devfs devfs ${DESTDIR}/dev
177         chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
178                 /usr/sbin/pkg bootstrap -y
179         chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
180                 /usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES}
181         umount_loop ${DESTDIR}/dev
182
183         return 0
184 }
185
186 vm_extra_install_ports() {
187         # Prototype.  When overridden, installs additional ports within the
188         # virtual machine environment.
189
190         return 0
191 }
192
193 vm_extra_pre_umount() {
194         # Prototype.  When overridden, performs additional tasks within the
195         # virtual machine environment prior to unmounting the filesystem.
196         # Note: When overriding this function, removing resolv.conf in the
197         # disk image must be included.
198
199         rm -f ${DESTDIR}/etc/resolv.conf
200         return 0
201 }
202
203 vm_extra_pkg_rmcache() {
204         if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then
205                 chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
206                         /usr/local/sbin/pkg clean -y -a
207         fi
208
209         return 0
210 }
211
212 umount_loop() {
213         DIR=$1
214         i=0
215         sync
216         while ! umount ${DIR}; do
217                 i=$(( $i + 1 ))
218                 if [ $i -ge 10 ]; then
219                         # This should never happen.  But, it has happened.
220                         echo "Cannot umount(8) ${DIR}"
221                         echo "Something has gone horribly wrong."
222                         return 1
223                 fi
224                 sleep 1
225         done
226
227         return 0
228 }
229
230 vm_create_disk() {
231         echo "Creating image...  Please wait."
232         echo
233
234         write_partition_layout || return 1
235
236         return 0
237 }
238
239 vm_extra_create_disk() {
240
241         return 0
242 }
243