31.3. Operating and Moving the Primary Role

Once the resource is healthy, the routine work is inspection and controlled role movement. The primary role must move only after writers have stopped and the filesystem is unmounted, and it must be restored deliberately after a reboot. Growing the device adds a second ordering problem: every layer from the backing volume to the filesystem must grow in sequence or the filesystem will not see the new space.

31.3.1. Inspecting Disk, Connection, and Replication State

drbdadm status is the normal overview. Individual state components are useful when a script needs one value or when the combined output is ambiguous. On the current primary, drbd1, as john:

$ sudo drbdadm role r0
Primary/Secondary
$ sudo drbdadm dstate r0
UpToDate/UpToDate
$ sudo drbdadm cstate r0
Connected

The slash separates local and peer values. role reports the local role first; dstate reports the local and peer disk state; cstate reports the connection between the nodes. Connected means the replication link is established. It does not by itself mean that both disks are current, which is why the disk state is checked separately.

The drbdadm status output combines those values and adds replication progress:

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

The important states in a two-node resource are:

Field Value Meaning
role Primary Local device may be written and mounted
role Secondary Local device receives replication but is not a normal writer
disk UpToDate Local backing device contains the current blocks
disk Inconsistent Local copy is being rebuilt or has not been initialized as a source
disk DUnknown The peer cannot currently determine this node's disk state
replication Established The link is connected and replication is current
replication SyncSource, SyncTarget A full or partial resynchronization is in progress
replication Connecting, WFConnection The peer is not currently reachable
peer-disk UpToDate The peer's backing device is current

/proc/drbd remains useful for counters and progress, especially during a long synchronization. The first line contains the module version and API, and each resource line begins with cs: (connection state), ro: (roles), and ds: (disk states). The flags after those fields indicate whether the resource is primary, whether it is suspended, and which write-ordering method is in use. The ns/nr/dw/dr counters and oos value show network and disk activity and the amount still out of sync. A healthy idle resource does not necessarily show zero in every counter, because the counters describe activity since the resource was configured, not a snapshot of a queue.

31.3.2. Stopping Writers, Unmounting, Demoting, and Promoting in Order

A planned switchover is a sequence, not a pair of independent role commands. The old primary must stop writing and release the filesystem before it is demoted. The new primary must be promoted before the filesystem is mounted. Reversing those steps either fails or, worse, leaves a window in which two nodes believe they may write.

The minimal order for a filesystem with no network service is:

  1. Stop applications that write to the mount.
  2. Unmount the filesystem on the old primary.
  3. Demote the old primary to secondary.
  4. Confirm the old primary's disk is still UpToDate.
  5. Promote the new node to primary.
  6. Mount the filesystem on the new primary.
  7. Verify application data and restart writers.

On drbd1, after ensuring no process is using the mount:

$ sudo umount /srv/drbd-data

The command normally prints nothing. If it reports that the target is busy, find the process with sudo fuser -vm /srv/drbd-data or sudo lsof +D /srv/drbd-data, stop it cleanly, and try the unmount again. Do not use a forced unmount to get past a database or service that is still writing.

Demote the old primary and inspect the result:

$ sudo drbdadm secondary r0

The demotion normally prints nothing. Inspect the result:

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

Both nodes are now secondary, and both disks are still current. This is a safe resting point. On drbd2, as john, promote and mount:

$ sudo drbdadm primary r0
$ sudo mount /dev/drbd0 /srv/drbd-data

Both commands normally print nothing. drbdadm primary succeeds because the peer's disk was UpToDate and no other primary existed. If it refuses, inspect drbdadm status rather than adding --force; a refusal usually means the peer is not current or the old primary has not been demoted.

When NFS or another network service is running, insert the service steps around the filesystem steps. On the client, unmount first so that no new requests arrive. On the old primary, stop the service, unmount, and demote. On the new primary, promote, mount, start the service, and then remount the client. In this example, the client and service side of that order is:

$ sudo umount /mnt/drbd
$ sudo systemctl stop nfs-server
$ sudo umount /srv/drbd-data
$ sudo drbdadm secondary r0

Each command normally prints nothing. The client unmount is on drbdclient; the service stop, filesystem unmount, and demotion are on the old primary. The NFS server must not be left running with an export whose underlying mount has disappeared. A full clustered NFS design, including lock recovery and virtual IP movement, is covered in Chapter 32.

31.3.3. Verifying Application Data After a Planned Switchover

