31.2. Configuring a Two-Node Resource

The configuration path is deliberately ordered: prepare the backing devices, write one resource definition on both nodes, initialize metadata, connect the peers, choose the initial source, wait for a complete copy, and only then make a filesystem. Skipping a step usually produces a resource that looks connected but has no authoritative copy, or a filesystem that is created on only one node.

31.2.1. Preparing Backing Devices and Writing Resource Definitions

The backing device should be dedicated to the DRBD resource. In this example each node has two 1 GiB virtio disks. /dev/vdb becomes an LVM physical volume, and a 512 MiB logical volume inside it is the DRBD backing device. The spare /dev/vdc is used later to replace a failed backing device. The block devices on drbd1 are:

$ lsblk -o NAME,SIZE,TYPE,MOUNTPOINT
NAME    SIZE TYPE MOUNTPOINT
vda     100G disk 
└─vda1  100G part /
vdb       1G disk 
vdc       1G disk 

The -o option selects the columns shown: device name, size, type, and mountpoint. The root filesystem is on vda; vdb and vdc have no filesystem and no mountpoint. Confirm the same layout on drbd2 before creating anything. If a device is already mounted or holds a filesystem you need, stop and identify it rather than overwriting it.

Warning: the following LVM commands destroy the existing partition table, filesystem, or volume-group metadata on the named device. Use a device that is dedicated to this resource, and double-check the name on each node.

The LVM commands used in this chapter take these options:

Option Description
vgcreate vg pv Create a volume group named vg on physical volume pv
lvcreate -L size -n name vg Create a logical volume with an absolute size
lvextend -L size lv Resize a logical volume to the stated absolute size
vgchange -an vg Deactivate all logical volumes in the volume group
vgremove -f vg Remove a volume group and its logical volumes without prompting
pvremove -ff pv Remove a physical-volume label, with the extra force needed for an in-use device

On drbd1, as john:

$ sudo vgcreate vgdrbd /dev/vdb
  Physical volume "/dev/vdb" successfully created.
  Volume group "vgdrbd" successfully created
$ sudo lvcreate -L 512M -n data vgdrbd
  Logical volume "data" created.

On drbd2, as john, run the same two commands with its own /dev/vdb:

$ sudo vgcreate vgdrbd /dev/vdb
  Physical volume "/dev/vdb" successfully created.
  Volume group "vgdrbd" successfully created
$ sudo lvcreate -L 512M -n data vgdrbd
  Logical volume "data" created.

The logical volume path is now /dev/vgdrbd/data on both nodes. Do not put a filesystem on this LV directly. DRBD will own it, and DRBD's internal metadata will be stored at the end of the LV. The usable DRBD device is smaller than the LV because the metadata and bitmap occupy space. That is normal; size the filesystem from /dev/drbd0, not from the LV.

A resource definition describes both nodes and the shared protocol. On each node, create /etc/drbd.d/r0.res with the same content:

resource r0 {
    protocol C;

    startup {
        wfc-timeout 30;
        degr-wfc-timeout 15;
    }

    disk {
        on-io-error detach;
        resync-rate 100M;
    }

    net {
        cram-hmac-alg sha256;
        shared-secret "handbook-drbd-secret";
        after-sb-0pri disconnect;
        after-sb-1pri disconnect;
        after-sb-2pri disconnect;
    }

    on drbd1 {
        device /dev/drbd0;
        disk /dev/vgdrbd/data;
        address 192.168.231.11:7789;
        meta-disk internal;
    }

    on drbd2 {
        device /dev/drbd0;
        disk /dev/vgdrbd/data;
        address 192.168.231.12:7789;
        meta-disk internal;
    }
}

The on blocks are per-node values even though the file is identical on both nodes: DRBD selects the block whose hostname matches the local node. The disk path happens to be the same here, but it does not have to be. The address is the local replication address that the peer connects to. The device is the local DRBD device name. meta-disk internal places metadata at the end of the backing device, so the metadata travels with that copy and does not need a separate partition.

The options in this file have specific purposes:

Option Description
protocol C Acknowledge writes only after the peer's disk write
wfc-timeout 30 Wait up to 30 seconds for a connection when the service starts
degr-wfc-timeout 15 Shorter wait when this node was already degraded
on-io-error detach Detach the backing device on a local I/O error instead of retrying indefinitely
resync-rate 100M Cap background resynchronization at roughly 100 MB/s in this example
cram-hmac-alg sha256 Authenticate the replication connection with HMAC-SHA256
shared-secret The secret both nodes must share
after-sb-0pri, after-sb-1pri, after-sb-2pri Refuse automatic split-brain resolution and disconnect for manual investigation

With on-io-error detach, a local I/O error detaches the backing device. The node keeps running, but it no longer has a usable local copy; the resulting diskless state needs deliberate repair or replacement rather than an automatic failover. The shared secret protects the replication link from an unrelated host that connects to port 7789. It is not a substitute for network isolation, and it is not a user password. Because it is a secret, restrict the file:

$ sudo chmod 0600 /etc/drbd.d/r0.res

The command normally prints nothing. Check the key lines of the installed file before bringing the resource up:

$ sudo grep -E "^(resource|    protocol|        address|        disk )" /etc/drbd.d/r0.res
resource r0 {
    protocol C;
        disk /dev/vgdrbd/data;
        address 192.168.231.11:7789;
        disk /dev/vgdrbd/data;
        address 192.168.231.12:7789;

The grep pattern selects the resource line, the protocol, and the per-node disk and address lines, so the output is a compact confirmation rather than the whole file. The file itself remains the source of truth. A syntax error is reported when drbdadm parses the file, for example during drbdadm up or drbdadm status. drbdadm reads /etc/drbd.conf and the files in /etc/drbd.d/ by default. An alternate file can be selected with -c, which is useful for testing a staged resource without disturbing the running one.

31.2.2. Installing Tools and Creating Resource Metadata

The node packages are drbd-utils, lvm2, and nfs-kernel-server. Install them on both nodes with the ordinary interactive command sudo apt install drbd-utils lvm2 nfs-kernel-server. Apt prints the package list and the download and disk-space summary, then waits for confirmation before changing the system; answer Y to continue. On drbdclient, install nfs-common with sudo apt install nfs-common. No automated confirmation flag is needed for either installation.

The service commands used later in the chapter have these meanings:

Command Effect
systemctl start unit Start the unit now without changing its boot state
systemctl stop unit Stop the unit now without disabling it
systemctl enable unit Arrange for the unit to start at boot, without starting it now
systemctl enable --now unit Enable the unit for boot and start it immediately
systemctl is-enabled unit Report whether the unit is enabled; a disabled unit returns a nonzero status

The DRBD kernel module is part of the installed kernel package, not a separate DKMS build. The first drbdadm operation loads it if it is not already present. After installation, check the module version as shown in section 31.1.4 before initializing metadata. If modinfo drbd fails, the running kernel does not have the module available; install the matching kernel package or correct the boot environment before continuing.

The high-level drbdadm commands used in this chapter are:

Command Effect
drbdadm create-md r0 Initialize DRBD metadata on the backing device
drbdadm up r0 Attach the backing device and connect to the peer
drbdadm down r0 Disconnect and detach the resource
drbdadm primary r0 Promote the local device, checking consistency
drbdadm primary --force r0 Promote without the normal peer-consistency check
drbdadm secondary r0 Demote the local device
drbdadm connect r0 Establish the replication link without changing attachment
drbdadm disconnect r0 Remove the replication link and enter StandAlone
drbdadm resize r0 Adopt a grown backing device
drbdadm status r0 Show role, disk, and peer state in a readable form
drbdadm role r0, dstate r0, cstate r0 Print one state component for scripts or quick checks

Global options that matter here are -c file (use an alternate configuration file), -d (dry run; print the backend calls without executing them), -P (select a peer when the resource has more than two host sections), and -- (pass the following options to the backend). The -- form matters for --discard-my-data, because that is a backend option rather than a drbdadm option.

Metadata must be created once on each fresh backing device. On drbd1, as john, the command's standard output is:

$ sudo drbdadm create-md r0
initializing activity log
Writing meta data...
New drbd meta data block successfully created.

The command also reports the bitmap initialization on standard error, for example initializing bitmap (16 KB) to all zero. That progress line is normal. On drbd2, run the same command against its own LV:

$ sudo drbdadm create-md r0
initializing activity log
Writing meta data...
New drbd meta data block successfully created.

create-md writes metadata and a bitmap; it does not copy data and it does not start replication. If the device already contains metadata from a previous resource, the command refuses to overwrite it without an explicit force option. That protection is useful: overwriting the metadata of a copy that still holds the only good data can make recovery harder. Do not force it until you have identified the old resource and decided which copy is authoritative.

31.2.3. Connecting Peers and Selecting the Initial Synchronization Source

With metadata on both devices, attach and connect each node:

$ sudo drbdadm up r0

The command normally prints nothing. On the peer, as john:

$ sudo drbdadm up r0

up is a shortcut for attach and connect. It opens the local backing device, creates /dev/drbd0, and tries the configured replication address. A firewall that blocks TCP 7789, a wrong address, or a name that does not resolve leaves the resource attached but not connected; drbdadm status shows the connection state rather than pretending the link is healthy.

Immediately after the first connection, both disks are Inconsistent because neither node has been chosen as the source. On drbd1:

$ sudo drbdadm status r0
r0 role:Secondary
  disk:Inconsistent
  peer role:Secondary
    replication:Established peer-disk:Inconsistent

The link is established, but there is no authoritative copy. DRBD will not invent one. On a brand-new pair with no data, choose one node as the initial source. On drbd1:

$ sudo drbdadm primary --force r0

The command normally prints nothing. This is the one-time authority decision: drbd1 becomes primary, its empty device is treated as the source, and drbd2 receives a full initial synchronization. If the devices already held data, --force would discard the peer's divergent blocks. That is why the command belongs at initial provisioning or at a deliberate recovery decision, not in a normal switchover script.

31.2.4. Following Initial Synchronization and Checking Resource State

drbdadm status summarizes the resource, but /proc/drbd exposes the kernel's counters and the progress of an active synchronization. While the initial sync is running on drbd1:

$ cat /proc/drbd
version: 8.4.11 (api:1/proto:86-101)
srcversion: C95F202D36359D5A59245EB 
 0: cs:SyncSource ro:Primary/Secondary ds:UpToDate/Inconsistent C r-----
    ns:131072 nr:0 dw:0 dr:131072 al:8 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:393164
    [====>...............] sync'ed: 25.8% (393164/524236)K
    finish: 0:00:08 speed: 43,688 (43,688) K/sec

The fields are compact but useful. cs:SyncSource means this node is sending the initial copy. ro:Primary/Secondary is the role pair. ds: is the local and peer disk state; the source is UpToDate, the target is Inconsistent until the copy finishes. ns, nr, dw, and dr count network send, network receive, disk write, and disk read activity. oos is the amount still out of sync. The bracket line and speed are progress estimates for this small device; they are not a storage benchmark. The source is reading and sending at roughly 43,688 KiB/s here, and the estimate says about eight seconds remain.

When the copy completes, the status on drbd1 becomes:

$ sudo drbdadm status r0
r0 role:Primary
  disk:UpToDate
  peer role:Secondary
    replication:Established peer-disk:UpToDate

On drbd2, the same resource is seen from the other side:

$ sudo drbdadm status r0
r0 role:Secondary
  disk:UpToDate
  peer role:Primary
    replication:Established peer-disk:UpToDate

Both disks are now UpToDate. Do not create a filesystem or resize the device until both sides report that state. A filesystem created while the peer is still Inconsistent is not automatically wrong, but a resize or a role change during synchronization is much harder to reason about. The safe checkpoint is simple: both roles known, both disks UpToDate, replication Established.

31.2.5. Creating a Filesystem and Mounting It on the Primary

The filesystem is created on the DRBD device, not on the backing LV. The mkfs.ext4 option used here is:

Option Description
-L DRBDDATA Set the filesystem volume label to DRBDDATA

On the primary drbd1, as john:

$ sudo mkfs.ext4 -L DRBDDATA /dev/drbd0
Discarding device blocks:      0/524236             done                            
Creating filesystem with 524236 1k blocks and 131072 inodes
Filesystem UUID: cf4161cd-e908-40f7-8450-1f2862050701
Superblock backups stored on blocks: 
    8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409

Allocating group tables:  0/64     done                            
Writing inode tables:  0/64     done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information:  0/64     done

The command also prints mke2fs 1.47.2 (1-Jan-2025) on standard error before the progress output. The device has 524236 1 KiB blocks, fewer than the 512 MiB LV because DRBD's internal metadata occupies the end of the device. The label DRBDDATA is optional, but it makes the filesystem easier to identify when inspecting the stack.

Create the mountpoint if your system does not have it, mount the filesystem on the primary, and check where it is mounted:

$ sudo mkdir -p /srv/drbd-data

That command normally prints nothing. Then:

$ sudo mount /dev/drbd0 /srv/drbd-data
$ findmnt /srv/drbd-data
TARGET         SOURCE     FSTYPE OPTIONS
/srv/drbd-data /dev/drbd0 ext4   rw,relatime

findmnt confirms that the mounted source is /dev/drbd0, not /dev/vgdrbd/data. If it shows the LV, you mounted the wrong layer: unmount it, check that no process is using the filesystem, and mount the DRBD device instead. The backing LV must never be mounted while DRBD is using it.

Give the reader account ownership of the practice directory and write a small file. The > in the next command is a shell redirection: it creates the file or truncates an existing one. The >> used later appends without truncating. Neither symbol is an argument to printf.

$ sudo chown john:john /srv/drbd-data
$ printf "alpha 1\nbeta 2\n" > /srv/drbd-data/records.txt

The chown and printf commands normally print nothing; the file write is redirected. The checksum prints the digest:

$ sha256sum /srv/drbd-data/records.txt
669ac839f1f45f3c9715a1f97fa63316870a1c4a9847d3218933811ef93f93ae  /srv/drbd-data/records.txt

The file now contains two lines. The checksum is a stable identifier for this version of the data. You will see the same hash on the peer after a planned switchover. A hash proves that the file contents match at the moment it is taken, not that the applications that produced them were consistent; for a database, use the application's own consistency check as well.