-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvm-command-setup.in
executable file
·224 lines (183 loc) · 5.03 KB
/
vm-command-setup.in
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/bin/sh -efu
[ -z "@MKLOCAL@" ] ||
export PATH="@PREFIX@/libshell:${PATH#@PREFIX@/libshell:}"
case "${VM_HELP-}" in
USAGE)
printf 'setup [options] <profile> [<profile> ...]\n'
;;
SUMMARY)
printf 'Setup a new sandbox\n'
;;
DESCRIPTION)
cat <<-EOF
Setup a new virtual machine. This creates a new rootfs in the .vm folder
of your home directory.
EOF
;;
OPTIONS)
cat <<-EOF
-d, --delete removes profile;
-g, --global=DIR creates global rootfs;
-r, --kconfig-requires shows kernel options required for the specified profile;
-R, --kconfig-missing-requires
shows missing kernel options;
-m, --kconfig-merge merges required kernel config fragments into .config.
EOF
;;
esac
[ -z "${VM_HELP-}" ] ||
exit 0
. shell-error
. shell-git-config
get_kernel_dir()
{
if [ -z "$kernel_dir" ]; then
n="${qemu_kernel-}"
while [ -e "$n" ]; do
if [ -s "${n%/*}/.config" ]; then
kernel_dir="${n%/*}"
break
fi
[ -z "${n##*/*}" ] ||
break
n="${n%/*}"
done
fi
[ -d "$kernel_dir" ] ||
fatal "unable to find kernel top directory."
}
GETOPT_ALLOW_UNKNOWN=
TEMP=`getopt -n $PROG -o 'g:,d,m,r,R' -l 'global:,kconfig-merge,kconfig-requires,kconfig-missing-requires,delete' -- "$@"` ||
show_usage
eval set -- "$TEMP"
global=
show_kconfig_requires=
merge_kconfig_requires=
delete_profile=
while :; do
case "$1" in
-g|--global) shift
global="$1"
;;
-d|--delete)
delete_profile=1
;;
-m|--kconfig-merge)
merge_kconfig_requires=1
;;
-r|--kconfig-requires)
show_kconfig_requires='req'
;;
-R|--kconfig-missing-requires)
show_kconfig_requires='missing'
;;
--no-check-kernel-config)
no_check_kernel_config=1
;;
--) shift; break
;;
esac
shift
done
if [ -n "$delete_profile" ]; then
vm_profiles=
git_config_get vm_profiles "$vm_config_file" "vm.profiles"
for profile; do
[ ! -d "$vm_profiles/$profile" ] ||
$vm_dryrun rm -rf -- "$vm_profiles/$profile"
done
git_config_list "$vm_config_file" |
sort |
while read -r s; do
n="${s%%=*}"
n="${n#vm.}"
n="${n%.*}"
found=
for profile; do
[ "$n" != "$profile" ] ||
found=1
done
[ -n "$found" ] ||
continue
$vm_dryrun git_config_unset "$vm_config_file" "${s%%=*}"
done
for profile; do
$vm_dryrun git_config_unset "$vm_config_file" "vm.$profile"
done
exit
fi
[ "$#" -gt 0 ] ||
show_usage "Profile name required"
vm_profile="$1"
shift
export vm_profile
if [ -z "$global" ]; then
. "@PREFIX@/vm-sh-config"
vm_read_common_config
vm_profiles=
git_config_get vm_profiles "$vm_config_file" "vm.profiles"
dest_dir="$vm_profiles/$vm_profile/rootfs"
config_dest_dir="$vm_profiles/@vm.name@/rootfs"
else
verbose "Setup global rootfs."
dest_dir="$global"
config_dest_dir="$dest_dir"
fi
if [ -n "$show_kconfig_requires" ]; then
[ "$show_kconfig_requires" = 'req' ] ||
get_kernel_dir
cd '@LIBEXECDIR@'
grep -h -v -e '^[[:space:]]*\(#.*\)\?$' $kernel_config_expect |
sort -u |
while read -r param; do
[ "$show_kconfig_requires" = 'req' ] ||
! grep -qs -xFe "$param" "$kernel_dir/.config" ||
continue
printf '%s\n' "$param"
done
exit
fi
if [ -n "$merge_kconfig_requires" ]; then
get_kernel_dir
[ -s "$kernel_dir/.config" ] ||
fatal "$kernel_dir/.config: kernel config not found."
merge_config="$kernel_dir/scripts/kconfig/merge_config.sh"
[ -x "$merge_config" ] ||
fatal "$merge_config: not found or not executable. Can't merge."
have_builtin=
! "$merge_config" -h | grep -qs -e '^[[:space:]]\+-y[[:space:]]' ||
have_builtin=1
cd '@LIBEXECDIR@'
"$merge_config" -m ${have_builtin:+-y} -O "$kernel_dir" "$kernel_dir/.config" $kernel_config_expect
exit
fi
vm_check_kernel_config
git_config_location_exists "$vm_config_file" "vm.$vm_profile" ||
verbose "Profile does not exist. Creating ..."
git_config_set "$vm_config_file" "vm.$vm_profile.rootfs" "$config_dest_dir"
[ -z "${qemu_kernel-}" ] ||
git_config_set "$vm_config_file" "vm.$vm_profile.kernel" "$qemu_kernel"
git_config_set "$vm_config_file" "vm.$vm_profile.append" "init=virt/init rootflags=trans=virtio,version=9p2000.L rootfstype=9p rw"
git_config_unset "$vm_config_file" "vm.$vm_profile.virtfs"
git_config_append "$vm_config_file" "vm.$vm_profile.virtfs" "@vm.rootfs@:/dev/root"
git_config_append "$vm_config_file" "vm.$vm_profile.virtfs" "/:hostfs"
[ -d "$dest_dir" ] &&
message "Updating rootfs: $dest_dir" ||
message "Creating rootfs: $dest_dir"
mkdir ${verbose-} -p -- "$dest_dir"
cd "$dest_dir"
for n in dev etc home host proc root sys tmp var var/lib virt virt/home virt/etc; do
mkdir ${verbose-} -p -- "./$n"
done
for n in bin lib lib64 sbin usr etc/alternatives etc/ld.so.conf; do
[ -e "./$n" ] || [ -L "./$n" ] ||
ln ${verbose-} -s -- "/host/$n" "./$n"
done
[ -f ./etc/passwd ] ||
printf 'root:x:0:0:root:/root:/bin/sh\n' > ./etc/passwd
[ -f ./etc/group ] ||
printf 'root:x:0:\n' > ./etc/group
cp_args=
[ "$(stat -c '%D' "@LIBEXECDIR@" ./virt |sort -u |wc -l)" != 1 ] ||
cp_args="-l"
cp -f ${verbose-} $cp_args -- "@LIBEXECDIR@/init" ./virt/init