Automating partitioning setup of /dev/sda on /dev/sdc


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Automating partitioning setup of /dev/sda on /dev/sdc
# 8  
Old 05-21-2015
That's incorrect, cylinders and sectors haven't mattered since 1996. The computer just asks for block 6, block 123485701, or whatever, and the hard drive handles all the rest.

What do you mean by "first 2048 sectors"? Do you mean blocks?

I see a couple of pitfalls here. First of all, did you reload the partition table after running dd? Linux needs to be told "the partition table has changed" when you do that, or it will just keep using the old layout until the next reboot. There's also a "backup" GPT sector which might have to be regenerated after you do the copy.
This User Gave Thanks to Corona688 For This Post:
# 9  
Old 05-21-2015
I'm out on the road right now I'll get back to my house and then I'll be able to respond thank you

---------- Post updated at 03:25 PM ---------- Previous update was at 02:25 PM ----------

This is the piece preceding the partitioning in the recovery script...
All drive designations are for the 2nd external USB Drive -- which gets attached as /dev/sdc.
The USB drive used for the backup process is /dev/sdb. /dev/sdb1 is a LiveCD partition and /dev/sdb2 is the data partition.
Code:
#!/bin/bash
#
# This scipt is to be run from a LiveCD environment in a root terminal
#
clear
echo
echo
echo "Mounting USB Hard Drive to mount point /mnt/USBDrive..."
# We'll create a mount point and mount the main directory structure of our backup drive to this point.
mkdir -p /mnt/USBDrive
mount -o defaults /dev/sdb2 /mnt/USBDrive
echo "Done!"
echo 
# We are going to need the physical volume name and UUID along with the Volume Group name later in this process
# We also want to determine the starting and ending sector (along with the partition number) of our luks partition
echo "Setting a few variables for later..."
pvname=$(grep Name /mnt/USBDrive/lvminfo/pvparse | cut -d'/' -f4)
pvuuid=$(grep UUID /mnt/USBDrive/lvminfo/pvparse | cut -b 25-62)
vgname=$(grep ^/dev /mnt/USBDrive/lvminfo/fstab_info | cut -d'/' -f4 | cut -d'-' -f1 | sed '2,99d')

This is the section following the partitioning piece...
Code:
# We need to inform the kernel about the changes
partprobe
# And format the partition we just created
mkfs -t ext4 /dev/sdc$partnum
echo
echo "Restoring Cryptsetup header and key slots..."
# This command always gives an error that can be disregarded. Thus, the redirection of stderr to null
cryptsetup luksHeaderRestore /dev/sdc$partnum --header-backup-file /mnt/USBDrive/lvminfo/cryptbackup.img 2>/dev/null
echo
echo "Opening crypt partition for writing..."
cryptsetup -vv open /dev/sdc$partnum --type luks $pvname
echo
echo "Restoring Physical Volume..."
# This command always gives an error that can be disregarded. Thus, the redirection of stderr to null
pvcreate -f --uuid $pvuuid --restorefile /mnt/USBDrive/lvminfo/$vgname /dev/mapper/$pvname 2>/dev/null
echo
echo "Restoring & Checking Volume Group and Logical Volumes structures..."
vgcfgrestore $vgname
lvs -a -o +devices
vgchange -ay $vgname
pvck -vv
vgck -vv
mkfs -t ext4 /dev/mapper/$vgname-*
mkswap /dev/mapper/$vgname-swap
echo
echo "Creating Mount Points, mounting individual LVM's from Volume Group and restoring data..."
for fs in ${farry[@]}
do
    mkdir -p /mnt/$vgname/$fs
    mount /dev/mapper/${vgname}-$fs /mnt/$vgname/$fs
    rsync -aHvu /mnt/USBDrive/Recover/$fs /mnt/$vgname/$fs
done
rsync -aHvu --ignore-existing /mnt/USBDrive/Recover/ /mnt/$vgname/
mv -f /mnt/USBDrive/lvminfo/cryptbackup.img /mnt/USBDrive/lvminfo/cryptbackup.img.bak
exec $SHELL

