Skip to main content

linux - Repartitioning two disks without a loss of data

I am interning at a software company and I have hit somewhat of a brick wall. Here is the deal:



The Problem: We have some boxes around here that were incorrectly partitioned for 2 x 500 GB drives. The actual drives are 2 x 1 TB drives. These are essentially machines with only half of their available disk space being used. I am tasked with writing a script to re-partition these drives.




Solution Thus Far: I have a script that disables all process and reboots, and then another script that fixes the partitions. The problem is that there is a loss of data.



What I'm Looking For: I need a solution that does this but saves all the data. My first though would be to just grow the partitions to their appropriate size, but I'm not sure if that is possible. The other solution is to copy all data onto Disk2, partition Disk1, move data back to Disk1, and finally partition Disk2. The problem is that I am pretty new to Linux and I don't really know how to do it. I have access to the fdisk utility and the parted utility.



They are all of type ext3.



EDIT: 11/3/11



Okay. So I have approximately 1GB of unused, unallocated space on both disks. I want to do as follows:





  • Create a new extended partition on SDB of size 1GB, called sdb99 for reference here

  • Copy sda5 sda6 sda7 to the new SDB partition sdb99


    • Can I just copy "/" from each of these to some folder in this new partition?

    • Do I need to put a filesystem on this new partition to copy any files on there?

    • If I just copy "/", will that preserve the whole directory structure?

    • Is it a simple task to move files between disks like this?



  • Delete sda5 sda6 sda7 and then re-create them with twice size


    • Do I actually need to delete these? I don't think that I can grow 3 contiguous partitions, and even if I could, the filesystem won't grow...right?


  • Copy back the data from sdb99 to the new sda5 sda6 sda7 partitions


    • This should be as simple as moving the contents of those directories containing all the "/"'s back, right?



  • Copy sdb5 sdb6 sdb7 into the new sda5 sda6 sda7 into separate folders.


    • There should be enough space because of the now doubled space available...I hope


  • Delete and re-partition SDB

  • Move the files from sdb5 sdb6 sdb7 back onto SDB




Does anyone see any glaring problems, have any pointers, warnings, suggestions, etc.?



Thanks everyone. Again, this needs to be scripted. Thanks.



EDIT 2
Here is the actual script...



#!/bin/bash

LOG=./repartition.log


date > $LOG 2>&1

echo "Ok, let's get started." >> $LOG 2>&1

# Resize logical partitons
parted -s /dev/sda resize 4 45GB 2000GB >> $LOG 2>&1
parted -s /dev/sdb resize 4 90GB 2000GB >> $LOG 2>&1

# Create the temporary file systems on disk 2

mke2fs -j /dev/sdb8 >> $LOG 2>&1

# Copy sda6 sda7 sda8 to sdb8
cp -r -L -p /dev/sda6 /dev/sdb8/home/sda6 >> $LOG 2>&1
cp -r -L -p /dev/sda7 /dev/sdb8/home/sda7 >> $LOG 2>&1
cp -r -L -p /dev/sda8 /dev/sdb8/home/sda8 >> $LOG 2>&1

# Remove NBD partitions on disk 1
parted -s /dev/sda rm 8 >> $LOG 2>&1
parted -s /dev/sda rm 7 >> $LOG 2>&1

parted -s /dev/sda rm 6 >> $LOG 2>&1

# Create NBD partitons on disk 1
parted -s /dev/sda mkpart logical 70GB 713GB >> $LOG 2>&1
parted -s /dev/sda mkpart logical 713GB 1356GB >> $LOG 2>&1
parted -s /dev/sda mkpart logical 1356GB 2000GB >> $LOG 2>&1

# Create the file systems on disk 1
mke2fs -j /dev/sda6 >> $LOG 2>&1
mke2fs -j /dev/sda7 >> $LOG 2>&1

mke2fs -j /dev/sda8 >> $LOG 2>&1

# Copy sda6 sda7 sda8 back to sda
cp -r -L -p /dev/sdb8/home/sda6 /dev/sda6 >> $LOG 2>&1
cp -r -L -p /dev/sdb8/home/sda7 /dev/sda7 >> $LOG 2>&1
cp -r -L -p /dev/sdb8/home/sda8 /dev/sda8 >> $LOG 2>&1

# Copy sdb5 sdb6 sdb7 to sda6 sda7 sda8
cp -r -L -p /dev/sdb5 /dev/sda6/home/sdb5 >> $LOG 2>&1
cp -r -L -p /dev/sdb6 /dev/sda7/home/sdb6 >> $LOG 2>&1

