Скрипт по переустановке операционной системы в контейнере
#!/bin/bash
if [ `whoami` != 'root' ];then
echo "Must be a root"
Else
VZ_DIR='/vz'
if [ -z $1 ];then
echo "Enter arguments 'new' or 'reinstall'"
elif [ $1 == 'new' ];then
echo newVM
echo "Select OS template"
list_template=`ls $VZ_DIR/template/cache/|sed 's/.tar.gz//'`
select select_template in ${list_template}
do
if [ "_$select_template" = "_" ];
then
echo "You choosed wrong!"
else
break
fi
done
while [ -z $vz_hostname ];do
printf "Enter hostname: "
read vz_hostname
done
while [ -z $vz_ip ];do
printf "Enter IP address: "
read vz_ip
done
while [[ -z ${DNS_CHOICE} ]]; do
printf "1) Lucky.Net DNS\n2) Google DNS\n3) Another DNS\nYour choice: "
read DNS_CHOICE
done
case ${DNS_CHOICE} in
1 ) DNS1='193.193.193.126'
DNS2='62.244.62.244'
;;
2 ) DNS1='8.8.8.8'
DNS2='8.8.4.4'
;;
3 ) while [[ -z DNS1 ]]; do
printf "Primary DNS: "
read DNS1
done
printf "Secondary DNS: "
read DNS2
;;
esac
while [ -z $vz_cpu ];do
printf "CPUs(1...64): "
read vz_cpu
done
while [ -z $vz_ram ];do
printf "RAM: "
read vz_ram
done
printf "SWAP: "
read vz_swap
while [ -z $vz_hdd ];do
printf 'HDD: '
read vz_hdd
done
echo "Build setings:"
echo -e "\tOS: $select_template"
echo -e "\tHostname: $vz_hostname"
echo -e "\tIP: $vz_ip"
echo -e "\tCPUs: $vz_cpu"
echo -e "\tRAM: $vz_ram"G
echo -e "\tSWAP: $vz_swap"G
echo -e "\tHDD: $vz_hdd"G
Echo
printf "Confirm(y/n): "
read vz_build_yn
if [ $vz_build_yn == 'y' ];then
vznum=`vzlist -a| tail -n 1|awk '{print $1}'`
let vznum=$vznum+1
printf "Creating VZ container [ id: $vznum ]...\n"
vzctl create $vznum --ostemplate $select_template --layout simfs > /tmp/$vznum.log
printf "Setting automatic boot on...\n"
vzctl set $vznum --save --onboot yes >> /tmp/$vznum.log
sleep 1
printf "Setting hostname [ $vz_hostname ]...\n"
vzctl set $vznum --save --name $vz_hostname >> /tmp/$vznum.log
vzctl set $vznum --save --hostname $vz_hostname >> /tmp/$vznum.log
sleep 1
printf "Setting IP address [ $vz_ip ]...\n"
vzctl set $vznum --save --ipadd $vz_ip >> /tmp/$vznum.log
sleep 1
if [ -z $DNS2 ];then
printf "Setting DNS [ $DNS1 ]...\n"
vzctl set $vznum --save --nameserver $DNS1 >> /tmp/$vznum.log
else
printf "Setting DNS [ $DNS1 and $DNS2]...\n"
vzctl set $vznum --save --nameserver $DNS1 --nameserver $DNS2 >> /tmp/$vznum.log
fi
sleep 1
printf "Setting CPU's [ $vz_cpu ]...\n"
vzctl set $vznum --save --cpus $vz_cpu >> /tmp/$vznum.log
sleep 1
printf "Setting RAM [ $vz_ram ]...\n"
vzctl set $vznum --save --ram "$vz_ram"G >> /tmp/$vznum.log
if [ -n $vz_swap ];then
vzctl set $vznum --save --swap "$vz_swap"G >> /tmp/$vznum.log
fi
sleep 1
printf "Setting HDD quota [ $vz_hdd ]...\n"
vzctl set $vznum --save --diskspace "$vz_hdd"G >> /tmp/$vznum.log
sleep 1
printf "Starting VZ container [ $vznum :: $select_template]...\n"
vzctl start $vznum >> /tmp/$vznum.log
sleep 1
printf "For set root password run 'vzctl exec $vznum passwd'\n"
Else
echo 'exit'
fi
elif [ $1 == 'reinstall' ];then
VZCONF_DIR='/etc/vz/conf'
RANDOM_CONF=`date|md5sum|cut -c-10`
if [ -z $2 ];then
printf "Enter <CTID> "
else
printf "Are you sure wont reinstall [ id:`vzlist |grep "$2"|awk '{print $1,$5}'|sed 's/ / name:/g'` ]?(y/n) "
read vz_reinstall_yesno
if [ $vz_reinstall_yesno == 'y' ];then
list_template=`ls /vz/template/cache/|sed 's/.tar.gz//'`
select select_template in $list_template
do
if [ "_${select_template}" = "_" ];then
printf "You choosed wrong!\n"
else
break
fi
done
OSTEMPLATE=`grep 'OSTEMPLATE=' "${VZCONF_DIR}"/"$2".conf|awk -F'"' '{print $2}'`
if [ ${OSTEMPLATE} == ${select_template} ];then
cp "${VZCONF_DIR}"/"${2}".conf "${VZCONF_DIR}"/"${RANDOM_CONF}"
printf "${OSTEMPLATE} == ${select_template}\n"
printf "Stoping container...\n"
vzctl stop ${2}
printf "Reinstalling container...\n"
vzctl destroy ${2}
vzctl create ${2} --ostemplate ${select_template} --layout simfs
mv "${VZCONF_DIR}"/"${RANDOM_CONF}" "${VZCONF_DIR}"/"${2}".conf
printf "Starting container...\n"
vzctl start ${2}
else
printf "Creating backup file ${VZCONF_DIR}"/"${RANDOM_CONF}\n"
sed -r "s/OSTEMPLATE=\"${OSTEMPLATE}\"/OSTEMPLATE=\"${select_template}\"/g" "${VZCONF_DIR}"/"$2".conf > "${VZCONF_DIR}"/"${RANDOM_CONF}"
printf "${OSTEMPLATE} != ${select_template}\n"
printf "Stoping container...\n"
vzctl stop ${2}
printf "Reinstalling container...\n"
vzctl destroy ${2}
vzctl create ${2} --ostemplate ${select_template} --layout simfs
printf "Restore file ${VZCONF_DIR}"/"${RANDOM_CONF}\n"
mv "${VZCONF_DIR}"/"${RANDOM_CONF}" "${VZCONF_DIR}"/"${2}".conf
printf "Starting container...\n"
vzctl start ${2}
fi
elif [ $vz_reinstall_yesno == 'n' ];then
printf "Bye!"
exit
fi
fi
elif [ $1 == 'delete' ];then
echo DeleteVM
fi
fi
Литература
1. Windows Vista и не только. Актуальное руководство: К. К. Ковалев — Москва, НТ Пресс, 2011 г.- 352 с.
2. Windows Vista. Просто и быстро: К. К. Ковалев — Санкт-Петербург, НТ Пресс, 2012 г.- 352 с.
3. Операционная система Windows XP: А. А. Гвоздев, Н. А. Дьяков — Санкт-Петербург, АСТ, Астрель-СПб, 2011 г.- 224 с.
4. Первые шаги с Windows Vista. Руководство для начинающих (+ CD-ROM): А. В. Поляк-Брагинский — Санкт-Петербург, БХВ-Петербург, 2007 г.- 288 с.
5. Современный самоучитель работы на компьютере: Сергей Топорков — Санкт-Петербург, ДМК Пресс, 2010 г.- 336 с.
Дата: 2019-02-18, просмотров: 635.