Refracta Development, Scripts, etc.
Post a reply

Problem changing hostname during install

Sun Dec 09, 2012 4:50 am

Using refractainstaller-gui-9.0.6-1, if you choose to change the hostname, and you use illegal characters, you'll end up with no hostname in the installed system. The hostname is changed by editing etc/hosts and etc/hostname with sed. I tried changing that so it uses the hostname command in chroot, and it works, but there's a problem with it.

When I try to change the user name and change the hostname, the script fails to ask for the new user name, and as a result, the usermod command fails. The system still installs and boots, but it retains the old user name. If I choose to change the user name and don't try to change the host name, it works as it should.

One more piece of information - I'm also creating a separate /home. I don't know if that's related to the problem, because I haven't yet tried it with a single partition. It's not that I'm lazy - I've done about 20 installs today, trying to identify the problem.

Here's the script that I'm using - https://gist.github.com/4243294
The code for changing the hostname starts at line 951
The code for changing the user name starts at line 1113

And the following have been added to the excludes list
Code:
- /lib/live/overlay
- /lib/live/image
- /lib/live/rootfs
- /lib/live/mount
- /run/*


And one more thing, completely unrelated to this problem, but you might notice it - for every yad window, there's a GTK error message. Those error messages go away if you install gtk2-engines-pixbuf.

Re: Problem changing hostname during install

Sun Dec 09, 2012 1:52 pm

OK, I think I fixed it, but I'm not allowed to tell you what I did, even if I place it in code brackets. I'm tired of troubleshooting, and I don't feel like figuring out what I can't say on my own damned forum, so here's the link to the gist. Look around lines 600 and 970 for the fixes.
https://gist.github.com/4244939

Re: Problem changing hostname during install

Sun Dec 09, 2012 3:13 pm

Don't know if I can help with this much, all systems here use a seperate "data" partition rather than seperate /home

However I can't see how (line 953) can work

Code:
chroot /target hostname "$new_hostname"


"$new_hostname" will be unset in the actual chroot session, I can see that in a manual chroot session of an uncompressed FS

I thought anyway, it was enough to just to sed [slash]etc/hostname and [slash]etc/hosts in the target mountpoint?

EDIT I wrote and posted this before I saw post before

Re: Problem changing hostname during install

Sun Dec 09, 2012 4:07 pm

The target is the uncompressed copy of the running system that was rsync'd to the hard drive. The reason I wanted to use the hostname command was because it would check for illegal characters, so I wouldn't have to do that. It did work to change the hostname, but it caused the problem with changing the user name. No clue why it interferes with usermod, but apparently, it does. The fix is to add code to check for illegal characters myself, and to just edit the hostname in the appropriate files. It works.

Re: Problem changing hostname during install

Thu Mar 20, 2014 12:01 pm

seems this problem is back in Debian testing. Im not sure which update broke it, but i have not touched the refracta installer script at all since the last build i made and it worked perfectly fine then, however now when i try and build and then install i get "ilegal hostname" when i try to change hostname during install, I can even type in the current hostname and i still get the same error.

So somewhere debian must have changed something on hostname front ?

I even tried on a completely different distro and got the same results, ilegal hostname during install. ( hostname i was trying to use was vbox, then tried testing and a dozen other names, none worked.

This is on Debian Testing, Any assistance would be greatly appreciated.

Re: Problem changing hostname during install

Thu Mar 20, 2014 2:34 pm

The installer does not use the hostname command to change the hostname - it just edits the hostname file on the installed system. It appears that the problem is with bash. If you remove some of the characters from the test, it works in sid. Seems it doesn't like $, %, (, ) and *. (dollar sign, percent, parentheses and asterisk. Here's a test script - the original line is commented out, and the edited line is below it. This one works. I'm posting this in hopes that someone can figure out what to do with those offending characters. Removing the escape didn't help with the dollar sign, and I didn't test that with the other characters. If you want to hack your script, it's in the test_hostname function, around line 754.

Code:
#!/bin/bash
set -x
# Enter new hostname (or use the old hostname as the new one)
# Test to make sure it's a legal hostname, and let user fix it
# if it's not.
fix_hostname () {
   new_hostname=$(yad --entry --title="Change hostname" \
   --text="Illegal hostname. Try again.
   
You can use alphanumeric characters anywhere in the hostname, and
you can use the minus sign (-) as long as it's not at the beginning or end." \
   --entry-text="$HOSTNAME" --width=500 --button="OK":0)
   test_hostname   
}

test_hostname () {
#   if [[ $new_hostname =~ [_]|[@]|[~]|[\!]|[\#]|[=]|[+]|[\&]|[\^]|[\$]|[%]|[\(]|[\)]|[\*]|[\:]|[\;]|[\"]|[\']|[\`]|[,]|[.]|[\<]|[\>]|[\?]|[\{]|[\}]|[\[]|[\]]|[/]|[\|]|[\ ] ]]; then
   if [[ $new_hostname =~ [_]|[@]|[~]|[\!]|[\#]|[=]|[+]|[\&]|[\^]|[\:]|[\;]|[\"]|[\']|[\`]|[,]|[.]|[\<]|[\>]|[\?]|[\{]|[\}]|[\[]|[\]]|[/]|[\|] ]]|[\ ]; then
      fix_hostname
   elif [[ $new_hostname = -* ]] || [[ $new_hostname = *- ]]; then
      fix_hostname
   elif [[ -z $new_hostname ]]; then
      new_hostname="$HOSTNAME"
   fi
}

select_hostname () {
   new_hostname=$(yad --entry --title="Change hostname" \
   --text="Enter new hostname for installed system." \
   --entry-text="$HOSTNAME" --width=500 --button="OK":0)
   test_hostname
}

select_hostname

echo "new_hostname is $new_hostname"

exit 0

Re: Problem changing hostname during install

Thu Mar 20, 2014 3:35 pm

This seems to work. I put the offending characters in a separate test from the others, removed the escapes, and quoted each one.
Code:
test_hostname () {
   if [[ $new_hostname =~ [_]|[@]|[~]|[\!]|[\#]|[=]|[+]|[\&]|[\^]|[\:]|[\;]|[\"]|[\']|[\`]|[,]|[.]|[\<]|[\>]|[\?]|[\{]|[\}]|[\[]|[\]]|[/]|[\|]|[\ ] ]]; then
      fix_hostname
   elif [[ $new_hostname =~ ["$"]|["%"]|["("]|[")"]|["*"] ]];then
      fix_hostname
   elif [[ $new_hostname = -* ]] || [[ $new_hostname = *- ]]; then
      fix_hostname
   elif [[ -z $new_hostname ]]; then
      new_hostname="$HOSTNAME"
   fi
}

Re: Problem changing hostname during install

Thu Mar 20, 2014 3:49 pm

Thanx, that works.

Re: Problem changing hostname during install

Tue Mar 25, 2014 4:05 pm

Well, that didn't work so well for me. Here's the latest arrangement. Removed all the square brackets except for the test. Each character is quoted, and they're separated by a single pipe. Only double-quote and back tick needed to be escaped. I think this works, but I haven't actually tested every character in an illegal hostname to make sure it gives a warning and lets me try again.

Bad line breaks on the display... .
Code:
test_hostname () {
   if [[ $new_hostname =~ "$"|"%"|"("|")"|"*"|"_"|"@"|"~"|"!"|"#"|"="|"+"|"&"|"^"|":"|";"|"'"|","|"."|"<"|">"|"?"|"{"|"}"|"["|"]"|"/"|"|"|" " ]]; then
      fix_hostname
   elif [[ $new_hostname =~ "\""|"\`" ]];then
      fix_hostname
   elif [[ $new_hostname = -* ]] || [[ $new_hostname = *- ]]; then
      fix_hostname
   elif [[ -z $new_hostname ]]; then
      new_hostname="$HOSTNAME"
   fi
}

Re: Problem changing hostname during install

Thu Apr 03, 2014 12:51 pm

Confirmed here in yesterday's sid build, just like raymerjacque said. I never saw this before, it worked before so it does look like an upgrade might have changed something, currently unknown.

I know there is a newer installer version with updated hostname code, will look at that later.

BTW I noticed that in a chroot of my build directory (with the usual bind-mounts) the "hostname" command returns the same as the host system although /e-t-c/hostname in the chroot is different. To directly edit it seems probably the only way, else some odd things may happen.

EDIT repaced test_hostname function with the code from previous post and the problem was solved. Tried deliberately some illegal characters and fix_hostname appeared as expected.
Post a reply