| 1 | #!/bin/bash |
| 2 | # Author: Steven Shiau, <steven _at_ clonezilla org> |
| 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 | # Option |
| 11 | nfsserver=$1 |
| 12 | RO_NFS_EXTRA_OPT=$2 |
| 13 | RL=$3 |
| 14 | |
| 15 | # functions |
| 16 | jobs_for_clonezilla_box() { |
| 17 | # 1. comment unnesessary mount, /opt, /var/spool/mail, /var/lib/rpm/ and /var/lib/dpkg |
| 18 | # 2. remove ntp |
| 19 | # 3. set runlevel in /etc/inittab to cheat kudzu |
| 20 | # 4. copy necessary files from rc1.d |
| 21 | |
| 22 | # (1) |
| 23 | for im in /opt /var/spool/mail /var/lib/rpm /var/lib/dpkg; do |
| 24 | # Use subshell to avoid some parameter to be overwriten |
| 25 | ( |
| 26 | . /etc/drbl/drbl.conf |
| 27 | # If $ocsroot is happened to be /opt/xxx or /var/spool/mail/xxx... we have |
| 28 | # to avoid that to be commented, since we need the $ocsroot as clonezilla |
| 29 | # image dir. |
| 30 | # /etc/fstab |
| 31 | # Example 1 (with $ocsroot=/opt/partimag): |
| 32 | # ------------------------- |
| 33 | # 192.168.123.254:/opt /opt nfs rw,rsize=65536,wsize=65536,tcp,defaults 0 0 |
| 34 | # 192.168.123.254:/opt/partimag /opt/partimag nfs rw,rsize=65536,wsize=65536,tcp,defaults 0 0 |
| 35 | # ------------------------- |
| 36 | # We will NOT comment 192.168.123.254:/opt and 192.168.123.254:/opt/partimag |
| 37 | # because we need /opt/ and its subdirectories as mount point. |
| 38 | # Example 2 (with $ocsroot=/opt2): |
| 39 | # ------------------------- |
| 40 | # 192.168.123.254:/opt /opt nfs rw,rsize=65536,wsize=65536,tcp,defaults 0 0 |
| 41 | # 192.168.123.254:/opt2 /opt2 nfs rw,rsize=65536,wsize=65536,tcp,defaults 0 0 |
| 42 | # ------------------------- |
| 43 | # We will comment 192.168.123.254:/opt, but will NOT comment 192.168.123.254:/opt2 |
| 44 | if [ -z "$(echo $ocsroot | grep -Ew "$im")" ]; then |
| 45 | perl -pi -e "s|(^[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+:.*$im[[:space:]]+.*)|# \$1|g" /etc/fstab |
| 46 | fi |
| 47 | ) |
| 48 | done |
| 49 | |
| 50 | # (2) |
| 51 | # Remove ntp setting, we do not need that in cloinezilla box |
| 52 | # Debian-based: /etc/default/ntpdate |
| 53 | # RH-like, SuSE: /etc/ntp.conf |
| 54 | for intp in /etc/ntp.conf /etc/default/ntpdate; do |
| 55 | if [ -f $intp ]; then |
| 56 | rm -f $intp |
| 57 | # create an empty file so that the NTPSERVERS is none. |
| 58 | touch $intp |
| 59 | fi |
| 60 | done |
| 61 | |
| 62 | # (3) |
| 63 | # We need to cheat the inittab (Note! Now init.org not start yet), and in FC, |
| 64 | # kudzu will check inittab, and if it's rc 5, it will switch to rc 5... |
| 65 | # Therefore here we force to make inittab 1. |
| 66 | # Command "runlevel" in this stage is useless. we have to check by ourself |
| 67 | # Check if "1" exists in /proc/cmdline |
| 68 | # For Ubuntu 6.10 or later, no /etc/inittab. |
| 69 | if [ -e /etc/inittab ]; then |
| 70 | perl -pi -e "s/^id:[1-5]:initdefault:/id:1:initdefault:/g" /etc/inittab |
| 71 | fi |
| 72 | |
| 73 | # (4) |
| 74 | # we need to mount rc1.d if ocs start, since this won't exist in the etc template. |
| 75 | if [ -n "$(unalias ls 2>/dev/null; ls /drbl_ssi/rc1.d/* 2>/dev/null)" ]; then |
| 76 | if [ -e /etc/debian_version ]; then |
| 77 | # Debian # RCX_ROOTDIR="/etc" |
| 78 | rc1_mnt_pnt=/etc/rc1.d |
| 79 | elif [ -e /etc/SuSE-release ]; then |
| 80 | # SuSE # RCX_ROOTDIR="/etc/init.d" |
| 81 | rc1_mnt_pnt=/etc/init.d/rc1.d |
| 82 | else |
| 83 | # RH-like # RCX_ROOTDIR="/etc" |
| 84 | rc1_mnt_pnt=/etc/rc1.d |
| 85 | fi |
| 86 | |
| 87 | # (1) |
| 88 | # If we use symbolic link, the ../init.d/ will not exist.. Any solution ? |
| 89 | #rm -rf $rc1_mnt_pnt |
| 90 | #ln -fs /drbl_ssi/rc1.d $rc1_mnt_pnt |
| 91 | # (2) # Use NFS mount ? |
| 92 | # mount -n $nfsserver:$nfsimagedir/drbl_ssi/rc1.d $rc1_mnt_pnt -o $RO_NFS_EXTRA_OPT |
| 93 | # (3) just copy the files... |
| 94 | [ -d "$rc1_mnt_pnt" -a "$(echo $rc1_mnt_pnt | grep "rc1.d")" ] && rm -rf $rc1_mnt_pnt/* |
| 95 | cp -af /drbl_ssi/rc1.d/* $rc1_mnt_pnt/ |
| 96 | fi |
| 97 | } # end of jobs_for_clonezilla_box |
| 98 | |
| 99 | # |
| 100 | jobs_for_DRBL_SSI() { |
| 101 | # (1) GDM/KDM autologin if necessary |
| 102 | # (2) Overwrite some setting from /drbl_ssi/clients/$IP/ to / |
| 103 | |
| 104 | # (1) |
| 105 | # GDM/KDM autologin if necessary |
| 106 | # Here we just force to change the login ID, and let the original config file to |
| 107 | # control auto/timed/normal login option. |
| 108 | |
| 109 | # Get the gdm or kdm config filename |
| 110 | get_gdm_kdm_conf_filename |
| 111 | |
| 112 | case "$HN" in |
| 113 | "[0-9]*") |
| 114 | echo "$HN can NOT be a login name for autologin, we won't change Gnome/KDE config file!" |
| 115 | ;; |
| 116 | *) |
| 117 | auto_login_id="$HN" |
| 118 | if [ -e "$GDM_CFG" ]; then |
| 119 | # change timed login account |
| 120 | perl -p -i -e "s/^[[:space:]]*#*[[:space:]]*TimedLogin=.*/TimedLogin=$auto_login_id/" $GDM_CFG |
| 121 | # change auto login account |
| 122 | perl -p -i -e "s/^[[:space:]]*#*[[:space:]]*AutomaticLogin=.*/AutomaticLogin=$auto_login_id/" $GDM_CFG |
| 123 | fi |
| 124 | if [ -e "$KDM_CFG" ]; then |
| 125 | # change auto login account |
| 126 | perl -p -i -e "s/^[[:space:]]*#*[[:space:]]*AutoLoginUser=.*/AutoLoginUser=$auto_login_id/" $KDM_CFG |
| 127 | fi |
| 128 | if [ -e "$LIGHTDM_CFG" ]; then |
| 129 | # To config these two parameters: |
| 130 | # autologin-user=<YOUR USER> |
| 131 | # autologin-user-timeout=0 |
| 132 | perl -pi -e "s/^[[:space:]]*#*[[:space:]]*autologin-user=.*/autologin-user=$auto_login_id/" $LIGHTDM_CFG |
| 133 | perl -pi -e "s/^[[:space:]]*#*[[:space:]]*autologin-user-timeout=.*/autologin-user-timeout=0/" $LIGHTDM_CFG |
| 134 | fi |
| 135 | ;; |
| 136 | esac |
| 137 | |
| 138 | # (2) Overwrite some setting from /drbl_ssi/clients/$IP/ |
| 139 | if [ -n "$(unalias ls 2>/dev/null; ls /drbl_ssi/clients/$ip/* 2>/dev/null)" ]; then |
| 140 | echo -n "Syncing files from /drbl_ssi/clients/$ip/ to /... " |
| 141 | rsync -aq /drbl_ssi/clients/$ip/* / |
| 142 | echo "done!" |
| 143 | fi |
| 144 | } # end of jobs_for_DRBL_SSI |
| 145 | |
| 146 | ############ |
| 147 | ### MAIN ### |
| 148 | ############ |
| 149 | # This part is common for DRBL SSI and clonezilla box |
| 150 | if [ -f /etc/fstab ]; then |
| 151 | # remove those /tftpboot/nodes/$IP. |
| 152 | # now it's single system image |
| 153 | # First we find the template machine IP address: |
| 154 | template_host_ip="$(LANG=C grep -E "[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+:/tftpboot/nodes/.*/etc" /etc/fstab | cut -d":" -f 1)" |
| 155 | echo "# This fstab is modified by DRBL SSI (init.drbl)." > /etc/fstab.mod |
| 156 | grep -vE "[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+:/tftpboot/nodes/" /etc/fstab >> /etc/fstab.mod |
| 157 | #perl -pi -e "s/^[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+:/$nfsserver:/g" /etc/fstab.mod |
| 158 | perl -pi -e "s/^$template_host_ip:/$nfsserver:/g" /etc/fstab.mod |
| 159 | mv -f /etc/fstab.mod /etc/fstab |
| 160 | fi |
| 161 | |
| 162 | # Modify hostname, yp.conf... |
| 163 | # Hostname, remove the hostname to let DHCP server provide that. |
| 164 | ip="" |
| 165 | netdevices="$(get-nic-devs)" |
| 166 | for device in $netdevices; do |
| 167 | ip="$(drbl-get-ipadd $device)" |
| 168 | [ -n "$ip" ] && break |
| 169 | done |
| 170 | # Try to mapping the hostname from /etc/hosts, if not, use IP-based hostname |
| 171 | HN="$(grep "^[[:space:]]*$ip\>" /etc/hosts | awk -F" " '{print $NF}')" |
| 172 | [ -z "$HN" ] && HN="$(echo $ip | sed -e "s/\./-/g")" |
| 173 | if [ -e /etc/debian_version ]; then |
| 174 | # Debian |
| 175 | echo $HN > /etc/hostname |
| 176 | elif [ -e /etc/SuSE-release ]; then |
| 177 | # SuSE |
| 178 | echo $HN > /etc/HOSTNAME |
| 179 | else |
| 180 | # RH-like |
| 181 | perl -pi -e "s/^[[:space:]]*HOSTNAME=.*/HOSTNAME=$HN/g" /etc/sysconfig/network |
| 182 | fi |
| 183 | # Set the hostname now |
| 184 | hostname $HN |
| 185 | |
| 186 | # for YP |
| 187 | perl -pi -e "s/(domain.*server)[[:space:]]+.*/\$1 $nfsserver/g" /etc/yp.conf |
| 188 | |
| 189 | # ssh public key so that we can run drbl-doit |
| 190 | # /drbl_ssi/root_ssh_key/authorized_keys is already copied from root's home by drbl-gen-ssi-files |
| 191 | if [ -d "/drbl_ssi/root_ssh_key" ]; then |
| 192 | mkdir -m 600 /root/.ssh |
| 193 | if [ -f "/drbl_ssi/root_ssh_key/authorized_keys" ]; then |
| 194 | cp -f /drbl_ssi/root_ssh_key/authorized_keys /root/.ssh/ |
| 195 | fi |
| 196 | fi |
| 197 | |
| 198 | # |
| 199 | if [ "$RL" = "1" ]; then |
| 200 | # If runlevel 1, it's clonezilla box mode. |
| 201 | jobs_for_clonezilla_box |
| 202 | else |
| 203 | # not in runleve 1, it's DRBL SSI mode. |
| 204 | jobs_for_DRBL_SSI |
| 205 | fi |