refactor: apply mkfs force flag only when overwrite is true
Introduce $forceFlag variable to set -f/-F for forced formatting while skipping force for regular runs.
This commit is contained in:
committed by
Jonathon Anderson
parent
378abfc373
commit
0c15c10515
@@ -39,26 +39,17 @@ if ! command -v die >/dev/null; then
|
||||
fi
|
||||
|
||||
already_formatted() {
|
||||
dev="$1"
|
||||
|
||||
# Step 1: check if blkid recognizes a filesystem
|
||||
fs_type=$(blkid -o value -s TYPE "$dev" 2>/dev/null)
|
||||
|
||||
if [ -z "$fs_type" ]; then
|
||||
# No recognized filesystem
|
||||
return 1
|
||||
if ! command -v wipefs >/dev/null ; then
|
||||
info "warewulf: wipefs not found, cannot check if device is already formatted"
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Step 2: try mounting read-only to a temp dir
|
||||
tmpdir=$(mktemp -d)
|
||||
if mount -o ro,norecovery -t "$fs_type" "$dev" "$tmpdir" >/dev/null 2>&1; then
|
||||
umount "$tmpdir"
|
||||
rmdir "$tmpdir"
|
||||
return 0 # usable filesystem
|
||||
else
|
||||
rmdir "$tmpdir"
|
||||
return 1 # filesystem exists but not mountable → treat as unformatted
|
||||
if wipefs -n "${1}" &>/dev/null; then
|
||||
info "warewulf: ${1} already formatted"
|
||||
return 0
|
||||
fi
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
if command -v mkfs >/dev/null ; then :
|
||||
@@ -98,26 +89,17 @@ if ! command -v die >/dev/null; then
|
||||
fi
|
||||
|
||||
already_formatted() {
|
||||
dev="$1"
|
||||
|
||||
# Step 1: check if blkid recognizes a filesystem
|
||||
fs_type=$(blkid -o value -s TYPE "$dev" 2>/dev/null)
|
||||
|
||||
if [ -z "$fs_type" ]; then
|
||||
# No recognized filesystem
|
||||
return 1
|
||||
if ! command -v wipefs >/dev/null ; then
|
||||
info "warewulf: wipefs not found, cannot check if device is already formatted"
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Step 2: try mounting read-only to a temp dir
|
||||
tmpdir=$(mktemp -d)
|
||||
if mount -o ro,norecovery -t "$fs_type" "$dev" "$tmpdir" >/dev/null 2>&1; then
|
||||
umount "$tmpdir"
|
||||
rmdir "$tmpdir"
|
||||
return 0 # usable filesystem
|
||||
else
|
||||
rmdir "$tmpdir"
|
||||
return 1 # filesystem exists but not mountable → treat as unformatted
|
||||
if wipefs -n "${1}" &>/dev/null; then
|
||||
info "warewulf: ${1} already formatted"
|
||||
return 0
|
||||
fi
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
if command -v mkfs >/dev/null ; then :
|
||||
@@ -129,7 +111,7 @@ if command -v mkfs >/dev/null ; then :
|
||||
fi
|
||||
if true || ! already_formatted /dev/disk/by-partlabel/scratch; then
|
||||
info "warewulf: mkfs: formatting /dev/disk/by-partlabel/scratch"
|
||||
mkfs --type=ext4 -f /dev/disk/by-partlabel/scratch || die "warewulf: mkfs: failed to format /dev/disk/by-partlabel/scratch"
|
||||
mkfs --type=ext4 -F /dev/disk/by-partlabel/scratch || die "warewulf: mkfs: failed to format /dev/disk/by-partlabel/scratch"
|
||||
else
|
||||
info "warewulf: mkfs: skipping /dev/disk/by-partlabel/scratch"
|
||||
fi
|
||||
@@ -173,26 +155,17 @@ if ! command -v die >/dev/null; then
|
||||
fi
|
||||
|
||||
already_formatted() {
|
||||
dev="$1"
|
||||
|
||||
# Step 1: check if blkid recognizes a filesystem
|
||||
fs_type=$(blkid -o value -s TYPE "$dev" 2>/dev/null)
|
||||
|
||||
if [ -z "$fs_type" ]; then
|
||||
# No recognized filesystem
|
||||
return 1
|
||||
if ! command -v wipefs >/dev/null ; then
|
||||
info "warewulf: wipefs not found, cannot check if device is already formatted"
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Step 2: try mounting read-only to a temp dir
|
||||
tmpdir=$(mktemp -d)
|
||||
if mount -o ro,norecovery -t "$fs_type" "$dev" "$tmpdir" >/dev/null 2>&1; then
|
||||
umount "$tmpdir"
|
||||
rmdir "$tmpdir"
|
||||
return 0 # usable filesystem
|
||||
else
|
||||
rmdir "$tmpdir"
|
||||
return 1 # filesystem exists but not mountable → treat as unformatted
|
||||
if wipefs -n "${1}" &>/dev/null; then
|
||||
info "warewulf: ${1} already formatted"
|
||||
return 0
|
||||
fi
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
if command -v mkfs >/dev/null ; then :
|
||||
@@ -204,7 +177,7 @@ if command -v mkfs >/dev/null ; then :
|
||||
fi
|
||||
if true || ! already_formatted /dev/disk/by-partlabel/scratch; then
|
||||
info "warewulf: mkfs: formatting /dev/disk/by-partlabel/scratch"
|
||||
mkfs --type=ext4 -f /dev/disk/by-partlabel/scratch || die "warewulf: mkfs: failed to format /dev/disk/by-partlabel/scratch"
|
||||
mkfs --type=ext4 -F /dev/disk/by-partlabel/scratch || die "warewulf: mkfs: failed to format /dev/disk/by-partlabel/scratch"
|
||||
else
|
||||
info "warewulf: mkfs: skipping /dev/disk/by-partlabel/scratch"
|
||||
fi
|
||||
|
||||
@@ -16,26 +16,17 @@ if ! command -v die >/dev/null; then
|
||||
fi
|
||||
|
||||
already_formatted() {
|
||||
dev="$1"
|
||||
|
||||
# Step 1: check if blkid recognizes a filesystem
|
||||
fs_type=$(blkid -o value -s TYPE "$dev" 2>/dev/null)
|
||||
|
||||
if [ -z "$fs_type" ]; then
|
||||
# No recognized filesystem
|
||||
return 1
|
||||
if ! command -v wipefs >/dev/null ; then
|
||||
info "warewulf: wipefs not found, cannot check if device is already formatted"
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Step 2: try mounting read-only to a temp dir
|
||||
tmpdir=$(mktemp -d)
|
||||
if mount -o ro,norecovery -t "$fs_type" "$dev" "$tmpdir" >/dev/null 2>&1; then
|
||||
umount "$tmpdir"
|
||||
rmdir "$tmpdir"
|
||||
return 0 # usable filesystem
|
||||
else
|
||||
rmdir "$tmpdir"
|
||||
return 1 # filesystem exists but not mountable → treat as unformatted
|
||||
if wipefs -n "${1}" &>/dev/null; then
|
||||
info "warewulf: ${1} already formatted"
|
||||
return 0
|
||||
fi
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
if command -v mkfs >/dev/null ; then :
|
||||
@@ -66,7 +57,15 @@ if command -v mkfs >/dev/null ; then :
|
||||
{{- if and $fs.type $fs.device }}
|
||||
if {{if $fs.overwrite}}true{{else}}false{{end}} || ! already_formatted {{ $fs.device }}; then
|
||||
info "warewulf: mkfs: formatting {{ $fs.device }}"
|
||||
mkfs {{ not (empty $fs.type) | ternary (print "--type=" $fs.type) "" }} {{ not (empty $fs.label) | ternary (print "-L " $fs.label) "" }} {{ not (empty $fs.uuid) | ternary (print "-U " $fs.uuid) "" }} {{ default "" $fs.options }} -f {{ $fs.device }} {{ default "" $fs.size }} || die "warewulf: mkfs: failed to format {{ $fs.device }}"
|
||||
{{- $forceFlag := "" }}
|
||||
{{- if $fs.overwrite }}
|
||||
{{- if eq $fs.type "ext4" }}
|
||||
{{- $forceFlag = "-F" }}
|
||||
{{- else }}
|
||||
{{- $forceFlag = "-f" }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
mkfs {{ not (empty $fs.type) | ternary (print "--type=" $fs.type) "" }} {{ not (empty $fs.label) | ternary (print "-L " $fs.label) "" }} {{ not (empty $fs.uuid) | ternary (print "-U " $fs.uuid) "" }} {{ default "" $fs.options }} {{ $forceFlag }} {{ $fs.device }} {{ default "" $fs.size }} || die "warewulf: mkfs: failed to format {{ $fs.device }}"
|
||||
else
|
||||
info "warewulf: mkfs: skipping {{ $fs.device }}"
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user