cp -r -L -p /dev/sdb7 /dev/sda8/home/sdb7 >> $LOG 2>&1

# Remove NBD partitions on disk 2
parted -s /dev/sdb rm 8 >> $LOG 2>&1
parted -s /dev/sdb rm 7 >> $LOG 2>&1
parted -s /dev/sdb rm 6 >> $LOG 2>&1
parted -s /dev/sdb rm 5 >> $LOG 2>&1

# Create NBD partitons on disk 2
parted -s /dev/sdb mkpart logical 90GB 726GB >> $LOG 2>&1

parted -s /dev/sdb mkpart logical 726GB 1362GB >> $LOG 2>&1
parted -s /dev/sdb mkpart logical 1362GB 2000GB >> $LOG 2>&1

# Create the file systems on disk 2
mke2fs -j /dev/sdb5 >> $LOG 2>&1
mke2fs -j /dev/sdb6 >> $LOG 2>&1
mke2fs -j /dev/sdb7 >> $LOG 2>&1

# Copy sdb5 sdb6 sdb7 back to sdb
cp -r -L -p /dev/sda8/home/sdb7 /dev/sdb7 >> $LOG 2>&1

cp -r -L -p /dev/sda7/home/sdb6 /dev/sdb6 >> $LOG 2>&1
cp -r -L -p /dev/sda6/home/sdb5 /dev/sdb5 >> $LOG 2>&1


rm /etc/init.d/fix_partitions >> $LOG 2>&1
rm /etc/init.d/local/99fix_partitions >> $LOG 2>&1
mv /etc/init.d/local/gca_init.off /etc/init.d/local/99gca_init >> $LOG 2>&1

echo "All set. Please reboot. Have a nice day." >> $LOG 2>&1


date >> $LOG 2>&1
reboot >> $LOG 2>&1

Comments

Popular posts from this blog

linux - iDRAC6 Virtual Media native library cannot be loaded

When attempting to mount Virtual Media on a iDRAC6 IP KVM session I get the following error: I'm using Ubuntu 9.04 and: $ javaws -version Java(TM) Web Start 1.6.0_16 $ uname -a Linux aud22419-linux 2.6.28-15-generic #51-Ubuntu SMP Mon Aug 31 13:39:06 UTC 2009 x86_64 GNU/Linux $ firefox -version Mozilla Firefox 3.0.14, Copyright (c) 1998 - 2009 mozilla.org On Windows + IE it (unsurprisingly) works. I've just gotten off the phone with the Dell tech support and I was told it is known to work on Linux + Firefox, albeit Ubuntu is not supported (by Dell, that is). Has anyone out there managed to mount virtual media in the same scenario?

hp proliant - Smart Array P822 with HBA Mode?

We get an HP DL360 G8 with an Smart Array P822 controller. On that controller will come a HP StorageWorks D2700 . Does anybody know, that it is possible to run the Smart Array P822 in HBA mode? I found only information about the P410i, who can run HBA. If this is not supported, what you think about the LSI 9207-8e controller? Will this fit good in that setup? The Hardware we get is used but all original from HP. The StorageWorks has 25 x 900 GB SAS 10K disks. Because the disks are not new I would like to use only 22 for raid6, and the rest for spare (I need to see if the disk count is optimal or not for zfs). It would be nice if I'm not stick to SAS in future. As OS I would like to install debian stretch with zfs 0.71 as file system and software raid. I have see that hp has an page for debian to. I would like to use hba mode because it is recommend, that zfs know at most as possible about the disk, and I'm independent from the raid controller. For us zfs have many benefits,

apache 2.2 - Server Potentially Compromised -- c99madshell

So, low and behold, a legacy site we've been hosting for a client had a version of FCKEditor that allowed someone to upload the dreaded c99madshell exploit onto our web host. I'm not a big security buff -- frankly I'm just a dev currently responsible for S/A duties due to a loss of personnel. Accordingly, I'd love any help you server-faulters could provide in assessing the damage from the exploit. To give you a bit of information: The file was uploaded into a directory within the webroot, "/_img/fck_uploads/File/". The Apache user and group are restricted such that they can't log in and don't have permissions outside of the directory from which we serve sites. All the files had 770 permissions (user rwx, group rwx, other none) -- something I wanted to fix but was told to hold off on as it wasn't "high priority" (hopefully this changes that). So it seems the hackers could've easily executed the script. Now I wasn't able