Package: partman-lvm Version: 40 Severity: wishlist Tags: patch
The attached patch allows partman-lvm to check the names given for VG's and LV's before trying to create them. The checks have been derived from reading the lvm2 sources and bug #254630.
The only thing that bothers me a bit is that the lvm tools use isalnum() as part of their checks which (if I understood things correctly) will work differently depending on locale. However, the use of [:alnum:] in the sed script should accomplish the same thing.
Regards, David
Index: debian/partman-lvm.templates =================================================================== --- debian/partman-lvm.templates (revision 38366) +++ debian/partman-lvm.templates (working copy) @@ -176,6 +176,16 @@ No name for the volume group has been entered. Please enter a name. +Template: partman-lvm/vgcreate_badnamegiven +Type: error +_Description: Invalid volume group name entered + An invalid name for the 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. + Template: partman-lvm/vgcreate_nameused Type: error _Description: Volume group name already in use @@ -317,6 +327,17 @@ No name for the logical volume has been entered. Please enter a name. +Template: partman-lvm/lvcreate_badnamegiven +Type: error +_Description: Invalid logical volume name entered + An invalid name for the logical volume 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 ".." as well as any name starting with + "snapshot" are not allowed. + Template: partman-lvm/lvcreate_exists Type: error _Description: Error while creating a new logical volume Index: lvm_tools.sh =================================================================== --- lvm_tools.sh (revision 38366) +++ 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 38366) +++ 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/vgcreate_badnamegiven "false" + db_input critical partman-lvm/vgcreate_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/lvcreate_badnamegiven "false" + db_input critical partman-lvm/lvcreate_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"