3

There appears to be an undesired side effect to parted creating a new partition, if the partition previously existed it will be automatically mounted, even before it has been formatted.

This is troublesome for a script that would for example:

parted -sm /dev/sdb mklabel gpt
parted -sm /dev/sdb mkpart primary ext4 1 1.8T
mkfs.ext4 -L drive /dev/sdb1

mkfs fails here because the partition is mounted.

This behavior is observed in CentOS 7, when the target disk was previously formatted and mounted since last boot.

Unmounting, removing the entry in /etc/fstab and the mount point directory, still triggers the condition; the mount point will be created and the partition automatically mounted.

It isn't clear who creates the mount point directory or from where does parted take the info.

A reboot between the fstab clearing and parted call will workaround this issue. There are no documented parameters in parted concerning auto mounting.

So is this behavior normal for parted and is there a reliable way to prevent it from auto mounting?

5
  • Someone else had a similar issue, I attempt to generalize the case and identify specific steps to reproduce. For my purpose I worked around the problem by unmounting the partition after the call to parted, but still it seems like this magic operation should not happen, especially in a linux environment. Commented Mar 22, 2018 at 15:31
  • I'd say parted tells the system to reread the disk as if new, and some udev mechanism sees a removable disk appearing and mounts it. So you might have to tell an other part of the system to not do anything when a removable device is inserted. Or check if parted has an option to wipe the filesystem signature of the former data (like fdisk or gdisk do, although perhaps not on centos) Commented Mar 22, 2018 at 17:15
  • Answerers might want to read the thread on the systemd-devel mailing list started on 2018-01-23 by Franck Bui, dealing with this. Commented Mar 22, 2018 at 20:11
  • Found the systemd-devel thread mentionned by @JdeBP interesting info in deed. I tried changing the fstab options; with noauto the partitions won't be mounted at boot, with nothing or auto the behavior is the same and the automagic mounting is in effect. So as Franck Bui points out, a change in systemd would be needed to address this. Commented Mar 23, 2018 at 13:54
  • If there was previously a mount entry in /etc/fstab, you should remember that in CentOS 7 (and any distributions using systemd) you should remember to run systemctl daemon-reload after modifying /etc/fstab, since you'll want systemd-fstab-generator to rebuild the *.mount unit files located in /run/systemd/generator/*.mount. If you don't do that, the old *.mount units generated from the previous /etc/fstab contents will still be in effect. Commented Jan 12, 2023 at 10:20

1 Answer 1

1

After some thorough testing, as of March 2018, the answer is that there's no way with parted to prevent systemd from automatically re-mounting a partition after re-creation unless it was explicitly configured with noauto in /etc/fstab beforehand.

Workaround

As hinted by @A.B, another tool like fdisk or gdisk can be used instead of parted to properly clear the partition info before re-creating. The only drawback is that these tools use an interactive shell and aren't designed for batch processing in a script.

Interactive Mode

# gdisk /dev/sda
GPT fdisk (gdisk) version 0.8.6

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): o
This option deletes all partitions and creates a new protective MBR.
Proceed? (Y/N): Y

Command (? for help): n
Partition number (1-128, default 1):
First sector (34-3907029134, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-3907029134, default = 3907029134) or {+-}size{KMGTP}:
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

Command (? for help): w

Final checks complete. About to write GPT data.
THIS WILL OVERWRITE EXISTING PARTITIONS!!

Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/sda.
The operation has completed successfully.

Automation by Pipe

This is not super clean or portable but the same result can be achieved by piping the user input, line by line. As in interactive mode, the empty lines will accept the defaults offered by gdisk, which in this case maximize the partition usage and select the proper type for a Linux system.

echo -ne 'o\nY\nn\n\n\n\n\nw\nY\n' | gdisk /dev/sda

SystemD Monitoring

While running gdisk we check dmesg for mounting activity and observe that there's only a couple requests for device info:

kern  :info  : [Mar27 10:38]  sda: sda1
kern  :info  : [  +1.019077]  sda: sda1

So with this method, our automation script can safely continue with the mkfs part.

1
  • 2
    The only drawback is that these tools use an interactive shell and aren't designed for batch processing in a script. sfdisk is a script-oriented version of fdisk, and sgdisk of gdisk, that might help Commented Nov 28, 2019 at 5:12

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.