This is what i DID have until I realized that I kept getting a corrupted partition table.
Code:
startsec=$(grep ^/dev /mnt/USBDrive/info/fdisk_info | grep G | cut -d" " -f3)
endsect=$(grep ^/dev /mnt/USBDrive/info/fdisk_info | grep G | cut -d" " -f4)
partnum=$(grep $startsec /mnt/USBDrive/info/sgdisk_info | cut -b4)
# The following line initializes an array of variables from a list of lvnames created from fstab_info
read -a farry <<< $(grep $vgname /mnt/USBDrive/lvminfo/fstab_info | cut -d'-' -f2 | cut -d'/' -f1 | cut -d" " -f1 | awk '{printf("%s%s",$0,NR%9?" ":"\n")}')
# Once we have gotten the volume group name from fstab we can create a mount point for the restore process 
mkdir -p /mnt/$vgname
echo
# We start the recovery process by writing our saved critical disk areas to the new drive
# This will give us a mountable drive with the partition table, EFI and boot partitions as they were on the original drive
# sgdisk /dev/sda --backup=/mnt/USBDrive/info/sgdisksda
# sgdisk  /dev/sdc --load-backup= /mnt/USBDrive/info/sgdisksda
echo "Restoring the first 2048 sectors of /dev/sda..."
dd if=/mnt/USBDrive/dd/partition-restore of=/dev/sdc bs=1 count=2047
echo
echo "Restoring the EFI partition as /dev/sda1..."
dd if=/mnt/USBDrive/dd/sda1EFI-restore of=/dev/sdc1 bs=8192
echo
echo "Restoring the Boot partition as /dev/sda2..."
dd if=/mnt/USBDrive/dd/sda2Boot-restore of=/dev/sdc2 bs=8192
echo
echo "Prepping to restore the luks partition, and VG/LVM structures..."
echo
# Just to ensure that there isn't any old luks data on our new drive that will present errors during the restortion of our saved luks partition header image, we need to wipe out any signs of any previous lvm info that might be there
echo "First we will ensure that no old crypt/lvm info remains on our recovery disk. If there is the restoration will error out."
cryptsetup -v isLuks /dev/sdc$partnum
head -c 3145728 /dev/zero > /dev/sdc$partnum; sync
# Next, we will recreate the partition from scratch and format it to ext4
echo "Then we will create a new /dev/sda3 and restore our backup img"
sgdisk -d $partnum -n $partnum:$startsec:$endsec -t $partnum:8300 /dev/sdc

(As you can see, in the beginning, to work around the corrupt partition issue,
I was trying to simply delete /dev/sdc3, put in a new partition, format it, run partprobe to update the kernel
and then do the cryptsetup luksRestore.
But I kept getting a "partition corrupt' error and exit.)
Which is what brings me to my original question....
How can I parse and initialize an array and a for loop that will just recreate these partitions from scratch rather than doing it otherwise. Smilie

Since all information gathered about /dev/sda showed that the EFI partition started at sector 2048,
I just used dd to copy sectors 0-2047 and then restored the other two partitions using the same method.

All of the information that I thought I might need to do the recovery part was saved during the execution of the backup script:
Code:
#!/bin/bash
mkdir -p /mnt/USBDrive
echo "Mounting USB Hard Drive..."
mount -o defaults /dev/sdb2 /mnt/USBDrive
echo "Done!"
echo
mkdir -p /mnt/USBDrive/dd
echo "Backing up first 2048 sectors of /dev/sda to /mnt/USBDrive/dd"
dd if=/dev/sda of=/mnt/USBDrive/dd/partition-restore bs=1 count=2047
echo
echo "Backing up the EFI partition to /mnt/USBDrive/dd "
dd if=/dev/sda1 of=/mnt/USBDrive/dd/sda1EFI-restore bs=8192
echo
echo "Backing up the Boot partition to /mnt/USBDrive/dd "
dd if=/dev/sda2 of=/mnt/USBDrive/dd/sda2Boot-restore bs=8192
echo
mkdir -p /mnt/USBDrive/info
echo "Backing up critical hardware information to  /mnt/USBDrive/info"
lspci -vv > /mnt/USBDrive/info/lspci_info
lshw > /mnt/USBDrive/info/lshw_info
hdparm -I /dev/sda > /mnt/USBDrive/info/hdparm_info
blkid > /mnt/USBDrive/info/blkid_info
fdisk -l /dev/sda > /mnt/USBDrive/info/fdisk_info
sgdisk -p /dev/sda > /mnt/USBDrive/info/sgdisk_info
smartctl -d ata -a -i /dev/sda > /mnt/USBDrive/info/smartctl_info
df -h > /mnt/USBDrive/info/df_info
echo
echo "Backing up critical LVM info to /mnt/USBDrive/lvminfo" 
mkdir -p /mnt/USBDrive/lvminfo
# sgdisk backup
# -b or --backup     filename     Save a backup of the disk to the specified file.
pvdisplay > /mnt/USBDrive/lvminfo/pvdisplay
vgdisplay > /mnt/USBDrive/lvminfo/vgdisplay
lvscan > /mnt/USBDrive/lvminfo/lvscan
lvdisplay > /mnt/USBDrive/lvminfo/lvdisplay
lvmdiskscan > /mnt/USBDrive/lvminfo/lvmdiskscan
lvs -a -o +devices > /mnt/USBDrive/lvminfo/lvscmd
echo
echo "Backing up cryptsetup info to /mnt/USBDrive/lvminfo"
cryptsetup -q luksHeaderBackup /dev/sda3 --header-backup-file /mnt/USBDrive/lvminfo/cryptbackup.img
echo
echo "Backing up LVM Structure to  /mnt/USBDrive/lvminfo "
vgcfgbackup
vgcfgbackup -f /mnt/USBDrive/lvminfo/fedora fedora
vgdisplay --verbose | grep PV > /mnt/USBDrive/lvminfo/pvparse
echo
echo "Backing up crypttab and fstab to /mnt/USBDrive/lvminfo"
cat /etc/crypttab > /mnt/USBDrive/lvminfo/crypttab_info
cat /etc/fstab > /mnt/USBDrive/lvminfo/fstab_info
echo
echo "Running full system backup: Backing up to /mnt/USBDrive/Recover"
mkdir -p /mnt/USBDrive/Recover
echo
rsync -aHvu --delete --exclude={/backup/*,/dev/*,/proc/*,/sys/*,/run/*,/mnt/*,/media/*,/lost+found,/home/*/.thumbnails/*,/home/*/.local/share/Trash/*,/home/*/.gvfs/*,/home/*/.cache/google/*,/home/*/.cache/mozilla/*} /* /mnt/USBDrive/Recover
echo
echo
# Add in prompt to either unmount or leave mounted. umount /mnt/USBDrive
exec $SHELL

But WAIT!! Something just hit me!!
Code:
echo "Backing up first 2048 sectors of /dev/sda to /mnt/USBDrive/dd"
dd if=/dev/sda of=/mnt/USBDrive/dd/partition-restore bs=1 count=2047

actually gives me 2048 BLOCKS and not 2047 sectors, right?!?! Am I on the right track here???

---------- Post updated at 03:58 PM ---------- Previous update was at 03:25 PM ----------

So if I understand it correctly, in order to back up everything on the disk prior to the first partition (which starts at sector 2048) I would actually have to use a bs of 512 and indicate a count of 2047. Correct?
Code:
dd if=/dev/sda of=/mnt/USBDrive/dd/partition-restore bs=512 count=2047

Since the sector size is 512 bytes, that should give me the equivalent of the first 2047 sectors, correct?
# 10  
Old 05-21-2015
dd if=/dev/sda of=/mnt/USBDrive/dd/partition-restore bs=1 count=2047 will give you 2047 bytes, not blocks, because you have set the block size to 1. Use bs=512, or just leave out bs entirely, since dd will assume 512 when you don't tell it otherwise. I think you want 2048, not 2047, as well.

There is no 'sector', 'cylinder', 'head', 'track', etc, etc, etc. None of that has been relevant for going on 20 years. Hard drives do have a block size though, of 512 bytes (and even the very newest drives, which use 4K blocks internally, still act like they have 512 byte blocks.)

34612341692783461 lines of script aren't going to help you when you don't know how to do it by hand.
This User Gave Thanks to Corona688 For This Post:
# 11  
Old 05-21-2015
Ok. How do I mark this thread closed? 😄
# 12  
Old 05-23-2015
I'm not familiar with sgdisk and wouldn't know how to construct commands for it. To extract the desired values form above mentioned output, try
Code:
sgdisk -p /dev/sda |
while read partnum startsec endsec size unit parttype name
   do [[ $partnum =~ [[:digit:]]{1,2} ]] &&
        echo $partnum $startsec $endsec $size $parttype $name
   done
1 2048 411647 200.0 EF00 EFI System Partition
2 411648 1435647 500.0 8300
3 1435648 1464401919 697.6 8300

You'd need to assign the values to e.g. an array in lieu of the echo, then.
# 13  
Old 05-23-2015
Thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. HP-UX

Dev/urandom and dev/random missing in HP-UX

Hi, In our HP-UX B.11.11. I could not find dev/urandom and dev/random Are all pseudo-devices implemented as device drivers, or in need to run /configure some package to install the package to have dev/urandom. Please help (4 Replies)
Discussion started by: rashi
4 Replies

2. Red Hat

Changing grub from /dev/sda to /dev/sdb

Hi, Please suggest steps to change grub from /dev/sda to /dev/sdb, (1 Reply)
Discussion started by: manoj.solaris
1 Replies

3. AIX

Problem in /dev/hd1 and /dev/hd9var

Hello AIXians, I can't boot my AIX, it hangs and stops at the code error: 0518 After searching google, I knew the problem is due to problems in File Systems. So the solution is booting from any bootable media, then run these commands in maintenance mode: #fsck -y /dev/hd4 #fsck -y... (3 Replies)
Discussion started by: Mohannad
3 Replies

4. AIX

Difference between /dev/hdisk and /dev/rhdisk

Hi, How can i check that i am using RAW devices for storage in my AIX machine... Also after adding a LUN from storage to a aix host, when i check /dev in the host, i can see both rhdisk and hdisk with same number eg: dcback1(root):/dev>ls -lrt | grep disk12 crw------- 1 root ... (4 Replies)
Discussion started by: jibujacob
4 Replies

5. Solaris

Lun remove, stuck in /dev/dsk and /dev/rdsk

So, we removed a LUN from the SAN and the system is refusing to remove the references to it in the /dev folder. I've done the following: devfsadm -Cv powermt -q luxadm -e offline <drive path> luxadm probe All those commands failed to remove the path. The drive stills shows up as <drive... (13 Replies)
Discussion started by: DustinT
13 Replies

6. Slackware

CUPS setup / dev/lp0 perms. ... Slow printing.

Hello, Ive got an HP LaserJet 2100 / parallel interface. I had some troubles getting going due to non-working cups drivers. Updated cups and also used a .ppd.gz file from something HP provided. found the files here... www.linuxprinting.org/show_printer.cgi?recnum=HP-LaserJet_2100 Anyways.... (1 Reply)
Discussion started by: agentrnge
1 Replies

7. UNIX for Dummies Questions & Answers

Sending alt-n to /dev/pts/1 from process bound to /dev/pts/2

Hello, i am using finch (unix commandline instant messaging client using libgnt) which is running connected to /dev/pts/1 Now I would like to "remote control" the program by sending the key combinations normally typed on the keyboard from a programm in another shell. So I tried:... (0 Replies)
Discussion started by: mentos
0 Replies

8. Solaris

What is /dev/tty /dev/null and /dev/console

Hi, Anyone can help My solaris 8 system has the following /dev/null , /dev/tty and /dev/console All permission are lrwxrwxrwx Can this be change to a non-world write ?? any impact ?? (12 Replies)
Discussion started by: civic2005
12 Replies

9. Solaris

URGENT - setup port and dump all output to /dev/null or a file

Please help urgently. I need to setup up some sort of service on a solaris server on a port. I dont need it do anything special, anything that is sent to this port from an external server should be dump to /dev/null or a flat file.. Can you help urgently? (1 Reply)
Discussion started by: frustrated1
1 Replies
Login or Register to Ask a Question