From 645e6932216fddd154865a9bebb0fd0e3beed301 Mon Sep 17 00:00:00 2001 From: Colin Guthrie Date: Sun, 1 Dec 2013 20:57:39 +0000 Subject: rescue: Avoid having to run dracut convertfs script by setting up the tree. We can easily avoid post processing the tree simply by moving the tree around and ensuring we set it up correctly (with the compat symlinks) early in the process. --- rescue/tree/usr/sbin/fakeshutdown | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 rescue/tree/usr/sbin/fakeshutdown (limited to 'rescue/tree/usr/sbin') diff --git a/rescue/tree/usr/sbin/fakeshutdown b/rescue/tree/usr/sbin/fakeshutdown new file mode 100755 index 000000000..309f26c6c --- /dev/null +++ b/rescue/tree/usr/sbin/fakeshutdown @@ -0,0 +1,39 @@ +#!/usr/bin/perl + +# +# Guillaume Cottenceau (gc) +# +# Copyright 2001 Mandrakesoft +# +# This software may be freely redistributed under the terms of the GNU +# public license. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +# + +# From MDK::Common: +sub cat_ { open(my $F, '<', $_[0]) or die "cat of file $_[0] failed: $!\n"; my @l = <$F>; wantarray() ? @l : join '', @l } +print "\n"; + + +#- try to umount as much as possible; uses Pixel's ultra optimized algo (let you guess how it works..) +print "Umounting:\n"; +my @mounts = cat_('/proc/mounts'); +my $something_moved; +do { + $something_moved = 0; + foreach (@mounts) { + my $where = (split)[1]; + next if $where eq '/'; #- ouch! umounting the ramdisk on / always succeeds, and makes it becoming ro :-( + if (!system("umount $where 2>/dev/null")) { + print "\t$where\n"; + $something_moved++; + } + } +} while $something_moved; + + +#- shutdown with init +exec '/sbin/init', 6; -- cgit v1.2.1 From c4634f60b5c6d67eedfedc02166499884e4d6625 Mon Sep 17 00:00:00 2001 From: Colin Guthrie Date: Sun, 1 Dec 2013 22:29:20 +0000 Subject: rescue: Use systemd as initsystem. This commit converts the rescue system to systemd rather than the legacy sysvinit. Besides being faster, one primary advantage of running systemd here is that we can "boot" the system we're inspecting via systemd-nspawn to properly test it. Note: Bits that don't work: * nspawn * old dracut initqueue udev rules in /etc/ are not cleaned out (drop in support error?) * the gui may loop differently now --- rescue/tree/usr/sbin/fakeshutdown | 39 --------------------------------------- 1 file changed, 39 deletions(-) delete mode 100755 rescue/tree/usr/sbin/fakeshutdown (limited to 'rescue/tree/usr/sbin') diff --git a/rescue/tree/usr/sbin/fakeshutdown b/rescue/tree/usr/sbin/fakeshutdown deleted file mode 100755 index 309f26c6c..000000000 --- a/rescue/tree/usr/sbin/fakeshutdown +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/perl - -# -# Guillaume Cottenceau (gc) -# -# Copyright 2001 Mandrakesoft -# -# This software may be freely redistributed under the terms of the GNU -# public license. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -# - -# From MDK::Common: -sub cat_ { open(my $F, '<', $_[0]) or die "cat of file $_[0] failed: $!\n"; my @l = <$F>; wantarray() ? @l : join '', @l } -print "\n"; - - -#- try to umount as much as possible; uses Pixel's ultra optimized algo (let you guess how it works..) -print "Umounting:\n"; -my @mounts = cat_('/proc/mounts'); -my $something_moved; -do { - $something_moved = 0; - foreach (@mounts) { - my $where = (split)[1]; - next if $where eq '/'; #- ouch! umounting the ramdisk on / always succeeds, and makes it becoming ro :-( - if (!system("umount $where 2>/dev/null")) { - print "\t$where\n"; - $something_moved++; - } - } -} while $something_moved; - - -#- shutdown with init -exec '/sbin/init', 6; -- cgit v1.2.1 From e7aedc3aa7e08f3337c86085c6da10ca83445024 Mon Sep 17 00:00:00 2001 From: Colin Guthrie Date: Mon, 2 Dec 2013 23:40:22 +0000 Subject: rescue: Move files around and introduce proper dep tracking. This moves any perl files into bin and sbin folders which will be directly installed in /usr/bin and /usr/sbin. This is done such that the extract modes in list.xml still work and the files end up in the right places. All other files have been moved into the tree folder in their final destination. --- rescue/tree/usr/sbin/diskdrake-resize | 29 +++ rescue/tree/usr/sbin/genpasswd | 13 ++ rescue/tree/usr/sbin/grabjournallogs | 51 +++++ rescue/tree/usr/sbin/restore-image.sh | 412 ++++++++++++++++++++++++++++++++++ rescue/tree/usr/sbin/startssh | 27 +++ 5 files changed, 532 insertions(+) create mode 100755 rescue/tree/usr/sbin/diskdrake-resize create mode 100755 rescue/tree/usr/sbin/genpasswd create mode 100755 rescue/tree/usr/sbin/grabjournallogs create mode 100755 rescue/tree/usr/sbin/restore-image.sh create mode 100755 rescue/tree/usr/sbin/startssh (limited to 'rescue/tree/usr/sbin') diff --git a/rescue/tree/usr/sbin/diskdrake-resize b/rescue/tree/usr/sbin/diskdrake-resize new file mode 100755 index 000000000..88372aac7 --- /dev/null +++ b/rescue/tree/usr/sbin/diskdrake-resize @@ -0,0 +1,29 @@ +#!/usr/bin/perl + +use lib qw(/usr/lib/libDrakX); + +use devices; +use log; +use resize_fat::main; +use diskdrake::resize_ntfs; +use diskdrake::resize_ext2; + +my ($device, $fs_type, $size) = @ARGV + or die "usage: $0 \n"; + +my %fs_pkgs = ( + vfat => 'resize_fat::main', + ntfs => 'diskdrake::resize_ntfs', + ext2 => 'diskdrake::resize_ext2', + ext3 => 'diskdrake::resize_ext2', + ext4 => 'diskdrake::resize_ext2', +); + +my $resize_pkg = $fs_pkgs{$fs_type} + or die "no support for $fs_type type\n"; + +log::l("resizing $device to $size"); + +my $resize = $resize_pkg->new($device, devices::make($device)) + or die "unable to initialize resize\n"; +$resize->resize($size); diff --git a/rescue/tree/usr/sbin/genpasswd b/rescue/tree/usr/sbin/genpasswd new file mode 100755 index 000000000..2f129673d --- /dev/null +++ b/rescue/tree/usr/sbin/genpasswd @@ -0,0 +1,13 @@ +#!/usr/bin/perl + +use strict; + +system "stty -echo"; +print STDERR "Give a password for ssh access: "; +chomp(my $word = ); +print STDERR "\n"; +system "stty echo"; + +my @salt = ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]; +print crypt($word, join('', @salt)); +print "\n"; diff --git a/rescue/tree/usr/sbin/grabjournallogs b/rescue/tree/usr/sbin/grabjournallogs new file mode 100755 index 000000000..ef2ea4a1d --- /dev/null +++ b/rescue/tree/usr/sbin/grabjournallogs @@ -0,0 +1,51 @@ +#!/bin/bash + +if ! mountpoint -q /mnt ; then + echo "I do not seem to see a Mageia install mounted on /mnt. You need to mount it first!" >&2 + exit 1 +fi + +if [ ! -f /mnt/etc/machine-id ]; then + echo "Cannot find machine-id file (/mnt/etc/machine-id)" >&2 + exit 1 +fi + +MID=$(cat /mnt/etc/machine-id) +echo +echo "Found machine-id: $MID" +if [ ! -d /mnt/var/log/journal/$MID ]; then + echo "Cannot find journal log directory (/mnt/var/log/journal/)" >&2 + exit 1 +fi + +TIMEFRAME=24 +if [ -n "$1" ]; then + NEWTIMEFRAME=$(( 0 + $1 )) + if [ $NEWTIMEFRAME -gt 0 ]; then + TIMEFRAME=$NEWTIMEFRAME + fi +fi +SINCE="$(LC_ALL=c date --date=$TIMEFRAME' hours ago' +'%F %T')" +echo "Will collect logs from the last $TIMEFRAME hour(s)" +echo " NB give numeric argument to override capture period" + +TEMPFILE=$(mktemp /tmp/grabjournallogs.XXXXXX) +echo -n "Extracting logs... " +journalctl -D /mnt/var/log/journal/$MID --since "$SINCE" -o short >$TEMPFILE +echo "done" + +if [ $(cat $TEMPFILE | wc -l) -lt 2 ]; then + rm -f $TEMPFILE + echo >&2 + echo "Cannot find any logs. Consider increasing the capture period by passing a" >&2 + echo "numeric argument larger than $TIMEFRAME." >&2 + exit 1 +fi + +echo -n "Compressing logs... " +cat $TEMPFILE | xz >/journallogs.xz +rm -f $TEMPFILE +echo "done" + +echo +echo "Your logs have been extracted to the file 'journallogs.xz'" diff --git a/rescue/tree/usr/sbin/restore-image.sh b/rescue/tree/usr/sbin/restore-image.sh new file mode 100755 index 000000000..f4f904f3f --- /dev/null +++ b/rescue/tree/usr/sbin/restore-image.sh @@ -0,0 +1,412 @@ +#!/bin/bash + +setterm -powersave off +setterm -blank 0 + +if [ -r ./restore-image-lib.sh ]; then + . ./restore-image-lib.sh +elif [ -r /usr/lib/restore-image-lib.sh ]; then + . /usr/lib/restore-image-lib.sh +fi + +export PATH="/sbin:/bin:/usr/sbin:/usr/bin" + +mnt_dir="/tmp/mnt" +restore_media="/tmp/media" +images_dir="$restore_media/images" +images="$images_dir/list" +images_config="$images_dir/config" +image="" +win32_part_dev= +win32_part_type= +win32_part_new_size= + +function read_config() +{ + if [ -r "$images_config" ]; then + . $images_config + fi +} + +function image_list() +{ + list=$(cat $images | awk -F',' \ + '{ print $1 " " $2 " " $4 }') + + echo $list +} + +function image_file() +{ + country="$1" + + file=$(grep ^$country $images | awk -F',' '{ print $3 }') + + echo $file +} + +function welcome() +{ + while true; do + clear + msg="\n Welcome to $TITLE\n\ +\nThe following images were found, select one:\n " + opcao=$(dialog --backtitle "$BACKTITLE" --title "$TITLE" \ + --stdout --radiolist "$msg" 0 0 0 \ + $(image_list)) + + if [ "$?" != "0" ]; then + _yesno "\nInterrupt installation?\n " + if [ "$?" = "0" ]; then + _shutdown + fi + else + if [ -z "$opcao" ]; then + continue + else + image=$(image_file $opcao) + break + fi + fi + done + + # disable kernel messages in the console + echo "1 4 1 7" > /proc/sys/kernel/printk +} + +function install_warning() +{ + if [ -n "${win32_part_dev}" ]; then + warn_msg="Windows installation detected.\nWe will set it up as dual boot. \ +You may lose some data.\nPlease backup before proceeding." + else + warn_msg="WARNING: This process will erase all data in this machine, \ +do you want to continue?" + fi + + clear + _yesno "\n$warn_msg\n" + if [ "$?" != "0" ]; then + _shutdown + fi +} + +function detect_root() +{ + inst_source_dev=$(awk "\$2 == \"$restore_media\" { print \$1 }" /proc/mounts | sed -e 's/[0-9]$//') + inst_source_dev=${inst_source_dev#/dev/} + devices=$(grep "^ .*[^0-9]$" < /proc/partitions | grep -v ${inst_source_dev} | awk '$3 > '$MIN_DISKSIZE' { print $4,$3 }') + + if [ -z "${devices}" ]; then + exit 1 + fi + + devs_found=$(($(echo $devices | wc -w)/2)) + + root_data=$(detect_win32 ${inst_source_dev}) + + if [ -z "${root_data}" ]; then + if [ "$devs_found" -gt "1" ]; then + if [ -n "${inst_source_dev}" ]; then + opcao=$(dialog --backtitle "$BACKTITLE" --title "$TITLE" --stdout --menu 'Choose one of the detected devices to restore to (check the blocks size column first):' 8 50 0 $devices ) + if [ "$?" != "0" ]; then + _yesno "\nInterrupt installation?\n " + if [ "$?" = "0" ]; then + _shutdown + fi + else + root_data=${opcao} + fi + fi + else + root_data=$(echo ${devices} | cut -d ' ' -f 1) + fi + fi + + echo ${root_data} +} + +function detect_win32() +{ + # from detect_root() + skip_dev=${1} + + # win32 detection won't handle complex layouts + if [ $(fdisk -l | grep "^/dev/" | grep -v ${skip_dev} | wc -l) -gt 1 ]; then + exit + fi + + # get the last created windows partition information + set -f + device=$(fdisk -l | grep "^/dev/" | grep -v ${skip_dev} | grep -e "FAT\|NTFS\|HPFS" | tail -1 | sed 's/ .*$//') + set +f + + if [ -z "${device}" ]; then + exit + fi + + # it might be needed, for safety + device_type=$(blkid -o value -s TYPE ${device}) + modprobe ${device_type} + + # df for that partition + mount ${device} /mnt + size=$(df ${device} | tail -1) + umount /mnt + + # its diskspace + used=$(echo ${size} | awk '{ print $3 }') + left=$(echo ${size} | awk '{ print $4 }') + avail=$((${left}/2)) + + if [ ! ${avail} -lt ${MIN_DISKSIZE} ]; then + win32_part_dev=${device} + win32_part_type=${device_type} + # our install takes half of 'left' + win32_part_new_size=$((${used}+${avail})) + + dev=${win32_part_dev#/dev/} + disk=${dev%[0-9]} + number=${dev#[a-w][a-w][a-w]} + let number++ + echo ${disk}${number} ${win32_part_dev} ${win32_part_type} ${win32_part_new_size} + fi +} + +function resize_win32() +{ + device=${1} + device_type=${2} + new_win32_size=${3} + + dialog --backtitle "$BACKTITLE" --title "$TITLE" --infobox "\nResizing Windows partition...\n" 4 55 + + dev=${device#/dev/} + disk=${dev%[0-9]} + win32_number=${dev#[a-w][a-w][a-w]} + + case ${device_type} in + vfat) device_id=b ;; + ntfs) device_id=7 ;; + hpfs) device_id=87 ;; + esac + + # wrapper around libdrakx by blino + diskdrake-resize ${device} ${device_type} $((${new_win32_size}*2)) &>/dev/null + + # we need some free sector here, rebuilding layout + fdisk /dev/${disk} &>/dev/null </dev/null </dev/null; dd bs=4M of=/dev/${root} >/tmp/backup.out 2>&1>>/tmp/log) & + + sleep 3 + pid=$(ps ax | grep 'dd bs=4M of' | grep -v grep | awk '{ print $1 }') + + while [ true ]; do + ps | grep -q $pid + if [ $? -eq 0 ]; then + /bin/kill -SIGUSR1 $pid + complete=$(tail -n 1 /tmp/backup.out | awk '{ print $1 }') + echo $((complete*100/total)) + sleep 1 + else + break + fi + done | dialog --backtitle "$BACKTITLE" --title "$TITLE" --gauge "\nWriting image..." 8 45 + + in=$(tail -n 3 /tmp/backup.out | grep 'in$' | cut -d' ' -f1) + out=$(tail -n 3 /tmp/backup.out | grep 'out$' | cut -d' ' -f1) + + if [ x"$in" != x"$out" ]; then + _msgbox "\nError writing image!\n" + sleep 24h + fi + + # Now re-read the partition table because 'dd' might have changed it + sfdisk -R /dev/${root} +} + +function grub_setup() +{ + root=${1} + grub_dir=${2} + + # install the bootloader + grub <> ${grub_dir}/menu.lst </dev/null| grep "Filesystem OS type" | awk '{ print $4 }') + if [ "${filesystem_type}" = "Linux" ]; then + dialog --backtitle "$BACKTITLE" --title "$TITLE" --infobox "Finishing Install... Expanding ${root}" 3 40 + disk=/dev/${root%[0-9]} + main_part=/dev/${root} + + # FIXME: absurdly dirty hack + main_part_num=${root:3} + swap_part_num=$((main_part_num+1)) + swap_part=${disk}${swap_part_num} + + main_part_sectors= + if [ -n "$SWAP_BLOCKS" ]; then + if [ -n "$EXPAND_FS" ]; then + total_blocks=$(sfdisk -s $disk) + main_part_blocks=$((total_blocks-SWAP_BLOCKS)) + main_part_sectors=$((main_part_blocks*2)) + else + main_part_sectors=$(sfdisk -d $disk | perl -lne 'm|^'$main_part'\b.*,\s*size\s*=\s*(\d+)\b| and print($1), exit') + fi + fi + if [ -n "$SWAP_BLOCKS" ]; then + parted $disk -- mkpartfs primary linux-swap ${main_part_sectors}s -1s yes + mkswap -L swap $swap_part + fi + if [ -n "$EXPAND_FS" ]; then + e2fsck -fy $main_part + fdisk $disk << EOF +d +$main_part_num +n +p +$main_part_num + + +a +$main_part_num +w +EOF + sfdisk -R $disk + e2fsck -fy $main_part + resize2fs $main_part + fi + mkdir -p $mnt_dir + mount $main_part $mnt_dir + grub_dir="$mnt_dir/boot/grub" + if [ -d "$grub_dir" ]; then + echo "(hd0) $disk" > "$grub_dir/device.map" + if [ -n "$win32_part_dev" ]; then + grub_setup ${root} ${grub_dir} + fi + fi + if [ -n "$MKINITRD" ]; then + mount -t sysfs none "$mnt_dir/sys" + mount -t proc none "$mnt_dir/proc" + chroot $mnt_dir bootloader-config --action rebuild-initrds + umount "$mnt_dir/sys" + umount "$mnt_dir/proc" + fi + umount $mnt_dir + fi +} + +# installation steps +welcome +read_config +detect_device +install_warning +write_image +expand_fs + +# all done! +_msgbox "\nInstallation process finished.\nPress ENTER to shutdown.\n " + +_shutdown + diff --git a/rescue/tree/usr/sbin/startssh b/rescue/tree/usr/sbin/startssh new file mode 100755 index 000000000..e14bb10e8 --- /dev/null +++ b/rescue/tree/usr/sbin/startssh @@ -0,0 +1,27 @@ +#!/bin/bash + +# test for an up interface != lo +if test "x"`ifconfig | cut -d ' ' -f 1 | grep -v '^$' | grep -v lo` = "x"; then + echo "There is no up interface, you can try to execute 'drvinst' to find drivers and 'dhcp-client' to configure your interface for DHCP." >&2 + exit 5 +fi + +# generate host keys +mkdir -p /etc/dropbear +[[ -f /etc/dropbear/dropbear_dss_host_key ]] || dropbearkey -t dss -f /etc/dropbear/dropbear_dss_host_key >/dev/null || { echo "Can't generate dsa host key" >&2 ; exit 4; } +[[ -f /etc/dropbear/dropbear_rsa_host_key ]] || dropbearkey -t rsa -f /etc/dropbear/dropbear_rsa_host_key >/dev/null || { echo "Can't generate rsa host key" >&2 ; exit 3; } + +# set a password +passwd=`genpasswd` +if (( $? != 0 )); then + reset + exit $? +fi +sed -E -i -e 's/^root:[^:]*:/root:'"${passwd//\//\\/}"':/' /etc/passwd || { echo "Can't set password" >&2 ; exit 2; } + +# test for /dev/pts +grep '^devpts /dev/pts devpts ' /proc/mounts >/dev/null 2>/dev/null || mount -t devpts devpts /dev/pts + +echo "" +echo -n "Starting ssh daemon... " +dropbear -E 2>/var/log/dropbear.log && echo "OK" || { echo "Failed" ; exit 1; } -- cgit v1.2.1