Skip to content

Commit e05d895

Browse files
authored
Update .config symlink logic and add .copy-files logic (#231)
Signed-off-by: David Kwon <dakwon@redhat.com>
1 parent 5f15a0a commit e05d895

File tree

3 files changed

+83
-23
lines changed

3 files changed

+83
-23
lines changed

base/ubi9/.stow-local-ignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@
1313

1414
# Ignore files under .config directory
1515
\.config
16+
17+
\.copy-files

base/ubi9/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ RUN mv /usr/bin/podman "${ORIGINAL_PODMAN_PATH}"
152152

153153
COPY --chown=0:0 entrypoint.sh /
154154
COPY --chown=0:0 .stow-local-ignore /home/tooling/
155+
COPY --chown=0:0 .copy-files /home/tooling/
155156
RUN \
156157
# add user and configure it
157158
useradd -u 10001 -G wheel,root -d /home/user --shell /bin/bash -m user && \
@@ -162,7 +163,7 @@ RUN \
162163
# Copy the global git configuration to user config as global /etc/gitconfig
163164
# file may be overwritten by a mounted file at runtime
164165
cp /etc/gitconfig ${HOME}/.gitconfig && \
165-
chown 10001 ${HOME}/ ${HOME}/.viminfo ${HOME}/.gitconfig ${HOME}/.stow-local-ignore && \
166+
chown 10001 ${HOME}/ ${HOME}/.viminfo ${HOME}/.gitconfig ${HOME}/.stow-local-ignore ${HOME}/.copy-files && \
166167
# Set permissions on /etc/passwd and /home to allow arbitrary users to write
167168
chgrp -R 0 /home && \
168169
chmod -R g=u /etc/passwd /etc/group /home && \

base/ubi9/entrypoint.sh

Lines changed: 79 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,6 @@ if [ ! -d "${HOME}/.config/containers" ]; then
2020
fi
2121
fi
2222

23-
# Find files under /home/tooling/.config and create symlinks. The /home/tooling/.config folder
24-
# is ignored by stow with the .stow-local-ignore file
25-
if [ -d /home/tooling/.config ]; then
26-
for file in $(find /home/tooling/.config -type f); do
27-
tooling_dir=$(dirname "$file")
28-
29-
# Create dir in /home/user if it does not exist already
30-
mkdir -p $(replace_user_home "$tooling_dir")
31-
32-
# Create symbolic link if it does not exist already
33-
if [ ! -f $(replace_user_home $file) ]; then
34-
ln -s $file $(replace_user_home $file)
35-
fi
36-
done
37-
fi
38-
3923
# Setup $PS1 for a consistent and reasonable prompt
4024
if [ -w "${HOME}" ] && [ ! -f "${HOME}"/.bashrc ]; then
4125
echo "PS1='[\u@\h \W]\$ '" > "${HOME}"/.bashrc
@@ -90,13 +74,86 @@ if [ $HOME_USER_MOUNTED -eq 0 ] && [ ! -f $STOW_COMPLETE ]; then
9074
#
9175
# Create symbolic links from /home/tooling/ -> /home/user/
9276
stow . -t /home/user/ -d /home/tooling/ --no-folding -v 2 > /tmp/stow.log 2>&1
93-
# Vim does not permit .viminfo to be a symbolic link for security reasons, so manually copy it
94-
cp /home/tooling/.viminfo /home/user/.viminfo
95-
# We have to restore bash-related files back onto /home/user/ (since they will have been overwritten by the PVC)
96-
# but we don't want them to be symbolic links (so that they persist on the PVC)
97-
cp /home/tooling/.bashrc /home/user/.bashrc
98-
cp /home/tooling/.bash_profile /home/user/.bash_profile
9977
touch $STOW_COMPLETE
10078
fi
10179

80+
# Read .copy-files and copy files from /home/tooling to /home/user
81+
if [ -f "/home/tooling/.copy-files" ]; then
82+
echo "Processing .copy-files..."
83+
while IFS= read -r line || [[ -n "$line" ]]; do
84+
# Skip empty and commented lines
85+
[[ -z "$line" || "$line" == \#* ]] && continue
86+
87+
if [ -e "/home/tooling/$line" ]; then
88+
tooling_path=$(realpath "/home/tooling/$line")
89+
90+
# Determine target path based on whether source is a directory
91+
if [ -d "$tooling_path" ]; then
92+
# For directories: copy to parent directory (e.g., dir1/dir2 -> /home/user/dir1/)
93+
target_parent=$(dirname "${HOME}/${line}")
94+
target_full="$target_parent/$(basename "$tooling_path")"
95+
96+
# Skip if target directory already exists
97+
if [ -d "$target_full" ]; then
98+
echo "Directory $target_full already exists, skipping..."
99+
continue
100+
fi
101+
102+
echo "Copying directory $tooling_path to $target_parent/"
103+
mkdir -p "$target_parent"
104+
cp --no-clobber -r "$tooling_path" "$target_parent/"
105+
else
106+
# For files: copy to exact target path
107+
target_full="${HOME}/${line}"
108+
target_parent=$(dirname "$target_full")
109+
110+
# Skip if target file already exists
111+
if [ -e "$target_full" ]; then
112+
echo "File $target_full already exists, skipping..."
113+
continue
114+
fi
115+
116+
echo "Copying file $tooling_path to $target_full"
117+
mkdir -p "$target_parent"
118+
cp --no-clobber -r "$tooling_path" "$target_full"
119+
fi
120+
else
121+
echo "Warning: /home/tooling/$line does not exist, skipping..."
122+
fi
123+
done < /home/tooling/.copy-files
124+
echo "Finished processing .copy-files."
125+
else
126+
echo "No .copy-files found, skipping copy operation."
127+
fi
128+
129+
# Create symlinks from /home/tooling/.config to /home/user/.config
130+
# Only create symlinks for files that don't already exist in destination.
131+
# This is done because stow ignores the .config directory.
132+
if [ -d /home/tooling/.config ]; then
133+
echo "Creating .config symlinks for files that don't already exist..."
134+
135+
# Find all files recursively in /home/tooling/.config
136+
find /home/tooling/.config -type f | while read -r file; do
137+
# Get the relative path from /home/tooling/.config
138+
relative_path="${file#/home/tooling/.config/}"
139+
140+
# Determine target path in /home/user/.config
141+
target_file="${HOME}/.config/${relative_path}"
142+
target_dir=$(dirname "$target_file")
143+
144+
# Only create symlink if target file doesn't exist
145+
if [ ! -e "$target_file" ]; then
146+
# Create target directory if it doesn't exist
147+
mkdir -p "$target_dir"
148+
# Create symbolic link
149+
ln -s "$file" "$target_file"
150+
echo "Created symlink: $target_file -> $file"
151+
else
152+
echo "File $target_file already exists, skipping..."
153+
fi
154+
done
155+
156+
echo "Finished creating .config symlinks."
157+
fi
158+
102159
exec "$@"

0 commit comments

Comments
 (0)