[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#375161: partman-lvm: check LV and VG names before trying to create them



Frans Pop wrote:
Is there any way the two templates can be combined? They are very similar and having 60+ translations of them is not very appealing, especially as they will be only rarely used.

Ok, I've attached a new version of the patch which merges the two templates. Beside the template name change it should be identical to the previous patch.

Regards,
David
Index: debian/partman-lvm.templates
===================================================================
--- debian/partman-lvm.templates	(revision 38836)
+++ debian/partman-lvm.templates	(working copy)
@@ -386,3 +386,13 @@
  You can switch to the third virtual console (VT3) by pressing Alt-F3,
  and you get back to the installer by pressing Alt-F1.
 
+Template: partman-lvm/badnamegiven
+Type: error
+_Description: Invalid name entered
+ An invalid name for the logical volume or volume group has been entered.
+ Please choose another name.
+ .
+ Names may only contain alphanumeric characters, hyphen, plus, period
+ and underscore. They must be 128 characters or less and may not begin
+ with a hyphen. The names "." and ".." are not allowed. In addition,
+ logical volume names may not begin with "snapshot".
Index: lvm_tools.sh
===================================================================
--- lvm_tools.sh	(revision 38836)
+++ lvm_tools.sh	(working copy)
@@ -132,7 +132,46 @@
 	return 0
 }	
 
+# Common checks for VG and LV names
+# Rules:
+# 1) At least one character
+# 2) Only alphanumeric characters (isalnum()) and "._-+"
+# 3) May not be "." or ".."
+# 4) must not start with a hyphen
+# 5) maximum name length 128 characters
+# See lvm2 source and bug #254630 for details
+lvm_name_ok() {
+	local name
+	name="$1"
 
+	# Rule 1
+	if [ -z "$name" ]; then
+		return 1
+	fi
+
+	# Rule 2
+	if [ "$(echo -n "$name" | sed 's/[^-+_\.[:alnum:]]//g')" != "$name" ]; then
+		return 1
+	fi
+
+	# Rule 3
+	if [ "$name" = "." -o "$name" = ".." ]; then
+		return 1
+	fi
+
+	# Rule 4
+	if [ "$(echo -n "$name" | sed 's/^-//')" != "$name" ]; then
+		return 1
+	fi
+
+	# Rule 5
+	if [ $(echo -n "$name" | wc -c) -gt 128 ]; then
+		return 1
+	fi
+
+	return 0
+}
+
 ###############################################################################
 #                                
 # Physical Volume utility functions
@@ -285,7 +324,26 @@
 	return $?
 }
 
+# Checks that a logical volume name is ok
+# Rules:
+# 1) The common rules (see lvm_name_ok)
+# 2) must not start with "snapshot"
+# See lvm2 source and bug #254630 for details
+lv_name_ok() {
+	local lvname
+	lvname="$1"
 
+	# Rule 1
+	lvm_name_ok "$lvname" || return 1
+
+	# Rule 2
+	if [ "${lvname#snapshot}" != "$lvname" ]; then
+		return 1
+	fi
+
+	return 0
+}
+
 ###############################################################################
 #                                
 # Volume Group utility functions
@@ -393,3 +451,17 @@
 	log-output -t partman-lvm vgreduce "$vg" "$pv"
 	return $?
 }
+
+# Checks that a logical volume name is ok
+# Rules:
+# 1) The common rules (see lvm_name_ok)
+# See lvm2 source and bug #254630 for details
+vg_name_ok() {
+	local vgname
+	vgname="$1"
+
+	# Rule 1
+	lvm_name_ok "$vgname" || return 1
+
+	return 0
+}
Index: choose_partition/lvm/do_option
===================================================================
--- choose_partition/lvm/do_option	(revision 38836)
+++ choose_partition/lvm/do_option	(working copy)
@@ -109,6 +109,8 @@
 	[ $? -eq 30 ] && return
 	db_get partman-lvm/vgcreate_name
 	vg="$RET"
+
+	# Check VG name
 	if [ -z "$vg" ]; then
 		db_set partman-lvm/vgcreate_nonamegiven "false"
 		db_input critical partman-lvm/vgcreate_nonamegiven
@@ -116,6 +118,13 @@
 		return
 	fi
 
+	if ! vg_name_ok "$vg"; then
+		db_set partman-lvm/badnamegiven "false"
+		db_input critical partman-lvm/badnamegiven
+		db_go
+		return
+	fi
+
 	# Check whether the VG name is already in use
 	if vgs "$vg" > /dev/null 2>&1; then
 		db_set partman-lvm/vgcreate_nameused "false"
@@ -421,14 +430,23 @@
 	db_go
 	[ $? -eq 30 ] && return
 	db_get partman-lvm/lvcreate_name
-	if [ -z "$RET" ]; then
+	lv="$RET"
+
+	# Check LV name
+	if [ -z "$lv" ]; then
 		db_set partman-lvm/lvcreate_nonamegiven "false"
 		db_input critical partman-lvm/lvcreate_nonamegiven
 		db_go
 		return
 	fi
-	lv="$RET"
 
+	if ! lv_name_ok "$lv"; then
+		db_set partman-lvm/badnamegiven "false"
+		db_input critical partman-lvm/badnamegiven
+		db_go
+		return
+	fi
+
 	# Make sure the name isn't already in use
 	if lvs "/dev/$vg/$lv" > /dev/null 2>&1; then
 		db_subst partman-lvm/lvcreate_exists LV "$lv"

Reply to: