Skip to content

Commit ee05ba4

Browse files
committed
add config reading
1 parent 73000f4 commit ee05ba4

13 files changed

+89
-82
lines changed

conf/pulxc-lxc.conf

Whitespace-only changes.

conf/pulxc.conf

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
BASE_PATH=/var/lib/pulxc
2+
BASE_SUBUID=100000
3+
BASE_SUBGID=100000
4+
CGROUP_CPUSET_CPUS=0
5+
CGROUP_CPUSET_MEMS=0
6+
BRIDGE_INTERFACE=pulxcbr0
7+
IP_PREFIX=192.168.100
8+
GATEWAY=192.168.100.254

script/pulxc-create

+29-31
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
#!/bin/bash
2-
BASEUID=100000
3-
BASEGID=100000
4-
LXC_PATH="/net/lxc/user"
5-
LXC_BASE="/net/lxc"
6-
LXC_USER="lxc"
7-
LXC_TEMPLATE="/net/lxc/lxc-archlinux-userns"
2+
source /etc/pulxc/pulxc.conf
3+
PULXC_USER="pulxc"
4+
PULXC_TEMPLATE="/usr/share/lxc/templates/lxc-archlinux"
5+
CONFIG_PATH="$BASE_PATH/lxc/$1/config"
86
if [[ $1 == "" ]]; then
97
echo "No Username"
10-
exit
8+
exit 1
119
fi
1210
id $1 > /dev/null 2>&1
1311
if [[ $? -ne 0 ]]; then
1412
echo "User didn't exist!"
1513
exit 1
1614
fi
17-
lxc-create -P $LXC_PATH -t $LXC_TEMPLATE -n "$1"
15+
lxc-create -P $BASE_PATH/lxc -t $PULXC_TEMPLATE -n "$1"
1816
if [[ $? -ne 0 ]]; then
1917
exit 1
2018
fi
2119
echo "Finish Creating lxc"
22-
notuid0=$(find $LXC_PATH/"$1"/rootfs -not -uid 0)
23-
notgid0=$(find $LXC_PATH/"$1"/rootfs -not -gid 0)
24-
facl=$(getfacl -R -s -p $LXC_PATH/"$1"/rootfs | grep "# file:" | awk '{print$3}')
20+
notuid0=$(find $BASE_PATH/lxc/"$1"/rootfs -not -uid 0)
21+
notgid0=$(find $BASE_PATH/lxc/"$1"/rootfs -not -gid 0)
22+
facl=$(getfacl -R -s -p $BASE_PATH/lxc/"$1"/rootfs | grep "# file:" | awk '{print$3}')
2523
if [[ "$notuid0" != "" ]]; then
2624
echo "Files not own by root"
2725
notuid0=$(echo "$notuid0" | awk '{printf "\"%s\" ",$1; system("stat -c \"%u\" "$1);}')
@@ -32,12 +30,12 @@ if [[ "$notgid0" != "" ]]; then
3230
notgid0=$(echo "$notgid0" | awk '{printf "\"%s\" ",$1; system("stat -c \"%g\" "$1);}')
3331
echo "$notgid0"
3432
fi
35-
chown -R $BASEUID:$BASEGID $LXC_PATH/"$1"/rootfs
33+
chown -R $BASE_SUBUID:$BASE_SUBGID $BASE_PATH/lxc/"$1"/rootfs
3634
if [[ "$notuid0" != "" ]]; then
37-
echo "$notuid0" | awk -v BASEUID=$BASEUID '{system("chown "$2+BASEUID" "$1);}'
35+
echo "$notuid0" | awk -v BASEUID=$BASE_SUBUID '{system("chown "$2+BASEUID" "$1);}'
3836
fi
3937
if [[ "$notgid0" != "" ]]; then
40-
echo "$notgid0" | awk -v BASEGID=$BASEGID '{system("chgrp "$2+BASEGID" "$1);}'
38+
echo "$notgid0" | awk -v BASEGID=$BASE_SUBGID '{system("chgrp "$2+BASEGID" "$1);}'
4139
fi
4240
if [[ "$facl" != "" ]]; then
4341
echo "Files using facl"
@@ -48,7 +46,7 @@ if [[ "$facl" != "" ]]; then
4846
getfacl -n $filename
4947
tmp=$(getfacl -p -n $filename)
5048
tmp=$(echo "$tmp" | grep '^[^#]' |
51-
awk -F: -vBASEUID=$BASEUID -vBASEGID=$BASEGID '{
49+
awk -F: -vBASEUID=$BASE_SUBUID -vBASEGID=$BASE_SUBGID '{
5250
OFS=":";
5351
if($1!="default"&&$1=="user"&&$2!="")
5452
{$2=$2+BASEUID}
@@ -67,20 +65,20 @@ if [[ "$facl" != "" ]]; then
6765
done
6866
fi
6967

70-
chown $BASEUID:$LXC_USER $LXC_PATH/$1
71-
chown $LCX_USER:$LXC_USER $LXC_PATH/$1/config
72-
sed -i 's/lxc.net.0.type = empty//g' $LXC_PATH/$1/config
73-
echo "lxc.include = /usr/share/lxc/config/userns.conf" >> $LXC_PATH/$1/config
74-
echo "lxc.include = $LXC_BASE/pulxc.conf" >> $LXC_PATH/$1/config
75-
echo "lxc.idmap = u 0 $BASEUID 65536" >> $LXC_PATH/$1/config
76-
echo "lxc.idmap = g 0 $BASEGID 65536" >> $LXC_PATH/$1/config
77-
nextip=$(cat $LXC_BASE/NEXTIP)
78-
echo "lxc.net.0.type = veth" >> $LXC_PATH/$1/config
79-
echo "lxc.net.0.link = lxcbr0" >> $LXC_PATH/$1/config
80-
echo "lxc.net.0.ipv4.address = 192.168.100.$nextip/24" >> $LXC_PATH/$1/config
81-
echo "lxc.net.0.ipv4.gateway = 192.168.100.1" >> $LXC_PATH/$1/config
82-
echo "lxc.net.0.name = eth0" >> $LXC_PATH/$1/config
83-
echo "lxc.net.0.flags = up" >> $LXC_PATH/$1/config
84-
echo "Using ip 192.168.100.$nextip"
68+
chown $BASE_SUBUID:$PULXC_USER $BASE_PATH/lxc/$1
69+
chown $PULXC_USER:$PULXC_USER $CONFIG_PATH
70+
sed -i 's/lxc.net.0.type = empty//g' $CONFIG_PATH
71+
echo "lxc.include = /usr/share/lxc/config/userns.conf" >> $CONFIG_PATH
72+
echo "lxc.include = /etc/pulxc/pulxc-lxc.conf" >> $CONFIG_PATH
73+
echo "lxc.idmap = u 0 $BASE_SUBUID 65536" >> $CONFIG_PATH
74+
echo "lxc.idmap = g 0 $BASE_SUBGID 65536" >> $CONFIG_PATH
75+
nextip=$(cat $BASE_PATH/nextip)
76+
echo "lxc.net.0.type = veth" >> $CONFIG_PATH
77+
echo "lxc.net.0.link = $BRIDGE_INTERFACE" >> $CONFIG_PATH
78+
echo "lxc.net.0.ipv4.address = $IP_PREFIX.$nextip/24" >> $CONFIG_PATH
79+
echo "lxc.net.0.ipv4.gateway = $GATEWAY" >> $CONFIG_PATH
80+
echo "lxc.net.0.name = eth0" >> $CONFIG_PATH
81+
echo "lxc.net.0.flags = up" >> $CONFIG_PATH
82+
echo "Using ip $IP_PREFIX.$nextip/24"
8583
nextip=$(($nextip+1))
86-
echo $nextip > $LXC_BASE/NEXTIP
84+
echo $nextip > $BASE_PATH/nextip

script/pulxc-destroy

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
source /etc/pulxc/pulxc.conf
3+
lxc-destroy -P $BASE_PATH/lxc -n $1

script/setup-cgroup.sh

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
#!/bin/bash
2+
source /etc/pulxc/pulxc.conf
23
for cgroup in /sys/fs/cgroup/*
34
do
45
if [[ $cgroup == "/sys/fs/cgroup/systemd" ]] || [[ $cgroup == "/sys/fs/cgroup/unified" ]];
56
then
67
continue
78
fi
89
echo "adding subcontroller in $cgroup"
9-
rmdir $cgroup/lxc
10-
mkdir -p $cgroup/lxc
11-
chown -R lxc:lxc $cgroup/lxc
10+
rmdir $cgroup/pulxc
11+
mkdir -p $cgroup/pulxc
12+
chown -R pulxc:pulxc $cgroup/pulxc
1213
done
1314

1415
#Change these as your system hardware!
15-
echo "0" > /sys/fs/cgroup/cpuset/lxc/cpuset.cpus
16-
echo "0" > /sys/fs/cgroup/cpuset/lxc/cpuset.mems
17-
echo "0" > /sys/fs/cgroup/cpuset/lxc/lxc/cpuset.cpus
18-
echo "0" > /sys/fs/cgroup/cpuset/lxc/lxc/cpuset.mems
16+
echo "$CGROUP_CPUSET_CPUS" > /sys/fs/cgroup/cpuset/pulxc/cpuset.cpus
17+
echo "$CGROUP_CPUSET_MEMS" > /sys/fs/cgroup/cpuset/pulxc/cpuset.mems
1918
exit 0

script/startone-dblg.sh

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#!/bin/sh
2-
#cgm create all lxc
3-
cat /proc/self/cgroup
2+
source /etc/pulxc/pulxc.conf
43
for cgroup in /sys/fs/cgroup/*
54
do
65
echo $cgroup
7-
if [[ $cgroup == "/sys/fs/cgroup/systemd" ]];
6+
if [[ $cgroup == "/sys/fs/cgroup/systemd" ]] || [[ $cgroup == "/sys/fs/cgroup/unified" ]];
87
then
98
continue
109
fi
11-
echo $$ > $cgroup/lxc/tasks
10+
echo $$ > $cgroup/pulxc/tasks
1211
done
12+
echo "cgroup info:"
1313
cat /proc/self/cgroup
14-
/usr/bin/lxc-start -p /net/lxc/userpid/$1.pid -P /net/lxc/user -n $1 --logfile=/tmp/lxc.log -l WARN
14+
/usr/bin/lxc-start -P $BASE_PATH/lxc -n $1 --logfile=$BASE_PATH/log/$1.log -l DEBUG

script/startone.sh

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
#!/bin/sh
2-
#cgm create all lxc
3-
cat /proc/self/cgroup
2+
source /etc/pulxc/pulxc.conf
43
for cgroup in /sys/fs/cgroup/*
54
do
65
echo $cgroup
7-
if [[ $cgroup == "/sys/fs/cgroup/systemd" ]];
6+
if [[ $cgroup == "/sys/fs/cgroup/systemd" ]] || [[ $cgroup == "/sys/fs/cgroup/unified" ]];
87
then
98
continue
109
fi
11-
echo $$ > $cgroup/lxc/tasks
10+
echo $$ > $cgroup/pulxc/tasks
1211
done
1312
cat /proc/self/cgroup
14-
/usr/bin/lxc-start -p /net/lxc/userpid/$1.pid -P /net/lxc/user -n $1
13+
/usr/bin/lxc-start -P $BASE_PATH/lxc -n $1

script/stopone.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
#!/bin/sh
2-
/usr/bin/lxc-stop -P /net/lxc/user -n $1
2+
source /etc/pulxc/pulxc.conf
3+
/usr/bin/lxc-stop -P $BASE_PATH/lxc -n $1

systemd/[email protected]

-16
This file was deleted.

systemd/[email protected]

-15
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
[Unit]
2-
Description=%i LXC
2+
Description=PULXC cgroup setup
33
After=local-fs.target
44

55
[Service]
66
User=root
77
Group=root
88
Type=oneshot
9-
ExecStart=/net/lxc/setup-cgroup.sh
9+
ExecStart=/usr/lib/pulxc/setup-cgroup.sh
1010

1111
[Install]
1212
WantedBy=multi-user.target

systemd/[email protected]

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[Unit]
2+
Description=%i PULXC with debug log
3+
After=network.target pulxc-cgroup.service
4+
Requires=pulxc-cgroup.service
5+
6+
[Service]
7+
User=pulxc
8+
Group=pulxc
9+
Type=forking
10+
Delegate=yes
11+
ExecStart=/usr/lib/pulxc/startone-dblg.sh %i
12+
ExecStop=/usr/lib/pulxc/stopone.sh %i
13+
14+
[Install]
15+
WantedBy=multi-user.target

systemd/[email protected]

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[Unit]
2+
Description=%i PULXC
3+
After=network.target pulxc-cgroup.service
4+
Requires=pulxc-cgroup.service
5+
6+
[Service]
7+
User=pulxc
8+
Group=pulxc
9+
Type=forking
10+
Delegate=yes
11+
ExecStart=/usr/lib/pulxc/startone.sh %i
12+
ExecStop=/usr/lib/pulxc/stopone.sh %i
13+
14+
[Install]
15+
WantedBy=multi-user.target

0 commit comments

Comments
 (0)