| 1 | #!/bin/bash |
| 2 | # Steven Shiau <steven _at_ clonezilla org>, Ceasar Sun <ceasar _at_ nchc org tw> |
| 3 | # License: GPL |
| 4 | # |
| 5 | # Loading setting |
| 6 | DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}" |
| 7 | |
| 8 | . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions |
| 9 | |
| 10 | # |
| 11 | check_if_root |
| 12 | # drbl_deploy.conf can only be access by root, so check_if_root should be run first. |
| 13 | . /etc/drbl/drbl_deploy.conf |
| 14 | |
| 15 | # |
| 16 | usage() { |
| 17 | echo "Usage: $0 Options" |
| 18 | echo "Options:" |
| 19 | language_help_prompt_by_idx_no |
| 20 | echo "-h, --host IP_ADDRESS: Generate the files of host with IP_ADDRESS" |
| 21 | echo "-k, --keep-files [y/N]: if assigned client files exists, keep newer files or not. Default is NO, i.e. overwrite all." |
| 22 | echo "-n, --nfsserver IP_ADDRESS: The nfsserver (IP address) for this client." |
| 23 | echo "-i, --nisserver IP_ADDRESS: The nisserver (IP address) for this client." |
| 24 | echo "-a, --label LABEL: The hostname of this client." |
| 25 | echo "-p, --pseudo: Pseudo mode, just create empty client dir to be used in DRBL SSI mode." |
| 26 | echo "Example:" |
| 27 | echo "$0 -l 0 -h 192.168.100.1 -k n -n 192.168.1.254 -a drbl101 -i 192.168.1.254" |
| 28 | } |
| 29 | |
| 30 | # |
| 31 | pseudo= |
| 32 | while [ $# -gt 0 ]; do |
| 33 | case "$1" in |
| 34 | -h|--host) |
| 35 | shift |
| 36 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
| 37 | # skip the -xx option, in case |
| 38 | ip="$1" |
| 39 | shift |
| 40 | fi |
| 41 | ;; |
| 42 | -k|--keep-files) |
| 43 | shift |
| 44 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
| 45 | # skip the -xx option, in case |
| 46 | keep_client_files="$1" |
| 47 | shift |
| 48 | fi |
| 49 | ;; |
| 50 | -n|--nfsserver) |
| 51 | shift |
| 52 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
| 53 | # skip the -xx option, in case |
| 54 | nfsserver="$1" |
| 55 | shift |
| 56 | fi |
| 57 | ;; |
| 58 | -a|--label) |
| 59 | shift |
| 60 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
| 61 | # skip the -xx option, in case |
| 62 | label="$1" |
| 63 | shift |
| 64 | fi |
| 65 | ;; |
| 66 | -i|--nisserver) |
| 67 | shift |
| 68 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
| 69 | # skip the -xx option, in case |
| 70 | nisserver="$1" |
| 71 | shift |
| 72 | fi |
| 73 | ;; |
| 74 | -l|--language) |
| 75 | shift |
| 76 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
| 77 | # skip the -xx option, in case |
| 78 | specified_lang="$1" |
| 79 | shift |
| 80 | fi |
| 81 | ;; |
| 82 | -p|--pseudo) |
| 83 | shift |
| 84 | pseudo=on |
| 85 | ;; |
| 86 | -*) echo "${0}: ${1}: invalid option" >&2 |
| 87 | usage >& 2 |
| 88 | exit 2 ;; |
| 89 | *) break ;; |
| 90 | esac |
| 91 | done |
| 92 | |
| 93 | # |
| 94 | ask_and_load_lang_set $specified_lang |
| 95 | |
| 96 | # |
| 97 | if [ -z "$ip" ]; then |
| 98 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 99 | echo "No specified client!" |
| 100 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 101 | usage |
| 102 | exit 1 |
| 103 | fi |
| 104 | # Some checkings |
| 105 | if [ -z "$label" ]; then |
| 106 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 107 | echo "No assigned label!" |
| 108 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 109 | usage |
| 110 | exit 1 |
| 111 | fi |
| 112 | |
| 113 | # |
| 114 | if [ -z "$nfsserver" ]; then |
| 115 | nfsserver="$(drbl-get-nfsserver $ip)" |
| 116 | fi |
| 117 | |
| 118 | # if nis server is not assigned, use the nfsserver. |
| 119 | if [ -z "$nisserver" ]; then |
| 120 | nisserver=$nfsserver |
| 121 | echo "NIS server is $nisserver" |
| 122 | fi |
| 123 | |
| 124 | # checking |
| 125 | if [ -z "$nfs_protocol" ]; then |
| 126 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 127 | echo "Unable to find the nfs_protocol!!!" |
| 128 | echo "Does something go wrong with /etc/drbl/drbl_deploy.conf ?" |
| 129 | echo "Program terminated!!!" |
| 130 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 131 | exit 1 |
| 132 | fi |
| 133 | |
| 134 | # define the variables for fstab |
| 135 | # nfs_client_extra_opt is loaded from drbl.conf |
| 136 | nfs_ver_opt="nfsvers=3" |
| 137 | # If need, you can add "rsize=65536,wsize=65536" |
| 138 | # FSTAB_NFS_ROOT_OPT is for nfs root only. It's actually ro, while due to the service /etc/init.d/checkroot.sh will parse it and judge if it's read-only or not. we have to make it "like" rw, but actually it's ro. |
| 139 | FSTAB_NFS_ROOT_OPT="rw,soft,$nfs_ver_opt,$nfs_protocol,$nfs_client_extra_opt,defaults 0 0" |
| 140 | FSTAB_NFS_RO_OPT="ro,soft,$nfs_ver_opt,$nfs_protocol,$nfs_client_extra_opt,defaults 0 0" |
| 141 | FSTAB_NFS_RW_OPT="rw,hard,intr,$nfs_ver_opt,$nfs_protocol,$nfs_client_extra_opt,defaults 0 0" |
| 142 | |
| 143 | # |
| 144 | case "$keep_client_files" in |
| 145 | y|Y|[yY][eE][sS]) |
| 146 | RSYNC_OPT_EXTRA="-u" |
| 147 | ;; |
| 148 | *) |
| 149 | RSYNC_OPT_EXTRA="" |
| 150 | ;; |
| 151 | esac |
| 152 | |
| 153 | # get the OS_Version and OS_type |
| 154 | check_distribution_name |
| 155 | |
| 156 | # varlib_NOT_2_be_copied_2_each_client is loaded from drbl.conf |
| 157 | # prepare the options for rsync to exclude when copy files in |
| 158 | # /tftpboot/node_root/var/lib/* to /tftpboot/nodes/$IP/var/lib/ |
| 159 | varlib_opts_common_to_each="" |
| 160 | for iv in $varlib_NOT_2_be_copied_2_each_client; do |
| 161 | varlib_opts_common_to_each="$varlib_opts_common_to_each --exclude=$iv/ " |
| 162 | done |
| 163 | |
| 164 | # |
| 165 | echo -n "Creating DRBL client: " |
| 166 | [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS |
| 167 | echo -n "$label $ip... " |
| 168 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 169 | |
| 170 | mkdir -p $drblroot/$ip |
| 171 | # If pseudo is on, then we finish here. |
| 172 | if [ "$pseudo" = "on" ]; then |
| 173 | echo "Pseudo client is created for DRBL SSI or clonezilla box mode! done!" |
| 174 | exit 0 |
| 175 | fi |
| 176 | |
| 177 | [ ! -d $drblroot ] && mkdir -p -m 700 $drblroot |
| 178 | mkdir -p $drblroot/$ip/root |
| 179 | mkdir -p $drblroot/$ip/dev |
| 180 | rsync -a $RSYNC_OPT_EXTRA $drbl_common_root/etc $drblroot/$ip/ |
| 181 | rsync -a $RSYNC_OPT_EXTRA $drbl_common_root/root $drblroot/$ip/ |
| 182 | |
| 183 | # Let the boot scripts will not try to check filesystems in the client |
| 184 | echo "" > $drbl_common_root/fastboot |
| 185 | # Put a tag for FC3 or newer so that rc.sysinit will know it's a readonly root |
| 186 | case "$OS_type" in |
| 187 | RH|MDK) |
| 188 | echo "READONLY=yes" > $drblroot/$ip/etc/sysconfig/readonly-root |
| 189 | # put an empty file so rc.sysinit won't complain. |
| 190 | touch $drblroot/$ip/etc/rc.readonly |
| 191 | ;; |
| 192 | esac |
| 193 | |
| 194 | # copy the node_root/var to client, |
| 195 | # we do not have to copy /tftproot/node_root/var/lib/rpm /tftproot/node_root/var/lib/dpkg (big directory!), |
| 196 | # it will be mounted. i.e. # we just share that for every client. |
| 197 | #rsync -a $RSYNC_OPT_EXTRA --exclude=rpm/ --exclude=dpkg/ $drbl_common_root/var $drblroot/$ip/ |
| 198 | rsync -a $RSYNC_OPT_EXTRA $varlib_opts_common_to_each $drbl_common_root/var $drblroot/$ip/ |
| 199 | # For nfs statd, the file /var/lib/nfs/state is important, othewise it won't be started in the 1st time in some distribution (like Debian >= 8). |
| 200 | rsync -a $RSYNC_OPT_EXTRA $drbl_common_root/var/lib/nfs/state $drblroot/$ip/var/lib/nfs/ |
| 201 | # create those directory excluding in varlib_opts_common_to_each |
| 202 | rsync -a $RSYNC_OPT_EXTRA --include="*/" --exclude="*" $drbl_common_root/var $drblroot/$ip/ |
| 203 | |
| 204 | # Instead, we need mount point $drblroot/$ip/var/lib/rpm and dpkg |
| 205 | # and $drblroot/$ip/var/spool/mail |
| 206 | # since /var/spool/mail maybe a symbolic dir (Debian-based, it is linked to /var/mail, RH-based, it's a dir), we use "-e" here |
| 207 | for idir in /var/lib/rpm /var/lib/dpkg /var/spool/mail; do |
| 208 | [ -e $idir ] && mkdir -p $drblroot/$ip/$idir |
| 209 | done |
| 210 | |
| 211 | # comment this, now we use "mount -t tmpfs none /dev" in the beginning of |
| 212 | # initrd, so we will use mknode to create console, null, tty[0-5] runtime |
| 213 | ## must have some dev for initrd when boot |
| 214 | #cp -a /dev/console /dev/null /dev/tty[0-5] $drblroot/$ip/dev |
| 215 | |
| 216 | # output the file to $ip/etc/fstab |
| 217 | cat <<-FSTAB > $drblroot/$ip/etc/fstab |
| 218 | # No more comment this /, since we use mount.nfs from distribution system, not from busybox any more. |
| 219 | ## Comment / to avoid mounting twice, since |
| 220 | ## / is already mounted by busybox's mount, which differs |
| 221 | ## to the mount in system. Mount options maybe differs, too. |
| 222 | ## Therefore if this line is not commented, it will mount again. |
| 223 | ## Keep this line here for ref only. |
| 224 | $nfsserver:$drbl_common_root / nfs nolock,$FSTAB_NFS_ROOT_OPT |
| 225 | $nfsserver:$drblroot/$ip/etc /etc nfs $FSTAB_NFS_RW_OPT |
| 226 | $nfsserver:$drblroot/$ip/var /var nfs $FSTAB_NFS_RW_OPT |
| 227 | $nfsserver:$drblroot/$ip/root /root nfs $FSTAB_NFS_RW_OPT |
| 228 | $nfsserver:/usr /usr nfs $FSTAB_NFS_RO_OPT |
| 229 | $nfsserver:/home /home nfs $FSTAB_NFS_RW_OPT |
| 230 | $nfsserver:/var/spool/mail /var/spool/mail nfs $FSTAB_NFS_RW_OPT |
| 231 | #none /proc proc defaults 0 0 |
| 232 | tmpfs /tmp tmpfs nosuid,nodev 0 0 |
| 233 | #/dev/fd0 /mnt/floppy auto noauto,owner,kudzu 0 0 |
| 234 | #/dev/sr0 /mnt/cdrom iso9660 iocharset=cp950,noauto,owner,kudzu,ro 0 0 |
| 235 | FSTAB |
| 236 | # Only when dir /opt/ exists, drbl-nfs-export will export it, then client should mount it, too. |
| 237 | if [ -d /opt/ ]; then |
| 238 | cat <<-FSTAB >> $drblroot/$ip/etc/fstab |
| 239 | $nfsserver:/opt /opt nfs $FSTAB_NFS_RO_OPT |
| 240 | FSTAB |
| 241 | fi |
| 242 | # if $ocsroot is not under /home, or if $ocsroot is a mount point, client must mount it. |
| 243 | if [ -z "$(echo $ocsroot | grep -Ew "^/home")" -o \ |
| 244 | -n "$(awk -F" " '{print $2}' /proc/mounts | grep -Ew "$ocsroot")" ]; then |
| 245 | cat <<-FSTAB >> $drblroot/$ip/etc/fstab |
| 246 | $nfsserver:$ocsroot $ocsroot nfs $FSTAB_NFS_RW_OPT |
| 247 | FSTAB |
| 248 | fi |
| 249 | |
| 250 | # no matter it's drbl box or clonezilla box, /var/lib/rpm or /var/lib/dpkg is exported. But when client is clonezilla box and in rc1.d, it won't mount them. |
| 251 | case "$OS_type" in |
| 252 | RH|MDK|SUSE) |
| 253 | if [ -d /var/lib/rpm ]; then |
| 254 | cat <<-FSTAB >> $drblroot/$ip/etc/fstab |
| 255 | $nfsserver:$drbl_common_root/var/lib/rpm /var/lib/rpm nfs $FSTAB_NFS_RO_OPT |
| 256 | FSTAB |
| 257 | fi |
| 258 | # Generate the /dev/pts, /dev/shm... dir. |
| 259 | for i in $memfs_dir_in_rh_client_fstab; do |
| 260 | if LC_ALL=C grep -q -F "$i" /etc/fstab &>/dev/null; then |
| 261 | LC_ALL=C grep -F "$i" /etc/fstab >> $drblroot/$ip/etc/fstab |
| 262 | fi |
| 263 | done |
| 264 | ;; |
| 265 | DBN) |
| 266 | if [ -d /var/lib/dpkg ]; then |
| 267 | cat <<-FSTAB >> $drblroot/$ip/etc/fstab |
| 268 | $nfsserver:$drbl_common_root/var/lib/dpkg /var/lib/dpkg nfs $FSTAB_NFS_RO_OPT |
| 269 | FSTAB |
| 270 | fi |
| 271 | ;; |
| 272 | esac |
| 273 | |
| 274 | # append /media, /mnt to fstab if necessary |
| 275 | for d in media mnt; do |
| 276 | if [ -d "$drbl_common_root/$d" ]; then |
| 277 | cat <<-FSTAB >> $drblroot/$ip/etc/fstab |
| 278 | tmpfs /$d tmpfs defaults 0 0 |
| 279 | FSTAB |
| 280 | fi |
| 281 | done |
| 282 | |
| 283 | # append user assigned dir |
| 284 | for d in $diskless_root_dir_ro_user_add; do |
| 285 | if [ -d "$drbl_common_root/$d" ]; then |
| 286 | cat <<-FSTAB >> $drblroot/$ip/etc/fstab |
| 287 | $nfsserver:$d $d nfs $FSTAB_NFS_RO_OPT |
| 288 | FSTAB |
| 289 | fi |
| 290 | done |
| 291 | for d in $diskless_root_dir_rw_user_add; do |
| 292 | if [ -d "$drbl_common_root/$d" ]; then |
| 293 | cat <<-FSTAB >> $drblroot/$ip/etc/fstab |
| 294 | $nfsserver:$d $d nfs $FSTAB_NFS_RW_OPT |
| 295 | FSTAB |
| 296 | fi |
| 297 | done |
| 298 | |
| 299 | # append user assigned local dir |
| 300 | # only when there is setting, we will append the fstab |
| 301 | if [ -n "$(grep -vE "^[[:space:]]*#" $drbl_syscfg/client-append-fstab 2>/dev/null)" ]; then |
| 302 | echo "" >> $drblroot/$ip/etc/fstab |
| 303 | echo "# Extra mount point specified in $drbl_syscfg/client-append-fstab" >> $drblroot/$ip/etc/fstab |
| 304 | # filter the comment lines |
| 305 | (grep -vE "^[[:space:]]*#" $drbl_syscfg/client-append-fstab; echo) | # make sure there is a LF at the end |
| 306 | while read dev mnt_pnt others; do |
| 307 | # create the mount point |
| 308 | [ ! -d "$drbl_common_root/$mnt_pnt" ] && mkdir -p $drbl_common_root/$mnt_pnt |
| 309 | # append the fstab for client. |
| 310 | echo $dev $mnt_pnt $others >> $drblroot/$ip/etc/fstab |
| 311 | done |
| 312 | fi |
| 313 | |
| 314 | # TODO, Debian/RH |
| 315 | # output the file to etc/sysconfig/network for DRBL clients. |
| 316 | case "$OS_type" in |
| 317 | RH|MDK) |
| 318 | cat <<-HOSTNAME_END > $drblroot/$ip/etc/sysconfig/network |
| 319 | NETWORKING=yes |
| 320 | HOSTNAME=$label |
| 321 | DOMAINNAME=$domain |
| 322 | GATEWAY=$nfsserver |
| 323 | NISDOMAIN=$nisdomain |
| 324 | HOSTNAME_END |
| 325 | [ -f "$drblroot/$ip/etc/hostname" ] && echo $label > $drblroot/$ip/etc/hostname |
| 326 | ;; |
| 327 | DBN) |
| 328 | echo $label > $drblroot/$ip/etc/hostname |
| 329 | echo $nisdomain > $drblroot/$ip/etc/defaultdomain |
| 330 | ;; |
| 331 | SUSE) |
| 332 | echo $label > $drblroot/$ip/etc/HOSTNAME |
| 333 | echo $nisdomain > $drblroot/$ip/etc/defaultdomain |
| 334 | echo "default $nfsserver - -" > $drblroot/$ip/etc/sysconfig/network/routes |
| 335 | ;; |
| 336 | esac |
| 337 | |
| 338 | # clean all network setting, since we already set the network in init |
| 339 | if [ -e /etc/debian_version ]; then |
| 340 | # Debian |
| 341 | rm -f $drblroot/$ip/etc/network/interfaces |
| 342 | # Create a dummy file so that hotplug won't complain that. |
| 343 | cat <<-NIC_END > $drblroot/$ip/etc/network/interfaces |
| 344 | # This dummy file is automatically created by DRBL to avoid the warning of hotplug. |
| 345 | # You should *NOT* put any network interface setting here, since it already automatically configured in PXE initrd in this DRBL client, and network service default is not on!!! |
| 346 | NIC_END |
| 347 | elif [ -e /etc/SuSE-release ]; then |
| 348 | # SuSE |
| 349 | rm -f $drblroot/$ip/etc/sysconfig/network/ifcfg-eth* |
| 350 | # For SuSE, we do not generate dummy NIC conf files since |
| 351 | # (1) Until OpenSuSE 10.1 Not config file will be automatically created, |
| 352 | # (2) The file name is like ifcfg-eth-id-00:0c:29:45:9b:81, based on MAC address, we can not foresee that. |
| 353 | else |
| 354 | # RH-like |
| 355 | rm -f $drblroot/$ip/etc/sysconfig/network-scripts/ifcfg-eth* |
| 356 | for ethx in $dummy_eth_nics; do |
| 357 | cat <<-NIC_END > $drblroot/$ip/etc/sysconfig/network-scripts/ifcfg-$ethx |
| 358 | # This dummy file is automatically created by DRBL to avoid it to be recreated.. |
| 359 | # You should *NOT* put any network interface setting here, since it already automatically configured in PXE initrd in this DRBL client, and network service default is not on!!! |
| 360 | DEVICE=$ethx |
| 361 | BOOTPROTO=dhcp |
| 362 | ONBOOT=no |
| 363 | NIC_END |
| 364 | done |
| 365 | fi |
| 366 | |
| 367 | cat <<-YPCONF_ETH0 > $drblroot/$ip/etc/yp.conf |
| 368 | domain $nisdomain server $nisserver |
| 369 | YPCONF_ETH0 |
| 370 | |
| 371 | # Generate the OpenSSH host key for clients. For RH-like, sshd service will |
| 372 | # generated the keys if they do not exist. |
| 373 | case "$OS_type" in |
| 374 | DBN) |
| 375 | drbl-gen-ssh-host-keys generate $ip |
| 376 | ;; |
| 377 | esac |
| 378 | |
| 379 | [ -f "$drblroot/$ip/etc/resolv.conf" ] && rm -f $drblroot/$ip/etc/resolv.conf |
| 380 | for nameserver in $nameserver_; do |
| 381 | echo "nameserver $nameserver" >> $drblroot/$ip/etc/resolv.conf |
| 382 | done |
| 383 | |
| 384 | # Comment this, it's not necessary now. |
| 385 | #ln -fs $drbl_common_root /tftpboot/$ip |
| 386 | |
| 387 | # For client login mode |
| 388 | PASSWD_OPT="" |
| 389 | [ -n "$client_autologin_passwd" ] && PASSWD_OPT="-p $client_autologin_passwd" |
| 390 | case "$login_gdm_opt" in |
| 391 | "auto_login") |
| 392 | drbl-login-switch --no-gen-ssi -l $lang --auto --no_nis_update $PASSWD_OPT --host $ip |
| 393 | ;; |
| 394 | "timed_login") |
| 395 | drbl-login-switch --no-gen-ssi -l $lang --timed $timed_login_time --no_nis_update $PASSWD_OPT --host $ip |
| 396 | ;; |
| 397 | *) |
| 398 | drbl-login-switch --no-gen-ssi -l $lang --normal --no_nis_update --host $ip |
| 399 | ;; |
| 400 | esac |
| 401 | |
| 402 | # Now it's not necessary to change client's /etc files when clonezilla start, so |
| 403 | # we can put the ocs related files in client's rc1.d first. |
| 404 | # Here we force to use hardware detect for client's rc1.d |
| 405 | ocs-related-srv --hw-detect on -n $ip put |
| 406 | # put S19ocs-run |
| 407 | # By Ceasar : |
| 408 | # The reason we can not just sync from $common_root is because FC17/18 can't leave ocs-run linking file in rc*.d and it has already been deleted in $common_root |
| 409 | ( cd $drblroot/$ip/$RCX_ROOTDIR/rc1.d/; ln -fs $RCX_REL_INITD/ocs-run S19ocs-run ) |
| 410 | if is_systemd_init; then |
| 411 | ( cd $drblroot/$ip/$RCX_ROOTDIR/rc1.d/; rm -rf S*ocs-run ) |
| 412 | fi |
| 413 | |
| 414 | # set the root's passwd for clients if set client_root_passwd |
| 415 | [ -n "$client_root_passwd" ] && drbl-client-root-passwd --no-gen-ssi --stdin $client_root_passwd --host $ip |