File: drbl-ssi-client-prepare

package info (click to toggle)
drbl 5.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,996 kB
  • sloc: sh: 43,555; perl: 8,820; xml: 867; makefile: 135
file content (205 lines) | stat: -rwxr-xr-x 7,430 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/bash
# Author: Steven Shiau, <steven _at_ clonezilla org>
# License: GPL

# Loading setting
DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"

. $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions

# Option
nfsserver=$1
RO_NFS_EXTRA_OPT=$2
RL=$3

# functions
jobs_for_clonezilla_box() {
# 1. comment unnesessary mount, /opt, /var/spool/mail, /var/lib/rpm/ and /var/lib/dpkg
# 2. remove ntp
# 3. set runlevel in /etc/inittab to cheat kudzu
# 4. copy necessary files from rc1.d

# (1)
for im in /opt /var/spool/mail /var/lib/rpm /var/lib/dpkg; do
  # Use subshell to avoid some parameter to be overwriten
  (
    . /etc/drbl/drbl.conf
    # If $ocsroot is happened to be /opt/xxx or /var/spool/mail/xxx... we have
    # to avoid that to be commented, since we need the $ocsroot as clonezilla
    # image dir.
    # /etc/fstab 
    # Example 1 (with $ocsroot=/opt/partimag):
    # -------------------------
    # 192.168.123.254:/opt            /opt       nfs    rw,rsize=65536,wsize=65536,tcp,defaults        0 0
    # 192.168.123.254:/opt/partimag            /opt/partimag       nfs    rw,rsize=65536,wsize=65536,tcp,defaults        0 0
    # -------------------------
    # We will NOT comment 192.168.123.254:/opt and 192.168.123.254:/opt/partimag
    # because we need /opt/ and its subdirectories as mount point.
    # Example 2 (with $ocsroot=/opt2):
    # -------------------------
    # 192.168.123.254:/opt            /opt       nfs    rw,rsize=65536,wsize=65536,tcp,defaults        0 0
    # 192.168.123.254:/opt2            /opt2       nfs    rw,rsize=65536,wsize=65536,tcp,defaults        0 0
    # -------------------------
    # We will comment 192.168.123.254:/opt, but will NOT comment 192.168.123.254:/opt2
    if [ -z "$(echo $ocsroot | grep -Ew "$im")" ]; then
      perl -pi -e "s|(^[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+:.*$im[[:space:]]+.*)|# \$1|g" /etc/fstab
    fi
  )
done

# (2)
# Remove ntp setting, we do not need that in cloinezilla box
# Debian-based: /etc/default/ntpdate
# RH-like, SuSE: /etc/ntp.conf
for intp in /etc/ntp.conf /etc/default/ntpdate; do
  if [ -f $intp ]; then
    rm -f $intp
    # create an empty file so that the NTPSERVERS is none.
    touch $intp
  fi
done

# (3)
# We need to cheat the inittab (Note! Now init.org not start yet), and in FC,
# kudzu will check inittab, and if it's rc 5, it will switch to rc 5...
# Therefore here we force to make inittab 1.
# Command "runlevel" in this stage is useless. we have to check by ourself
# Check if "1" exists in /proc/cmdline
# For Ubuntu 6.10 or later, no /etc/inittab.
if [ -e /etc/inittab ]; then
  perl -pi -e "s/^id:[1-5]:initdefault:/id:1:initdefault:/g" /etc/inittab
fi

# (4)
# we need to mount rc1.d if ocs start, since this won't exist in the etc template.
if [ -n "$(unalias ls 2>/dev/null; ls /drbl_ssi/rc1.d/* 2>/dev/null)" ]; then
  if [ -e /etc/debian_version ]; then
    # Debian # RCX_ROOTDIR="/etc"
    rc1_mnt_pnt=/etc/rc1.d
  elif [ -e /etc/SuSE-release ]; then
    # SuSE # RCX_ROOTDIR="/etc/init.d"
    rc1_mnt_pnt=/etc/init.d/rc1.d
  else
    # RH-like # RCX_ROOTDIR="/etc"
    rc1_mnt_pnt=/etc/rc1.d
  fi

  # (1)
  # If we use symbolic link, the ../init.d/ will not exist.. Any solution ?
  #rm -rf $rc1_mnt_pnt
  #ln -fs /drbl_ssi/rc1.d $rc1_mnt_pnt
  # (2) # Use NFS mount ?
  # mount -n $nfsserver:$nfsimagedir/drbl_ssi/rc1.d $rc1_mnt_pnt -o $RO_NFS_EXTRA_OPT
  # (3) just copy the files...
  [ -d "$rc1_mnt_pnt" -a "$(echo $rc1_mnt_pnt | grep "rc1.d")" ] && rm -rf $rc1_mnt_pnt/*
  cp -af /drbl_ssi/rc1.d/* $rc1_mnt_pnt/
fi
} # end of jobs_for_clonezilla_box

#
jobs_for_DRBL_SSI() {
# (1) GDM/KDM autologin if necessary
# (2) Overwrite some setting from /drbl_ssi/clients/$IP/ to /

# (1)
# GDM/KDM autologin if necessary
# Here we just force to change the login ID, and let the original config file to
# control auto/timed/normal login option.

# Get the gdm or kdm config filename
get_gdm_kdm_conf_filename

case "$HN" in
  "[0-9]*")
      echo "$HN can NOT be a login name for autologin, we won't change Gnome/KDE config file!"
      ;;
   *)
      auto_login_id="$HN"
      if [ -e "$GDM_CFG" ]; then
        # change timed login account
        perl -p -i -e "s/^[[:space:]]*#*[[:space:]]*TimedLogin=.*/TimedLogin=$auto_login_id/" $GDM_CFG
        # change auto login account
        perl -p -i -e "s/^[[:space:]]*#*[[:space:]]*AutomaticLogin=.*/AutomaticLogin=$auto_login_id/" $GDM_CFG
      fi
      if [ -e "$KDM_CFG" ]; then
        # change auto login account
        perl -p -i -e "s/^[[:space:]]*#*[[:space:]]*AutoLoginUser=.*/AutoLoginUser=$auto_login_id/" $KDM_CFG
      fi
      if [ -e "$LIGHTDM_CFG" ]; then
        # To config these two parameters:
        # autologin-user=<YOUR USER>
        # autologin-user-timeout=0
        perl -pi -e "s/^[[:space:]]*#*[[:space:]]*autologin-user=.*/autologin-user=$auto_login_id/" $LIGHTDM_CFG
        perl -pi -e "s/^[[:space:]]*#*[[:space:]]*autologin-user-timeout=.*/autologin-user-timeout=0/" $LIGHTDM_CFG
      fi
      ;;
esac

# (2) Overwrite some setting from /drbl_ssi/clients/$IP/
if [ -n "$(unalias ls 2>/dev/null; ls /drbl_ssi/clients/$ip/* 2>/dev/null)" ]; then
   echo -n "Syncing files from /drbl_ssi/clients/$ip/ to /... "
   rsync -aq /drbl_ssi/clients/$ip/* /
   echo "done!"
fi
} # end of jobs_for_DRBL_SSI

############
### MAIN ###
############
# This part is common for DRBL SSI and clonezilla box
if [ -f /etc/fstab ]; then
  # remove those /tftpboot/nodes/$IP.
  # now it's single system image
  # First we find the template machine IP address:
  template_host_ip="$(LANG=C grep -E "[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+:/tftpboot/nodes/.*/etc" /etc/fstab | cut -d":" -f 1)"
  echo "# This fstab is modified by DRBL SSI (init.drbl)." > /etc/fstab.mod
  grep -vE "[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+:/tftpboot/nodes/" /etc/fstab  >> /etc/fstab.mod
  #perl -pi -e "s/^[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+:/$nfsserver:/g" /etc/fstab.mod
  perl -pi -e "s/^$template_host_ip:/$nfsserver:/g" /etc/fstab.mod
  mv -f /etc/fstab.mod /etc/fstab
fi

# Modify hostname, yp.conf...
# Hostname, remove the hostname to let DHCP server provide that.
ip=""
netdevices="$(get-nic-devs)"
for device in $netdevices; do
  ip="$(drbl-get-ipadd $device)"
  [ -n "$ip" ] && break
done
# Try to mapping the hostname from /etc/hosts, if not, use IP-based hostname 
HN="$(grep "^[[:space:]]*$ip\>" /etc/hosts | awk -F" " '{print $NF}')"
[ -z  "$HN" ] && HN="$(echo $ip | sed -e "s/\./-/g")"
if [ -e /etc/debian_version ]; then
  # Debian
  echo $HN > /etc/hostname
elif [ -e /etc/SuSE-release ]; then
  # SuSE
  echo $HN > /etc/HOSTNAME
else
  # RH-like
  perl -pi -e "s/^[[:space:]]*HOSTNAME=.*/HOSTNAME=$HN/g" /etc/sysconfig/network
fi
# Set the hostname now
hostname $HN

# for YP
perl -pi -e "s/(domain.*server)[[:space:]]+.*/\$1 $nfsserver/g" /etc/yp.conf

# ssh public key so that we can run drbl-doit
# /drbl_ssi/root_ssh_key/authorized_keys is already copied from root's home by drbl-gen-ssi-files
if [ -d "/drbl_ssi/root_ssh_key" ]; then
  mkdir -m 600 /root/.ssh
  if [ -f "/drbl_ssi/root_ssh_key/authorized_keys" ]; then
    cp -f /drbl_ssi/root_ssh_key/authorized_keys /root/.ssh/
  fi
fi

#
if [ "$RL" = "1" ]; then 
  # If runlevel 1, it's clonezilla box mode. 
  jobs_for_clonezilla_box
else
  # not in runleve 1, it's DRBL SSI mode.
  jobs_for_DRBL_SSI
fi