-
Notifications
You must be signed in to change notification settings - Fork 0
/
portage-tmpfs
executable file
·149 lines (112 loc) · 3.55 KB
/
portage-tmpfs
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/sbin/openrc-run
# nic@boet.cc
# init script to populate tmpfs mounted portage repo
#
extra_started_commands="snapshot"
extra_stopped_commands="setup remove"
portage="/var/db/repos"
tar=/var/db/repos-snapshot.tar.gz
depend() {
before nfs
}
snapshot() {
if [ -f ${tar} ]; then
if test `find ${tar} -mmin -1440` ; then
einfo " skipping snapshot creation, recent archive exists"
return 0
fi
fi
if [ -f ${portage}/tmpfs-not-mounted ] ; then
eerror " refusing to create snapshot, ${portage}/tmpfs-not-mounted exists"
return 1
fi
einfo " creating portage snapshot"
tar -czp --absolute-names ${portage} -f ${tar} > /dev/null 2>&1 || return 1
return 0
}
restore(){
einfo " restoring ${tar} to ${portage}"
tar -xzf ${tar} ${portage} -C / > /dev/null 2>&1 || return 1
return 0
}
setup() {
ebegin "Deploying ${SVCNAME}"
if grep "${portage}" /etc/fstab > /dev/null; then
ewarn " already setup, ${portage} defined in /etc/fstab"
return 1
fi
if [ -f ${portage}/tmpfs-not-mounted ] ; then
ewarn " already setup, ${portage}/tmpfs-not-mounted exists"
return 1
fi
if mountpoint ${portage} > /dev/null; then
eerror " mountpoint already exists at ${portage}"
return 1
fi
if [ ! -f ${portage}/gentoo/metadata/timestamp ] ; then
eerror " existing gentoo repo not found under ${portage}"
return 1
fi
snapshot || return 1
einfo " purging ${portage}"
rm -r ${portage} || return 1
mkdir ${portage} || return 1
einfo " creating mountpoint"
touch $portage/tmpfs-not-mounted || return 1
echo "none /var/db/repos/ tmpfs defaults,size=2G 0 0 ## ${SVCNAME} ##" >> /etc/fstab || return 1
einfo "setup complete, please start service"
eend $?
}
remove() {
ebegin "Removing ${SVCNAME}"
if mountpoint ${portage} > /dev/null; then
umount -fl ${portage} > /dev/null || return 1
if mountpoint ${portage} > /dev/null ; then
eerror " env not stable, ${portage} still mounted"
return 1
fi
fi
if [ -f ${portage}/tmpfs-not-mounted ] ; then
rm ${portage}/tmpfs-not-mounted || return 1
fi
if grep "${portage}" /etc/fstab > /dev/null; then
sed -i "/## ${SVCNAME} ##/d" /etc/fstab || return 1
fi
einfo " purging ${portage}"
rm -r ${portage} || return 1
mkdir ${portage} || return 1
restore || return 1
if [ -f ${tar} ] ; then
rm $tar || return 1
fi
einfo "complete"
eend $?
}
start() {
ebegin "Starting ${SVCNAME}"
if mountpoint ${portage} > /dev/null ; then
ewarn " unmounting ${portage}"
umount -fl ${portage} > /dev/null || eerror " failed"
fi
if [ ! -f ${portage}/tmpfs-not-mounted ] ; then
eerror " please run setup first"
return 1
fi
mount ${portage} > /dev/null || eerror " failed to mount ${portage}"
if [ ! -f ${portage}/gentoo/metadata/timestamp ] ; then
restore || eerror " failed to restore ${tar}"
fi
eend $?
}
stop() {
ebegin "Stopping ${SVCNAME}"
if mountpoint ${portage} > /dev/null ; then
if [ -f ${portage}/gentoo/metadata/timestamp ] ; then
snapshot || eerror " failed to archive current tmpfs contents"
fi
umount -fl ${portage} > /dev/null || ewarn " failed to umount tmpfs"
else
ewarn " openrc-run started state, ${portage} not mounted"
fi
eend $?
}