A role change is not complete until the new primary serves the data that the old primary wrote. On drbd2, now primary and mounted, read the file and compare its checksum:

$ cat /srv/drbd-data/records.txt
alpha 1
beta 2
$ sha256sum /srv/drbd-data/records.txt
669ac839f1f45f3c9715a1f97fa63316870a1c4a9847d3218933811ef93f93ae  /srv/drbd-data/records.txt

The hash matches the value recorded on drbd1 before the switchover. That establishes that the block device seen by drbd2 contains the same file bytes. It does not prove that a database's internal transaction log is consistent; for a database, run the database's own recovery and consistency checks before allowing clients to write.

This example uses NFSv4 as a concrete client. On the primary drbd2, create /etc/exports with the client network:

/srv/drbd-data 192.168.231.0/24(rw,sync,no_subtree_check)

Start the server and inspect the active export table:

$ sudo systemctl start nfs-server

Starting the service normally prints nothing. The export table confirms the active options:

$ sudo exportfs -v
/srv/drbd-data  192.168.231.0/24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash)

exportfs -v asks for a verbose listing and shows the export options that the kernel is actually using, including defaults that were not written in /etc/exports. root_squash maps a remote root user to the anonymous user, so a client root cannot write as root into this export. The practice directory is owned by john, and the client writes as john.

On drbdclient, as john, create the mountpoint if needed, mount the export, and read the file. The -t nfs option selects the NFS filesystem type; without it, mount would try to infer the type from the source or from /etc/fstab.

Option Description
-t nfs Mount using the NFS filesystem driver
$ sudo mkdir -p /mnt/drbd
$ sudo mount -t nfs drbd2:/srv/drbd-data /mnt/drbd

The mkdir and mount commands normally print nothing. Read the export:

$ cat /mnt/drbd/records.txt
alpha 1
beta 2

The NFS mount uses the server name drbd2, which resolves through /etc/hosts in this example. A production client would use a service name or a virtual IP that moves with the service, not the physical name of the current primary. Before writing more data, take the chapter's independent copy of the records file in the client's workspace:

$ cp /mnt/drbd/records.txt ~/debian-handbook/drbd/backup-records.txt

The cp command normally prints nothing. Verify the copy:

$ sha256sum ~/debian-handbook/drbd/backup-records.txt
669ac839f1f45f3c9715a1f97fa63316870a1c4a9847d3218933811ef93f93ae  /home/john/debian-handbook/drbd/backup-records.txt

The copy currently matches the live two-line file. It will not match later writes, which is exactly what makes it a point-in-time copy rather than a third replica. It lives on the client, outside the DRBD resource, and is not updated automatically.

Write a client line and confirm that the primary sees it:

$ printf "client 3\n" >> /mnt/drbd/records.txt

The append normally prints nothing. Read the client's view:

$ cat /mnt/drbd/records.txt
alpha 1
beta 2
client 3

On the primary, as john:

$ tail -n 1 /srv/drbd-data/records.txt
client 3

The write traveled through NFS, the ext4 filesystem, /dev/drbd0, and DRBD to the local backing device and the peer. NFS caching can make a read appear before the data is durable in every failure scenario; the sync export option reduces that window, and protocol C ensures that DRBD has written the blocks to both backing devices before acknowledging them.

31.3.4. Reboot Behavior and Preventing Unmanaged Promotion

Debian installs drbd.service disabled. Enabling the service is a deliberate administrative decision, not something the package does during installation. On a node, before enabling it:

$ sudo systemctl is-enabled drbd
disabled

systemctl is-enabled exits with a nonzero status when the unit is disabled; that is an expected result, not a failure of the command. To test the boot behavior, first return the resource to a clean secondary state. On the current primary drbd2, after stopping clients and the NFS server (section 31.3.2), unmount and demote:

$ sudo umount /srv/drbd-data
$ sudo drbdadm secondary r0

Both commands normally print nothing. Reboot drbd1 with sudo systemctl reboot, wait for it to come back, and log in again as john. The service is still disabled, so the resource is not configured:

$ sudo drbdadm status r0
r0: No such resource
Command 'drbdsetup-84 status r0' terminated with exit code 10

The error appears on standard error, and the exit status is 10. No node was promoted during boot because no resource was started. The second node may still have its resource loaded because it was not rebooted; that does not make it primary or make the rebooting node primary.

Enable and start the service:

