forked from pool/libguestfs
230 lines
6.7 KiB
Bash
230 lines
6.7 KiB
Bash
|
#!/bin/bash
|
||
|
# Create an openSUSE image with just enough packages to allow boot to login prompt
|
||
|
#
|
||
|
# Theory of operation:
|
||
|
# This script uses zypper from the host to resolve dependencies
|
||
|
# for zypper which runs within the appliance. If zypper on the host
|
||
|
# is too old, it will be unable to handle repo data from 13.1:
|
||
|
# http://lists.opensuse.org/zypp-devel/2013-11/msg00000.html
|
||
|
# "[zypp-devel] Package conflicting with itself"
|
||
|
# For this reason zypper from 12.3 can be used to install the pattern
|
||
|
# of the final repo.
|
||
|
# First the dependencies of zypper are resolved, the required packages
|
||
|
# are downloaded and extracted with unrpm. Now the guest is started and
|
||
|
# the partitions in the diskimage are prepared. Then the extracted
|
||
|
# package content is copied into the guest. Once that is done zypper
|
||
|
# inside the guest will install the base pattern and a few extra packages.
|
||
|
# Finally the bootloader grub is configured. Once all that is done
|
||
|
# kvm is started. If all goes well a login prompt appears.
|
||
|
# The password for root is "root".
|
||
|
# The guest has also network access to the outside.
|
||
|
#
|
||
|
# Expected runtime: ca. 120 seconds
|
||
|
#
|
||
|
# Expected output:
|
||
|
# guest should start
|
||
|
# no "obvious" errors should be shown during the disk operation
|
||
|
# at the end kvm is started with the generated disk image
|
||
|
# login should be possible
|
||
|
#
|
||
|
set -e
|
||
|
unset LANG
|
||
|
unset ${!LC_*}
|
||
|
cpus=`grep -Ec 'cpu[0-9]' /proc/stat || echo 1`
|
||
|
|
||
|
output_diskimage=/dev/shm/$LOGNAME/testcase.img
|
||
|
final_repo=http://download.opensuse.org/distribution/12.3/repo/oss/
|
||
|
initial_repo=http://download.opensuse.org/distribution/13.1/repo/oss/
|
||
|
force=false
|
||
|
guest_zypper_in__pattern_name="base"
|
||
|
guest_zypper_in__package_list="less vim grub master-boot-code parted nfs-utils"
|
||
|
guest_root_password="root"
|
||
|
diskname_inside_vm=/dev/sda
|
||
|
|
||
|
_exit() {
|
||
|
echo "Exiting '$0 $*'."
|
||
|
exit 1
|
||
|
}
|
||
|
until test $# -lt 1
|
||
|
do
|
||
|
case "$1" in
|
||
|
-n) diskname_inside_vm="$2" ; shift ;;
|
||
|
-o) output_diskimage="$2" ; shift ;;
|
||
|
-R) initial_repo="$2" ; shift ;;
|
||
|
-r) final_repo="$2" ; shift ;;
|
||
|
-f) force=true ;;
|
||
|
-x) set -x ;;
|
||
|
*) echo "Unknown option '$1'" exit 1 ;;
|
||
|
esac
|
||
|
shift
|
||
|
done
|
||
|
if test -z "${initial_repo}"
|
||
|
then
|
||
|
echo "URL to initial repo required. Wrong -R option."
|
||
|
_exit
|
||
|
fi
|
||
|
if test -z "${final_repo}"
|
||
|
then
|
||
|
echo "URL to final repo required. Wrong -r option."
|
||
|
_exit
|
||
|
fi
|
||
|
if test -z "${output_diskimage}"
|
||
|
then
|
||
|
echo "Filename for temporary disk image required. Wrong -o option."
|
||
|
_exit
|
||
|
fi
|
||
|
if test -e "${output_diskimage}"
|
||
|
then
|
||
|
if test "${force}" = "false"
|
||
|
then
|
||
|
echo "Output diskimage '${output_diskimage}' exists."
|
||
|
echo "It will not be overwritten. Option '-f' exists to force overwrite."
|
||
|
_exit
|
||
|
fi
|
||
|
fi
|
||
|
mkdir -vp "${output_diskimage%/*}"
|
||
|
td=`mktemp -d --tmpdir=/dev/shm/${LOGNAME}`
|
||
|
tf=`mktemp --tmpdir=/dev/shm/${LOGNAME}`
|
||
|
_exit() {
|
||
|
rm -rf "$tf"
|
||
|
rm -rf "$td"
|
||
|
}
|
||
|
trap _exit EXIT
|
||
|
dir_repo=${td}/repos.d
|
||
|
dir_root=${td}/root
|
||
|
dir_cache=${td}/cache
|
||
|
mkdir -vp \
|
||
|
${dir_root} \
|
||
|
${dir_cache} \
|
||
|
${dir_repo}
|
||
|
cat > ${tf} <<EOF
|
||
|
[main]
|
||
|
reposdir = ${dir_repo}
|
||
|
EOF
|
||
|
cat > ${dir_repo}/tmp.repo <<EOF
|
||
|
[tmp]
|
||
|
name=tmp
|
||
|
enabled=1
|
||
|
autorefresh=1
|
||
|
keeppackages=0
|
||
|
baseurl=${initial_repo}
|
||
|
EOF
|
||
|
packages="
|
||
|
curl
|
||
|
zypper
|
||
|
"
|
||
|
head ${dir_repo}/tmp.repo ${tf}
|
||
|
zypper \
|
||
|
--verbose \
|
||
|
--verbose \
|
||
|
--config ${tf} \
|
||
|
--root ${dir_root} \
|
||
|
--reposd-dir ${dir_repo} \
|
||
|
--cache-dir ${dir_cache} \
|
||
|
--gpg-auto-import-keys \
|
||
|
--no-gpg-checks \
|
||
|
--non-interactive \
|
||
|
lr -d
|
||
|
zypper \
|
||
|
--verbose \
|
||
|
--verbose \
|
||
|
--config ${tf} \
|
||
|
--root ${dir_root} \
|
||
|
--reposd-dir ${dir_repo} \
|
||
|
--cache-dir ${dir_cache} \
|
||
|
--gpg-auto-import-keys \
|
||
|
--no-gpg-checks \
|
||
|
--non-interactive \
|
||
|
install \
|
||
|
--auto-agree-with-licenses \
|
||
|
--no-recommends \
|
||
|
--dry-run \
|
||
|
--download-only \
|
||
|
${packages}
|
||
|
cd ${dir_root}
|
||
|
find ${dir_cache} -xdev -name "*.rpm" -print0 | sort -z | xargs -0 -n 1 -P ${cpus} unrpm
|
||
|
mkdir -vp etc/zypp/repos.d
|
||
|
grep -w search /etc/resolv.conf >> etc/resolv.conf
|
||
|
echo nameserver 169.254.2.3 >> etc/resolv.conf
|
||
|
grep -w root /etc/passwd > etc/passwd
|
||
|
grep -w root /etc/group > etc/group
|
||
|
echo 'root::15209::::::' > etc/shadow
|
||
|
cat > etc/fstab <<EOF
|
||
|
LABEL=SWAP swap swap defaults 0 0
|
||
|
LABEL=ROOT / ext4 noatime 1 2
|
||
|
EOF
|
||
|
mkdir -p boot/grub
|
||
|
cat > etc/grub.conf <<EOF
|
||
|
setup --stage2=/boot/grub/stage2 --force-lba (hd0,1) (hd0,1)
|
||
|
quit
|
||
|
EOF
|
||
|
echo "(hd0) ${diskname_inside_vm}" > boot/grub/device.map
|
||
|
cat > boot/grub/menu.lst <<EOF
|
||
|
serial --unit=0 --speed=115200
|
||
|
terminal --timeout=10 console serial
|
||
|
title ${0} $*
|
||
|
kernel /boot/vmlinuz panic=9 quiet video=800x600
|
||
|
initrd /boot/initrd
|
||
|
EOF
|
||
|
du -sm .
|
||
|
find ${dir_cache} -xdev -name "*.rpm" -delete
|
||
|
guestfish \
|
||
|
-x \
|
||
|
\
|
||
|
sparse ${output_diskimage} 1024M : \
|
||
|
set-smp 2 : \
|
||
|
set-memsize 1024 : \
|
||
|
set-network true : \
|
||
|
run : \
|
||
|
list-devices : \
|
||
|
part-init ${diskname_inside_vm} mbr : \
|
||
|
part-add ${diskname_inside_vm} primary 1 $(( ((1024*1024)* 256)/512 - 1)) : \
|
||
|
part-add ${diskname_inside_vm} primary $(( ((1024*1024)* 256)/512 )) $(( ((1024*1024)*1024)/512 - 1)) : \
|
||
|
part-list ${diskname_inside_vm} : \
|
||
|
mkswap-opts ${diskname_inside_vm}1 label:SWAP : \
|
||
|
mke2fs ${diskname_inside_vm}2 label:ROOT fstype:ext4 blocksize:1024 : \
|
||
|
list-devices : \
|
||
|
list-partitions : \
|
||
|
part-set-bootable ${diskname_inside_vm} 2 true : \
|
||
|
list-filesystems : \
|
||
|
swapon-label SWAP : \
|
||
|
mount-options discard ${diskname_inside_vm}2 / : \
|
||
|
set-verbose false : \
|
||
|
copy-in `echo *` / : \
|
||
|
set-verbose true : \
|
||
|
command /sbin/ldconfig : \
|
||
|
cat /etc/resolv.conf : \
|
||
|
command "ip a" : \
|
||
|
command "curl google.com" : \
|
||
|
command "zypper help" : \
|
||
|
command "zypper -v -v ar -c -K -f ${final_repo} tmp" : \
|
||
|
sh "(set -x -e ; z_in='zypper -v -v --gpg-auto-import-keys --no-gpg-checks --non-interactive in --auto-agree-with-licenses --no-recommends' ; \$z_in -t pattern ${guest_zypper_in__pattern_name} ; chkstat --set /etc/permissions /etc/permissions.easy ; echo root:${guest_root_password} | chpasswd ; \$z_in ${guest_zypper_in__package_list} ) 2>&1 " : \
|
||
|
sh "depmod -a \$(get_kernel_version /boot/vmlinuz) ; mkinitrd -B" : \
|
||
|
sh "dd if=/usr/lib/boot/MBR of=${diskname_inside_vm}" : \
|
||
|
sh "cp --verbose --sparse=never --remove-destination --target-directory=/boot/grub /usr/lib/grub/*" : \
|
||
|
sh "grub --batch --verbose < /etc/grub.conf" : \
|
||
|
sh "echo BOOTPROTO='dhcp' >> /etc/sysconfig/network/ifcfg-eth0" : \
|
||
|
sh "echo STARTMODE='auto' >> /etc/sysconfig/network/ifcfg-eth0" : \
|
||
|
sh "echo 'Password for root is ${guest_root_password}' >> /etc/issue" : \
|
||
|
cat /etc/fstab : \
|
||
|
quit
|
||
|
ls -lhsS "${output_diskimage}"
|
||
|
|
||
|
qemu-kvm \
|
||
|
-global virtio-blk-pci.scsi=off \
|
||
|
-enable-fips \
|
||
|
-machine accel=kvm:tcg \
|
||
|
-cpu host,+kvmclock \
|
||
|
-m 500 \
|
||
|
-no-reboot \
|
||
|
-no-hpet \
|
||
|
-drive file=${output_diskimage},cache=writeback,id=hd0,if=none \
|
||
|
-device virtio-blk-pci,drive=hd0 \
|
||
|
-device virtio-serial-pci \
|
||
|
-serial stdio \
|
||
|
-device sga \
|
||
|
-netdev user,id=usernet,net=169.254.0.0/16 \
|
||
|
-device virtio-net-pci,netdev=usernet
|
||
|
|
||
|
exit 0
|