This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
initCentOS.sh
executable file
·76 lines (60 loc) · 2 KB
/
initCentOS.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash
SCRIPTSPATH=`dirname ${BASH_SOURCE[0]}`
source $SCRIPTSPATH/lib.sh
distro="centos"
release="9-Stream"
if [ -z $2 ]
then
echo "please call $0 <name of new container> <cid> <release, default is $release> <arch, default is amd64> <autostart, default is 1>"
echo " eg. $0 mymachine.example.org 50"
exit 1
fi
name=$1
cid=$2
if [ ! -z $3 ]
then
release=$3
fi
arch="amd64"
if [ ! -z $4 ]
then
arch=$4
fi
autostart=1
if [ ! -z $5 ]
then
autostart=$5
fi
origname=$name
name=$(createContainerName $name $cid)
hostname=$(createHostName $origname $cid)
rootfs_path=$container_path/$name/rootfs
bridgeInterface=$(getBridgeInterface) || die "cannot find the bridge interface"
bridgeAddress=$(getIPOfInterface $bridgeInterface) || die "cannot find the address for the bridge $bridgeInterface"
networkAddress=$(echo $bridgeAddress | cut -f1,2,3 -d".")
IPv4=$networkAddress.$cid
lxc init images:$distro/$release/$arch $name
lxc network attach lxdbr0 $name eth0 eth0
lxc config device set $name eth0 ipv4.address $IPv4
ssh-keygen -f "/root/.ssh/known_hosts" -R $IPv4
# mount yum cache repo, to avoid redownloading stuff when reinstalling the machine
#hostpath="/var/lib/repocache/$cid/$distro/$release/$arch/var/cache/yum"
#$SCRIPTSPATH/initMount.sh $hostpath $name "/var/cache/yum"
# configure timezone
cd $rootfs_path/etc && rm -f localtime && ln -s ../usr/share/zoneinfo/Europe/Berlin localtime && cd -
# yum: keep the cache
sed -i 's/^keepcache=0/keepcache=1/g' $rootfs_path/etc/yum.conf
# install openssh-server
lxc start $name
sleep 5
lxc exec $name -- dhclient
lxc exec $name -- /bin/bash -c "yum -y install openssh-server && systemctl enable sshd && systemctl start sshd"
lxc exec $name -- /bin/bash -c "hostnamectl set-hostname $hostname"
# drop root password completely
lxc exec $name -- passwd -d root
# disallow auth with null password
lxc exec $name -- sed -i 's/nullok//g' /etc/pam.d/system-auth
install_public_keys $rootfs_path $name
configure_autostart $autostart $name
info $cid $name $IPv4
lxc stop $name