Skip to content

Commit 3e8c105

Browse files
silverhadchConan-Kudo
authored andcommitted
src/useradd: Support config for creating home dirs as Btrfs subvolumes
Closes: #1162 Co-authored-by: Neal Gompa <[email protected]> Signed-off-by: Hadi Chokr <[email protected]> Signed-off-by: Neal Gompa <[email protected]> Reviewed-by: Alejandro Colomar <[email protected]>
1 parent a0d5a61 commit 3e8c105

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

man/useradd.8.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,33 @@
134134
</para>
135135
</listitem>
136136
</varlistentry>
137+
<varlistentry>
138+
<term>
139+
<option>--btrfs-subvolume-home</option>
140+
</term>
141+
<listitem>
142+
<para>
143+
Create the user's home directory as a Btrfs subvolume.
144+
</para>
145+
<para>
146+
If this option is not specified,
147+
<command>useradd</command> will follow the default behavior
148+
defined by the <option>BTRFS_SUBVOLUME_HOME</option> variable
149+
in <filename>/etc/default/useradd</filename>.
150+
If this variable is not set, the default value is no.
151+
</para>
152+
<para>
153+
When the <option>--btrfs-subvolume-home</option> command-line option
154+
is specified,
155+
a Btrfs subvolume is created
156+
regardless of any configuration file settings.
157+
</para>
158+
<para>
159+
Note: this feature works only if the underlying filesystem supports
160+
Btrfs subvolumes.
161+
</para>
162+
</listitem>
163+
</varlistentry>
137164
<varlistentry>
138165
<term>
139166
<option>-c</option>, <option>--comment</option>&nbsp;<replaceable>COMMENT</replaceable>

src/useradd.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ static const char *def_shell = "/bin/bash";
114114
static const char *def_template = SKEL_DIR;
115115
static const char *def_usrtemplate = USRSKELDIR;
116116
static const char *def_create_mail_spool = "yes";
117+
static const char *def_btrfs_subvolume_home = "no";
117118
static const char *def_log_init = "yes";
118119

119120
static long def_inactive = -1;
@@ -222,6 +223,7 @@ static bool home_added = false;
222223
#define DSKEL "SKEL"
223224
#define DUSRSKEL "USRSKEL"
224225
#define DCREATE_MAIL_SPOOL "CREATE_MAIL_SPOOL"
226+
#define DBTRFS_SUBVOLUME_HOME "BTRFS_SUBVOLUME_HOME"
225227
#define DLOG_INIT "LOG_INIT"
226228

227229
/* local function prototypes */
@@ -456,6 +458,7 @@ get_defaults(const struct option_flags *flags)
456458
def_usrtemplate = xstrdup(ccp);
457459
}
458460
}
461+
459462
/*
460463
* Create by default user mail spool or not ?
461464
*/
@@ -466,6 +469,15 @@ get_defaults(const struct option_flags *flags)
466469
def_create_mail_spool = xstrdup(ccp);
467470
}
468471

472+
/*
473+
* Create home directories as Btrfs subvolumes by default?
474+
*/
475+
else if (streq(buf, DBTRFS_SUBVOLUME_HOME)) {
476+
if (streq(ccp, ""))
477+
ccp = "no";
478+
def_btrfs_subvolume_home = xstrdup(ccp);
479+
}
480+
469481
/*
470482
* By default do we add the user to the lastlog and faillog databases ?
471483
*/
@@ -500,6 +512,7 @@ static void show_defaults (void)
500512
printf ("SKEL=%s\n", def_template);
501513
printf ("USRSKEL=%s\n", def_usrtemplate);
502514
printf ("CREATE_MAIL_SPOOL=%s\n", def_create_mail_spool);
515+
printf ("BTRFS_SUBVOLUME_HOME=%s\n", def_btrfs_subvolume_home);
503516
printf ("LOG_INIT=%s\n", def_log_init);
504517
}
505518

@@ -523,6 +536,7 @@ set_defaults(void)
523536
bool out_skel = false;
524537
bool out_usrskel = false;
525538
bool out_create_mail_spool = false;
539+
bool out_btrfs_subvolume_home = false;
526540
bool out_log_init = false;
527541
char buf[1024];
528542
char *new_file = NULL;
@@ -639,6 +653,11 @@ set_defaults(void)
639653
DCREATE_MAIL_SPOOL "=%s\n",
640654
def_create_mail_spool);
641655
out_create_mail_spool = true;
656+
} else if (!out_btrfs_subvolume_home && streq(buf, DBTRFS_SUBVOLUME_HOME)) {
657+
fprintf(ofp,
658+
DBTRFS_SUBVOLUME_HOME "=%s\n",
659+
def_btrfs_subvolume_home);
660+
out_btrfs_subvolume_home = true;
642661
} else if (!out_log_init && streq(buf, DLOG_INIT)) {
643662
fprintf(ofp, DLOG_INIT "=%s\n", def_log_init);
644663
out_log_init = true;
@@ -673,6 +692,8 @@ set_defaults(void)
673692

674693
if (!out_create_mail_spool)
675694
fprintf (ofp, DCREATE_MAIL_SPOOL "=%s\n", def_create_mail_spool);
695+
if (!out_btrfs_subvolume_home)
696+
fprintf (ofp, DBTRFS_SUBVOLUME_HOME "=%s\n", def_btrfs_subvolume_home);
676697
if (!out_log_init)
677698
fprintf (ofp, DLOG_INIT "=%s\n", def_log_init);
678699
/*
@@ -1431,6 +1452,9 @@ static void process_flags (int argc, char **argv, struct option_flags *flags)
14311452
}
14321453
}
14331454

1455+
if (!subvolflg && strcaseeq(def_btrfs_subvolume_home, "yes"))
1456+
subvolflg = true;
1457+
14341458
if (!gflg && !Nflg && !Uflg) {
14351459
/* Get the settings from login.defs */
14361460
Uflg = getdef_bool ("USERGROUPS_ENAB");

0 commit comments

Comments
 (0)