OSDN Git Service

7c14fcbb432f33e1680cfad3799ed64e44afd8d1
[alterlinux/LUBS.git] / channels / share / airootfs / root / customize_airootfs.sh
1 #!/usr/bin/env bash
2 # SPDX-License-Identifier: GPL-3.0
3 #
4 # mk-linux419
5 # Twitter: @fascoder_4
6 # Email  : m.k419sabuaka@gmail.com
7 #
8 # Yamada Hayao
9 # Twitter: @Hayao0819
10 # Email  : hayao@fascode.net
11 #
12 # (c) 2019-2020 Fascode Network.
13 #
14 # customize_airootfs.sh
15 #
16
17 set -e
18
19 # Default value
20 # All values can be changed by arguments.
21
22 password="liveuser"
23 username="liveuser"
24 usershell="/bin/bash"
25 debug=true
26
27 # Parse arguments
28 while getopts 'p:u:s:dxa:' arg; do
29     case "${arg}" in
30         p) password="${OPTARG}" ;;
31         u) username="${OPTARG}" ;;
32         s) usershell="${OPTARG}" ;;
33         d) debug=true ;;
34         x) debug=true; set -xv ;;
35         a) arch="${OPTARG}"
36     esac
37 done
38
39
40 # Creating a root user.
41 # usermod -s /usr/bin/zsh root
42 function user_check () {
43 if [[ $(getent passwd $1 > /dev/null ; printf $?) = 0 ]]; then
44     if [[ -z $1 ]]; then
45         echo -n "false"
46     fi
47     echo -n "true"
48 else
49     echo -n "false"
50 fi
51 }
52
53 if [[ $(user_check root) = false ]]; then
54     usermod -s "${usershell}" root
55     cp -aT /etc/skel/ /root/
56     chmod 700 /root
57     LC_ALL=C LANG=C xdg-user-dirs-update
58 fi
59 echo -e "${password}\n${password}" | passwd root
60
61 # Allow sudo group to run sudo
62 sed -i 's/^#\s*\(%sudo\s\+ALL=(ALL)\s\+ALL\)/\1/' /etc/sudoers
63
64 # Create a user.
65 # create_user <username> <password>
66 function create_user () {
67     local _password
68     local _username
69
70     _username=${1}
71     _password=${2}
72
73     set +u
74     if [[ -z "${_username}" ]]; then
75         echo "User name is not specified." >&2
76         return 1
77     fi
78     if [[ -z "${_password}" ]]; then
79         echo "No password has been specified." >&2
80         return 1
81     fi
82     set -u
83
84     if [[ $(user_check ${_username}) = false ]]; then
85         useradd -m -s ${usershell} ${_username}
86         usermod -U -g ${_username} ${_username}
87         usermod -aG sudo ${_username}
88         cp -aT /etc/skel/ /home/${_username}/
89     fi
90     chmod 700 -R /home/${_username}
91     chown ${_username}:${_username} -R /home/${_username}
92     echo -e "${_password}\n${_password}" | passwd ${_username}
93     set -u
94 }
95
96 create_user "${username}" "${password}"
97
98
99 # Set up auto login
100 if [[ -f /etc/systemd/system/getty@tty1.service.d/autologin.conf ]]; then
101     sed -i s/%USERNAME%/"${username}"/g /etc/systemd/system/getty@tty1.service.d/autologin.conf
102 fi
103
104
105 # Set to execute calamares without password as alter user.
106 cat >> /etc/sudoers << "EOF"
107 Defaults pwfeedback
108 EOF
109 echo "${username} ALL=NOPASSWD: ALL" >> /etc/sudoers.d/alterlive
110
111
112 # Chnage sudoers permission
113 chmod 750 -R /etc/sudoers.d/
114 chown root:root -R /etc/sudoers.d/