$ sudo systemctl enable --now drbd
Synchronizing state of drbd.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable drbd
Created symlink '/etc/systemd/system/multi-user.target.wants/drbd.service' → '/usr/lib/systemd/system/drbd.service'.

The output confirms the unit is now enabled for future boots. The resource is configured and connected, but still secondary:

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

On the peer, the resource also remains secondary:

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

Repeat sudo systemctl enable --now drbd on the peer if it should also configure the resource at boot. The status remains secondary on both nodes.

Promotion is still an explicit action. This is intentional. A node that boots first should not automatically become the writer, because the other node might still be alive and writing. A cluster manager can make a promotion decision after it has established membership, checked the resource's health, and fenced a failed peer. Without a cluster manager, the administrator must decide.

DRBD 8.4 offers become-primary-on in the startup section for deployments that deliberately want a node to take the primary role at startup. Debian also ships drbd-promote@.service for systemd-managed promotion. Neither is a substitute for a cluster manager: a startup rule based only on boot order can promote the wrong node or create two writers. Leave those mechanisms alone in a manually managed pair.

31.3.5. Growing Replicated Storage Through the Complete Layer Stack

Growth must be applied in order from the physical backing device up to the filesystem. The logical volume must grow on both nodes, DRBD must adopt the new size, the peer must catch up, and only then may the filesystem grow. Resizing the filesystem first leaves it limited to the old device size, and resizing DRBD on the primary before the peer's LV is large enough causes a size mismatch.

Layers of the replicated data path

Figure 31.3. Growing a DRBD filesystem changes four layers in order. The LVM logical volume is extended on both nodes, DRBD adopts the new device size on the primary, the peer resynchronizes the added blocks, and the filesystem grows inside the completed device.

Start from a mounted primary. If the reboot exercise left both nodes secondary, promote drbd2 and mount the filesystem again with the commands from section 31.3.2. On drbd2, as john, record the current size:

$ df -h /srv/drbd-data
Filesystem      Size  Used Avail Use% Mounted on
/dev/drbd0      470M  142K  440M   1% /srv/drbd-data

The 512 MiB LV yields a 470 MiB ext4 filesystem after DRBD metadata and filesystem overhead. Extend the logical volume to 768 MiB on both nodes. On drbd1:

$ sudo lvextend -L 768M /dev/vgdrbd/data
  Size of logical volume vgdrbd/data changed from 512.00 MiB (128 extents) to 768.00 MiB (192 extents).
  Logical volume vgdrbd/data successfully resized.

On drbd2:

$ sudo lvextend -L 768M /dev/vgdrbd/data
  Size of logical volume vgdrbd/data changed from 512.00 MiB (128 extents) to 768.00 MiB (192 extents).
  Logical volume vgdrbd/data successfully resized.

-L 768M sets the absolute size; it is not an increment. If you use -L +256M, the result is the same here, but the absolute form makes the target size explicit. Do not extend only one node and expect DRBD to grow: the peer must be able to hold the same number of blocks.

On the primary, tell DRBD to adopt the new backing size:

$ sudo drbdadm resize r0

The command normally prints nothing. It also adjusts DRBD's internal metadata to account for the larger device. The peer now needs to receive the new blocks. Wait until the status shows both sides current:

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

Do not run resize2fs at the first UpToDate if a resynchronization is still in progress; check that the status no longer reports SyncSource or SyncTarget. Once the peer is current, grow the mounted filesystem online:

$ sudo resize2fs /dev/drbd0
Filesystem at /dev/drbd0 is mounted on /srv/drbd-data; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 6
The filesystem on /dev/drbd0 is now 786372 (1k) blocks long.

resize2fs also prints resize2fs 1.47.2 (1-Jan-2025) on standard error. The new filesystem size is 786372 1 KiB blocks. Check the mounted result:

$ df -h /srv/drbd-data
Filesystem      Size  Used Avail Use% Mounted on
/dev/drbd0      710M  142K  670M   1% /srv/drbd-data

The filesystem reports 710 MiB to df, still smaller than the 768 MiB device because ext4 metadata occupies space inside it. Confirm that both kernels see the same device size in 512-byte sectors:

$ cat /sys/block/drbd0/size
1572744

On the peer, as john:

$ cat /sys/block/drbd0/size
1572744

Matching sector counts are a quick check that both nodes adopted the same geometry. If they differ, stop and investigate before writing more data. A common cause is extending only one LV, or running drbdadm resize on a node whose backing device did not actually grow.