示例#1
0
    def config(self):
        logger.debug("*** Generate /etc/drbd.conf.")
        drbdconf = """
global {
usage-count no;
}

resource r0 {
protocol C;
#incon-degr-cmd "echo !DRBD! pri on incon-degr | wall ; sleep 60 ; halt -f";

on %(m0_hostname)s {
device     %(r0_device)s;
disk       %(r0_disk)s;
address    %(m0_linkip)s:7789;
meta-disk  internal;
}
on %(m1_hostname)s {
device     %(r0_device)s;
disk       %(r0_disk)s;
address    %(m1_linkip)s:7789;
meta-disk  internal;
}

disk {
on-io-error   detach;
}

net {
max-buffers     2048;
ko-count 4;
}

syncer {
rate 25M;
al-extents 257; # must be a prime number
}

startup {
wfc-timeout  20;
degr-wfc-timeout 120;    # 2 minutes.
}
}

resource r1 {
protocol C;
#incon-degr-cmd "echo !DRBD! pri on incon-degr | wall ; sleep 60 ; halt -f";

on %(m0_hostname)s {
device     %(r1_device)s;
disk       %(r1_disk)s;
address    %(m0_linkip)s:7790;
meta-disk  internal;
}
on %(m1_hostname)s {
device     %(r1_device)s;
disk       %(r1_disk)s;
address    %(m1_linkip)s:7790;
meta-disk  internal;
}

disk {
on-io-error   detach;
}

net {
max-buffers     2048;
ko-count 4;
}

syncer {
rate 25M;
al-extents 257; # must be a prime number
}

startup {
wfc-timeout  20;
degr-wfc-timeout 120;    # 2 minutes.
}
}
        """ % { "m0_hostname":  self.machines["m0"]["hostname"], 
                "m1_hostname":  self.machines["m1"]["hostname"], 
                "r0_device":    self.resources["r0"]["device"], 
                "r1_device":    self.resources["r1"]["device"], 
                "r0_disk":      self.resources["r0"]["disk"], 
                "r1_disk":      self.resources["r1"]["disk"], 
                "m0_linkip":    self.machines["m0"]["linkip"], 
                "m1_linkip":    self.machines["m1"]["linkip"], 
               }

        logger.debug("/etc/drbd.conf: %s" % drbdconf)
        self._txt.load(text=drbdconf)
        self._txt.save("/etc/drbd.conf")
    
        logger.debug("*** Initialize disk partitions.")
        OS.linux.umount( self.resources["r0"]["disk"] )
        OS.linux.umount( self.resources["r1"]["disk"] )
        OS.runex("dd if=/dev/zero bs=1M count=1 of=%s" % self.resources["r0"]["disk"])
        OS.runex("dd if=/dev/zero bs=1M count=1 of=%s" % self.resources["r1"]["disk"])
        
        logger.debug("*** Create Resources.")
        
        OS.runex("yes yes | drbdadm create-md r0")
        OS.runex("yes yes | drbdadm create-md r1")
示例#2
0
    def startup(self, instance):
        nic = self.machines["m%s" % instance]["nic"]    
        self._txt.load(file="/etc/sysconfig/network-scripts/ifcfg-%s" % nic)
        self._txt.set_param("BOOTPROTO", "static")
        self._txt.set_param("DHCPCLASS", "")
        self._txt.set_param("IPADDR", self.machines["m%s" % instance]["linkip"])
        self._txt.set_param("NETMASK", "255.255.255.0")
        self._txt.set_param("ONBOOT", "yes")
        self._txt.save()
        
        logger.debug("restart the network interface")
        OS.runex("ifdown %s" % nic)
        OS.runex("ifup %s" % nic)
        
        self._svc.start()
        if not os.path.exists("/proc/drbd"):
            raise Exception("ERROR: cannot start drbd service.")

        drbd_status = None
        
        logger.debug("*** active resources.")
        if instance == 0 :
            drbd_status = ".*Primary.*UpToDate.*"
            logger.debug("r0, r1 are set as primary.")
            OS.runex("drbdadm -- --overwrite-data-of-peer primary r0")
            OS.runex("drbdadm -- --overwrite-data-of-peer primary r1")
        elif instance == 1:
            drbd_status = ".*Secondary.*UpToDate/UpToDate.*"
            logger.debug("r0, r1 are set as secondary.")
            OS.runex("drbdadm secondary r0")
            OS.runex("drbdadm secondary r1")
            
            step_pass = False
            
            logger.debug("*** Waiting for secondary drbd resources to sync")
            logger.debug("*** It will take half an hour or more depends on the disk size.")
    
            while not step_pass:
                self._txt.load(file="/proc/drbd")
                proc_drbd = self._txt.read()
                logger.debug("/proc/drbd: \n%s", proc_drbd)
                if "sync'ed" not in proc_drbd:
                    step_pass = True
                time.sleep(30)
        else:
            raise Exception("instance must be 0 or 1")

        logger.debug("*** Waiting for drbd resources ready")
        drbd_r0_status = 0
        drbd_r1_status = 0
        while drbd_r0_status != 1 or drbd_r1_status != 1: 
            self._txt.load(file="/proc/drbd")
            proc_drbd = self._txt.read()
            logger.debug("/proc/drbd: \n%s", proc_drbd)
            if re.search("0:%s" % drbd_status, proc_drbd):
                drbd_r0_status = 1
            
            if re.search("1:%s" % drbd_status, proc_drbd):
                drbd_r1_status = 1
            time.sleep(10)

        logger.debug("*** Resources are ready.")
        self._txt.load(file="/proc/drbd")
        proc_drbd = self._txt.read()
        logger.debug("/proc/drbd: \n%s", proc_drbd)
        
        if instance == 0:
            logger.debug("*** Format DRBD devices.")
            OS.runex("mkfs.ext4 %s" % self.resources["r0"]["device"])
            OS.runex("mkfs.ext4 %s" % self.resources["r1"]["device"])
            OS.linux.mount( self.resources["r0"]["device"], self.resources["r0"]["dir"], "ext4" )
            OS.linux.mount( self.resources["r1"]["device"], self.resources["r1"]["dir"], "ext4" )
            (retcode, svc_status) = OS.run("service drbd status")
            if retcode != 0:
                raise Exception("get drbd service status failed")
            if re.search("0:r0%s%s" % (drbd_status, self.resources["r0"]["dir"]), svc_status) and re.search("1:r1%s%s" % (drbd_status, self.resources["r1"]["dir"]), svc_status):
                logger.debug("drbd installation successfully.")
            else:
                raise Exception("drbd installation failed.")
                
            File.move( "%s.cache" % self.resources["r0"]["dir"], self.resources["r0"]["dir"] )
            File.move( "%s.cache" % self.resources["r1"]["dir"], self.resources["r1"]["dir"] )
    
        self._svc.config(autostart=True)
        self._txt.load(file="/etc/fstab")
        self._txt.delete(r"%s|%s" % (self.resources["r0"]["disk"], self.resources["r1"]["dir"]))
        self._txt.save()
        logger.debug("*** DRBD Setup Finished.")