@@ -11,7 +11,12 @@ VIRT_TYPE="$(systemd-detect-virt)"
1111source " ${SCRIPT_DIR} /utils.sh"
1212
1313ensure_docker_config_json () {
14- mkdir -p /root/.docker
14+ mkdir -p /data/root/
15+ rm -Rf /root
16+ ln -sf /data/root /root
17+
18+ mkdir -p /data/.docker
19+ ln -sf /data/.docker /root/.docker
1520 rmdir /root/.docker/config.json 2> /dev/null && \
1621 echo " Warning: deleted config.json as dir"
1722 if [[ ! -f " /root/.docker/config.json" ]]; then
@@ -349,14 +354,73 @@ EOF
349354 setupcon || true
350355}
351356
357+ set_root_password () {
358+ local root_password
359+ root_password=" $( jq_config -r ' .os_root_password // empty' ) "
360+
361+ if [[ -z " ${root_password} " ]]; then
362+ usermod -L root
363+ return
364+ fi
365+
366+ # Check if password looks like a hash (contains typical hash delimiters or is too long for a plain password)
367+ # Common hash formats: $1$ (MD5), $2a/$2y/$2b (bcrypt), $5$ (SHA-256), $6$ (SHA-512)
368+ if [[ " ${root_password} " =~ ^\$ [0-9a-z]+\$ ]] || [[ ${# root_password} -gt 50 ]]; then
369+ # Treat as hash - use chpasswd with -e flag for encrypted passwords
370+ echo " root:${root_password} " | chpasswd -e
371+ else
372+ # Treat as plain password - use chpasswd without -e flag
373+ echo " root:${root_password} " | chpasswd
374+ fi
375+
376+ usermod -U root
377+
378+ report_info " cuos:init:root_access" " Root access is enabled"
379+ }
380+
352381create_ssh_hostkey () {
382+ local SSH_PERSIST_DIR=" /data/ssh-hostkeys"
383+ local SSH_CONFIG_DIR=" /etc/ssh"
384+
353385 shopt -s nullglob
354- hostkeys=(/etc/ssh/ssh_host_* _key)
355- if [ ${# hostkeys[@]} -eq 0 ]; then
356- echo " Generating SSH host keys"
357- mkdir -p /etc/ssh
358- chmod 700 /etc/ssh
386+ hostkeys=(" ${SSH_CONFIG_DIR} " /ssh_host_* _key)
387+ if [ ${# hostkeys[@]} -gt 0 ]; then
388+ return
389+ fi
390+
391+ mkdir -p " ${SSH_PERSIST_DIR} "
392+ mkdir -p " ${SSH_CONFIG_DIR} "
393+ chmod 700 " ${SSH_CONFIG_DIR} "
394+
395+ local persisted_keys=(" ${SSH_PERSIST_DIR} " /ssh_host_* _key)
396+ if [ ${# persisted_keys[@]} -gt 0 ]; then
397+ # Restore keys from persistent storage
398+ report_info " cuos:init:ssh_hostkeys" " Restoring SSH host keys from ${SSH_PERSIST_DIR} "
399+ cp " ${SSH_PERSIST_DIR} " /ssh_host_* _key " ${SSH_CONFIG_DIR} /" 2> /dev/null || true
400+ cp " ${SSH_PERSIST_DIR} " /ssh_host_* _key.pub " ${SSH_CONFIG_DIR} /" 2> /dev/null || true
401+ chmod 600 " ${SSH_CONFIG_DIR} " /ssh_host_* _key
402+ chmod 644 " ${SSH_CONFIG_DIR} " /ssh_host_* _key.pub
403+ else
404+ # Generate new keys
405+ report_info " cuos:init:ssh_hostkeys" " Generating SSH host keys"
359406 ssh-keygen -A
407+
408+ # Persist the newly generated keys
409+ report_info " cuos:init:ssh_hostkeys" " Persisting SSH host keys to ${SSH_PERSIST_DIR} "
410+ cp " ${SSH_CONFIG_DIR} " /ssh_host_* _key " ${SSH_PERSIST_DIR} /" 2> /dev/null || true
411+ cp " ${SSH_CONFIG_DIR} " /ssh_host_* _key.pub " ${SSH_PERSIST_DIR} /" 2> /dev/null || true
412+ chmod 600 " ${SSH_PERSIST_DIR} " /ssh_host_* _key
413+ chmod 644 " ${SSH_PERSIST_DIR} " /ssh_host_* _key.pub
414+ fi
415+ }
416+
417+ configure_ssh_server () {
418+ local ssh_enabled
419+ ssh_enabled=" $( jq_config -r ' .os_ssh_server // false' ) "
420+
421+ if [[ " ${ssh_enabled} " == " true" ]]; then
422+ report_info " cuos:init:ssh_server" " Start SSH server"
423+ systemctl start ssh
360424 fi
361425}
362426
@@ -550,8 +614,12 @@ import_custom_ca_certs
550614
551615configure_keyboard
552616
617+ set_root_password
618+
553619create_ssh_hostkey
554620
621+ configure_ssh_server
622+
555623configure_docker
556624
557625create_swap_and_resize_fs
0 commit comments