]> git.saurik.com Git - apple/launchd.git/blame - launchd/src/rc.netboot
launchd-106.13.tar.gz
[apple/launchd.git] / launchd / src / rc.netboot
CommitLineData
e91b9f68
A
1#!/bin/sh
2##
3# Copyright 2002 Apple Computer, Inc.
4#
5# This script configures NetBoot
6##
7
8. /etc/rc.common
9
10#
11# Define: NETBOOT_SHADOW
12# Purpose:
13# To change the behavior of the system when choosing a netboot shadow
14# to use.
15# Values:
16# -NETWORK- Try to use the network for the shadow file, if
17# that fails, use the local drive
18# -NETWORK_ONLY- Only use the network, fail if not available
19# -LOCAL- Use the local drive for the shadow file, if that
20# fails, use the network
21# -LOCAL_ONLY- Only use the local drive for the shadow, fail if
22# not available
23
24NETBOOT_MOUNT=/var/netboot
25NETBOOT_SHADOW=${NETBOOT_SHADOW:-NETWORK-}
26
27Failed()
28{
29 echo rc.netboot: $1
30 exit 1
31}
32
33common_start()
34{
35 netboot_dir=$1
36 netboot_shadow=$2
37 if [ "${netboot_dir}" = "" ] ; then
38 Failed "netboot_dir is empty"
39 fi
40 if [ "${netboot_shadow}" = "" ] ; then
41 Failed "netboot_shadow is empty"
42 fi
43 netboot_shadow="${netboot_dir}/${netboot_shadow}"
44 if ! mkdir -p "${netboot_dir}" ; then
45 Failed "create ${netboot_dir} failed"
46 fi
47 chmod 700 "${netboot_dir}"
48 mount -u -o ro /
49 root_device=$(mount | sed -n 's:/dev/\(.*\) on / .*:\1:p')
50 case "${root_device}" in
51 vn*)
52 if ! touch "${netboot_shadow}" ; then
53 Failed "create ${netboot_shadow} failed"
54 fi
55 chmod 600 "${netboot_shadow}"
56 if ! /usr/libexec/vndevice shadow "/dev/r${root_device}" "${netboot_shadow}" ; then
57 Failed "vndevice shadow failed"
58 fi
59 ;;
60 "")
61 Failed "root device unknown"
62 ;;
63 *)
64 if ! touch "${netboot_shadow}" ; then
65 Failed "failed to create shadow ${netboot_shadow}"
66 fi
67 chmod 600 "${netboot_shadow}"
68 if ! /usr/bin/nbdst -recycle "${root_device}" "${netboot_shadow}" ; then
69 Failed "nbdst failed"
70 fi
71 ;;
72 esac
73}
74
75local_mount()
76{
77 volinfo=`autodiskmount -F 2>/dev/null`
78 if [ $? -ne 0 ]; then
79 echo "autodiskmount -F found no local drives"
80 return 1
81 fi
82 set ${volinfo}
83 devname=$1
84 fstype=$2
85
86 mount -t "${fstype}" -o nosuid,nodev "/dev/${devname}" "${NETBOOT_MOUNT}" 2>&1
87 if [ $? -ne 0 ]; then
88 echo "mount of ${devname} failed"
89 return 1
90 fi
91 common_start "${NETBOOT_MOUNT}/.com.apple.NetBootX" shadowfile
92 return 0
93}
94
95network_mount()
96{
97 mount_from=$(ipconfig netbootoption shadow_mount_path 2>&1)
98 if [ $? -ne 0 ]; then
99 echo "no network shadow mount path available"
100 return 1
101 fi
102 shadow_path=$(ipconfig netbootoption shadow_file_path 2>&1)
103 if [ $? -ne 0 ]; then
104 echo "no network shadow file path available"
105 return 1
106 fi
107 case "${mount_from}" in
108 afp:*) fstype=afp;;
109 nfs:*) fstype=nfs;;
110 *) echo "unknown network filesystem mount from ${mount_from}"
111 return 1
112 ;;
113 esac
114 mount -t "${fstype}" "${mount_from}" "${NETBOOT_MOUNT}"
115 if [ $? -ne 0 ]; then
116 echo "mount -t ${fstype} ${mount_from} ${NETBOOT_MOUNT} failed"
117 return 1
118 fi
119 common_start "${NETBOOT_MOUNT}" "${shadow_path}"
120 return 0
121}
122
123do_start()
124{
125 case "${NETBOOT_SHADOW}" in
126 -LOCAL_ONLY-)
127 err=$(local_mount)
128 if [ $? -ne 0 ]; then
129 Failed "${err}"
130 fi
131 ;;
132 -LOCAL-)
133 err=$(local_mount)
134 if [ $? -ne 0 ]; then
135 err=$(network_mount)
136 if [ $? -ne 0 ]; then
137 Failed "Could not find a local or network drive"
138 fi
139 fi
140 ;;
141 -NETWORK_ONLY-)
142 err=$(network_mount)
143 if [ $? -ne 0 ]; then
144 Failed "${err}"
145 fi
146 ;;
147
148 *)
149 err=$(network_mount)
150 if [ $? -ne 0 ]; then
151 err=$(local_mount)
152 if [ $? -ne 0 ]; then
153 Failed "Could not find a network or local drive"
154 fi
155 fi
156 ;;
157 esac
158
159}
160
161do_computername()
162{
163 machine_name=$(ipconfig netbootoption machine_name 2>&1)
164 if [ $? -ne 0 ]; then
165 echo "no machine name option available"
166 else
167 echo "Setting ComputerName to ${machine_name}"
168 scutil --set ComputerName "${machine_name}"
169 fi
170}
171
172do_vm()
173{
174 swapdir=${1:-/private/var/vm}
175
176 mounted_from=$(mount | sed -n 's:\(.*\) on .*/var/netboot.*:\1:p')
177
178 case "${mounted_from}" in
179 /dev/*)
180 netboot_dir="${NETBOOT_MOUNT}/.com.apple.NetBootX"
181 if [ -d "${netboot_dir}" ]; then
182 rm -rf "${netboot_dir}/app_profile"
183 rm -rf "${swapdir}"
184 ln -s "${netboot_dir}" "${swapdir}"
185 fi
186 ;;
187 *)
188 ;;
189 esac
190}
191
192if [ $# -lt 1 ] ; then
193 exit 0
194fi
195
196command=$1
197
198shift
199
200case "${command}" in
201 start)
202 do_start $@
203 ;;
204 setup_vm)
205 do_vm $@
206 ;;
207 setup_computername)
208 do_computername $@
209 ;;
210esac
211
212##
213# Exit
214##
215exit 0